0% found this document useful (0 votes)
46 views

Binary Search Trees: Lecture 10, Oct 28 2014

The document discusses binary search trees and their properties. It describes how binary search trees can support insertion, deletion, searching, finding the minimum/maximum element as well as the predecessor and successor of a node. The operations take time proportional to the height of the tree. However, the height can become large, so techniques like red-black trees are used to rebalance the tree and guarantee an height of O(log n).

Uploaded by

anon_324341592
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views

Binary Search Trees: Lecture 10, Oct 28 2014

The document discusses binary search trees and their properties. It describes how binary search trees can support insertion, deletion, searching, finding the minimum/maximum element as well as the predecessor and successor of a node. The operations take time proportional to the height of the tree. However, the height can become large, so techniques like red-black trees are used to rebalance the tree and guarantee an height of O(log n).

Uploaded by

anon_324341592
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Lecture 10, Oct 28 2014

Binary Search Trees

 In addition to insert/delete:
» Heaps supported min/max.
» Hashing supported search.
» What if we want both min/max/search, and also pred/succ ?

 Binary Search trees:


x
a  left tree key(a)  key( x)
a  right tree key(a)  key( x)
y z

122

Examples
 Legal Binary Search Trees: 25
8
9 30
3 25
8

1 5 9 30 5

 In-Order traversal: InOrder(left(x)) 1


print(x)
InOrder(right(x))

 Note that given Binary tree, can output sorted in O(n) time !
Gives lower bound on constructing Binary Tree.
(Compare with building a Heap)

123

1
Searching in Binary Tree
8
 Check if “current node” is =x or =NIL.
Equal to x -- return,
Equal to NIL -- return “not found” 3 25
else:
if x  current key ---- search in the left tree
otherwise ---- search in the right tree.
1 5 9 30

 “Go down the tree, turning right/left as appropriate…”

 Running time: (h), h=height of the tree.

 Note that this was impossible to do with a heap

124

Inserting into Binary Tree

 Insertion: search for key, and put it in the first empty space.

 Insertion takes (h).


25

 Sort: 9
» Insert item-by-item,
» in-order walk. 8

» (n2)… 5

125

2
Relation to Quicksort

 Randomly permute input.

 Consider example: 3 1 8 2 6 7 5
» Quicksort chooses 3, then compares 1,8,2,6,7,5 to 3.
Then chooses 1, compares to 2
chooses 8, compares 6,7,5 to 8.
» Binary Tree: chooses 3, places as root
Then chooses 1, compares with 3, put in place.
chooses 8, compares with 3, put in place
etc…
» Overall, same comparisons, only different order !!

126

Successor/Predecessor
 Min/max - go all the way left or all the way right.

 Successor:
If right(x) != NIL, return TreeMin(right(x)) Easy case
y=parent(x)
while y != NIL & x=right(y)
x=y
y=parent(x) Go up the tree to the LEFT
return y

3 25

1 5 9 30

4 6

 Successor of 6 is 8. Predecessor of 8 is 6.
 What if we go “up left” and reach root, i.e. cannot ever go “up right” ?
127

3
Deletion

 3 Cases:
» No children – simple delete, replace pointer from parent with null
» 1 child – delete, redirect pointer from parent to point to child
» 2 children – cannot simply delete

 3rd case: put successor(x) instead of x.


» Binary Tree property satisfied.
» Delete “hole” using case 1 or 2.
» Why ??
» (successor does not have left child !)

128

Delete element - example

8
8

3 25
4 25

1 5 9 30
1 5 9 30

4 6
4 6
4.5
8 4.5

3 25

1 5 9 30

4.5 6

4.5 129

4
Summary

 Data structure implements insert/delete/search/pred/successor


 Speed depends on height of the tree, in general can be large
 Need a way to “rebalance the tree”
 Possible to rebalance so that height < 2log n
 Basic operation: “Rotation”

130

Red/Black trees

 One of the ways to approximately balance, i.e. depth O(lg n).

 Red/Black property:
» Node is either black (default) or red.
» x red children(x) are black.
» Every simple root-leaf path has same # of black nodes.
» Root is black (for convenience).
» Add NIL to leaves so “real nodes” have 2 children, color leaves black.

 One of the ways to approximately balance.


» We will show that above condition implies depth O(lg n).

131

5
Black height

 bh(x) = # of black nodes on x leaf path, not counting x.

132

Balancing

 Observation: bh(root)h/2

 THM: h  2 log(n+1)
Proof:
» Claim: #nodes in subtree rooted at x 2bh(x)-1.
» By induction on h(x). Base case is trivial. x

» if y black, bh(y)=bh(x)-1 y z
otherwise, bh(y)=bh(x)
» Thus, bh(y) bh(x)-1

» #nodes in x-tree at least (2bh(x)-1-1)2+1= 2bh(x)-1,


which proves the claim.
» But: bh(root)  h/2, QED.

133

You might also like