Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Height-Balanced Binary Search Trees AVL Trees. 2 Background zBinary Search Trees allow dynamic allocation (like linked lists), but O(log 2 (n)) average.

Similar presentations


Presentation on theme: "1 Height-Balanced Binary Search Trees AVL Trees. 2 Background zBinary Search Trees allow dynamic allocation (like linked lists), but O(log 2 (n)) average."— Presentation transcript:

1 1 Height-Balanced Binary Search Trees AVL Trees

2 2 Background zBinary Search Trees allow dynamic allocation (like linked lists), but O(log 2 (n)) average case search time (like arrays). zProblem: they still have O(n) worst case search time. zSolution: if we can balance the trees on insert, we can have worst case O(log 2 (n)) search time.

3 3 AVL Trees zNamed for inventors Adelsson, Velski, and Landis. zOne way to maintain a balanced tree. zIdea: keep the height of every node in the tree. zWhen the difference between the height of a node’s left and right subtrees exceeds 1, rotate the nodes to eliminate the imbalance.

4 4 AVL Details zEach node will have an additional piece of information – its height. It is calculated as: yH(node) = MAX(H(left), H(right)) + 1 zEach node’s balance factor will be calculated when needed; the formula is yBF(node) = H(left) – H(right) zWhen BF(node) = +2 or –2, a fix is needed.

5 5 AVL Algorithm zInsert a new key value in the normal way yCall the newly inserted node “newnode” zSet H(newnode) = 1; zFor each node from newnode back to root yRecalculate H(node) and BF(node) yIf BF(node) = +2 or BF(node) = –2 xPerform appropriate rotation xOnce a rotation is performed, stop.

6 6 AVL Rotations zThe appropriate rotation depends on where “newnode” is with respect to the node that is out of balance. zThe four types are yRight-Right (newnode was inserted into the right subtree of the right subtree of the node) yLeft-Left yLeft-Right yRight-Left

7 7 AVL Rotations II zThe rotations are usually specified in general, since a rotation can occur anywhere in the tree. zSubtrees move as whole units zNote in the following examples that the height of the tree after rotation is the same as before the insertion. zThis means that no heights or balance factors above this node will change.

8 8 AVL Tree: LL Rotation zThis is the general tree set up. zNow suppose a new node is inserted into B L that increases its height: A B BLBL BRBR ARAR h h H : h+1 BF : 0 H : h+2 BF : +1

9 9 AVL Tree: LL Rotation zBF of node A is now +2, so a rotation is necessary: A B BLBL BRBR ARAR h h H : h+2 BF : +1 H : h+2 BF : +2 New

10 10 AVL Tree: LL Rotation zThis is the result of the rotation. zNote the height of the root is the same as before the insertion. A B BLBL BRBR ARAR h h H : h+1 BF : 0 H : h+2 BF : +0 New

11 11 LL Rotation Algorithm zSet up pointers for A, B, BL, BR, AR zA->left = BR zB->right = A zRoot = B zSet H and BF for nodes A and B. zNOTE: the book does this more “cleverly”, with fewer pointers. I think this is more clear.

12 12 RR Rotation zThis is the general tree set up. zNow suppose a new node is inserted into B R that increases its height: A B BLBL BRBR ALAL h h H : h+1 BF : 0 H : h+2 BF : -1

13 13 RR Rotation zBF of node A is –2, so a rotation is needed A B BLBL BRBR ALAL h h H : h+2 BF : -1 H : h+3 BF : -2 New

14 14 RR Rotation zThis is the result of the rotation. zNote the height of the root returns to h+2 B A BLBL BRBR ALAL h h H : h+2 BF : 0 H : h+1 BF : 0 New

15 15 RR Rotation Algorithm zSet up pointers for A, B, BL, BR, AL zA->right = BL zB->left = A zRoot = B zSet H and BF for nodes A and B.

16 16 LR Rotation zThis is the general tree set up. zNow suppose a new node is inserted into C L that increases its height: A B BLBL CLCL h ARAR h H : h+1 BF : 0 H : h+2 BF : +1 C CRCR h–1 H : h BF : 0

17 17 LR Rotation zThe balance factor of A goes to +2, so a left-right rotation is needed. A B BLBL CLCL h ARAR h H : h+2 BF : -1 H : h+3 BF : +2 C CRCR h–1 H : h+1 BF : +1 New

18 18 LR Rotation zAgain, note the height of the root returns to h+2. C B BLBL CLCL h ARAR h H : h+1 BF : 0 H : h+2 BF : 0 A CRCR h–1 H : h+1 BF : -1 New

19 19 LR Rotation Algorithm zSet up pointers for A, B, C, BL, AR, CL, CR zA->left = CR zB->right = CL zC->left = B zC->right = A zRoot = C zSet H and BF for nodes A, B and C.

