###Binary Search Tree - Non-Linear Data Structure
A Binary Tree by nature, also known by ordered or sorted binary trees
- Since a Binary Tree, each node can have zero, one or a maximum of two children.
- The value contained in left-child must be lesser than or equal to the value of this node (parent).
- The value contained in right-child must be greater than or equal to the value of this node (parent).
Since there are particular constraints while forming the search tree, we can apply the same during tree lookup. For an example if we are looking for a value that is lower than the key value of root, we can be sure of one thing - if the value do exist, it will be in the left subtree of this node.
BINARY_SEARCH_BST(x, value_to_search):
WHILE x != NULL AND k != x.key:
IF k < x.key:
x = x.left
ELSE:
x = x.right
END IF
END WHILE
RETURN x
END BINARY_SEARCH_BST
INSERT_NODE(T, NODE z):
/* Inserts Node z at it's appropriate position in the Binary Search Tree */
y = NULL
x = T.root
WHILE x != NULL:
y = x
IF z.key < x.key:
x = x.left
ELSE:
x = x.right
END WHILE
z.parent = y
IF y == NULL:
T.root = z // Tree T is empty, First element
ELSE IF z.key < y.key:
y.left = z
ELSE
y.right = z
END IF
END INSERT
For Deletion, there might be few tricky cases like nodes with children. Hence the logic has been divided into two sub modules.
The TRANSPLANT
method replaces one node with another, along with the elements below them. Hence we are talking about replacing the subtree starting from NODE u
, with the subtree starting with NODE v
.
[ Yes there might be cases where u
or v
do not have any further children, then it's like replacing node u
with node v
]
TRANSPLANT(TREE T, NODE u, NODE v) :
IF u.parent = NULL:
T.root = v
ELSE IF u == u.parent.left :
u.parent.left = v
ELSE :
u.parent.right = v
END IF
IF v != NULL:
v.parent = u.parent
END IF
END TRANSPLANT
The method MINIMUM_NODE
finds out the node with minimum key value. In this case, the minimum will always be contained in the lowest left-most node.
MINIMUM_NODE:
WHILE x.left != NULL
x = x.left
END WHILE
RETURN x
END MINIMUM_NODE
Making use of the TRANSPLANT
and MINIMUM_NODE
, the DELETE_NODE
method removes a node from the tree, making required changes along with leaving the resultant in form of Binary Search Tree.
DELETE_NODE(TREE T, NODE z):
IF z.left == NULL:
TRANSPLANT(T, z, z.right)
ELSE IF z.right == NULL:
TRANSPLANT(T, z, z.left)
ELSE:
y = MINIMUM_NODE(z.right)
IF y.parent != z:
TRANSPLANT(T, y, y.right)
y.right = z.right
y.right.parent = y
TRANSPLANT(T, z, y)
y.left = z.left
y.left.parent = y
END IF
END IF
END DELETE_NODE
For traversal, we will be using Inorder Traversal on the tree.
TRAVERSE_BST(TREE T):
IF T.root != NULL:
TRAVERSE_BST(T.left)
PRINT "NODE z :: z.key = " + T.key
TRAVERSE_BST(T.right)
END IF
END TRAVERSE_BST
While searching and inserting elements, after every comparission the problem set reduces to half of its previous and hence yields a conclusion within O(log n).
The methodMINIMUM_NODE
depends upon the height of tree emerging from that node and thus can take upto O(h), where h is the height of the concerned tree.
During the deletion operation, the node to be deleted is already supplied. It makes use ofTRANSPLANT
[which has a constant time complexity] andMINIMUM_NODE
[complexity O(h)]. Therefore it runs in O(h) for a tree of height h.