Professional Documents
Culture Documents
Insertion Deletion
6 6
To perform operation < To perform operation <
inser(k, o), we search for 2 9 remove(k), we search for 2 9
> >
key k (using TreeSearch) key k
1 4 8 1 4 v 8
Assume k is not already in > Assume key k is in the tree,
w
the tree, and let let w be and let let v be the node 5
the leaf reached by the w storing k
search If node v has a leaf child w,
6
We insert k at node w and we remove v and w from the
expand w into an internal tree with operation 6
2 9
node removeExternal(w), which
2 9
Example: insert 5 1 4 8 removes w and its parent
w Example: remove 4 1 5 8
5
© 2004 Goodrich, Tamassia Binary Search Trees 7 © 2004 Goodrich, Tamassia Binary Search Trees 8
Deletion (cont.) Performance
1
We consider the case where v Consider a dictionary
3
the key k to be removed is with n items
stored at a node v whose 2 8 implemented by means
children are both internal 6 9 of a binary search tree
we find the internal node w w of height h
that follows v in an inorder 5
the space used is O(n)
traversal z methods find, insert and
we copy key(w) into node v remove take O(h) time
we remove node w and its
left child z (which must be a
1
v The height h is O(n) in
leaf) by means of operation 5 the worst case and
removeExternal(z) 2 8 O(log n) in the best
Example: remove 3 6 9
case
© 2004 Goodrich, Tamassia Binary Search Trees 9 © 2004 Goodrich, Tamassia Binary Search Trees 10