You are on page 1of 15

Lovely Professional University, Punjab

Data Structures

Lecture: Binary Search Tree


A Binary Search Tree is a binary tree with
the following properties:

•All items in the left subtree are less than the root.

•All items in the right subtree are greater or equal to the root.

•Each subtree is itself a binary search tree.


Basic Concepts
Binary search trees provide an excellent structure for
searching a list and at the same time for inserting and deleting
data into the list.
BST Operations
We discuss four basic BST operations: traversal, search, insert,
and delete; and develop algorithms for searches, insertion, and
deletion.

• Traversal
• Search
• Insertion
• Deletion
Searching and inserting in Binary Search Tree
FIND (INFO, LEFT, RIGHT, ROOT, ITEM, LOC, PAR )
1.If ROOT = NULL, then Set LOC = NULL and PAR = NULL and return
2.If ITEM = INFO[ROOT], then Set LOC = ROOT and PAR = NULL and return
3.If ITEM < INFO[ROOT], then:
Set PTR = LEFT[ROOT] and SAVE = ROOT
Else
Set PTR = RIGHT[ROOT] and SAVE = ROOT
4.Repeat step 5 and 6 while PTR != NULL
5. If ITEM = INFO[PTR], then Set LOC = PTR and PAR = SAVE and return
6. If ITEM < INFO[PTR], then:
Set SAVE = PTR and PTR = LEFT[PTR]
Else
Set SAVE = PTR and PTR = RIGHT[PTR]
7.Set LOC = NULL and PAR = SAVE
8.Exit
INSBST (INFO, LEFT, RIGHT, ROOT, AVAIL, ITEM, LOC)
1. Call FIND (INFO, LEFT, RIGHT, ROOT, ITEM, LOC, PAR )
2. If LOC != NULL then Exit
3. [copy item into new node]
(a) If AVAIL = NULL then write OVERFLOW and Exit
(b) Set NEW = AVAIL, AVAIL = LEFT [AVAIL] and INFO[NEW] = ITEM
(c) LEFT[NEW] = NULL and ROGHT[NEW] = NULL
4. [Add item to tree]
If PAR = NULL then:
Set ROOT = NEW
Else if ITEM < INFO[PAR] then
Set LEFT[PAR] = NEW
Else
Set RIGHT[PAR] = NEW
7. Exit
Deletion in Binary Search Tree
CASEA(INFO, LEFT, RIGHT, ROOT, LOC, PAR )
1. If LEFT[LOC] = NULL and RIGHT[LOC] = NULL then:
Set CHILD = NULL
Else If LEFT[LOC] != NULL then:
Set CHILD = LEFT[LOC]
Else
Set CHILD = RIGHT[LOC]
4. If PAR != NULL then
5. If LOC =LEFT[PAR] then
Set LEFT[PAR] = CHILD
Else
Set RIGHT[PAR] = CHILD
Else
Set ROOT = CHILD
7. Return
CASEB(INFO, LEFT, RIGHT, ROOT, LOC, PAR )
1. (a) Set PTR = RIGHT[LOC] and SAVE = LOC
(b) Repeat while LEFT[PTR] != NULL
Set SAVE = PTR and PTR = LEFT[PTR]
(c) Set SUC = PTR and PARSUC = SAVE
2. Call CASEA(INFO, LEFT, RIGHT, ROOT, SUC, PARSUC)
4. (a) If PAR != NULL then
If LOC =LEFT[PAR] then
Set LEFT[PAR] = SUC
Else
Set RIGHT[PAR] = SUC
Else
Set ROOT = SUC
(b) Set LEFT[SUC] = LEFT[LOC] and
RIGHT[SUC] = RIGHT[LOC]
7. Return
DEL(INFO, LEFT, RIGHT, ROOT, AVAIL, ITEM)
1. Call FIND(INFO, LEFT, RIGHT, ROOT, ITEM, LOC, PAR)
2. If LOC = NULL then Write ITEM not in tree, and Exit
3. If RIGHT[LOC] != NULL and LEFT[LOC] != NULL then
Call CASEB (INFO, LEFT, RIGHT, ROOT, LOC, PAR)
Else
Call CASEA (INFO, LEFT, RIGHT, ROOT, LOC, PAR)
4. Set LEFT[LOC] =AVAIL and AVAIL = LOC
5. Exit.
6.
#include <stdio.h>
#include <stdlib.h> int main() {
// Insert some elements into the binary tree
#define MAX_SIZE 100 insert(50);
insert(30);
// Define the binary tree array and initialize it insert(20);
int binaryTree[MAX_SIZE]; insert(40);
int currentSize = 0; insert(70);
insert(60);
// Function to insert a new node into the binary tree insert(80);
void insert(int data) {
if (currentSize < MAX_SIZE) { // Perform an in-order traversal to print the elements
binaryTree[currentSize] = data; printf("In-order traversal: ");
currentSize++; inorderTraversal(0); // Start traversal from the root (index
} else { 0)
printf("Binary tree is full. Cannot insert %d.\n", data); printf("\n");
}
} return 0;
}
// Function to perform an in-order traversal of the binary
tree
void inorderTraversal(int index) {
if (index < currentSize) {
inorderTraversal(2 * index + 1); // Traverse left
subtree
printf("%d ", binaryTree[index]);
inorderTraversal(2 * index + 2); // Traverse right
subtree
}
}
#include <stdio.h> // insertion
#include <stdlib.h> struct node* insert(struct node * root, int x) {
struct node { //searching for the place to insert
int data; //node will store some data if (root == NULL)
struct node *right_child; // right child return new_node(x);
struct node *left_child; // left child else if (x > root -> data) // x is greater. Should be inserted
}; to the right
//function to create a node root -> right_child = insert(root -> right_child, x);
struct node* new_node(int x) { else // x is smaller and should be inserted to left
struct node *temp; root -> left_child = insert(root -> left_child, x);
temp = malloc(sizeof(struct node)); return root;
temp -> data = x; }
temp -> left_child = NULL; //function to find the minimum value in a node
temp -> right_child = NULL; struct node* find_minimum(struct node * root) {
if (root == NULL)
return temp; return NULL;
} else if (root -> left_child != NULL) // node with minimum
// searching operation value will have no left child
struct node* search(struct node * root, int x) { return find_minimum(root -> left_child); // left most
if (root == NULL || root -> data == x) //if root->data is x element will be minimum
then the element is found return root;
return root; }
else if (x > root -> data) // x is greater, so we will search the // Inorder Traversal
right subtree void inorder(struct node *root) {
return search(root -> right_child, x); if (root != NULL) // checking if the root is not null
else //x is smaller than the data, so we will search the left {
subtree inorder(root -> left_child); // traversing left child
return search(root -> left_child, x); printf(" %d ", root -> data); // printing data at root
} inorder(root -> right_child); // traversing right child
}
}
// deletion
struct node* delete(struct node * root, int x) { int main() {
//searching for the item to be deleted struct node *root;
if (root == NULL) root = new_node(20);
return NULL; insert(root, 5);
if (x > root -> data) insert(root, 1);
root -> right_child = delete(root -> right_child, x); insert(root, 15);
else if (x < root -> data) insert(root, 9);
root -> left_child = delete(root -> left_child, x); insert(root, 7);
else { insert(root, 12);
//No Child node insert(root, 30);
if (root -> left_child == NULL && root -> right_child == NULL) { insert(root, 25);
free(root); insert(root, 40);
return NULL; insert(root, 45);
} insert(root, 42);

//One Child node inorder(root);


else if (root -> left_child == NULL || root -> right_child == NULL) { printf("\n");
struct node *temp;
if (root -> left_child == NULL) root = delete(root, 1);
temp = root -> right_child;
else root = delete(root, 40);
temp = root -> left_child;
free(root); root = delete(root, 45);
return temp;
} root = delete(root, 9);
//Two Children
else { inorder(root);
struct node *temp = find_minimum(root -> right_child); printf("\n");
root -> data = temp -> data;
root -> right_child = delete(root -> right_child, temp -> data); return 0;
} }
}
return root;
}
The search function checks
•if(root==NULL || root->data==x) which means if the root is
NULL then the element is not in the tree and if the root is equal
to the target value x then the element is found.

•Otherwise, we implement else if(x > root->data), i.e. if the target


element is greater than the root node data, we will search in the
right subtree using search(root->right_child, x).

•Else, in the left subtree using search(root->left_child, x).


Questions

You might also like