How efficient are binary search trees? Balanced search trees - - PowerPoint PPT Presentation

how efficient are binary search trees balanced search
SMART_READER_LITE
LIVE PREVIEW

How efficient are binary search trees? Balanced search trees - - PowerPoint PPT Presentation

CS206 CS206 How efficient are binary search trees? Balanced search trees Balancing a tree means to keep the left and right subtree of Binary search tree operations take time O ( h ) , where h is the every node of roughly equal size.


slide-1
SLIDE 1

CS206

How efficient are binary search trees?

Binary search tree operations take time O(h), where h is the height of the tree. But what is the height of a binary search tree for n elements? It depends on the insertion order! In the best case O(log n). (Perfect binary tree) In the worst case O(n) (the tree is really a linked list). If the insertions are in random order, then the expected height

  • f the tree is O(log n).

CS206

Balanced search trees

Balancing a tree means to keep the left and right subtree of every node of roughly “equal” size. There are many kinds of balanced search trees:

  • Height-balanced trees (AVL-trees), (Adelson-Velsky and

Landis, 1962);

  • Weight-balanced trees (Nievergelt and Reingold, 1973);
  • (a, b)-trees (Bayer and McCreight 1972);
  • Red-black trees (Guibas and Sedgewick 1978);
  • Splay-trees (Sleator and Tarjan 1985).

CS206

AVL-Trees

An AVL-tree is a binary search tree with an additional balance property: For every node of the tree, the height of the left subtree and the right subtree differ by at most one. 12 16 8 4 2 6 10 14 16 4 2 6 10 14 1 AVL-Tree Not an AVL-Tree 12 8 CS206

AVL-Trees have logarithmic height

We have N(0) = 1, N(1) = 2, N(2) = 4, and N(h) ≥ N(h − 1) + N(h − 2) + 1. So N(h) ≥ 2N(h − 2), and induction gives us N(h) ≥ 2⌈h/2⌉. And therefore an AVL-tree with n nodes has height at most 2 log n. We ask the opposite question: For a given height h, what is the smallest number N(h) of nodes an AVL-tree can have? A more careful analysis shows that N(h) = Fh+3 − 1, and using the known formula for the Fibonacci numbers, we get the better bound h ≤ 1.44 log(n + 2).

slide-2
SLIDE 2

CS206

Maintaining balance

We have to maintain the balancing condition when we insert or remove nodes in the tree. Consider the insertion/deletion of a node w. Heights change only on the path from the root to w. Let z be the lowest ancestor of w that is now unbalanced. Let y be its child of larger height, and x the child of y of larger height (outer child in case of equal height). We restructure the subtree rooted at z, by moving x, y, and z and their subtrees. There are four cases. CS206

The four cases of restructuring

z y x T3 T2 T1 T0 z T3 T0 T2 T1 y x z y x T0 T1 T2 T3 z T0 T3 T1 T2 y x CS206

Single Rotation

z y x T0 T1 T2 T3 z y x T0 T1 T2 T3 Left rotation The new subtree at y is balanced since h(T0) − 1 ≤ h(T3) ≤ h(T0) = h(T2) ≤ h(T1) ≤ h(T0) + 1 CS206

Single Rotation

16 4 2 6 10 14 1 12 8 16 14 12 z y x 10 4 8 2 1 6

slide-3
SLIDE 3

CS206

Double rotation

z T0 T3 T1 T2 y x Right rotation around y z T0 T3 T1 T2 y x Left rotation around z z T0 T3 T1 T2 y x h(T0) = h(T1) = h(T3) h(T1) − 1 ≤ h(T2) ≤ h(T1) CS206

Double rotation

16 4 2 6 10 14 12 8 5 z y x 16 14 12 8 2 5 10 6 4