0% found this document useful (0 votes)
23 views7 pages

Assignment 4444 DS

The document provides a solution to designing a program that manages employee records using a binary search tree, including implementing functions for inserting employees into the tree, deleting employees, searching for employees, and performing an in-order traversal to print all employee details; it involves defining an Employee class, a Node class to build the tree, and functions for each operation along with a main program to provide a menu interface for testing the different functions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views7 pages

Assignment 4444 DS

The document provides a solution to designing a program that manages employee records using a binary search tree, including implementing functions for inserting employees into the tree, deleting employees, searching for employees, and performing an in-order traversal to print all employee details; it involves defining an Employee class, a Node class to build the tree, and functions for each operation along with a main program to provide a menu interface for testing the different functions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 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

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy