1 / 38

Height-Balanced Binary Search Trees

Height-Balanced Binary Search Trees. AVL Trees. Background. Binary Search Trees allow dynamic allocation (like linked lists), but O(log 2 (n)) average case search time (like arrays). Problem: they still have O(n) worst case search time.

gwen
Download Presentation

Height-Balanced Binary Search Trees

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Height-Balanced Binary Search Trees AVL Trees

  2. Background • Binary Search Trees allow dynamic allocation (like linked lists), but O(log2(n)) average case search time (like arrays). • Problem: they still have O(n) worst case search time. • Solution: if we can balance the trees on insert, we can have worst case O(log2(n)) search time.

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

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

  5. AVL Algorithm • Insert a new key value in the normal way • Call the newly inserted node “newnode” • Set H(newnode) = 1; • For each node from newnode back to root • Recalculate H(node) and BF(node) • If BF(node) = +2 or BF(node) = –2 • Perform appropriate rotation • Once a rotation is performed, stop.

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

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

  8. AVL Tree: LL Rotation H : h+2 BF : +1 A • This is the general tree set up. • Now suppose a new node is inserted into BL that increases its height: H : h+1 BF : 0 B AR h BL BR h

  9. AVL Tree: LL Rotation H : h+2 BF : +2 A • BF of node A is now +2, so a rotation is necessary: H : h+2 BF : +1 B AR h BL BR h New

  10. AVL Tree: LL Rotation H : h+2 BF : +0 B • This is the result of the rotation. • Note the height of the root is the same as before the insertion. H : h+1 BF : 0 A BL h BR AR h New

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

  12. RR Rotation H : h+2 BF : -1 A • This is the general tree set up. • Now suppose a new node is inserted into BR that increases its height: H : h+1 BF : 0 B AL h BL BR h

  13. RR Rotation H : h+3 BF : -2 A • BF of node A is –2, so a rotation is needed H : h+2 BF : -1 B AL h BL BR h New

  14. RR Rotation H : h+2 BF : 0 B • This is the result of the rotation. • Note the height of the root returns to h+2 H : h+1 BF : 0 A BR h AL BL h New

  15. RR Rotation Algorithm • Set up pointers for A, B, BL, BR, AL • A->right = BL • B->left = A • Root = B • Set H and BF for nodes A and B.

  16. AR h LR Rotation H : h+2 BF : +1 A • This is the general tree set up. • Now suppose a new node is inserted into CL that increases its height: H : h+1 BF : 0 B H : h BF : 0 C BL h CL CR h–1

  17. AR h LR Rotation H : h+3 BF : +2 A • The balance factor of A goes to +2, so a left-right rotation is needed. H : h+2 BF : -1 B H : h+1 BF : +1 C BL h CL CR h–1 New

  18. AR h LR Rotation H : h+2 BF : 0 C • Again, note the height of the root returns to h+2. H : h+1 BF : 0 B H : h+1 BF : -1 A BL CL CR h–1 h New

  19. LR Rotation Algorithm • Set up pointers for A, B, C, BL, AR, CL, CR • A->left = CR • B->right = CL • C->left = B • C->right = A • Root = C • Set H and BF for nodes A, B and C.

  20. BR AL h h RL Rotation H : h+2 BF : -1 A • This is the general tree set up. • Now suppose a new node is inserted into CL that increases its height: H : h+1 BF : 0 B H : h BF : 0 C CL CR h–1

  21. BR AL h h RL Rotation H : h+3 BF : -2 A • BF of Node A is now –2, so a rotation is necessary. H : h+2 BF : +1 B H : h+1 BF : +1 C CL CR h–1 New

  22. BR AL h h RL Rotation H : h+2 BF : 0 • This shows the result of the rotation. C H : h+1 BF : -1 H : h+1 BF : 0 A B CL CR h–1 New

  23. RL Rotation Algorithm • Set up pointers for A, B, C, BR, AL, CL, CR • A->right = CL • B->left = CR • C->left = A • C->right = B • Root = C • Set H and BF for nodes A, B and C.

  24. BR AL h h RL Rotation Method 2 H : h+3 BF : -2 A • First, Perform a LL rotation around B: H : h+2 BF : +1 B H : h+1 BF : +1 C CL CR h–1 New

  25. BR AL h h RL Rotation Method 2 H : h+3 BF : -2 A • Next, perform a RR Rotation around A: H : h+2 BF : -1 C H : h+1 BF : -1 CL B h–1 CR New

  26. BR AL h h RL Rotation Method 2 H : h+2 BF : 0 • This result is the same as the first method. C H : h+1 BF : -1 H : h+1 BF : 0 A B CL CR h–1 New

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

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

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

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

  31. AVL Example H : 2 BF : 0 50 • This is the result; note height of the root. • Next, insert 10: H : 1 BF : 0 25 90 H : 1 BF : 0 Left to insert: 10, 15, 20, 75

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

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

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

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

  36. AVL Example H : 3 BF : 0 25 • This is the result. Note the movements of the subtrees. • Finally, insert 75: H : 2 BF : 0 15 50 H : 2 BF : -1 H : 1 BF : 0 10 20 H : 1 BF : 0 90 H : 1 BF : 0 Left to insert: 75

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

  38. AVL Example H : 3 BF : 0 25 • This is the result. • Done. H : 2 BF : 0 15 75 H : 2 BF : 0 H : 1 BF : 0 10 20 H : 1 BF : 0 H : 1 BF : 0 50 90 H : 1 BF : 0 Left to insert: done

More Related