Presentation is loading. Please wait.

Presentation is loading. Please wait.

AVL tree self-adjusting tree Lai Ah Fur. AVL tree discovers: Adelson-Velskii and Landis balanced Binary search tree the depth of the tree: O(lg N) definition:

Similar presentations


Presentation on theme: "AVL tree self-adjusting tree Lai Ah Fur. AVL tree discovers: Adelson-Velskii and Landis balanced Binary search tree the depth of the tree: O(lg N) definition:"— Presentation transcript:

1 AVL tree self-adjusting tree Lai Ah Fur

2 AVL tree discovers: Adelson-Velskii and Landis balanced Binary search tree the depth of the tree: O(lg N) definition: An AVL tree is a binary search tree with the additional balance property that, for any node in the tree, the height of the left and right subtrees can be differ by at most 1.

3 Balance factor (BF) 如果 node T 之 the height of left subtree is H l, the height of right subtree is H r, then the BF of node T is H l - H r 高度平衡之 BST: BF(t)<2 … for any node t AVL tree: BF(t)<2 Full binary tree: BF=? complete binary tree: BF=?

4 5173 9563 78 31 5173 9563 78 31 51 78 95 63 73 2 1 1 0 1 Right rotate LL 型

5 105 83 9563 78 105 83 9563 78 100 -2 1 105 78 95 6383 100 Left rotate RR 型

6 插入 66 78 63 66 63 66 78 2 0 0 0 0 第一種 LR 型

7 插入 65 64557336 5170 90 9563 78 65 55 64 36 51 63 78 90 95 70 73 65 55 64 36 51 6373 90 9570 78 65 第二種 LR 型 2 1 0 0 0 1 0 0

8 插入 75 78 6395 90 5573 70 65 51 36 75 78 7095 90 65 55 73 36 63 51 75 70 6378 73 55 95 65 90 51 36 75 第三種 LR 型 2 1

9 插入 90 96 78 9096 78 90 78 96 第一種 RL 型 -2 1 0

10 插入 80 51 63 78 95 85 80 100 83 105 79200 51 63 78 83 200 80 100 79 95 85105 63 78 83 95 51 80 100 85 105 79 200 第二種 RL 型 -2 1 1

11 插入 88 51 63 78 95 85 88 100 83 105 79200 63 78 83 95 88 51 100 85 105 79 200 第三種 RL 型 -2 1 0 0 1 1 0

12 Del 7 1 2 5 8 9 7 10 3 11 1 2 5 8 3 9 10 11 RR 1 2 5 10 8 11 3 9 1 2 5 8 9 7 10 3 11 Del 7 1 2 5 8 3 9 10 11 RL 1 2 5 9 8 10 3 11 1. 刪除 7 或是

13 1 2 5 10 8 11 3 9 2. 刪除 5 Del 5 1 2 3 10 8 11 9 1 2 8 10 9 11 3 RL

14 Insertion case 1: an insertion into the left subtree of the left child of X case 2: an insertion into the right subtree of the left child of X case 3: an insertion into the left subtree of the right child of X case 4: an insertion into the right subtree of the right child of X

15 case 1: single rotation left subtree 較高.A single rotation switches the role of the parent and the child while maintaining the search order.rotate binary tree with left child static BinaryNode withLeftChild ( BinaryNode k2) { BinaryNode k1=k2.left; k2.left=k1.right; k1.right=k2; return k1; }

16 K2 K1 C B A K2 K1 C B A case 1 (single L-L rotation, 單一左左迴轉 ) single rotation +2 +1 0 0

17 case 1 12 816 14 104 62 1 12 8 16 14 10 4 6 2 1 k2 k1 BA c A K2 BC Insert “ 1 ”

18 case 2: double rotation (double L-R rotation) double rotate binary tree node: first left child with its right child; then, node k3 with new left child. static BinaryNode doubleWithLeftChild( BinaryNode k3) { k3.left=withRightChild(k3.left); return withLeftChild(k3); }

19 case 2: double rotation K3 K1 D A B K2 C K1 D C K3 K2 AB K3 K2 D A B K1 C +2 0

20 case 2: double rotation 12 816 14 104 62 5 12 8 16 14 10 6 5 4 2 k2 k1 B A D A K2 B D Insert “ 5 ” k3 C C K3

21 case 3:double rotation (double R-L rotation) double rotate binary tree node: first right child with its left child; then, node k1 with new right child. static BinaryNode doubleWithRightChild( BinaryNode k1) { k1.right=withLeftChild(k1.right); return withRightChild(k1); }

22 case 3 K3 K1 D B K2 C K1 D C K3 K2 AB K1 K2 D B C A K3 A -2 +1 0

