You are on page 1of 5

BINARY SEARCH TREE (BST)

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

1. DEFINE A NODE IN BST


struct binary_node
{
struct binary_node *lchild;
int data;
struct binary_node *rchild;
}

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

If (T==NULL) //x is not present


Begin
//create new node
temp = new (binary_node);
temp->lchild = NULL;
temp->data = x;
temp->rchild = NULL;

if (P==NULL) //BST is empty


root = temp;
elseif (x < P->data)
P->lchild = temp; //Insert as left child to parent (P)
Else
P->rchild = temp; //Insert as right child to parent (P)
End
Return 0; //Duplicate element
End
7. DELETE
Algorithm BST_Delete (binary_node *T, binary_node *P, int x)
//Initially T = root and P = NULL (parent of root). Delete x from 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

If (T->data == x) //Element Found


Begin
If (T->lchild == NULL and T->rchild == NULL) //Zero child
Begin
if (T->data < P->data) then P->lchild = NULL;
else P->rchild = NULL;
temp = T; free (temp);
End

Elseif (T->lchild == NULL or T->rchild == NULL) //One child


Begin
If (T->lchild != NULL) then temp = T->child;
Else temp = T->rchild;

if (T->data < P->data) then P->lchild = temp;


else R->rchild = temp;
temp = T; free (temp);
End

Else //Two Child


Begin
T->data = BST_Find_Max(T->lchild);
BST_Delete(T->lchild, T, T->data)
End
End
Return 0; //Element Not Found
End
8. TRAVERSAL
Algorithm Inorder_Traversal (binary_node *T)
//Initially T = root. LDR
Begin
If (T != NULL)
Inorder_Traversal (T -> lchild)
Print (T -> data)
Inorder_Traversal (T -> rchild)
End

Algorithm Preorder_Traversal (binary_node *T)


//Initially T = root. DLR
Begin
If (T != NULL)
Print (T -> data)
Preorder_Traversal (T -> lchild)
Preorder_Traversal (T -> rchild)
End

Algorithm Postorder_Traversal (binary_node *T)


//Initially T = root. LRD
Begin
If (T != NULL)
Postorder_Traversal (T -> lchild)
Postorder_Traversal (T -> rchild)
Print (T -> data)
End

Algorithm Level_Order_Traversal (BinaryNode *T)


// Initially T = root. Initialize Queue Q
Begin
If (T != NULL) then Enqueue (Q, T)
While (! Is_Empty_Q(Q))
T1 = Dequeue (Q)
Print (T1 -> data)
If (T1 -> lchild != NULL)
Enqueue (Q, T1 -> lchild)
If (T1 -> rchild != NULL)
Enqueue (Q, T1 -> rchild)
End

You might also like