Binary Search Tree Property: Let be a node in a BST. If is a node in the left subtree of , then . If is a node in the right subtree of , then
Preorder/inorderpostorder traversal
Querying a binary search tree:
Tree-Search procedure:
- To search for key in the entire BST
TREE-SEARCH(x,k)
if x == NIL or k == x.key
return key
if k < x.key
return TREE-SEARCH(x.left, k)
else return TREE-SEARCH(x.right, k)
ITERATIVE-TREE-SEARCH(x,k)
while x != NIL and k != x.key
if k < x.key
x = x.left
else
x = x.right
return x
TREE-Minimum(x)
while x.left != NIL
x = x.left
return x
TREE-SUCCESSOR(x)
if x.right != NIL
return TREE-MINIMUM(x.right)
else:
y = x.p
while y != NIL and x == y.right
x = y
y = y.p
return y
The running time of TREE-SUCCESSOR on a tree of height is .
TREE-INSERT (T,z)
x = T.root // node being compared with z
y = NIL // y will be parent of z
while x != NIL // descend until reaching a leaf
y = x
if z.key < x.key
x = x.left
else
x = x.right
z.p = y // foud the location - insert z with parent y
if y == NIL
T.root = z
elseif z.key < y.key
y.left = z
else y.right = z
Tree-TRANSPLANT(T, u, v)
TREE-DELETE(T,z)