Lecture 19
Learning objectives
After this class, you should be able to:
- Given a BST and a sequence of insert and delete operations on it, draw the tree that results from these operations.
- Find the smallest and largest elements in a tree, and the successor and predecessor of an element.
- Write code to implement features of a binary search tree, such as insertion, deletion, finding the smallest and largest element, and finding the successor and predecessor of an element.
- Derive the time complexities for the above operations on a binary search tree.
- Give that advantages and disadvantages of a binary search tree over other data structures that we have discussed in class.
- Identify applications where a binary search tree will be useful.
Reading assignment
- Section 6.5 (excluding threaded trees) and section 6.6 (excluding 6.6.1).
- None.
Exercises and review questions
- Exercises and review questions on current lecture's material
- Insert the following elements into a binary search tree in the order given below and draw the resulting binary search tree: 11, 7, 5, 13, 9, 15.
- Draw the binary search tree constructed by inserting the following numbers in the order given below, and then answer the following questions: 42, 36, 71, 18, 39, 53, 80, 5, 22, 64, 79.
- List the nodes encountered when you search for the number
22
.
- Draw the tree that results after deleting the node with value
22
.
- Draw the tree that results after deleting the node with value
80
from the tree in (2).
- Draw the tree that results after deleting the node with value
42
from the tree in (3).
- In any question below, you may assume that functions to implement features that precede that question exit.
- Write code to find the smallest element of a BST. (If your group Number % 8 = 0)
- Write code to find the largest element of a BST. (If your group Number % 8 = 1)
- Write code to delete a node that has fewer than 2 children. (If your group Number % 8 = 2)
- Write code to find the successor of a node that has a right child. (If your group Number % 8 = 3)
- Write a code or pseudocode to find the successor of a node that has no right child. You may assume that pointers to parents exist. (If your group Number % 8 = 4)
- Write code to find the predecessor of a node that has a left child. (If your group Number % 8 = 5)
- Write a code or pseudocode to find the predecessor of a node that has no left child. You may assume that pointers to parents exist. (If your group Number % 8 = 6)
- Write code to delete a node that has two children (If your group Number % 8 = 7)
- Give an application where a BST is better than other data structures discussed in class, and one where another data structure is better than a BST.
- Questions on next lecture's material
- None.
Last modified: 26 Oct 2004