overview binary search tree data structures and algorithms - - PowerPoint PPT Presentation

overview
SMART_READER_LITE
LIVE PREVIEW

overview binary search tree data structures and algorithms - - PowerPoint PPT Presentation

overview binary search tree data structures and algorithms AVL-trees 2020 10 05 lecture 11 overview how many binary trees with n nodes exist? 1 2 n C n = n + 1 n binary search tree first Catalan numbers: AVL-trees 1 , 1 , 2 , 5 ,


slide-1
SLIDE 1

data structures and algorithms 2020 10 05 lecture 11

  • verview

binary search tree AVL-trees

  • verview

binary search tree AVL-trees

how many binary trees with n nodes exist?

Cn = 1 n + 1 2n n

  • first Catalan numbers:

1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862, 16796, 58786, 208012, 742900, 2674440, 9694845, 35357670, 129644790, 477638700, 1767263190, 6564120420, 24466267020, 91482563640, 343059613650, 1289904147324, 4861946401452, . . .

slide-2
SLIDE 2

binary search trees: recall search

input: node v in binary search tree, and key k Algorithm treeSearch(v, k): if v = nil or k = v.key then return v if k < v.key then return treeSearch(v.left, k) else return treeSearch(v.right, k) see also the iterative version; know pseudo-codes

binary search tree: worst-case time complexity

  • perations for searching, adding, deleting all in O(height)

best case: height is in O(log n) worst case: height is in O(n) expected case: height is in O(log n) (no proof)

binary search trees: further improvements

because the height is crucial for the time complexity of the operations there are many subclasses of balanced binary search tree AVL trees, red-black trees, splay trees compromise between the optimal and arbitrary binary search tree

  • verview

binary search tree AVL-trees

slide-3
SLIDE 3

AVL-tree: definition

height of a node: maximal length of a path to a leaf height of a tree is height of the root is maximal length of a path to a leaf binary search tree with in addition balance-property: for every node the absolute value of the difference between the height of the left sub-tree and the height of the right sub-tree is at most 1 (take height −1 for the empty tree)

AVL-tree: example

the key is in the node, the height of the sub-tree above the node 3 2 8 6 4 7 9 3 1 1 2

ALV-tree: height

height of an AVL-tree with n nodes is in O(log n) (no proof)

  • perations on AVL-trees

min, max: algorithm as for BSTs; worst-case time complexity in O(h) so in O(log n) successor, predecessor: algorithm as for BSTs; worst-case time complexity in O(h) so in O(log n) search: algorithm as for BSTs; worst-case time complexity in O(h) so in O(log n) insert, delete: first step as algorithms for BSTs; balance may be destroyed so we need to do more !

slide-4
SLIDE 4

binary search trees: recall insert

Algorithm insert(T, z): y := nil x := T.root while not x = nil do y = x if z.key < x.key then x := x.left else x := x.right z.p := y if y = nil then T.root := z else if z.key < y.key then y.left := z else y.right := z

example: insertion with left-left imbalance

adding yields left-left unbalanced node:

3 2 1

rebalance using single rotation of nodes with labels 3 and 2:

1 2 3

here h = −1, and x is node with label 2, and y is node with label 3

insertion with left-left unbalance

suppose node y is the lowest node that becomes unbalanced before insertion, its left subtree has height h + 1 and its right subtree (C) height h after insertion, its left subtree has height h + 2 and its right subtree has height h so left subtree is not empty; say it is node x with left subtree A and right subtree B suppose insertion takes place in subtree A: case left-left

insertion with left-left unbalance

so suppose insertion takes place in subtree A height of x increases from h + 1 to h + 2 so height of A increases from h to h + 1 x balanced after insertion so height of B is not h − 1 height x increases by insertion so height of B is not h + 1 so height of B is h recall: the height of C is h rebalance by single rotation

slide-5
SLIDE 5

left-left rebalancing: correctness

before insertion, the top node y in our analysis has height h + 2 after rotation, the top node x in our analysis has height h + 2 so no further actions are needed for rebalancing insertion is correct for BST trees (from BST theory) rotation preserves inorder; informally, A ≤ x ≤ B ≤ y ≤ C

left-left rebalancing general

left-left unbalanced node:

B C A

rebalance using single rotation (compare (ab)c = a(bc)):

A B C

example insertion with left-right inbalance

adding yields left-right unbalanced node

3 2 1

rebalance using double rotation

1 2 3

here h = −1, x is node with key 1 and y is node with key 3

insertion with left-right unbalance

