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 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
3 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
3.1 Examples
valid:
valid:
valid:
valid:
valid:
INVALID:
3.2 Exercises: practice problem 3; all possible BSTs with 1, 2, 3, 4
4 BST Operations
4.1 Animations for searching and insertion
4.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
4.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!
4.3 Animations for removal
5 Imeplementing a BST
See accomanying BST.java
for:
BST
class- inner
Node
class with key and value - implementation of
get
,contains
, andput
- inner
6 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
Footnotes:
1
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: (B), if duplicates are allowed; (C); and (E).
+--------+ | 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 | +------------+ +------------+