23 R-L type Insert 85 or 92 -2 80 95 70 85 +1 +1/-1 0 90 99 92 80 90 70 85 95 92 99 80 90 7085 95 92 99

24 case 4: single rotation right subtree 較高 rotate binary tree with right child static BinaryNode withRightChild ( BinaryNode k1) { BinaryNode k2=k1.right; k1.right=k2.left; k2.left=k1; return k2; }

25 case 4 (single R-R rotation, 單一右右迴轉 ) K2 K1 C B A K2 K1 C B A single rotation -2 0

26 R-R type 80 95 60 5070 80 95 70 60 50 Insert 50 0 0 0 0 0 0

27 exercise Insert the following data into the empty AVL tree, 90 80 70 60 50 40

28 exercise Insert the following data into the empty AVL tree, “ Mar,May,Nov,Aug,Apr,Jane,Dec,July,Feb,June,Oct,Sept ”

29 3-1 May Nov AprMar +1 Jan +2 Insert Jan Aug 2 May Nov Aug Apr +1 +2 Insert Aug, Apr Mar 1 Nov -2 Insert Mar, May, Nov May 3-2 May Nov AprJan Mar Aug 4-1 May Nov Aug Apr Mar July Feb Jan Dec +1 -2 Insert Dec, July, Feb

30 4-2 May Nov Aug Apr Mar July Dec Jan Feb 5-1 May Nov Apr Mar July Dec Jan Feb Aug June +2 Insert June 5-2 May Nov Apr Mar JulyDec Jan Feb Aug June 6 May Nov Apr July Dec Feb Aug June Jan Mar Oct Insert Oct -2

31 The Answer: 7 Apr Dec Feb Aug June Jan Insert Sept July May Nov Mar Sept Oct

32 Jan Dec Mar AugFebJulyNov Apr JuneMay Oct Sept +1 0 +10 000

33 After delete 40, … 60 40 30 80 7090 6575 Double R-L rotations

34 After delete 85, … 80 40 30 85 7090 6575 Double L-R rotations

35 After delete 40, … 80 4085 90 Single R-R rotations

36 After delete 85, … 80 4085 20 Single L-L rotations

37 After delete 85, … Single L-L rotations 80 4085 3050

38 How a newly arriving element enters … If a newly arriving element endangers the tree balance, how to rectify the problem immediately? By restructuring the tree locally (the AVL method) or by re-creating the tree (the DSW method)

39 self-adjusting tree But, not all elements are used with the same frequency. 在低層之 node 若 infrequently accessed, 則對於程式效能 影響不大 The strategy in self-adjusting tree is to restructure trees only by moving up the tree those elements that are used more often, creating a kind of “ priority tree ”

40 Self-restructuring tree Proposed by Brian Allen and Ian Munro and James Bitner Strategy: Single rotation: rotate a child about its parent if an element in a child is accessed unless it is the root Moving to the root: repeat the child- parent rotation until the element being accessed is in the root

41 splaying A modification of the “ move to the root ” Apply single rotation in pairs in an order depending on the links between the child, parent and grandparent. (node R is accessed) Case 1:node R ’ s parent is the root Case 2:homogeneous configuration: node R is the left child of its parent Q and Q is the left child of its parent P, or R and Q are both right children. Case 3: heterogeneous configuration: node R the right child of its parent Q and Q is the left child of its parent P, or R is the left child of Q and Q is the right child of P

42 3 cases of splaying Accessing node R

43 Examples of splaying Restructuring a tree with splaying (a-c) after accessing T and (c-d) then R

44 Algorithm of splaying Splaying(P,Q,R) while R is not the root if the R ’ s parent is the root perform a singular splay, rotate R about its parent; else if R is in homogeneous configuration with its predecessor perform a homoegeous splay, first rotate Q about P and then R about Q; else if R is in heterogeneous configuration with its predecessor perform a heterogeneous splay, first rotate R about Q and then about P;

45 The problem of splaying Splaying is a strategy focusing upon the elements rather than the shape of the tree. It may perform well in situation in which some elements are used much more frequently than others If the elements near the root are accessed with about the same frequency as elements on the lowest levels, then splaying may not be the best choice.

46 semisplaying A modification that requires only one rotation for a homogeneous splay and continues splaying with the parent of the accessed node.

47 Example of semisplaying (a)-(c) accessing T and restructuring the tree with semisplaying (c ) -(d)accessing T again


Download ppt "AVL tree self-adjusting tree Lai Ah Fur. AVL tree discovers: Adelson-Velskii and Landis balanced Binary search tree the depth of the tree: O(lg N) definition:"

Similar presentations


Ads by Google