CSC312 -- Lecture 5b
CSC312 -- Lecture 5b
Authors
Michael T. Goodrich, Roberto Tamassia, and David M.
Mount.
2
Recap – What are Trees?
3
Binary Search Trees
4
An inorder traversal of a binary search trees visits the keys in
increasing order
2 9
1 4 8
6
Algorithm TreeSearch(k, v)
if v.isExternal ()
return v
if k < v.key()
return TreeSearch(k, v.left())
else if k = v.key()
return v
else { k > v.key() }
return TreeSearch(k, v.right())
Call TreeSearch(4,root) 7
Insertion
10
Deletion: Three Cases
Case 1: Deleting Node “5”
If the node has no children, then
release the memory occupied by this
node.
The pointer that originally points to
this child is set to NULL. The
figure on the slide illustrates this
scenario
11
Deletion: Three Cases
Case 2: Deleting Node “14”
12
What about “remove 1”?
14
Example: remove 3 15
Printing a Binary Tree
16