suppose node y is the lowest node that becomes unbalanced before insertion, its left subtree has height h + 1 and its right subtree (C) height h after insertion, its left subtree has height h + 2 and its right subtree has height h so left subtree is not empty; say it is node x with left subtree A and right subtree B now suppose the insertion was in subtree B: case left-right

slide-6
SLIDE 6

insertion with left-right unbalance

height of x increases from h + 1 to h + 2 so height of B increases from h to h + 1 x balanced after insertion so height of A is not h − 1 height x increases by insertion so height of A is not h + 1 so height of A is h recall: the height of C is h rebalance by double rotation

left-right rebalancing: correctness

before insertion, the top node y in our analysis has height h + 2 after rotation, the top node z in our analysis has height h + 2 so no further actions are needed for rebalancing insertion is correct for BST trees (from BST theory) rotation preserves inorder; informally, A ≤ x ≤ B1 ≤ z ≤ B2 ≤ y ≤ C

left-right general

left-right unbalanced node

A B C D

rebalance using double rotation (cf.(a(bc)d) = (ab)(cd))

A B C D

symmetric cases

we have in total four cases for unbalance caused by insertion left-left and right-right left-right and right-left and that is all: in total four cases of imbalance due to insertion

slide-7
SLIDE 7

the four cases of unbalanced nodes after insertion

Left Left

  • 2
  • 1

A h + 1

inserted

B h C h Left Right

  • 2

1 A h B h + 1

inserted

C h

Right Right 2 1 A h B h C h + 1

inserted

Right Left 2

  • 1

A h B h + 1

inserted

C h

AVL-trees: insertion summary

first, insert new node using algorithm for BST second, identify the lowest unbalanced node

  • n the path from the new node to the root

third, rebalance that node using a single or double rotation then balance is restored we need at most 1 rebalance step insertion operation is in O(log n)

question: add node with key 0

8 5 3 2 1 4 6 7 10 9 12 11

binary search tree: recall deletion

Algorithm treeDelete(T, z): if z.left = nil then transplant(T, z, z.right) else if z.right = nil then transplant(T, z, z.left) else y := treeMinimum(z.right) if y.p = z then transplant(T, y, y.right) y.right := z.right y.right.p := y transplant(T, z, y) y.left := z.left y.left.p := y

slide-8
SLIDE 8

and pseudo-code for transplant

Algorithm transplant(T, u, v): if u.p = nil then T.root := v else if u = u.p.left then u.p.left := v else u.p.right := v if v = nil then v.p := u.p

removal of node with key k for BST

remove as for binary search tree restore balance

  • ne rebalancing step is not necessarily sufficient

we have 2 (symmetric) additional cases we do not need additional rules

revisit case left-left for removal

removal in subtree C can cause imbalance because height of C decreases from h + 1 to h single rotation for case left-left does not necessarily restore balance similarly: double rotation for case left-right does not necessarily restore balance

example new case: imbalance left

4 2 1

5

3

rebalance using single rotation

4

1 2 3

slide-9
SLIDE 9

the six cases of unbalanced nodes after removal

Left Left

  • 2
  • 1

A h + 1 B h C h

deleted

Left Right

  • 2

1 A h B h + 1 C h

deleted

Left

  • 2

A h + 1 B h + 1 C h

deleted

Right Right 2 1 A h

deleted

B h C h + 1 Right Left 2

  • 1

A h

deleted

B h + 1 C h Right 2 A h

deleted

B h + 1 C h + 1

AVL-trees: removal

first, remove node using the algorithm for BST second, consider the first unbalanced node

  • n the path from the removed node upwards

third, rebalance by considering the ‘heaviest child’ of the unbalanced node if necessary, consider the next unbalanced node and iterate rebalancing 6 possible cases of unbalanced nodes after update we do not need more rules for rebalancing removal is in O(log n)

example: remove node with key 9

8 5 3 2 1 4 6 7 10 9 12 11

time complexity of updating operations for AVL-trees

worst case time complexity of searching, adding, deleting is in O(log n) with n the number of nodes single and double rotations are in O(1)

slide-10
SLIDE 10
  • verview worst-case time complexities for ordered

dictionaries

search insert remove log file O(n) O(1) O(n) lookup table O(log n) O(n) O(n) binary search tree O(n) O(n) O(n) AVL-tree O(log n) O(log n) O(log n)

AVL-trees: inventors

Adelson-Velskii (1922-2014) and Landis (1921-1997) in 1962

material

background material on AVL trees: paper by Frank Pfenning additional material: wiki tree rotations