Docsity

Binary Search Tree, Exams of Data Structures and Algorithms

Toggle sidebar
bg1
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
pf3
pf4
pf5

Partial preview of the text

Download Binary Search Tree and more Exams Data Structures and Algorithms in PDF only on Docsity!

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,

• 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

public class KeyValuePair<K, V> { private K key; private V value; 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/ / import java.util.Comparator; / BST class */ public class BST <K, V> { private TreeNode<K, V> root; private Comparator comparator; public BST (Comparator comp) { root = null; comparator = comp; } public void add (K k, V v) { if (root == null) root = new TreeNode<K, V> (k, v, null, null); else root.add(k, v, comparator); } 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<K, V> remove (K key) { if (root == null) return null; return root.remove(key, root, comparator); } public static void main (String[] args) { System.out.println("Create a tree"); BST<String, Integer> tree = new BST<String, Integer>(new AscendingStringComparator()); tree.add("e", 5 );

else return right.search(key, comp); } } public TreeNode<K, V> remove (K key, TreeNode<K, V> node, Comparator comp) { if (node == null) return null; else if (comp.compare(key, node.pair.getKey()) < 0 ) node.left = remove(key, node.left, comp); else if (comp.compare(key, node.pair.getKey()) > 0 ) node.right = remove(key, node.right, comp); else node = removeNode(node, comp); return node; } private TreeNode<K, V> removeNode (TreeNode<K, V> node, Comparator comp) { if (node.left == null) return node.right; else if (node.right == null) return node.left; else { KeyValuePair<K, V> p = getPredecessor(node.left); node.pair = p; node.left = remove(p.getKey(), node.left, comp); return node; } } private KeyValuePair<K, V> getPredecessor (TreeNode<K, V> subtree) { TreeNode<K, V> tmp = subtree; while (tmp.right != null) { tmp = tmp.right; } return tmp.pair; } } /* Comparator class */ class AscendingStringComparator implements Comparator { public int compare (String s1, String s2) { return s1.compareTo(s2); } }

removeNode (node, comp) : returns TreeNode<K, V>

if (node.left is null) AND (node.right is null)

return null

else if node.left is null

return node.right

else if node.right is null

return node.left

else

Find predecessor

node.pair = predecessor.pair

node.left = remove(predecessor.pair.key, node.left)

return node

Method remove(key, node, comp) return TreeNode<K, V>

Definition: Removes node with key from tree rooted at node

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.