Professional Documents
Culture Documents
PROPERTIES OF BST
It is a binary tree
All elements are unique
Left Child less than the parent
Right Child greater than the parent
IMPORTANCE OF BST
To search an element in O(log n) time
2. CREATE
struct binary_node *root = NULL;
3. SEARCH
Algorithm BST_Search (binary_node *T, int x)
//Initially T = root; Search x is in the BST
Begin
if (T != NULL) then
Begin
If (x < T->data) then BST_Search (T->lchild, x); //Move Left
Elseif (x > T->data) then BST_Search (T->rchild, x); //Move Right
Else return 1; //x is found
End
return 0; //x is not found
End
4. FIND MINIMUM
Algorithm BST_Find_Min (binary_node *T)
// Initially T = root; Find the minimum element in the BST
Begin
If (T != NULL) then
Begin
If (T->lchild != NULL) then BST_Find_Min (T->lchild);
Else return T->data; //return minimum
End
return 0; //BST is empty
End
5. FIND MAXIMUM
Algorithm BST_Find_Max (binary_node *T)
// Initially T = root; Find the maximum element in the BST
Begin
If (T != NULL) then
Begin
If (T->rchild != NULL) then BST_Find_Min (T->rchild);
Else return T->data; //return maximum
End
return 0; //BST is empty
End
6. INSERT
Algorithm BST_Insert (binary_node *T, binary_node *P, int x)
//Initially T = root and P = NULL (parent of root). Insert x in BST
Begin
If (T != NULL && T->data != x)
Begin
If (x < T->data) then BST_Insert (T->lchild, T, x); //Move Left
Else BST_Insert (T->lchild, T, x); //Move Right
End