(1) Binary Search Tree ADT 56 26 200 18 28 190 213 12 24 27
(2) Binary Search Tree ADT (BST) It is a binary tree with the following properties 1. with each node associate a key 2. the key of each node is greater than the keys of the nodes in its left subtree and it is less that the keys of the nodes in its right subtree 56 26 200 18 28 190 213 12 24 27
Note: The property 2 is called binary-search-tree property This property guarantees that: - The minimum is located at the left-most node. - The maximum is located at the right-most node. (3) 56 26 200 18 28 190 213 right-most iht t left-most 12 24 27
Why BST? It speeds up search operation on a collection of data elements Specification of Binary Search Tree ADT Elements: Any valid data type Structure: A binary tree such that if N is any node in the tree then all nodes in its left subtree are smaller than N, and all nodes in its right subtree are larger than N. Note: A node Ni is larger than the node Mifk key value of fni is larger than that of M and vice versa. Domain: Number of elements is bounded Operations: Operation Specification void empty() Precondition: none. Process: returns true if the BST has no nodes. (4)
(5) Operation Specification void findkey (int k) Precondition: none. Process: searches the BST for a node whose key = k. If found then that node will be set as the current node and returns true. And if search failed, then (1) BST empty then just return false; or (2) BST not empty then current node is the node to which a node containing k would be attached as a child if it were added to BST and return false. void insert(intint k, Type val) Precondition: BST not full. Process: (1) if we already have a node with key = k then current retain its old value (the value prior to calling this operation) and return false; or (2) insert a new node with the given key and data and setting it as current node, return true. void update(type) Precondition/Requires : BST is not empty. Process: update the value of data of the current node, key remains unchanged. Type retrieve() Precondition: BST is not empty. Process: returns data of the current node.. bool remove_key(int k) Precondition: none. Process: removes the node containing the key = k, and in case BST is not empty py then sets the root as the current node. Returns true or false depending on whether the operation was successful or not.
Represent ion of Binary Search Tree ADT (6) Since BST is a special kind binary tree, it can be represented using - Linked List Note : Array is not suitable for BST
(7) Implementation of BST public class BSTNode <T> { public int key; public T data; public BSTNode<T> left, right; public BSTNode(int k, T val) { key = k; data = val; left = right = null; public BSTNode (int k, BSTNode<T> l, BSTNode<T> r) { key = k; left = l; right = r;
(8) Implementation of BST public class Flag { boolean value; /** Creates a new instance of Flag */ public Flag() { value = false; public Flag(boolean v){ value = v; public boolean get_value (){ return value; public void set_value(boolean v){ value = v;
(9) Implementation of BST public class BST <T> { BSTNode<T> root, current; public BST() private BSTNode<T> findparent (BSTNode<T> p) private BSTNode<T> find_min(bstnode<t> p) private BSTNode<T> remove_ aux(int key, BSTNode<T> p, Flag flag) public boolean empty() public T retrieve () public boolean findkey(int tkey) public boolean insert (int k, T val) public boolean remove_key (int tkey) public boolean update(int key, T data)
Implementation of Operations private BSTNode<T> findparent (BSTNode<T> p) { LinkStack<BSTNode<T>> stack = new LinkStack<BSTNode<T>>(); BSTNode<T> q = root; while (q.right!= p && q.left!= p){ if (q.right!= null) stack.push(q.right); return q; if (q.left!= null) q = q.left; else q = stack.pop(); private BSTNode<T> find_min(bstnode<t> p){ if (p == null) return null; while (p.left!= null){ p = p.left; return p; (10)
Implementation of Operations (11) private BSTNode<T> remove_aux(int key, BSTNode<T> p, Flag flag) { BSTNode<T> q, child = null; if (p == null) return null; if (key < p.key) p.left = remove_aux(key, p.left, flag); else if (key > p.key) p.right = remove_aux(key, p.right, flag); else { flag.set_value(true); if (p.left!= null && p.right!= null){ q = find_ min(p.right); p.key = q.key; p.data = q.data; p.right = remove_aux(q.key, p.right, flag); else { if (p.right == null) child = p.left; else if (p.left == null) child = p.right; return child; return p; Start with node p and remove the node whose key is key recursively.
Implementation of Operations (12) public BST() { root = current = null; public boolean empty() { return root == null? true: false; public T retrieve () { return current.data;
Implementation of Operations public boolean findkey(int tkey){ BSTNode<T> p,q; p = root; q = root; if (empty()) return false; while (p!= null){ q = p; if (p.key == tkey){ current = p; return true; else if (tkey < p.key) p = p.left; else p = p.right; current = q; return false; (13)
Implementation of Operations (14) public boolean insert (int k, T val){ BSTNode<T> p, q = current; if (findkey(k)){ current = q; return false; /* key already in the BST */ p = new BSTNode<T>(k, val); if (empty()) { root = current = p; return true; else { /* current is pointing to the parent of the new key. if (k < current.key) current.left = p; else current.right t = p; current = p; return true;
Implementation of Operations (15) public boolean remove_key (int tkey){ Flag removed = new Flag(false); BSTNode<T> p; p = remove_aux(tkey, root, removed); current = root = p; return removed.get_value(); value();