20 20 RL Rotation zThis is the general tree set up. zNow suppose a new node is inserted into C L that increases its height: A B CLCL BRBR h H : h+1 BF : 0 H : h+2 BF : -1 C CRCR h–1 H : h BF : 0 ALAL h

21 21 RL Rotation zBF of Node A is now –2, so a rotation is necessary. A B CLCL BRBR h H : h+2 BF : +1 H : h+3 BF : -2 C CRCR h–1 H : h+1 BF : +1 ALAL h New

22 22 RL Rotation zThis shows the result of the rotation. AB CLCL BRBR h H : h+1 BF : -1 H : h+2 BF : 0 C CRCR h–1 H : h+1 BF : 0 ALAL h New

23 23 RL Rotation Algorithm zSet up pointers for A, B, C, BR, AL, CL, CR zA->right = CL zB->left = CR zC->left = A zC->right = B zRoot = C zSet H and BF for nodes A, B and C.

24 24 RL Rotation Method 2 zFirst, Perform a LL rotation around B: A B CLCL BRBR h H : h+2 BF : +1 H : h+3 BF : -2 C CRCR h–1 H : h+1 BF : +1 ALAL h New

25 25 RL Rotation Method 2 zNext, perform a RR Rotation around A: A B CLCL BRBR h H : h+1 BF : -1 H : h+3 BF : -2 C CRCR h–1 H : h+2 BF : -1 ALAL h New

26 26 RL Rotation Method 2 zThis result is the same as the first method. AB CLCL BRBR h H : h+1 BF : -1 H : h+2 BF : 0 C CRCR h–1 H : h+1 BF : 0 ALAL h New

27 27 AVL Example zNow let’s do an extended example, inserting a series of key values into an AVL tree and rotating as necessary. zWe will be inserting 25, 50, 90, 10, 15, 20, 75. zLet’s start with 25:

28 28 AVL Example z25 is a new root; no problem. zNow, insert 50: 25 Left to insert: 50, 90, 10, 15, 20, 75 H : 1 BF : 0

29 29 AVL Example zNo problem here. zNow, insert 90: 25 50 Left to insert: 90, 10, 15, 20, 75 H : 2 BF : -1 H : 1 BF : 0

30 30 AVL Example zBF(25) = –2; what do we do? zThis is a RR rotation: 25 50 90 Left to insert: 10, 15, 20, 75 H : 3 BF : -2 H : 2 BF : -1 H : 1 BF : 0

31 31 AVL Example zThis is the result; note height of the root. zNext, insert 10: 50 2590 Left to insert: 10, 15, 20, 75 H : 2 BF : 0 H : 1 BF : 0 H : 1 BF : 0

32 32 AVL Example zNo problems here. zNext, insert 15: 50 25 10 90 Left to insert: 15, 20, 75 H : 3 BF : +1 H : 2 BF : +1 H : 1 BF : 0 H : 1 BF : 0

33 33 AVL Example zBF(25)=+2, so time to rotate zThis is a Left-Right (LR) rotation: z(Note that I didn’t update 50) 50 25 10 15 90 Left to insert: 20, 75 H : 3 BF : +1 H : 3 BF : +2 H : 1 BF : 0 H : 2 BF : -1 H : 1 BF : 0

34 34 AVL Example zThis shows the result. zNote that I didn’t need to update 50… zNow, insert 20: 50 15 10 90 25 Left to insert: 20, 75 H : 3 BF : +1 H : 2 BF : 0 H : 1 BF : 0 H : 1 BF : 0 H : 1 BF : 0

35 35 AVL Example zBF(50) = +2, so time to rotate… zThis is a left right (LR) rotation again: 50 15 10 20 90 25 Left to insert: 75 H : 4 BF : +2 H : 3 BF : -1 H : 1 BF : 0 H : 2 BF : +1 H : 1 BF : 0 H : 1 BF : 0

36 36 AVL Example zThis is the result. Note the movements of the subtrees. zFinally, insert 75: 25 15 10 50 2090 Left to insert: 75 H : 3 BF : 0 H : 2 BF : 0 H : 2 BF : -1 H : 1 BF : 0 H : 1 BF : 0 H : 1 BF : 0

37 37 AVL Example zH(50) = –2, so time to rotate again. zThis is a right-left (RL) rotation: 25 15 10 75 50 2090 Left to insert: done H : 3 BF : 0 H : 2 BF : 0 H : 3 BF : -2 H : 1 BF : 0 H : 1 BF : 0 H : 2 BF : +1 H : 1 BF : 0

38 38 AVL Example zThis is the result. zDone. 25 15 10 75 205090 Left to insert: done H : 3 BF : 0 H : 2 BF : 0 H : 2 BF : 0 H : 1 BF : 0 H : 1 BF : 0 H : 1 BF : 0 H : 1 BF : 0


Download ppt "1 Height-Balanced Binary Search Trees AVL Trees. 2 Background zBinary Search Trees allow dynamic allocation (like linked lists), but O(log 2 (n)) average."

Similar presentations


Ads by Google