欢迎来到麦多课文档分享! | 帮助中心 海量文档,免费浏览,给你所需,享你所想!
麦多课文档分享
全部分类
  • 标准规范>
  • 教学课件>
  • 考试资料>
  • 办公文档>
  • 学术论文>
  • 行业资料>
  • 易语言源码>
  • ImageVerifierCode 换一换
    首页 麦多课文档分享 > 资源分类 > PPT文档下载
    分享到微信 分享到微博 分享到QQ空间

    AVL-Trees.ppt

    • 资源ID:378774       资源大小:475KB        全文页数:24页
    • 资源格式: PPT        下载积分:2000积分
    快捷下载 游客一键下载
    账号登录下载
    微信登录下载
    二维码
    微信扫一扫登录
    下载资源需要2000积分(如需开发票,请勿充值!)
    邮箱/手机:
    温馨提示:
    如需开发票,请勿充值!快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。
    如需开发票,请勿充值!如填写123,账号就是123,密码也是123。
    支付方式: 支付宝扫码支付    微信扫码支付   
    验证码:   换一换

    加入VIP,交流精品资源
     
    账号:
    密码:
    验证码:   换一换
      忘记密码?
        
    友情提示
    2、PDF文件下载后,可能会被浏览器默认打开,此种情况可以点击浏览器菜单,保存网页到桌面,就可以正常下载了。
    3、本站不支持迅雷下载,请使用电脑自带的IE浏览器,或者360浏览器、谷歌浏览器下载即可。
    4、本站资源下载后的文档和图纸-无水印,预览文档经过压缩,下载后原文更清晰。
    5、试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。

    AVL-Trees.ppt

    1、AVL-Trees,COMP171 Fall 2005,Balanced binary tree,The disadvantage of a binary search tree is that its height can be as large as N-1 This means that the time needed to perform insertion and deletion and many other operations can be O(N) in the worst case We want a tree with small height A binary tree

    2、 with N node has height at least (log N) Thus, our goal is to keep the height of a binary search tree O(log N) Such trees are called balanced binary search trees. Examples are AVL tree, red-black tree.,AVL tree,Height of a node The height of a leaf is 1. The height of a null pointer is zero. The hei

    3、ght of an internal node is the maximum height of its children plus 1Note that this definition of height is different from the one we defined previously (we defined the height of a leaf as zero previously).,AVL tree,An AVL tree is a binary search tree in which for every node in the tree, the height o

    4、f the left and right subtrees differ by at most 1.,AVL property violated here,AVL tree,Let x be the root of an AVL tree of height h Let Nh denote the minimum number of nodes in an AVL tree of height h Clearly, Ni Ni-1 by definition We haveBy repeated substitution, we obtain the general formThe bound

    5、ary conditions are: N1=1 and N2 =2. This implies that h = O(log Nh). Thus, many operations (searching, insertion, deletion) on an AVL tree will take O(log N) time.,Rotations,When the tree structure changes (e.g., insertion or deletion), we need to transform the tree to restore the AVL tree property.

    6、 This is done using single rotations or double rotations.,x,y,A,B,C,Before Rotation,After Rotation,e.g. Single Rotation,Rotations,Since an insertion/deletion involves adding/deleting a single node, this can only increase/decrease the height of some subtree by 1 Thus, if the AVL tree property is viol

    7、ated at a node x, it means that the heights of left(x) ad right(x) differ by exactly 2. Rotations will be applied to x to restore the AVL tree property.,Insertion,First, insert the new key as a new leaf just as in ordinary binary search tree Then trace the path from the new leaf towards the root. Fo

    8、r each node x encountered, check if heights of left(x) and right(x) differ by at most 1. If yes, proceed to parent(x). If not, restructure by doing either a single rotation or a double rotation next slide. For insertion, once we perform a rotation at a node x, we wont need to perform any rotation at

    9、 any ancestor of x.,Insertion,Let x be the node at which left(x) and right(x) differ by more than 1 Assume that the height of x is h+3 There are 4 cases Height of left(x) is h+2 (i.e. height of right(x) is h) Height of left(left(x) is h+1 single rotate with left child Height of right(left(x) is h+1

    10、double rotate with left child Height of right(x) is h+2 (i.e. height of left(x) is h) Height of right(right(x) is h+1 single rotate with right child Height of left(right(x) is h+1 double rotate with right child,Note: Our test conditions for the 4 cases are different from the code shown in the textbo

    11、ok. These conditions allow a uniform treatment between insertion and deletion.,Single rotation,The new key is inserted in the subtree A. The AVL-property is violated at xheight of left(x) is h+2height of right(x) is h.,Single rotation,Single rotation takes O(1) time. Insertion takes O(log N) time.,T

    12、he new key is inserted in the subtree C. The AVL-property is violated at x.,5,Insert 0.8,AVL Tree,8,0.8,x,y,A,B,C,After rotation,Double rotation,The new key is inserted in the subtree B1 or B2. The AVL-property is violated at x. x-y-z forms a zig-zag shape,also called left-right rotate,Double rotati

    13、on,The new key is inserted in the subtree B1 or B2. The AVL-property is violated at x.,also called right-left rotate,5,Insert 3.5,AVL Tree,8,1,After Rotation,An Extended Example,Insert 3,2,1,4,5,6,7, 16,15,14,Single rotation,Single rotation,Single rotation,Single rotation,Double rotation,Double rota

    14、tion,Deletion,Delete a node x as in ordinary binary search tree. Note that the last node deleted is a leaf. Then trace the path from the new leaf towards the root. For each node x encountered, check if heights of left(x) and right(x) differ by at most 1. If yes, proceed to parent(x). If not, perform

    15、 an appropriate rotation at x. There are 4 cases as in the case of insertion. For deletion, after we perform a rotation at x, we may have to perform a rotation at some ancestor of x. Thus, we must continue to trace the path until we reach the root.,Deletion,On closer examination: the single rotation

    16、s for deletion can be divided into 4 cases (instead of 2 cases) Two cases for rotate with left child Two cases for rotate with right child,Single rotations in deletion,rotate with left child,In both figures, a node is deleted in subtree C, causing the height to drop to h. The height of y is h+2. Whe

    17、n the height of subtree A is h+1, the height of B can be h or h+1. Fortunately, the same single rotation can correct both cases.,Single rotations in deletion,rotate with right child,In both figures, a node is deleted in subtree A, causing the height to drop to h. The height of y is h+2. When the hei

    18、ght of subtree C is h+1, the height of B can be h or h+1. A single rotation can correct both cases.,Rotations in deletion,There are 4 cases for single rotations, but we do not need to distinguish among them. There are exactly two cases for double rotations (as in the case of insertion) Therefore, we can reuse exactly the same procedure for insertion to determine which rotation to perform,


    注意事项

    本文(AVL-Trees.ppt)为本站会员(proposalcash356)主动上传,麦多课文档分享仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知麦多课文档分享(点击联系客服),我们立即给予删除!




    关于我们 - 网站声明 - 网站地图 - 资源地图 - 友情链接 - 网站客服 - 联系我们

    copyright@ 2008-2019 麦多课文库(www.mydoc123.com)网站版权所有
    备案/许可证编号:苏ICP备17064731号-1 

    收起
    展开