Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

DSA(LAB)

ASSIGNMENT# 04

Submitted To:
SIR IMRAN

Submitted By:
NOREEN JAMIL

SP22-BCS-169

Department of Computer Science

COMSATS University Islamabad

Sahiwal Campus
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.

Tasks:

Insertion:

 Implement a function to insert a new employee record into the BST. The
employee records are sorted based on their Employee ID.

Deletion:

 Extend the program to allow the removal of an employee record by


Employee ID.

Searching:

 Create a function to search for an employee by their Employee ID. If found,


print the employee's details; otherwise, indicate that the employee was not
found.

In-order Traversal:

 Implement a function to perform an in-order traversal of the BST, printing


the details of each employee.
Solution :

1. Define the Employee Record:

 Create a class or struct to represent an employee record with the following attributes:
o Employee ID (int or string): Unique identifier for each employee.
o Name (string): Employee's full name.
o Position (string): Employee's job title or position.

2. Create a Node for the BST:

 Define a Node structure to represent a single node in the BST:


o Data (Employee): The employee record stored in the node.
o Left (Node): Pointer to the left child node.
o Right (Node): Pointer to the right child node.

3. Implement Insertion:

 Function: insert(root, newEmployee)


 Logic:
o If the tree is empty, create a new node as the root.
o If the new employee ID is less than the current node's ID:
 Recursively insert into the left subtree.
o If the new employee ID is greater than the current node's ID:
 Recursively insert into the right subtree.

4. Implement Deletion:

 Function: delete(root, employeeID)


 Logic:
o Handle three cases:
 Node to be deleted has no children (leaf node).
 Node to be deleted has one child.
 Node to be deleted has two children (find in-order successor).
o Use appropriate pointer adjustments to maintain BST properties.

5. Implement Searching:

 Function: search(root, employeeID)


 Logic:
o Recursively traverse the tree:
 If the current node's ID matches the target ID, return the employee record.
 If the target ID is less than the current node's ID, search the left subtree.
 If the target ID is greater than the current node's ID, search the right subtree.
o If not found, return an appropriate message.

6. Implement In-order Traversal:

 Function: inorderTraversal(root)
 Logic:
o Recursively traverse the tree in in-order fashion:
 Traverse the left subtree.
 Print the current node's employee details.
 Traverse the right subtree.

7. Main Program:

 Create an empty BST.


 Provide a menu-driven interface with options for
insertion, deletion, searching, traversal, and exit.
 Use appropriate functions for each operation.

Remember:

 Thoroughly test your code with various employee records and BST operations.
 Consider handling edge cases and potential errors.
 Pay attention to BST properties (left child's ID < parent's ID < right child's ID) during
insertion and deletion.

CODE:
Output of this code

You might also like