CS 231 Spring 2018
Binary Search Tree - A binary search tree is a sorted binary tree (tree node can contain a key value pair) in which the key field value of the root node is greater than the key field values of all of the nodes in the root’s left subtree, and less than key field values of all of the nodes in the root’s right subtree. In addition, each subtree in the tree is also a binary search tree.
- Example:
• The value of the key field of the tree’s root node, 50, is greater than all the keys in its left subtree (40, 35, 47, 43), and it is also less than all the keys in its right subtree (63, 55, 70, 68, 80). • In addition, all subtrees in the tree are also binary trees, which can be verified by inspecting them. • For example, consider the subtree whose root node is 63. This subtree is also a binary search tree because all keys in 63’s left subtree (55) are less than 63, and all of the keys in the root’s right subtree (70, 68, and 80) are greater than 63.
- BST combines the flexibility of insertion in linked lists with the efficiency of search in an ordered array.
- Binary Search Tree Operations • Add • Search • Remove
/** * Ying Li * KeyValuePair.java CS 231 Spring 2018
* 03/28/2018 */ public class KeyValuePair
public KeyValuePair (K k, V v) { key = k; value = v; }
public K getKey () { return key; }
public V getValue () { return value; }
public void setValue (V v) { value = v; }
public String toString () { return "(" + key + ", " + value + ")"; } }
/** * Ying Li * BST.java * 03/28/2018 */ import java.util.Comparator;
/* BST class */ public class BST
public BST (Comparator
public void add (K k, V v) { if (root == null) root = new TreeNode
public void traverse () { if (root == null) System.out.println("Empty tree!"); else root.traverse(); }
public V search (K key) { if (root == null) return null; return root.search(key, comparator); }
public TreeNode
public static void main (String[] args) { System.out.println("Create a tree"); BST
tree.add("c", 3); tree.add("h", 8); tree.add("b", 2); tree.add("d", 4); tree.add("g", 7); tree.add("f", 6); tree.add("a", 1); tree.add("i", 9); System.out.println("In-order traverse the tree"); tree.traverse(); System.out.println("The value associated with e is " + tree.search("e")); tree.remove("a"); System.out.println("In-order traverse the tree after removing 'a'"); tree.traverse(); tree.remove("i"); System.out.println("In-order traverse the tree after removing 'i'"); tree.traverse(); tree.remove("e"); System.out.println("In-order traverse the tree after removing 'e'"); tree.traverse(); } }
/* Tree Node class */ class TreeNode
public TreeNode (K k, V v, TreeNode
public void add (K key, V val, Comparator
public void traverse () { if (left != null) left.traverse(); System.out.println(pair); if (right != null) right.traverse(); }
public V search (K key, Comparator
if (compRslt == 0) return pair.getValue(); else if (compRslt < 0) { if (left == null) return null; else return left.search(key, comp); } else { if (right == null) return null; CS 231 Spring 2018
else return right.search(key, comp); } }
public TreeNode
return node; }
private TreeNode
private KeyValuePair
/* Comparator class */ class AscendingStringComparator implements Comparator
Method remove(key, node, comp) return TreeNode
Base case 1: If key is not in the tree (node is null), return null Base case 2: If key matches node’s key, remove node General case: If key < node.pair.key, return remove(key, node.left.comp) else return remove(key.node.right.comp)
REF: Object-Oriented Data Structures using Java 4th edition, Nell Dale, Daniel T. Joyce, Chip Weems Disclaimer: Notes adapted from previous CS 231 lecture materials at Colby College.