Binary Search Trees Definition (recursive): A binary tree is said to be a binary search tree if it is the empty tree or, if there is a left-child, then the data in the left-child is less than the data in the root, if there is a right-child, then the data in the right-child is no less than the data in the root, and every sub-tree is a binary search tree. Therefore, for any node in the a binary search tree, every data in the left-sub-tree is less than every data in the right-sub-tree. 9/21/2018 IT 179
A Binary Search Tree < 19 10 25 1 17 23 30 5 14 18 22 4 7 11 21 3 13 20 9/21/2018 IT 179
Binary Search Trees data < data Compare to We can search the tree “efficiently” < data data 9/21/2018 IT 179
Insert a data into a Binary Search Tree: compare data data data data T < data < data data data data data 9/21/2018 IT 179
Construct a Binary Search Tree 1 4 11 22 13 3 18 14 30 17 13 20 5 21 10 7 25 23 19 9/21/2018 IT 179
Using an inner class for the internal Tree nodes public class BinarySearchTree <E extends Comparable<E>> { /********** This is an inner class for tree nodes************/ private static class TNode<E> { private E data; private TNode<E> left,right; private TNode(E data, TNode<E> left, TNode<E> right) { //Construct a node with two children this.data = data; this.left = left; this.right = right; } /********** This is the end of the inner class Node<E> *******/ private TNode<E> root; private boolean found=false; public BinarySearchTree() { root = null; ..... 9/21/2018 IT 179
Add a new data to the BST Overload the add method root is private; //add data to this BST, return false if data is already in the tree public boolean add(E data) { if (root != null) return add(root, data); root = new TNode<E>(data, null, null); return true; } Overload the add method root is private; 9/21/2018 IT 179
data t x 19 40 10 // add data to BST t private boolean add(TNode<E> t, E data){ if (data.compareTo(t.data) == 0) return false; if (data.compareTo(t.data) < 0) { // data is smaller if (t.left == null) t.left = new TNode<E>(data, null, null); else return add(t.left, data); } else { // data is bigger if (t.right == null) t.right = new TNode<E>(data, null, null); return add(t.right, data); } return true; ..... data x t 19 40 10 9/21/2018 IT 179
t x 19 40 10 public boolean search(E data) { if (root != null) return search(root, data); return false; } private boolean search(TNode<E> t, E data){ if (data.compareTo(t.data) == 0) return true; if (data.compareTo(t.data) < 0) { // data is smaller if (t.left == null) return false; return search(t.left, data); if (t.right == null) return false; return search(t.right, data); ..... x t 19 40 10 9/21/2018 IT 179
toString (inorder) 2 1 3 2 1 5 4 7 3 public String toString() { return toString(root); } private String toString(TNode<E> t) { if (t == null) return ""; return toString(t.left)+ " "+t.data.toString()+" "+ toString(t.right); ..... 2 2 1 1 5 4 3 7 3 9/21/2018 IT 179
Max and Min min max 2 1 5 4 7 3 public E max() { if (root == null) throw new NoSuchElementException(); return max(root); } public E min() { return min(root); private E max(TNode<E> t) { if (t.right != null) return max(t.right); return t.data; private E min(TNode<E> t) { if (t.left != null) return min(t.left); ..... min max 2 1 5 4 7 3 9/21/2018 IT 179
Size 1 2 2+4+1=7 4 2 1 5 4 7 3 public int size() { return size(root); } private int size(TNode<E> t) { if (t == null) return 0; return size(t.left)+size(t.right)+1; ..... 1 2 2 1 5 2+4+1=7 4 4 7 3 9/21/2018 IT 179
Height 2 4 3+1=4 3 2 1 5 4 7 3 public int height() { return height(root); } private int height(TNode<E> t) { if (t == null) return 0; int L = height(t.left); int R = height(t.right); return (L < R ? R : L)+1; }..... ..... 2 1 5 2 4 3+1=4 3 4 7 3 9/21/2018 IT 179
Remove a data from a Binary Search Tree: = T < < Remove data from the left-sub-tree Remove data from the right-sub-tree data data 9/21/2018 IT 179
L R Remove data from a BST delete Find the min in R, 2. If there is no R, Find the man in L and do the same L 3. If there is no L and R, simply delete the node R delete Rmin 9/21/2018 IT 179
Remove a data to the BST remove 19 root copy remove 10 8 19 10 22 //remove data from this BST, return false if data is not in this BST public boolean remove(E data) { found = false; root = remove(root, data); return found; } remove 19 root 8 19 copy 10 22 9/21/2018 remove 10 IT 179
Remove a new data from the BST //remove data from this BST, return false if data is not in this BST private TNode<E> remove(TNode<E> t, E data) { if (t==null) return null; if (data.compareTo(t.data)==0){ // data is found at T if (t.left == null && t.right == null) return null; E x; if (t.left != null) { x = max(t.left); t.left = remove(t.left, x); } else { x = min(t.right); t.right = remove(t.right, x); t.data=x; return t; if (data.compareTo(t.data)< 0) t.left = remove(t.left, data); else t.right = remove(t.right, data); 9/21/2018 IT 179
What is the benefit of using BST? Search 17, 29, 3 19 10 28 4 14 23 35 1 7 12 17 26 32 21 37 3 5 11 13 15 18 20 22 25 27 30 34 36 38 log230 = 5 9/21/2018 IT 179
What is the real benefit of using BST? O(log2n) Search 18, 2, 26 20 10 30 1 17 25 35 5 14 19 28 34 38 23 4 7 12 15 18 22 27 32 36 3 11 13 21 26 37 9/21/2018 IT 179