L05 BST
L05 BST
L05 BST
Overview
Binary Search Trees
http://izismile.com/tags/Gibraltar/
1 6
6.006 Lecture 5 7
Image from Wikipedia.org
1
9/22/2016
DS
three pointers:
5 12
left[x] : points to left child
right[x] : points to right child 1 6
p[x] : points to parent
E.g. key[x1]=10 7
left[x1]=x2
p[x2]=x1
p[x1]=NIL
2
9/22/2016
>key[x]
7 2 1
O(n) time!
3
9/22/2016
4
9/22/2016
49 Running time?
?
49
Running time?
5
41 6 ??41
41 5
6
4 3 4
37 6 7 ?6
46
5
9/22/2016
successor( 5 ) = 6 successor( 5 ) = 6
successor( 7 ) successor( 7 ) = 10
6
9/22/2016
Analysis Analysis
• Height can be n-1 1
• How much time do operations 5
take ? 10
• Still O(n) for the runway 6
• Worst case: O(height)
5 12 reservation system operations? 7
=> height really important
10
1 6 • Next lecture:
• After we insert n elements,
12
what is the worst possible BST
height?
7
Balanced BSTs