Binary Search Trees 15-121 Fall 2020 Margaret Reid-Miller Today - - PowerPoint PPT Presentation

binary search trees
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

Binary Search Trees

15-121 Fall 2020 Margaret Reid-Miller

slide-2
SLIDE 2

Today

  • Prune leaves example
  • Binary Search Trees
  • Definition
  • Algorithms for
  • Contains
  • Add
  • Remove
  • Complexity

Fall 2020 15-121 (Reid-Miller) 2

slide-3
SLIDE 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 } 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);

slide-4
SLIDE 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

15-121 (Reid-Miller) Fall 2020 4

slide-5
SLIDE 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; }

15-121 (Reid-Miller) Fall 2020 5

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

15-121 (Reid-Miller) Fall 2020 6

slide-7
SLIDE 7

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

slide-8
SLIDE 8

Binary Search Trees

Fall 2020 15-121 (Reid-Miller) 8

slide-9
SLIDE 9

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

slide-10
SLIDE 10

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)

slide-11
SLIDE 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

slide-12
SLIDE 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

slide-13
SLIDE 13

Where is the maximum? 15 40 26 45 62 11 96 39 83 21 52 45

15-121 (Reid-Miller) Fall 2020 13

96

slide-14
SLIDE 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) { if (_______________) return t.data; else return max(t.right); }

Fall 2020 15-121 (Reid-Miller) 14

Only one recursive call!

t.right == null

slide-15
SLIDE 15

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

slide-16
SLIDE 16

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

slide-17
SLIDE 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

slide-18
SLIDE 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

slide-19
SLIDE 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)

slide-20
SLIDE 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

slide-21
SLIDE 21

C A R N E G I E M E L L O N

Fall 2020 15-121 (Reid-Miller) 25

slide-22
SLIDE 22

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

slide-23
SLIDE 23

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!

slide-24
SLIDE 24

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)

slide-25
SLIDE 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)

15-121 (Reid-Miller) Fall 2020 31

slide-26
SLIDE 26

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

slide-27
SLIDE 27

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

slide-28
SLIDE 28

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

slide-29
SLIDE 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. null

64

null 31

49

X

15-121 (Reid-Miller) Fall 2020 37

root:

slide-30
SLIDE 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.

15-121 (Reid-Miller) Fall 2020 38

r p

S A

p

A S

root root Subtree A could be null

slide-31
SLIDE 31

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

slide-32
SLIDE 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.

15-121 (Reid-Miller) Fall 2020 40

r

A S G

p p

A S G

root root Subtree G could be null

slide-33
SLIDE 33

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

slide-34
SLIDE 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