Binary Search Trees

17.3

Binary Search Trees

A binary search tree is a special case of a binary tree

bst



Binary Search Tree

We can only create a binary search tree of objects that are comparable in some fashion.

Here is a diagram of a binary search tree of integers.

bst of integers

Binary Search Tree

 

Here is a diagram of a binary search tree of Strings.

 

a bst

Binary Search Tree

This diagram has more detail

complex bst

Not a Binary Search Tree

This is not a binary search tree

not a bst

 

Romeo could be the left child

 

Implementing a Binary Search Tree

Implementing a Binary Search Tree

public class BinarySearchTree 
{  
   private Node root; 
  
   public BinarySearchTree() { . . . } 
   public void add(Comparable obj) { . . . } 
   . . . 
   class Node 
   {  
      public Comparable data; 
      public Node left; 
      public Node right;

      public void addNode(Node newNode) { . . . } 
      . . . 
   } 
}

Insertion

Insersion Example

BinarySearchTree tree = new BinarySearchTree();
tree.add("Juliet"); 
tree.add("Tom"); 
tree.add("Diana"); 
tree.add("Harry"); 
tree.add("Romeo"); 

Class Question 1

Which tree is the result of inserting F R E D into an empty binary search tree

  1.    F
      / \
     D   R
      \
       E
  2.    F
        \ 
         R
        / 
       E
      /
     D
  3.     F
       / \
      E   R
     /  
    D   
  4. Something else

Implementing Insertion

add method in the BinarySearchTree Class

public void add(Comparable obj) 
{  
   Node newNode = new Node(); 
   newNode.data = obj; 
   newNode.left = null; 
   newNode.right = null; 
   if (root == null) root = newNode; 
   else root.addNode(newNode); 
}

Implementing Insertion

addNode method of the Node class

class Node
{
   . . .
   public void addNode(Node newNode)
   {
      int comp = newNode.data.compareTo(data);
      if (comp < 0)
      {
         if (left == null) { left = newNode; }
         else { left.addNode(newNode); }
      }
      else if (comp > 0)
      {
         if (right == null) { right = newNode; }
         else { right.addNode(newNode); }
      }
   }
   . . .
}

Searching for an Element

Searching

Class Question 2

Assume 1000 random integers were inserted into a BST. What is the maximum comparisons needed to find whether the integer 42 is among them?

  1. 1000
  2. 512
  3. 10
  4. 1

Removal

Removal

 

Class Question 3

Consider this BST:

          C
         /  \
        A   G
           / \
          D   K

What is the result of removing C?

  1.      G
        / \
       A   K
          /
         D
  2.      A
          \
           G
          / \
         D   K
  3.      D
        / \
       A   G
            \
             K
  4. Someting else

Efficiency

Class Question 4

You need to keep a set of lots of objects of class X. You don't care about traversal in sorted order, but you want maximum efficiency. What should you choose, and why?

  1. A hash table because its add/remove/find operations are O(1)+
  2. A re-balancing BST because its add/remove/find operations are guaranteed to be O(log n)
  3. A hash table because it is easier to come up with a hashCode method for X than a compareTo method
  4. A re-balancing BST because it is easier to come up with a compareTo method for X than a hashCode method