Docsity

Lecture 22: Binary Search Trees, Study notes of Data Structures and Algorithms

Partial preview of the text

Download Lecture 22: Binary Search Trees and more Study notes Data Structures and Algorithms in PDF only on Docsity!

CS 62

Fall 2018

Alexandra Papoutsaki & William Devanny

Lecture 22: Binary Search Trees

public class Student { private String name; private int age; public Student(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public int getAge() { return age; } @Override public String toString() { return "name=" + this.getName()+" age="+this.getAge(); } } How can you compare students by name in ascending order? How can you compare students by age in descending order?

Binary Search Trees A binary tree is a binary search tree iff

  • it is empty or
  • if the value of every node is both greater than or equal to every value in its left subtree and less than or equal to every value in its right subtree.
  • Definition allows duplicate values of the root node to fall on either side, but our code will prefer to have them on the left.

BST ADT

public class BinarySearchTree<E extends Comparable> {

protected BinaryTree root;

protected Comparator ordering;

public BinarySearchTree();

public BinarySearchTree(Comparator alternateOrder);

public void add(E value);

public boolean contains(E value);

public E remove(E value);

protected BinaryTree locate(BinaryTree root, E val);

protected BinaryTree predecessor(BinaryTree node);

protected Iterator iterator(); //in-order traversal

Locating a Value

  • Check current value vs. the search value
    • If equal, return this node
    • If smaller, locate within left subtree
      • Else within right subtree
    • If the appropriate subtree is empty, return this node

protected BinaryTree locate(BinaryTree root, E value) { E rootValue = root.value(); BinaryTree child; if (rootValue.equals(value)) return root; // found at root // look left if less-than, right if greater-than if (ordering.compare(rootValue,value) < 0) { child = root.right(); } else { child = root.left(); } // no child there: not in tree, return this node, // else keep searching if (child.isEmpty()) { return root; } else { return locate(child, value); } }

Using locate to add a node Case Two: locate returns pointer to node with same value

  • Duplicates go in left subtree (could have chosen right)
  • Where in the left subtree?

Using locate to add a node Case Two: locate returns pointer to node with same value

  • Duplicates go in left subtree (could have chosen right)
  • Should be the rightmost descendant of left tree

protected BinaryTree predecessor(BinaryTree root) { BinaryTree result = root.left(); while (!result.right().isEmpty()) { result = result.right(); } return result; } protected BinaryTree successor(BinaryTree root) { BinaryTree result = root.right(); while (!result.left().isEmpty()) { result = result.left(); } return result; }

Removing nodes โ€“ Node is a leaf r emove(52)

Removing nodes โ€“ only one child remove(70)

Removing nodes

  • Calling remove(E val) removes node with value val
  • Predecessor of root becomes new root
    • Predecessor is in left subtree
    • Predecessor has no right subtree
    • Complexity is !(โ„Ž) where โ„Ž is height of tree
      • Worst-case !(โ„Ž) to locate
      • Worst-case !(โ„Ž) to find predecessor

Complexity

  • locate, add, contains, remove are all !(โ„Ž)
  • Can we guarantee that โ„Ž is !(log ()?
    • Only if tree stays balanced!!
  • Binary search trees that stay balanced:
    • AVL trees
    • Red-black trees
  • Weโ€™ll do splay trees, which donโ€™t guarantee balance
    • but guarantee good average behavior
    • easier to understand than alternatives
    • better than others if likely to go back to recent nodes