ADS Assignment-3A&4B
ADS Assignment-3A&4B
PRN: 22311902
Batch: SY-A3
Problem Statement:
Design a phone book application using suitable Trees data structure, enabling
users to efficiently insert, search, update, and delete contacts. The application
must maintain sorted order of contacts based on their phone numbers while
ensuring O(log n) time complexity for each operation.
Objective:
1. Utilize the Binary Search Tree (BST) data structure to efficiently
store and manage contact details.
SYSTEM REQUIREMENTS:
Minimum Hardware Requirements
MEMORY (RAM): 2GB
Hard Disk Capacity: 40GB
Processor Speed: 1.6 GHz or faster processor
Minimum Software Requirements
Operating System: Windows / Linux
Editor: Notepad/sublime/Visula Studio Code/ Atom
Browser: Chrome/ Microsoft Edge / Mozila Firefox / Safari /Opera
Theory:
A Binary Search Tree (BST) is a hierarchical data structure where each node
has:
A value (in this case, the phone number).
A left child, containing values smaller than the node.
A right child, containing values larger than the node.
This property allows operations such as insertion, deletion, and search to be
performed efficiently with an average time complexity of O(log n), provided
the tree remains balanced. BSTs are particularly suitable for dynamic sets of
data where sorting and search efficiency are critical.
Core BST Operations for the Phone Book Application:
1. Insertion: Adds a new contact to the BST at the correct position based on
the phone number.
2. Search: Finds a contact by phone number.
3. Update: Locates a contact and modifies its associated information.
4. Deletion: Removes a contact while maintaining BST properties.
5. Traversal: Outputs contacts in various orders (e.g., sorted by phone
number using in-order traversal).
Code:
package main;
import java.util.Scanner;
class node{
int val;
node left;
node right;
node (int val){
this.val=val;
right=null;
left=null;
}
}
node root=null;
Scanner sc=new Scanner(System.in);
int option;
System.out.println("Enter value: ");
root=new node(sc.nextInt());
do {
System.out.println("1. Insert\n2. Delete\n3. Search\n4.
Preorder\n5. Postorder\n6. Inorder\n7. Exit\nChoose your option: ");
option=sc.nextInt();
switch(option) {
case 1:{
System.out.println("Enter the value: ");
int val=sc.nextInt();
insert(root,val);
}
break;
case 2:{
System.out.println("Enter the value: ");
int val=sc.nextInt();
root=delete(root,val);
}
break;
case 3:{
System.out.println("Enter the value: ");
int val=sc.nextInt();
if(search(root,val)) {
System.out.println("The value is present");
}else {
System.out.println("The value is NOT
present");
}
}
break;
case 4:{
preorder(root);
System.out.println();
}
break;
case 5:{
postorder(root);
System.out.println();
}
break;
case 6:{
inorder(root);
System.out.println();
}
break;
case 7:{
System.out.println("Exiting...");
}
break;
default:{
System.out.println("INvalid option.\nTry again.");
}
break;
}
}while(option!=7);
Conclusion:
The phone book application designed using the binary search tree (BST)
allows efficient contact management. The operations—insert, search, update,
and delete—are handled in O(log n) time complexity, ensuring optimal
performance as the contact list grows. The use of BST helps maintain sorted
contacts by phone number and allows easy retrieval and modification.