Binary Search Trees
Table of Contents
Adapted from Lenhart and Jannen
- https://williams-cs.github.io/cs136-f20-www/slides/BinarySearchTreesIa.pdf
- https://williams-cs.github.io/cs136-f20-www/slides/BinarySearchTreesIb.pdf
1 Reading
Read Algorithms 3.2
2 Video
Here is a video lecture for today's lesson. It's on the longer side, so it's okay if you don't get through the whole thing before Monday's class. The video for Wednesday will be shorter to give you time to catch up.
The video contains sections on
- Learning goals (0:09)
- Defining a binary search tree (0:53)
- Example BSTs (4:28)
- Animations of search and insert operations (8:07)
- How to handle removing from a BST (12:14)
- Animations of removal operations (25:21)
- BST implementation (30:52)
The Panopto viewer has table of contents entries for these sections: https://carleton.hosted.panopto.com/Panopto/Pages/Viewer.aspx?id=20443fd6-dac0-4d93-87c6-accd0171fcdd
3 Learning Goals
After this lesson you will be able to
- Identify when a binary tree meets the definition of a binary search tree
- Use a binary search tree to implement the Map ADT
- Perform and implement the
contains
,get
, andput
operations - Describe the four cases for
remove
on a binary search tree
- Perform and implement the
- Perform and implement
firstKey
andlastKey
operations
4 Defining a Binary Search Tree (BST)
- Definition: A BST T is either:
- Empty
- Has root r with subtrees TL and TR such that
- All nodes in TL have smaller key than r (or are empty)
- All nodes in TR have larger key than r (or are empty)
- TL and TR are also BSTs
4.1 Examples
valid:
valid:
valid:
valid:
valid:
INVALID:
5 BST Operations
5.1 Animations for searching and insertion
5.2 Removal
Removing the root is a (not so) special case
- Let's figure that out first
- If we can remove the root, we can remove any element in a BST in the same way
- Every node is the root of a subtree
- After removing node as root of its own subtree, add subtree back as child of node's parent
5.2.1 Case 4: General Case
- Consider BST requirements:
- Left subtree must be <= root
- Right subtree must be > root
- Strategy: replace the root with the largest value that is less than or equal to it
- predecessor(root) : rightmost left descendant
- Alternatively, could use successor(root): leftmost right descendant
- This may require reattaching the predecessor's left subtree!
5.3 Animations for removal
6 Imeplementing a BST
See accomanying BST.java
for:
BST
class- inner
Node
class with key and value - implementation of
get
,contains
, andput
- inner
7 Practice Problems1
- What distinguishes a binary search tree from a binary tree?
- Suppose values have only been added into a binary search tree. Where is the first node added to the tree? Where is the last node added to the tree?
Which of the following trees are valid binary search trees?
(A)
(B)
(C)
(D)
(E)
- For each of the next four problems, draw the binary search tree that would result if the given elements were added to an empty binary search tree in the given order.
- Leia, Boba, Darth, R2D2, Han, Luke, Chewy, Jabba
- Meg, Stewie, Peter, Joe, Lois, Brian, Quagmire, Cleveland
- Kirk, Spock, Scotty, McCoy, Chekov, Uhuru, Sulu, Khaaaan!
- Lisa, Bart, Marge, Homer, Maggie, Flanders, Smithers, Milhouse
8 Learning Block
Footnotes:
Solutions:
- A binary search tree is one that is ordered such that smaller nodes appear to the left and larger nodes appear to the right.
- The first node is at the root, the last node is at a leaf.
- Valid binary search trees: 2, if duplicates are allowed; 3; and 5.
+--------+ | Leia | +--------+ / \ / \ +--------+ +--------+ | Boba | | R2D2 | +--------+ +--------+ \ / \ / +--------+ +--------+ | Darth | | Luke | +--------+ +--------+ / \ / \ +--------+ +--------+ | Chewy | | Han | +--------+ +--------+ \ \ +--------+ | Jabba | +--------+
+-----+ | Meg | +-----+ / \ / \ +-----+ +--------+ | Joe | | Stewie | +-----+ +--------+ / \ / / \ / +-------+ +------+ +-------+ | Brian | | Lois | | Peter | +-------+ +------+ +-------+ \ \ \ \ +-----------+ +----------+ | Cleveland | | Quagmire | +-----------+ +----------+
+------------+ | Kirk | +------------+ / \ / \ / \ +------------+ +------------+ | Chekov | | Spock | +------------+ +------------+ \ / \ \ / \ \ / \ +------------+ +------------+ +------------+ | Khaaaan! | | Scotty | | Uhuru | +------------+ +------------+ +------------+ / / / / / / +------------+ +------------+ | McCoy | | Sulu | +------------+ +------------+
+------------+ | Lisa | +------------+ / \ / \ / \ +------------+ +------------+ | Bart | | Marge | +------------+ +------------+ \ / \ \ / \ \ / \ +------------+ +------------+ +------------+ | Homer | | Maggie | | Smithers | +------------+ +------------+ +------------+ / / / / / / +------------+ +------------+ | Flanders | | Milhouse | +------------+ +------------+