190 likes | 285 Views
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,
E N D
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. IT 179
ABinary Search Tree 19 10 < 25 1 17 23 30 5 14 18 22 4 7 11 21 3 13 20 IT 179
Binary Search Trees T data Compare to data data T < < We can search the tree “efficiently” < data data IT 179
Insert a data into a Binary Search Tree: data compare data data data data T < data < data data data data data IT 179
Construct a Binary Search Tree 13 11 3 4 1 22 30 17 14 18 20 13 5 7 10 21 23 25 19 IT 179
publicclass BinarySearchTree <E extends Comparable<E>> { /**********Thisisaninnerclassfortreenodes************/ privatestaticclass 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; } } /**********ThisistheendoftheinnerclassNode<E>*******/ private TNode<E> root; privatebooleanfound=false; public BinarySearchTree() { root = null; } ..... ..... Using an inner class for the internal Tree nodes IT 179
//add data to this BST, return false if data is already in the tree publicboolean add(E data) { if (root != null) return add(root, data); root = new TNode<E>(data, null, null); returntrue; } Add a new data to the BST Overload the add method root is private; IT 179
// add data to BST t privateboolean add(TNode<E> t, E data){ if (data.compareTo(t.data) == 0) returnfalse; 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); else return add(t.right, data); } returntrue; } ..... ..... data x t 19 40 10 IT 179
publicboolean search(E data) { if (root != null) return search(root, data); returnfalse; } privateboolean search(TNode<E> t, E data){ if (data.compareTo(t.data) == 0) returntrue; if (data.compareTo(t.data) < 0) { // data is smaller if (t.left == null) returnfalse; return search(t.left, data); } if (t.right == null) returnfalse; return search(t.right, data); } ..... ..... x t 19 40 10 IT 179
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); } ..... ..... toString (inorder) 2 2 1 1 5 4 3 7 3 IT 179
public E max() { if (root == null) thrownew NoSuchElementException(); return max(root); } public E min() { if (root == null) thrownew NoSuchElementException(); 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); return t.data; } ..... ..... Max and Min min max 2 1 5 4 7 3 IT 179
publicint size() { return size(root); } privateint size(TNode<E> t) { if (t == null) return 0; return size(t.left)+size(t.right)+1; } ..... ..... Size 1 2 2 1 5 2+4+1=7 0 4 4 7 3 IT 179
publicint height() { return height(root); } privateint 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; }..... ..... Height 2 1 5 2 4 3+1=4 3 0 4 7 3 IT 179
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 data IT 179
Remove data from a BST • Find the min in R, data 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 IT 179
//remove data from this BST, return false if data is not in this BST publicboolean remove(E data) { found = false; root = remove(root, data); returnfound; } Remove a data to the BST remove 19 root 8 19 copy 10 22 remove 10 IT 179
//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) returnnull; if (data.compareTo(t.data)==0){ // data is found at T if (t.left == null && t.right == null) returnnull; 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); return t; } Remove a new data from the BST IT 179
What is the benefit of using BST? N=30 Search 17, 29, 3 19 10 28 4 14 23 35 1 7 12 17 26 32 21 37 0 3 5 11 13 15 18 20 22 25 27 30 34 36 38 log230 = 5 IT 179
O(log2n) What is the real benefit of using BST? Search 18, 2, 26 20 10 30 1 17 25 35 0 5 14 19 28 34 38 23 4 7 12 15 18 22 27 32 36 3 11 13 21 26 37 IT 179