data structures and algorithms 2015 09 21
lecture 7
schema
binary search trees
AVL-trees
material
schema
binary search trees
AVL-trees material
binary search tree
binary tree: linked data structure with nodes containing
• x .key from a totally ordered set
• x .left points to left child of node x
• x .right points to right child of nodex
• x .p points to parent of node x
binary search tree property:
for every node x with key k we have:
its left sub-tree contains only keys less than (equal to) k its right sub-tree contains only keys greater than (equal to) k
binary search tree: example
6 2 1 4 9 8inorder traversal
flattening the binary search tree yields an ordered sequence
Algorithm inOrder(v ): if not v = nil then inOrder(v .left) print v .key inOrder(v .right)
inorder: time complexity
we visit all nodes, so in Ω(n)
recurrence equation with k nodes in the left sub-tree
T (0) = c
T (n) = T (k) + T (n − k − 1) + d we show: T (n) ≤ (c + d ) · n + c
this yields: T (n) in O(n) hence T (n) in Θ(n)
questions
what are the binary search trees with keys 1, 2, 3 give a min-heap that is not a binary search tree give a binary search tree that is not a min-heap
searching in a binary search tree: example
look for key 4
6 2 1 4 9 8 < >
searching in a binary search tree
input: node v and key k
Algorithm treeSearch(v , k): if v = null or k = v .key then
return v if k < v .key then
return treeSearch(v .left, k) else
treeSearch(v .right, k) alternative: use a while-loop
what is the worst-case running time of treeSearch? in O(h)
binary search tree: search smallest key
as far as possible to the left
6 2
1 4
9 8
binary search tree: search largest key
as far as possible to the right
6 2
1 4
9 8
minimum and maximum: pseudo-code
Algorithm treeMimimum(x ): while x .left 6= nil do
x := x .left return x
Algorithm treeMaximum(x ): while x .right 6= nil do
x := x .right return x
question
given a node
how to compute the node that is visited next in inorder traversal?
successor: pseudo-code
input: node x
Algorithm treeSuccessor(x ): if x .right 6= nil then
return treeMimimum(x .right) y := x .p
while y 6= nil and x = y .right do x := y
y := y .p return y
what happens if x contains the largest key?
what is the worst-case running time of treeSuccessor?
adding: example
add node with key 5 6 2 1 4 9 8 < > > 6 2 1 4 5 9 8
binary search tree: adding
Algorithm insert(T , z): y := nil
x := T .root
while not x = null do y = x
if z.key < x .key then x := x .left else x := x .right z.p := y if y = null then T .root := z
else if z.key < y .key then y .left := z
else y .right := z
example removal easy case
6 2 1 4 9 8 7 remove 6 2 1 4 8 7
example removal difficult case
6 2 1 4 9 8 7 remove 7 2 1 4 9 8
removal
remove node z from binary search tree T if z has at most 1 child thentransplant
if z has 2 children then take treeMinimum of right subtree transplant that one on the place of z
removal: pseudo-code
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 6= 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
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 6= nil then
v .p := u.p
binary search tree
operations 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 ofbalanced binary search tree
schema
binary search trees AVL-trees
material
AVL-tree: definition
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 0 2 8 6 4 7 9 3 1 0 0 0 0 1 2
ALV-tree: height
height of an AVL-tree with n nodes is in O(log n) (no proof)
operations on AVL-trees
search: exactly as for binary search trees
worst-case time complexity is in O(h) so in O(log n)
add: as for binary search trees, and then restore balance
remove: as for binary search trees, and then restore balance
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
example: left-left
adding yields left-left unbalanced node:
3
2
1
rebalance using single rotation:
1
2
3
left-left general
left-left unbalanced node
B C
A
rebalance using single rotation: (compare (ab)c = a(bc))
A
symmetry: right-right
right-right unbalanced node rebalance using single rotation
example: left-right
adding yields left-right unbalanced node
3
2
1
rebalance using double rotation:
1
2
3
left-right general
left-right unbalanced node
A
B C
D
rebalance using double rotation
A B C D
symmetry: right-left
right-left unbalanced node rebalance using double rotation
AVL-trees: insertion
step 1: insert new node as for binary search tree step 2: identify the first unbalanced node on the path from the new node to the root
step 3: rebalance that node (there are 4 possible cases)
we need at most 1 rebalance (single or double rotation) step (why?) 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
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 0 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 0 A h deleted B h+ 1 C h+ 1
example: left
removal yields left unbalance
4
2
1
5
3
rebalance using single rotation
4 1
2
symmetry: right
symmetry gives unbalance right rebalance using single rotation
AVL-trees: removal
step 1: remove node as for binary search trees step2: walk from the removal point upwards and rebalance the first unbalanced node
how to rotate ? take the heaviest child step 3: go back to step 1 if necessary
removal: remarks
6 possible cases of unbalanced nodes after update we do not need more rules for rebalancing
after removal we possibly need more than one rebalance step removal is in O(log n)
example: remove node with key 9
8 5 3 2 1 4 6 7 10 9 12 11
overview 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
schema
binary search trees
AVL-trees
material
material
wiki binary search tree wiki AVL-tree
how many binary trees with n nodes exist?
Cn= 1 n + 1 2n nfirst 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,