Binary Search Trees
Total Page:16
File Type:pdf, Size:1020Kb
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 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 prune(left); } BTNode<E> right = t.getRight(); if (right != null) { if (isLeaf(right)) t.setRight(null); else prune(right); } } Fall 2020 15-121 (Reid-Miller) 3 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 Fall 2020 15-121 (Reid-Miller) 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; } Fall 2020 15-121 (Reid-Miller) 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. Fall 2020 15-121 (Reid-Miller) 6 Examples A A B C B C D E F G D E F PERFECT COMPLETE (COMPLETE (but not PERFECT and FULL) and not FULL) Fall 2020 15-121 (Reid-Miller) 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. Fall 2020 15-121 (Reid-Miller) 9 Example: Is this a BST? 4 4 1 7 1 7 6 8 3 6 8 3 9 9 no yes Fall 2020 15-121 (Reid-Miller) 10 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? 62 15 11 96 40 39 83 26 45 21 52 Fall 2020 15-121 (Reid-Miller) 13 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 (_______________)t.right == null return t.data; else return max(t.right); } Only one recursive call! Fall 2020 15-121 (Reid-Miller) 14 Searching for 83 and 42 62 62 11 96 11 96 39 83 39 83 21 45 21 45 NOT FOUND Fall 2020 15-121 (Reid-Miller) 15 To find an element follow a path in the BST // 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; } 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 62 96 11 39 21 83 45 11 96 39 83 21 45 Fall 2020 15-121 (Reid-Miller) 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 if (obj.compareTo(t.data) > 0) t.right = add(t.right, obj); // else already in the tree return t; } Fall 2020 15-121 (Reid-Miller) 20 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. Fall 2020 15-121 (Reid-Miller) 26 Worst-case runtime of add on a BST • Are all BSTs perfect? NO! 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. Fall 2020 15-121 (Reid-Miller) 27 Aside: How many BSTs with n distinct values have height n? 1 5 1 2 4 5 3 3 2 4 2 4 3 5 1 Total number BST with height n is 2n-1 That's a small fraction of the total number BST: O(4n) Fall 2020 15-121 (Reid-Miller) 28 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) Fall 2020 15-121 (Reid-Miller) 31 A Predecessor is largest value in left subtree K Successor is smallest value in right subtree D R C E O W H M S L N T Suppose we replace the root with its in-order predecessor. Where is the in-order predecessor? • on 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) r S S Fall 2020 15-121 (Reid-Miller) 35 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. 64 null X root: null 31 49 Fall 2020 15-121 (Reid-Miller) 37 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. root r root p p S A S A Subtree A could be null Fall 2020 15-121 (Reid-Miller) 38 Removing from a BST Predecessor is left child • Example: 45 Remove 29. root 29 X 17 predecessor of 29 17 null 36 6 Fall 2020 15-121 (Reid-Miller) 39 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. root r root p A S A S p G G Subtree G could be null Fall 2020 15-121 (Reid-Miller) 40 Removing from a BST Predecessor is right of left child 45 • Example: root 29 Remove 29. 27 15 36 6 21 X 27 null predecessor of 29 24 Fall 2020 15-121 (Reid-Miller) 41 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.