Binary Search Trees Definition recursive A binary tree
Binary Search Trees Definition (recursive): A binary tree is said to be a binary search tree if it is the empty tree or, 1. 2. 3. 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 rightchild 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. 10/20/2021 IT 179 1
A Binary Search Tree 19 10 1 17 5 4 3 10/20/2021 25 < 14 7 23 18 11 30 22 21 13 20 IT 179 2
Binary Search Trees data Compare to < data T T < data We can search the tree “efficiently” < data 10/20/2021 IT 179 3
Insert a data into a Binary Search Tree: data compare data < data data 10/20/2021 IT 179 T data < data 4
Construct a Binary Search Tree 10/20/2021 IT 179 13 22 11 1 3 4 30 17 18 4 20 13 10 21 7 5 23 25 19 5
Using an inner class for the internal Tree nodes public class Binary. Search. Tree <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 Binary. Search. Tree() { root = null; }. . 10/20/2021 IT 179 6
Add a new data to the BST //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); return true; } Overload the add method root is private; 10/20/2021 IT 179 7
// add data to BST t private boolean add(TNode<E> t, E data){ if (data. compare. To(t. data) == 0) return false; if (data. compare. To(t. data) < 0) { // data is smaller if (t. left == null) t. left = new TNode<E>(data, null); else return add(t. left, data); } else { // data is bigger if (t. right == null) t. right = new TNode<E>(data, null); else return add(t. right, data); } t 19 return true; }. . 10/20/2021 10 IT 179 data x 40 8
public boolean search(E data) { if (root != null) return search(root, data); return false; } private boolean search(TNode<E> t, E data){ if (data. compare. To(t. data) == 0) return true; if (data. compare. To(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); } t 19. . 10 10/20/2021 IT 179 x 40 9
to. String (inorder) public String to. String() { return to. String(root); } private String to. String(TNode<E> t) { if (t == null) return ""; return to. String(t. left)+ " "+t. data. to. String()+" "+ to. String(t. right); }. . 1 2 2 1 5 4 3 7 3 10/20/2021 IT 179 10
Max and Min public E max() { if (root == null) throw new No. Such. Element. Exception(); return max(root); } public E min() { if (root == null) throw new No. Such. Element. Exception(); return min(root); } min 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); return t. data; }. . 10/20/2021 IT 179 max 2 1 5 4 7 3 11
Size 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 0 2+4+1=7 4 4 7 3 10/20/2021 IT 179 12
Height 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 3+1=4 4 2 5 0 4 7 3 10/20/2021 IT 179 13 3
Remove a data from a Binary Search Tree: data < = T < Remove data from the left-sub-tree Remove data from the right-sub-tree data 10/20/2021 IT 179 14
Remove data from a BST 1. Find the min in R, 2. If there is no R, Find the man in L and do the same data L R delete 10/20/2021 3. If there is no L and R, simply delete the node Rmin IT 179 15
Remove a data to the BST //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 10/20/2021 remove 10 IT 179 16
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. compare. To(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. compare. To(t. data)< 0) t. left = remove(t. left, data); else t. right = remove(t. right, data); return t; } 10/20/2021 IT 179 17
What is the benefit of using BST? N=30 Search 17, 29, 3 19 10 28 4 1 0 14 7 3 5 23 12 11 13 17 15 18 21 20 22 35 26 25 32 27 30 34 37 36 log 230 = 5 10/20/2021 IT 179 18 38
O(log 2 n) What is the real benefit of using BST? Search 18, 2, 26 20 10 30 1 0 17 5 4 3 10/20/2021 25 14 7 12 11 19 15 18 13 21 IT 179 28 23 22 35 27 34 32 38 36 26 37 19
- Slides: 19