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

ADS Assignment-3A&4B

Uploaded by

shivam.22311902
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)
7 views7 pages

ADS Assignment-3A&4B

Uploaded by

shivam.22311902
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

Name: Shivam Sanjay Koli

Roll No.: 221066

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.

2. Perform core operations such as inserting, searching, updating, and


deleting contacts.

3. Maintain a sorted order of contacts based on their phone numbers.

4. Provide an interactive menu-driven application for user-friendly


management of the phone book.
5. Optimize operations to achieve an average time complexity of O(log
n).

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;
}
}

public class Main {


static void preorder(node root) {
if(root==null) {
return;
}
System.out.print(root.val+" ");
preorder(root.left);
preorder(root.right);
}
static void inorder(node root) {
if(root==null) {
return;
}
inorder(root.left);
System.out.print(root.val+" ");
inorder(root.right);
}
static void postorder(node root) {
if(root==null) {
return;
}
postorder(root.left);
postorder(root.right);
System.out.print(root.val+" ");
}
static void insert(node root,int val) {
if(root==null) {
root=new node(val);
return;
}else {
if(root.left==null&&root.right==null) {
if(root.val>val) {
root.left=new node(val);
}else {
root.right=new node(val);
}
return;
}else if(root.left!=null&&root.right!=null) {
if(root.val>val) {
insert(root.left,val);
}else {
insert(root.right,val);
}
}else if(root.left==null) {
if(root.val>val) {
root.left=new node(val);
}else {
insert(root.right,val);
}
}else {
if(root.val>val) {
insert(root.left,val);
}else {
root.right=new node(val);
}
}
}
}
static node delete(node root,int val) {
if (root==null) {
return root;
}
if(root.left==null&&root.right==null&&root.val==val) {
return null;
}
if(root.val>val) {
root.left=delete(root.left,val);
}else if(root.val<val) {
root.right=delete(root.right,val);
}else {
if(root.right==null) {
return root.left;
}else if(root.left==null) {
return root.right;
}else if(root.right!=null&&root.left!=null) {
node temp=root;
temp=temp.right;
while(temp!=null&&temp.left!=null) {
temp=temp.left;
}
root.val=temp.val;
root.right=delete(root.right,temp.val);
}
}
return root;
}
static boolean search(node root,int val) {
if(root.right==null&&root.left==null&&root.val!=val) {
return false;
}
if(root.val==val) {
return true;
}else if(root.val>val) {
return search(root.left,val);
}else {
return search(root.right,val);
}
}

public static void main(String[] args) {

// TODO Auto-generated method stub

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);

Sample input and output:


Enter value:
55
1. Insert
2. Delete
3. Search
4. Preorder
5. Postorder
6. Inorder
7. Exit
Choose your option:
1
Enter the value:
50000
1. Insert
2. Delete
3. Search
4. Preorder
5. Postorder
6. Inorder
7. Exit
Choose your option:
1
Enter the value:
5
1. Insert
2. Delete
3. Search
4. Preorder
5. Postorder
6. Inorder
7. Exit
Choose your option:
6
5 55 50000
1. Insert
2. Delete
3. Search
4. Preorder
5. Postorder
6. Inorder
7. Exit
Choose your option:
2
Enter the value:
55
1. Insert
2. Delete
3. Search
4. Preorder
5. Postorder
6. Inorder
7. Exit
Choose your option:
6
5 50000
1. Insert
2. Delete
3. Search
4. Preorder
5. Postorder
6. Inorder
7. Exit
Choose your option:
1
Enter the value:
1
1. Insert
2. Delete
3. Search
4. Preorder
5. Postorder
6. Inorder
7. Exit
Choose your option:
7
Exiting...

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.

You might also like