Binary Search Tree
Binary Search Tree
Binary Trees
• Recursive definition
1. An empty tree is a binary tree
2. A node with two child subtrees is a binary tree
3. Only what you get from 1 by a finite number of
applications of 2 is a binary tree. 56
26 200
12 24 27
12 24 27
Inorder-Tree-Walk
Inorder-Tree-Walk(x) (x)
1. ififxxNIL
1. NIL 56
2.
2. thenthenInorder-Tree-Walk(lef[p])
Inorder-Tree-Walk(lef[p])
26 200
3.
3. print
printkey[x]
key[x] 18 28 190 213
4.
4. Inorder-Tree-Walk(right[p])
Inorder-Tree-Walk(right[p]) 12 24 27
18 28 190 213
Running time: O(h)
12 24 27
4.
4. elsexx
else right[x]
right[x]
5.
5. return
returnxx
12 24 27
Tree-Minimum(x)
Tree-Minimum(x) Tree-Maximum(x)
Tree-Maximum(x)
1.
1. while lef[x]NIL
whilelef[x] NIL 1.
1. while right[x]NIL
whileright[x] NIL
2.
2. do doxxlef[x]
lef[x] 2.
2. doxx
do right[x]
right[x]
3.
3. return
returnxx 3.
3. return
returnxx