A Binary Search Tree (BST) is a binary tree such that for each node, say N, the following statements are true:
* If L is any node in the left subtree of N, then L is less than N.
* If R is any node in the right subtree of N, then R is greater than N.
Elements: the elements are nodes (BSTNode), each node contains the following data type: Type,Key and has LeftChild and RightChild references. .
Structure: hierarchical structure; each node can have two children: left or right child; there is a root node and a current node. If N is any node in the tree, nodes in the left subtree < N and nodes in the right subtree > N.
Domain: the number of nodes in a BST is bounded; type/class name is BST
Method FindKey (int tkey, boolean found). requires: none. input: tkey.
results: If bst contains a node whose key value is tkey, then that node is made the current node and found is set to true; otherwise found is set to false and either the tree is empty or the current node is the node to which the node with key = tkey would be attached as a child if it were added to the BST. output: found.
Method Insert (int k, Type e, boolean inserted)
requires: Full (bst) is false. input: key, e.
results: if bst does not contain k then inserted is set to true and node with k and e is inserted and made the current element; otherwise inserted is set to false and current value does not change.
Method Remove_Key (int tkey, boolean removed) input: tkey
results: Node with key value tkey is removed from the bst and removed set to true. If BST is not empty then root is made the current.
Method Update(int key, Type e, boolean updated)
requires: Empty(bst) is false. input: key, e. results: current node’s element is replaced with Output: updated.
Other methods that are same specification:
Method Traverse (Order ord)
Method DeleteSub ( )
Method Retrieve (Type e)
Method Empty ( boolean empty ).
Method Full (boolean full)
The search operation in a binary search tree can be carried out as:
While (the target element is not found and there is more tree to search) do
if the target element is “less than” the current element then search the left
subtree else search the right subtree.
There are three cases:
Case 1: Node to be deleted has no children.
Case 2: Node to be deleted has one child.
Case 3: Node to be deleted has two children.
In all these case it is always a leaf node that gets deleted
Delete Case: 2
Node to be deleted has one child.
Remove the node, and place its child (along with its subtree) in its place.
The parent will be linked with the child of the deleted node.