lec7

advertisement
Binary Search Trees
Intro 1
What is a binary tree?
 Property 1: each node can have up to two
successor nodes.
btrees - 2
Intro 2
What is a binary tree? (cont.)
 Property 2: a unique path exists from the
root to every other node
Not a valid binary tree!
btrees - 3
Intro 3
Some terminology




btrees - 4
The successor nodes of a node are called its children
The predecessor node of a node is called its parent
The "beginning" node is called the root (has no parent)
A node without children is called a leaf
Intro 4
Some terminology (cont’d)
 Nodes are organize in levels (indexed from 0).
 Level (or depth) of a node: number of edges in the path
from the root to that node.
 Height of a tree h: #levels = L
(Warning: some books define h
as #levels-1).
not full!
 Full tree: every node has exactly
two children and all the
leaves are on the same level.
btrees - 5
Intro 5
What is the max #nodes
at some level l?
The max #nodes at level
isl
2l where l=0,1,2, ...,L-1
 20
 21
 22
 23
btrees - 6
Intro 6
What is the total #nodes N
of a full tree with height h?
N  2  2  ...  2
0
l=0
1
l=1
x  x  ...  x
0
1
h 1
l=h-1
n 1
n 1
 x 
i
i 0
btrees - 7
 2 1
h
x n 1
x 1
Intro 7
Why is h important?
 Tree operations (e.g., insert, delete, retrieve etc.)
are typically expressed in terms of h.
 So, h determines running time!
btrees - 8
Intro 8
Binary Search Trees
 View today as data structures that can support
dynamic set operations.
» Search, Minimum, Maximum, Predecessor,
Successor, Insert, and Delete.
 Can be used to build
» Dictionaries.
» Priority Queues.
 Basic operations take time proportional to the
height of the tree – O(h).
btrees - 9
Intro 9
BST – Representation
 Represented by a linked data structure of nodes.
 root(T) points to the root of tree T.
 Each node contains fields:
»
»
»
»
btrees - 10
key
left – pointer to left child: root of left subtree.
right – pointer to right child : root of right subtree.
p – pointer to parent. p[root[T]] = NIL (optional).
Intro 10
Binary Search Tree Property
 Stored keys must satisfy
the binary search tree
property.
»  y in left subtree of x,
then key[y]  key[x].
»  y in right subtree of x,
then key[y]  key[x].
» X (pointer) any node in a
tree
12
btrees - 11
56
26
200
28
18
24
190
213
27
Intro 11
Tree Search
Tree-Search(x, k)
1. if x = NIL or k = key[x]
2. then return x
3. if k < key[x]
4. then return Tree-Search(left[x], k)
5. else return Tree-Search(right[x], k)
56
26
btrees - 12
28
18
Running time: O(h)
Aside: tail-recursion
200
12
24
190
213
27
Intro 12
Finding Min & Max
The binary-search-tree property guarantees that:
» The minimum is located at the left-most node.
» The maximum is located at the right-most node.
Tree-Minimum(x)
1. while left[x]  NIL
2. do x  left[x]
3. return x
2.
Tree-Maximum(x)
1. while right[x]  NIL
do x  right[x]
3. return x
Q: How long do they take?
btrees - 13
Intro 13
BST Insertion – Pseudocode
 Change the dynamic set
represented by a BST.
 Ensure the binarysearch-tree property
holds after change.
 Insertion is easier than
deletion.
56
26
200
28
18
12
btrees - 14
24
27
190
213
Tree-Insert(T, z)
1. y  NIL
2. x  root[T]
3. while x  NIL
4.
do y  x
5.
if key[z] < key[x]
6.
then x  left[x]
7.
else x  right[x]
8. p[z]  y
9. if y = NIL
10.
then root[t]  z
11.
else if key[z] < key[y]
12.
then left[y]  z
13.
else right[y]  z
Intro 14
Analysis of Insertion
 Initialization: O(1)
 While loop in lines 3-7
searches for place to
insert z, maintaining
parent y.
This takes O(h) time.
 Lines 8-13 insert the
value: O(1)
 TOTAL: O(h) time to
insert a node.
btrees - 15
Tree-Insert(T, z)
1. y  NIL
2. x  root[T]
3. while x  NIL
4.
do y  x
5.
if key[z] < key[x]
6.
then x  left[x]
7.
else x  right[x]
8. p[z]  y
9. if y = NIL
10.
then root[t]  z
11.
else if key[z] < key[y]
12.
then left[y]  z
13.
else right[y]  z
Intro 15
Download