1 / 12

Balanced Binary Search Trees

Learn about balanced binary search trees, including AVL trees and red-black trees, and how to insert and search nodes efficiently.

harlandb
Download Presentation

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. Balanced Binary Search Trees • Pathological BST • Insert nodes from ordered list : O(___) ? • Search: O(___) ? • The Balanced Tree • Binary Tree is balanced if height of left and right subtree differ by no more than one, recursively for all nodes. • (Height of empty tree is -1) • Examples

  2. Balanced Binary Search Trees • Keeping BSTrees Balanced • Keeps find, insert, delete O(log(N)) worst case. • Pay small extra amount at each insertion to keep it balanced • Several Well-known Systems Exist for This • AVL Trees • Red-Black Trees • . . . • Will “look at” AVL Trees

  3. AVL Trees • AVL Trees • Adelson-Velskii and Landis • Discovered ways to keep BSTrees Balanced • Insertions • Insert into BST in normal way • If tree no longer balanced, perform “rotation(s)” • Rotations restore balance to the tree

  4. AVL Trees • Single Rotation • An insertion into the left subtree of the left child of tree • Adapted from Weiss, pp 567-568 /** Used if insert has caused loss of balance at k2 * (Also used as part of double rotation operations) * @return root of adjusted tree */ TNode rotateWithLeftChild(TNode k2){ TNode k1 = k2.left; k2.left = k1.right; k1.right = k2; return k1; }

  5. AVL Trees • Single Rotation

  6. AVL Trees • Single Rotation k1 k2 before after k2 k1 C B B C A A • Also: mirror image

  7. AVL Trees • Single Rotation • Mirror image case /** Used if insert has caused loss of balance at k2 * (Also used as part of double rotation operations) * @return root of adjusted tree */ TNode rotateWithRightChild(TNode k2){ TNode k1 = k2.right; k2.right = k1.left; k1.left = k2; return k1; }

  8. AVL Tree • Double Rotation • An insertion into the right subtree of the left child of tree • Adapted from Weiss, p 57 /** Used after insertion into right subtree, k2, * of left child, k1, of k3 (if it has caused * loss of balance) * @return root of adjusted tree */ TNode doubleRotateWithLeftChild(TNode k3) { k3.left = rotateWithRightChild(k3.left); return rotateWithLeftChild(k3); }

  9. AVL Tree • Double Rotation

  10. AVL Trees • Double Rotation k3 after before k2 k3 k1 k1 k2 C B D A D A B C • Also: mirror image

  11. AVL Tree • Double Rotation • An insertion into the right subtree of the left child of tree • Adapted from Weiss, p 571 /** Used after insertion into right subtree, k2, * of right child, k1, of k3 (if it has caused * loss of balance) * @return root of adjusted tree */ TNode doubleRotateWithRightChild(TNode k3) { k3.right = rotateWithLeftChild(k3.right); return rotateWithRightChild(k3); }

  12. AVL Trees • Deletions can also cause imbalance • Use similar rotations to restore balance • Big Oh? • That was the “big picture” • How can we insure performance? • Think about some of the implementation issues.

More Related