binary search trees
play

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


  1. Binary Search Trees 15-121 Fall 2020 Margaret Reid-Miller

  2. Today • Prune leaves example • Binary Search Trees • Definition • Algorithms for • Contains • Add • Remove • Complexity Fall 2020 15-121 (Reid-Miller) 2

  3. 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); prune(right); else } } Fall 2020 15-121 (Reid-Miller) 3

  4. 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

  5. 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

  6. 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

  7. 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

  8. Binary Search Trees Fall 2020 15-121 (Reid-Miller) 8

  9. Binary Search Trees • A binary tree T is a binary search t ree if • T is empty, or • T has two subtrees, T L and T R , such that • All the values in T L are less than the value in the root of T, • All the values in T R are greater than the value in the root of T, and • T L and T R are binary search trees • Typically, duplicates are not allowed. Fall 2020 15-121 (Reid-Miller) 9

  10. Example: Is this a BST? 4 4 1 7 1 7 8 3 6 8 6 9 3 3 9 yes no Fall 2020 15-121 (Reid-Miller) 10

  11. 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

  12. 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

  13. Where is the maximum? 62 15 96 96 11 40 39 83 26 45 45 21 52 Fall 2020 15-121 (Reid-Miller) 13

  14. 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) { t.right == null if (_______________) return t.data; else return max(t.right); } Only one recursive call! Fall 2020 15-121 (Reid-Miller) 14

  15. Searching for 83 and 42 62 62 62 62 96 96 96 11 11 11 39 83 83 39 39 83 21 45 21 45 45 NOT FOUND Fall 2020 15-121 (Reid-Miller) 15

  16. 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

  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

  18. Add to a BST in the order given 62 96 11 39 21 83 45 62 96 11 39 83 21 45 Fall 2020 15-121 (Reid-Miller) 19

  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

  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

  21. C A R N E G I E M E L L O N Fall 2020 15-121 (Reid-Miller) 25

  22. Runtime of add on a perfect BST • A perfect binary search tree with height H has how many nodes? • n = 1 + 2 + 4 + ... + 2 H = 2 H+1 - 1 • Thus, H = log 2 (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

  23. 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

  24. Aside: How many BSTs with n distinct values have height n? 1 1 5 5 2 4 2 3 3 4 4 2 3 1 5 Total number BST with height n is 2 n-1 That's a small fraction of the total number BST: O(4 n ) Fall 2020 15-121 (Reid-Miller) 28

  25. 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

  26. 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

  27. 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

  28. 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

  29. 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 null 31 root: 49 Fall 2020 15-121 (Reid-Miller) 37

  30. 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. p r root root p A S S Subtree A could be null A Fall 2020 15-121 (Reid-Miller) 38

  31. Removing from a BST Predecessor is left child • Example: 45 Remove 29. root 29 17 X 17 null 36 predecessor of 29 6 Fall 2020 15-121 (Reid-Miller) 39

  32. 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. p root r root S A A S p G G Subtree G could be null Fall 2020 15-121 (Reid-Miller) 40

  33. Removing from a BST 45 Predecessor is right of left child • Example: 29 root Remove 29. 27 15 36 6 21 X 27 null predecessor of 29 24 Fall 2020 15-121 (Reid-Miller) 41

  34. 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

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend