Académique Documents
Professionnel Documents
Culture Documents
ESO207
Indian Institute of Technology, Kanpur
Introduction
We will now begin our study of the Search trees data
even as a priority-queue.
The basic operations on binary search trees take time
proportional to the height of the tree.
1. For a complete binary tree with n nodes, these operations
run in time (log n).
2. However, if the tree is a linear chain of n nodes, the same
operations take worst-case (n) time.
Introduction
struct BSTNode{
BSTNode *left
BSTNode *right
BSTNode *parent
KEY key
}
y .key x.key .
Remark
Remark. Note that the following condition does not
(recursively).
on left subtree.
If right child exists, then, recursively call preorder
on left subtree.
If right child exists, then, recursively call postorder
Remark on traversals
A single traversal sequence does not uniquely determine
postorder traversals.
Remark on traversals
Example
subtree.
This holds for each sub-tree.
Inorder sequence is
3 6 6 8 10 11 13 13
The key T .key at the root node partitions the set of keys
Search: pseudo-code
A recursive search procedure
S EARCH(BST T , Key k )
// Search for a node with key k in binary search tree T
1. if T == NIL or T .key == k
2.
return T
3. if k < T .key
4.
S EARCH(T .left, k )
5. else
6.
S EARCH(T .right, k )
F IND -M INIMUM(T )
// finds the minimum key in the binary search tree
1. x = T
1. while x.left 6= NIL
2.
x = x.left
3. return x.key
Maximum
x.key .
Then, its successor node is a node y with key
y .key > x.key and such that y .key is the smallest key
value in the tree that is larger than x.key .
(Note that this holds only for binary search tree s with
consider.
The first is when x has a right child.
In this case, the successor node is in right subtree of x.
Successor: Case 1a
Successor: Case 1b
Successor: Case 1
Successor: Case 2
left subtree of a.
This node is the successor of x.
Note: lowest ancestor satisfying a property means the
S UCCESSOR(T , x)
1. if x.right 6= NIL
2.
return F IND -M INIMUM(x)
3. else
4.
y =x
5.
z = y .parent
6.
while z 6= NIL and z.right == y
7.
y =z
8.
z = z.parent
9.
return z
Proof of Case 2
Let x be a node with no right child and let a be the
lowest ancestor a of x such that x lies in the left subtree
of a. Then, a is the successor node of x.
Proof:
Let a be the lowest ancestor of x such that x lies in the
left-subtree of a.
Since x lies in the left-subtree of x, x.key a.key .
Further, in the inorder traversal of the tree, a appears
after x.
Any node b strictly between a and x has key at most
x.key and its left subtree has keys at most b.key x.key .
Hence none of these nodes or nodes in their left-subtree
can be a successor of x.
Also none of the nodes in the right subtree of a can be a
successor of x since they have keys at least
a.key x.key .
Proof of Case 2
left-subtree of a0 .
Thus, a0 or any of the nodes in the left-subtree of a0 can
Predecessor
Predecessor: Pseudo-code
P REDECESSOR(T , x)
1. if x.left 6= NIL
2.
return F IND -M AXIMUM(x)
3. else
4.
y =x
5.
z = y .parent
6.
while z 6= NIL and z.left == y
7.
y =z
8.
z = z.parent
9.
return z