AVL-Trees.ppt
《AVL-Trees.ppt》由会员分享,可在线阅读,更多相关《AVL-Trees.ppt(24页珍藏版)》请在麦多课文档分享上搜索。
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
- 1.请仔细阅读文档,确保文档完整性,对于不预览、不比对内容而直接下载带来的问题本站不予受理。
- 2.下载的文档,不会出现我们的网址水印。
- 3、该文档所得收入(下载+内容+预览)归上传者、原创作者;如果您是本文档原作者,请点此认领!既往收益都归您。
下载文档到电脑,查找使用更方便
2000 积分 0人已下载
下载 | 加入VIP,交流精品资源 |
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- AVLTREESPPT
