FA22-BCS-219 - Binary Search Tree
FA22-BCS-219 - Binary Search Tree
ASSIGNMENT # 4
SUBMITTED BY:
DANISH RIASAT
SUBMITTED TO:
REGISTRATION NO.
FA22-BCS-219
A Binary Search Tree (BST) is a tree in which all the nodes follow the below-mentioned properties −
• The left sub-tree of a node has a key less than or equal to its parent node's key.
• The right sub-tree of a node has a key greater than or equal to its parent node's key.
Search Operation
The algorithm depends on the property of BST that if each left subtree has values below root and each
right subtree has values above the root.
If the value is below the root, we can say for sure that the value is not in the right subtree; we need to
only search in the left subtree and if the value is above the root, we can say for sure that the value is
not in the left subtree; we need to only search in the right subtree.
Algorithm:
If root == NULL
return NULL;
If number == root->data
return root->data;
If number < root->data
return search(root->left)
If number > root->data
return search(root->right)
Let us try to visualize this with a diagram.
If the value is found in any of the subtrees, it is propagated up so that in the end it is returned,
otherwise null is returned
If the value is not found, we eventually reach the left or right child of a leaf node which is NULL and
it gets propagated and returned.
Insert Operation
Inserting a value in the correct position is similar to searching because we try to maintain the rule that
the left subtree is lesser than root and the right subtree is larger than root.
We keep going to either right subtree or left subtree depending on the value and when we reach a
point left or right subtree is null, we put the new node there.
Algorithm:
If node == NULL
return createNode(data)
if (data < node->data)
node->left = insert(node->left, data);
else if (data > node->data)
node->right = insert(node->right, data);
return node;
The algorithm isn't as simple as it looks. Let's try to visualize how we add a number to an existing
BST.
Image showing the importance of returning the root element at the end so that the elements don't lose
their position during the upward recursion step.
Deletion Operation
There are three cases for deleting a node from a binary search tree.
Case I
In the first case, the node to be deleted is the leaf node. In such a case, simply delete the node from the
tree.
4 is to be deleted
Delete the node
Case II
In the second case, the node to be deleted lies has a single child node. In such a case follow the steps
below:
1. Replace that node with its child node.
2. Remove the child node from its original position.
6 is to be deleted
copy the value of its child to the node and delete the child
Final tree
Case III
In the third case, the node to be deleted has two children. In such a case follow the steps below:
1. Get the inorder successor of that node.
2. Replace the node with the inorder successor.
3. Remove the inorder successor from its original position.
3 is to be deleted
Question #1
You are tasked with designing a program that manages a directory of employees using a
Binary Search Tree (BST). Each employee record has the following attributes: Employee
ID, Name, and Position. The program needs to support insertion, deletion, searching, and
in-order traversal operations.
#include <iostream>
using namespace std;
// Employee structure
struct Employee {
int employeeID;
string name;
string position;
Employee* left;
Employee* right;
Employee(int id, string n, string pos) : employeeID(id), name(n), position(pos), left(nullptr),
right(nullptr) {}
};
// Node with two children: Get the inorder successor (smallest in the right subtree)
Employee* temp = findMin(current->right);
public:
EmployeeDirectory() : root(nullptr) {}
int main() {
EmployeeDirectory directory;
directory.insert(219, "Danish Riasat", "Hacker");
directory.insert(238, "Sajjad Irshad", "Grahpic Designer");
directory.insert(227, "Usama Abbas", "Gamer");
directory.traverseInOrder();
directory.search(219);
directory.search(242);
directory.remove(238);
directory.traverseInOrder();
return 0;
}
Output
In-order traversal:
Employee ID: 219, Name: Danish Riasat, Position: Hacker
Employee ID: 227, Name: Usama Abbas, Position: Gamer
Employee ID: 238, Name: Sajjad Irshad, Position: Grahpic Designer
Employee found - Employee ID: 219, Name: Danish Riasat, Position: Hacker
Employee with ID 242 not found.
In-order traversal:
Employee ID: 219, Name: Danish Riasat, Position: Hacker
Employee ID: 227, Name: Usama Abbas, Position: Gamer