Binary Search Trees 15-121 Fall 2020 Margaret Reid-Miller Today - - PowerPoint PPT Presentation
Binary Search Trees 15-121 Fall 2020 Margaret Reid-Miller Today - - PowerPoint PPT Presentation
Binary Search Trees 15-121 Fall 2020 Margaret Reid-Miller Today Prune leaves example Binary Search Trees Definition Algorithms for Contains Add Remove Complexity Fall 2020 15-121 (Reid-Miller) 2 Remove all
Today
- Prune leaves example
- Binary Search Trees
- Definition
- Algorithms for
- Contains
- Add
- Remove
- Complexity
Fall 2020 15-121 (Reid-Miller) 2
Remove all leaves from tree t
public static <E> void prune(BTNode<E> t){ if (t == null) return; BTNode<E> left = t.getLeft(); if (left != null) { if (isLeaf(left)) t.setLeft(null); else } BTNode<E> right = t.getRight(); if (right != null) { if (isLeaf(right)) t.setRight(null); else } }
15-121 (Reid-Miller) Fall 2020 3
prune(left); prune(right);
Remove all leaves from tree t Return reference to the resulting tree
public static <E> BTNode<E> prune(BTNode<E> t){ What are the base case(s)? What does the recursive cases return? How do you update the tree? Submit answers to
https://forms.gle/a5qj8rTF1tndp1tH6
15-121 (Reid-Miller) Fall 2020 4
Remove all leaves from tree t Return reference to the resulting tree
public static <E> BTNode<E> prune(BTNode<E> t){ if (t == null || isLeaf(t)) return null; t.setLeft = prune(t.getLeft()); t.setRight = prune(t.getRight()); return t; }
15-121 (Reid-Miller) Fall 2020 5
More Terminology
- A perfect binary tree is a binary tree such that
- all leaves have the same level, and
- every non-leaf node has 2 children.
- A full tree is a binary tree such that
- every non-leaf node has 2 children.
- A complete binary tree is a binary tree such that
- every level of the tree has the maximum
number of nodes
- except possibly at the deepest level, where the
nodes are as far left as possible.
15-121 (Reid-Miller) Fall 2020 6
Examples
A G F E D C B F A E D C B
PERFECT (COMPLETE and FULL) COMPLETE (but not PERFECT and not FULL)
15-121 (Reid-Miller) Fall 2020 7
Binary Search Trees
Fall 2020 15-121 (Reid-Miller) 8
Binary Search Trees
- A binary tree T is a binary search tree if
- T is empty, or
- T has two subtrees, TL and TR, such that
- All the values in TL are less than the value
in the root of T,
- All the values in TR are greater than the
value in the root of T, and
- TL and TR are binary search trees
- Typically, duplicates are not allowed.
15-121 (Reid-Miller) Fall 2020 9
Example: Is this a BST?
10
4 7 1 6 9 8 3
yes no
3 4 7 1 6 9 8 3
Fall 2020 15-121 (Reid-Miller)
Elements of a BinarySearchTree must be Comparable
public class BinarySearchTree <E extends Comparable<E>> { private class BTNode { // inner class private E data private BTNode left; private BTNode right; private BTNode(E data) { this.data = data} } private BTNode root; // field public BinarySearchTree() { // constructor root = null; }
Fall 2020 15-121 (Reid-Miller) 11
Example : max
How did we find the maximum value in a Binary Tree? Recursively got the max of the root and the left and right subtrees. If the tree is a Binary Search Tree (BST), do we need to search the whole tree? No Where is the maximum in a Binary Search Tree? The right most node
Fall 2020 15-121 (Reid-Miller) 12
Where is the maximum? 15 40 26 45 62 11 96 39 83 21 52 45
15-121 (Reid-Miller) Fall 2020 13
96
The node with the maximum value in a BST is the rightmost node.
// recursive implementation public E max() { if (root == null) return null; return max(root); } //precondition: tree is not empty private E max(BTNode t) { if (_______________) return t.data; else return max(t.right); }
Fall 2020 15-121 (Reid-Miller) 14
Only one recursive call!
t.right == null
Searching for 83 and 42 62 11 96 39 83 21 45 62 11 96 39 83 21 45 62 96 83 62 11 39 45
NOT FOUND
15-121 (Reid-Miller) Fall 2020 15
// iterative implementation public boolean contains(E obj) { BTNode t = root; while (t != null) { if (obj.equals(t.data)) return true; else if (obj.compareTo(t.data) < 0) t = t.left; else t = t.right; } return false; }
To find an element follow a path in the BST
Fall 2020 15-121 (Reid-Miller) 17
Adding to a BST
- Where should we add an element to a BST?
- If the tree is empty,
make the element the root
- Otherwise attach it as a leaf node.
Fall 2020 15-121 (Reid-Miller) 18
Add to a BST in the order given
62 96 11 39 21 83 45
62 11 96 39 83 21 45
15-121 (Reid-Miller) Fall 2020 19
Add element as a leaf node
// recursive implementation public void add(E obj) { root = add(root, obj); } // returns a reference to a tree with obj in it private BTNode add(BTNode t, E obj) { if (t == null) return new BTNode(obj); else if (obj.compareTo(t.data) < 0) t.left = add(t.left, obj); else t.right = add(t.right, obj); // else already in the tree return t; }
Fall 2020 15-121 (Reid-Miller) 20
if (obj.compareTo(t.data) > 0)
Exercise: add
- Draw the binary search tree that results from
adding the each character to an empty tree in the following order: C A R N E G I E M E L L O N Do not add any duplicate values to your tree.
Fall 2020 15-121 (Reid-Miller) 24
C A R N E G I E M E L L O N
Fall 2020 15-121 (Reid-Miller) 25
Runtime of add on a perfect BST
- A perfect binary search tree with height H has how
many nodes?
- n = 1 + 2 + 4 + ... + 2H = 2H+1 - 1
- Thus, H = log2(n+1) - 1.
- Add will take O(log n) time on a perfect BST
- We have to examine one node at each level before
we find the insertion point, and height is H.
15-121 (Reid-Miller) Fall 2020 26
Worst-case runtime of add on a BST
- Are all BSTs perfect?
Add the following numbers in order into a BST: 11 21 39 45 62 83 96 What do you get?
- An add will take O(n) time in the worst case on an
arbitrary BST since there may be up to n levels in a BST with n nodes.
15-121 (Reid-Miller) Fall 2020 27
NO!
Aside: How many BSTs with n distinct values have height n?
Fall 2020 15-121 (Reid-Miller) 28
1 2 3 4 5 5 4 3 2 1 1 4 5 3 2
Total number BST with height n is 2n-1 That's a small fraction of the total number BST: O(4n)
Removing from a BST
General idea:
- Search for the item to remove until either:
- the item is found (at the root of a subtree) or
- we reach a leaf and the item is not found
- If the item is found:
- remove the root (of the subtree) and
- replace it with its closest value
(either its in-order predecessor or successor)
15-121 (Reid-Miller) Fall 2020 31
K D R O W C E L H S N M A T
Predecessor is largest value in left subtree Successor is smallest value in right subtree
Suppose we replace the root with its in-order predecessor.
Where is the in-order predecessor?
- n the left side and
- is the rightmost node (max on left side)
Three cases:
- 1. no predecessor
- 2. predecessor is left child
- 3. predecessor is rightmost of left child
Fall 2020 15-121 (Reid-Miller) 34
Case 1: The root has no predecessor
- Return the right subtree (to the parent of the
root)
15-121 (Reid-Miller) Fall 2020 35
r
S S
Removing from a BST
Data value is found at a root with no left child
- Return the right subtree
- Result is to set the parent reference to its grandchild.
- Example:
Remove 31. null
64
null 31
49
X
15-121 (Reid-Miller) Fall 2020 37
root:
Case 2: The predecessor is the left child of the root
- Copy the data from the root's left child into the root.
- Set the root's left reference to the left child of the root's
left child.
15-121 (Reid-Miller) Fall 2020 38
r p
S A
p
A S
root root Subtree A could be null
Removing from a BST
Predecessor is left child
- Example:
Remove 29. 45 29 17 null 36 6 17
X
15-121 (Reid-Miller) Fall 2020 39
predecessor of 29 root
Case 3: The root’s predecessor is rightmost of the left child
- Copy the data from the predecessor to the root
- Then set the predecessor's parent to reference its left
child.
15-121 (Reid-Miller) Fall 2020 40
r
A S G
p p
A S G
root root Subtree G could be null
Removing from a BST
Predecessor is right of left child
- Example:
Remove 29. 45 29 15 36 6 21 27 null 27
predecessor of 29
24
X
15-121 (Reid-Miller) Fall 2020 41
root
Complexity of Binary Search Trees
Worst case Best case contains O(height) O(1) add O(height) O(1) remove O(height) O(1) traverse O(n) Worst-case height of binary search tree: O(n) Best-case height of binary search tree: O(log n) Balanced Binary Search Trees have height O(log n): E.g, AVL, Red-Black, Splay, Treaps, Weight-balanced, …
Fall 2020 15-121 (Reid-Miller) 42