Data Structures Using Java - Lab Manual
Data Structures Using Java - Lab Manual
Data Structures Using Java - Lab Manual
Algorithm:
Algorithm create( )
1. Start
2. head = null
3. size = 0
4. print "List created successfully"
5. return
Algorithm isEmpty()
1. Start
2. if size == 0 then
2.1 return true
3. else
3.1 return false
Algorithm count()
1. Start
2. print “Number of elements in the list are :” , size
3. return
Algorithm traverse( )
1. start
2. if size == 0 then
2.1 print "List is empty, hence no elements to traverse"
3. else
3.1 print "The elements in the list are:"
3.2 p= head
3.3 while (p != null)
3.3.1 print p.info
3.3.2 p = p.link
4. return
Program:
import java.util.*;
interface LinearList
{ void create();
boolean isEmpty();
int count();
void insert(int index,Object data);
Object delete(int index);
void traverse();
}
class Node
{ Object info;
Node link;
}
class SLLDemo
{ public static void main(String args[])
{ SLL ob = new SLL();
ob.create();
System.out.println("No. of elements in the list is :"+ ob.count());
System.out.println("List is empty : " + ob.isEmpty());
ob.insert(0,10);
ob.insert(1,20);
ob.insert(2,30);
System.out.println("No. of elements in the list is :"+ ob.count());
System.out.println("List is empty : " + ob.isEmpty());
System.out.println("The deleted element is : " + ob.delete(2) );
ob.traverse();
}
}
Output:
List created successfully
No. of elements in the list is : 0
List is empty: true
10 inserted into the list successfully
20 inserted into the list successfully
30 inserted into the list successfully
No. of elements in the list is: 3
List is empty: false
The deleted element is: 30
The elements in the list are:
10 20
4
2. Aim: To write a program to implement stack operations using an array.
Algorithm:
Algorithm create( )
1. Start
2. top = -1
3. stack = new Object[capacity]
4. print "Stack is Created”
5. return
A1lgorithm isEmpty()
1. Start
2. if top == -1 then
2.1 return true
3. else
3.1 return false
Algorithm count( )
1. Start
2. return top+1
Algorithm pop( )
1. Start
2. if top == -1 then
2.1 print “The stack is empty"
3. else
3.1 removedElement = stack[top]
3.2 top--
4. return removedElement
Algorithm peek( )
1. Start
2. if top == -1 then
2.1 print “Stack is empty"
3. else
3.1 print "Element at top of the stack is : " + stack[top]
4. return
Algorithm traverse( )
5
1. start
2. if top==-1 then
2.1 print "The stack is empty"
3. else
3.1 print "Elements in the stack are: "
3.2 for (int i=top; i>=0; i--)
print stack[i]
4. return.
Program:
import java.util.*;
interface StackADT
{
void create();
boolean isEmpty();
int count();
void push(Object data);
Object pop();
void peek();
void traverse();
}
class StackDemo
{
public static void main(String args[])
{ Scanner sc = new Scanner(System.in);
int ch;
StackArray ob = new StackArray();
do
{
System.out.println("1.Creating an empty stack");
System.out.println("2.Checking whether stack is empty or not");
System.out.println("3.Displaying number of elements in the stack");
System.out.println("4.Pushing an element into the stack");
System.out.println("5.Popping an element from the stack");
System.out.println("6.Displaying element at top of the stack");
System.out.println("7.Traversing elements in a stack");
System.out.println("8.Exit");
7
System.out.print("\nEnter your choice:");
ch = sc.nextInt();
switch(ch)
{ case 1 :ob.create();
break;
case 2 :System.out.println("Stack is empty : " + ob.isEmpty());
break;
case 3 :System.out.println("Number of elements in the stack are: "
+ ob.count() );
break;
case 4 :
System.out.println("Enter the Element to insert into the
stack:");
int element = sc.nextInt();
ob.push(element);
break;
case 5 :System.out.println("The element popped from stack is : " +
ob.pop());
break;
case 6 :ob.peek();
break;
case 7 :ob.traverse();
break;
}
}while(ch>=1 && ch<=7);
}
}
Output:
F:\>javac StackDemo.java
F:\>java StackDemo
1.Creating an empty stack
2.Checking whether stack is empty or not
3.Displaying number of elements in the stack
4.Pushing an element into the stack
5.Popping an element from the stack
6.Displaying element at top of the stack
7.Traversing elements in a stack
8.Exit
Enter your choice:1
Stack is Created....
9
3. Aim: To write a program to implement stack operations using a single linked list.
Algorithm:
Algorithm create( )
1. Start
2. top = null
3. size = 0
4. print "Stack is Created”
5. return
A1lgorithm isEmpty()
1. Start
2. if size == 0 then
2.1 return true
3. else
3.1 return false
Algorithm count( )
1. Start
2. return size
Algorithm pop( )
1. Start
2. if size == 0 then
2.1 print “The stack is empty"
3. else
3.1 temp = top
3.2 top = top.link
3.3 size--
4. return temp.info
Algorithm peek( )
1. Start
2. if size == 0 then
2.1 print “Stack is empty"
3. else
3.1 print "Element at top of the stack is : " + top.info
4. return
10
Algorithm traverse( )
1. start
2. if size == 0 then
2.1 print "The stack is empty"
3. else
3.1 p = top
3.2 print "Elements in the stack are: "
3.3 while (p != null )
3.3.1 print p.info
3.3.2 p = p.link
4. return.
Program:
import java.util.*;
interface StackADT
{
void create();
boolean isEmpty();
int count();
void push(Object data);
Object pop();
void peek();
void traverse();
}
class Node
{
Object info;
Node link;
}
class StackSLL implements StackADT
{
Node top,node,temp,p;
int size=0;
public void create()
{ top = null;
size = 0;
System.out.println("Stack is Created....");
}
public boolean isEmpty()
{
if(size == 0)
return true;
else
return false;
}
public int count()
{
return size;
}
11
public void push(Object data)
{ node = new Node();
node.info = data;
node.link = top;
top = node;
size++;
System.out.println(data + " inserted successfully into the stack");
}
public Object pop()
{
if(size == 0)
System.out.println("The stack is empty");
else
{
temp = top;
top = top.link;
size--;
}
return temp.info;
}
public void peek()
{ if(size == 0)
System.out.println("Stack is empty");
else
System.out.println("Element at top of the stack is : "+ top.info);
}
public void traverse()
{ if(size==0)
System.out.println("The stack is empty");
else
{
p = top;
System.out.println("Elements in the stack are: ");
while ( p!= null)
{
System.out.println(p.info);
p = p.link;
}
}
}
}
class StackDemo1
{
public static void main(String args[])
{ Scanner sc = new Scanner(System.in);
int ch;
StackSLL ob = new StackSLL();
do
12
{
System.out.println("1.Creating an empty stack");
System.out.println("2.Checking whether stack is empty or not");
System.out.println("3.Displaying number of elements in the stack");
System.out.println("4.Pushing an element into the stack");
System.out.println("5.Popping an element from the stack");
System.out.println("6.Displaying element at top of the stack");
System.out.println("7.Traversing elements in a stack");
System.out.println("8.Exit");
System.out.print("\nEnter your choice:");
ch = sc.nextInt();
switch(ch)
{ case 1 :ob.create();
break;
case 2 :System.out.println("Stack is empty : " + ob.isEmpty());
break;
case 3 :System.out.println("Number of elements in the stack are: "
+ ob.count() );
break;
case 4 :
System.out.println("Enter the Element to insert into the
stack:");
int element = sc.nextInt();
ob.push(element);
break;
case 5 :System.out.println("The element popped from stack is : " +
ob.pop());
break;
case 6 :ob.peek();
break;
case 7 :ob.traverse();
break;
}
}while(ch>=1 && ch<=7);
}
}
Output:
F:\>javac StackDemo1.java
F:\>java StackDemo1
1.Creating an empty stack
2.Checking whether stack is empty or not
3.Displaying number of elements in the stack
4.Pushing an element into the stack
5.Popping an element from the stack
6.Displaying element at top of the stack
7.Traversing elements in a stack
8.Exit
Enter your choice:1
13
Stack is Created....
14
4. Aim: To write a program to implement the Queue operations using an array.
Algorithm:
Algorithm create()
1. Start
2. rear = -1
3. front = 0
4. queue = new Object[capacity]
5. print "Linear Queue Created Successfully"
6. return
Algorithm isEmpty()
1. start
2. if rear == -1 then
2.1 return true
3. else
3.1 return false
Algorithm count()
1. start
2. return size
Algorithm delete()
1. start
2. if rear == -1 then
2.1 print "The linear queue is empty"
3. else
3.1 removedElement = queue[front]
3.2 (for i= front; i<rear; i++)
3.2.1 queue[i] =queue[i+1];
3.3 rear--;
4. return removedElement;
Algorithm frontQueue()
1. start
2. if rear == -1 then
2.1 print "Linear Queue is empty"
3. else
3.1 print "Element at front of the queue is : ", queue[front]
4. return
15
Algorithm rearQueue()
1. start
2. if rear == -1 then
2.1 print "Linear Queue is empty"
3. else
3.1 print "Element at front of the queue is : ", queue[rear]
4. return
Algorithm traverse()
1. start
2. if rear == -1 then
2.1 print "Linear Queue is empty"
3. else
3.1 print "Elements in the linear queue are: "
3.2 for(int i=front;i<=rear;i++)
3.2.1 print queue[i]
4. return
Program:
import java.util.*;
interface QueueADT
{ void create();
boolean isEmpty();
int count();
void insert(Object data);
Object delete();
void frontQueue();
void rearQueue();
void traverse();
}
class QueueDemo
{ public static void main(String args[])
{ Scanner sc = new Scanner(System.in);
17
int ch;
QueueArray ob = new QueueArray();
do
{ System.out.println("1.Creating an empty linear queue");
System.out.println("2.Checking whether linear queue is empty or not");
System.out.println("3.Displaying number of elements in the linear
queue");
System.out.println("4.Inserting an element into the linear queue");
System.out.println("5.Deleting an element from the linear queue");
System.out.println("6.Displaying element at front of the queue");
System.out.println("7.Displaying element at rear of the queue");
System.out.println("8.Traversing elements in a linear queue");
System.out.println("9.Exit");
System.out.print("\nEnter your choice:");
ch = sc.nextInt();
switch(ch)
{ case 1 :ob.create();
break;
case 2 :System.out.println("Linear queue is empty : " +
ob.isEmpty());
break;
case 3 :System.out.println("Number of elements in the Linear
queue are: " + ob.count() );
break;
case 4 :
System.out.println("Enter the Element to insert into the
linear queue:");
int element = sc.nextInt();
ob.insert(element);
break;
case 5 :System.out.println("The element deleted from linear queue
is : " + ob.delete());
break;
case 6 :ob.frontQueue();
break;
case 7: ob.rearQueue();
break;
case 8 :ob.traverse();
break;
}
}while(ch>=1 && ch<=8);
}
}
18
5. Aim: To write a program to implement the Queue operations using singly linked list.
Algorithm:
Algorithm create()
1. Start
2. rear = null
3. front = null
4. size = 0
5. print "Linear Queue Created Successfully"
6. return
Algorithm isEmpty()
1. start
2. if size == 0 then
2.1 return true
3. else
3.1 return false
Algorithm count()
1. start
2. return size
Algorithm delete()
1. start
2. if size == 0 then
2.1 print "The linear queue is empty"
3. else
3.1 temp = front
3.2 if size == 1 then
3.2.1 front = null
3.2.2 rear = null
3.3 else
3.3.1 front = front.link;
4. size--;
5. return temp.info
19
Algorithm frontQueue()
1. start
2. if size == 0 then
2.1 print "Linear Queue is empty"
3. else
3.1 print "Element at front of the queue is : ", front.info
4. return
Algorithm rearQueue()
1. start
2. if size == 0 then
2.1 print "Linear Queue is empty"
3. else
3.1 print "Element at rear of the queue is : ", rear.info
4. return
Algorithm traverse()
1. start
2. if size == 0 then
2.1 print "Linear Queue is empty"
3. else
3.1 print "Elements in the linear queue are: "
3.2 p = front
3.3 while p!= null
3.3.1 print p.info
3.3.2 p = p.link
4. return
Program:
import java.util.*;
interface QueueADT
{ void create();
boolean isEmpty();
int count();
void insert(Object data);
Object delete();
void frontQueue();
void rearQueue();
void traverse();
}
class Node
{ Object info;
Node link;
}
class QueueSLL implements QueueADT
{ Node node, temp, p, rear, front;
int size = 0;
public void create()
{ rear = null;
20
front = null;
size = 0;
System.out.println("\nLinear Queue Created Successfully");
}
public boolean isEmpty()
{ if(size == 0)
return true;
else
return false;
}
public int count()
{ return size;
}
public void insert(Object data)
{ node = new Node();
node.info = data;
node.link = null;
if (size == 0)
{ front = node;
rear = node;
}
else
{
rear.link = node;
rear = node;
}
size++;
System.out.println(data + " inserted successfully into the linear queue");
}
}
public Object delete()
{ if(size == 0)
System.out.println("The linear queue is empty");
else
{ temp = front;
if( size == 1)
{ front = null;
rear = null;
}
else
{ front = front.link;
}
size--;
}
return temp.info;
}
public void front_queue()
{ if(size == 0)
System.out.println("Linear Queue is empty");
21
else
System.out.println("Element at front of the queue is : "+ front.info);
}
public void rear_queue()
{ if(size == 0)
System.out.println("Linear Queue is empty");
else
System.out.println("Element at rear of the queue is : "+ rear.info);
}
public void traverse()
{ if(size == 0)
System.out.println("The Linear queue is empty");
else
{ p = front;
System.out.println("Elements in the linear queue are: ");
while ( p!= null)
{
System.out.println(p.info);
p = p.link;
}
}
}
}
class QueueDemo1
{
public static void main(String args[])
{ Scanner sc = new Scanner(System.in);
int ch;
QueueSLL ob = new QueueSLL();
do
{ System.out.println("1.Creating an empty linear queue");
System.out.println("2.Checking whether linear queue is empty or not");
System.out.println("3.Displaying number of elements in the linear
queue");
System.out.println("4.Inserting an element into the linear queue");
System.out.println("5.Deleting an element from the linear queue");
System.out.println("6.Displaying element at front of the queue");
System.out.println("7.Displaying element at rear of the queue");
System.out.println("8.Traversing elements in a linear queue");
System.out.println("9.Exit");
System.out.print("\nEnter your choice:");
ch = sc.nextInt();
switch(ch)
{ case 1 :ob.create();
break;
case 2 :System.out.println("Linear queue is empty : " +
ob.isEmpty());
break;
22
case 3 :System.out.println("Number of elements in the Linear
queue are: " + ob.count() );
break;
case 4 :
System.out.println("Enter the Element to insert into the
linear queue:");
int element = sc.nextInt();
ob.insert(element);
break;
case 5 :System.out.println("The element deleted from linear queue
is : " + ob.delete());
break;
case 6 :ob.front_queue();
break;
case 7: ob.rear_queue();
break;
case 8 :ob.traverse();
break;
}
}while(ch>=1 && ch<=8);
}
}
23
6. Aim: To write a program to implement Double Ended Queue operations using a doubly
linked list.
Algorithm:
Algorithm create()
1. Start
2. rear = null
3. front = null
4. size = 0
5. print "Deque Created Successfully"
6. return
Algorithm isEmpty()
1. start
2. if size == 0 then
2.1 return true
3. else
3.1return false
Algorithm count()
1. start
2. return size
Algorithm frontDelete( )
1. start
2. if size == 0 then
2.1 print "The deque is empty"
3. else
3.1 temp = front
3.2 if size == 1 then
3.2.1 front = null
3.2.2 rear = null
3.3 else
3.3.1 front = front.right
3.3.2 front.left = null
3.4 size--;
4. return temp.info
Algorithm rearDelete( )
1. start
2. if size == 0 then
2.1 print "The deque is empty"
3. else
3.1 temp = front
3.2 if size == 1 then
3.2.1 front = null
3.2.2 rear = null
3.3 else
3.3.1 rear = rear.left
3.3.2 rear.right = null
3.4 size--;
4. return temp.info
Algorithm frontQueue()
1. start
2. if size == 0 then
2.1 print "Deque is empty"
3. else
3.1 print "Element at front of the queue is : ", front.info
4. return
Algorithm rearQueue()
1. start
2. if size == 0 then
25
2.1 print "Deque is empty"
3. else
3.1 print "Element at rear of the queue is : ", rear.info
4. return
Algorithm traverse()
1. start
2. if size == 0 then
2.1 print "Deque is empty"
3. else
3.1 print "Elements in the Deque are: "
3.2 p = front
3.3 while p!= null do
3.3.1 print p.info
3.3.2 p = p.right
4. return
Program:
import java.util.*;
interface DequeADT
{ void create( );
boolean isEmpty( );
int count( );
void frontInsert(Object data);
void rearInsert(Object data);
Object frontDelete( );
Object rearDelete( );
void frontDeque( );
void rearDeque();
void traverse( );
}
class Node
{ Object info;
Node left, right;
}
class DequeDLL implements DequeADT
{ Node node, temp, p, rear, front;
int size = 0;
public void create()
{ rear = null;
front = null;
size = 0;
System.out.println("\nDeque Created Successfully");
}
public boolean isEmpty()
{ if(size == 0)
return true;
else
26
return false;
}
public int count()
{ return size;
}
public void frontInsert(Object data)
{ node = new Node();
node.info = data;
node.right = null;
node.left = null;
if (size == 0)
{ front = node;
rear = node;
}
else
{
node.right = front;
front.left = node;
front = node;
}
size++;
System.out.println(data + " inserted successfully into the deque");
}
public void rearInsert(Object data)
{ node = new Node();
node.info = data;
node.right = null;
node.left = null;
if (size == 0)
{ front = node;
rear = node;
}
else
{
rear.right = node;
node.left = rear;
rear = node;
}
size++;
System.out.println(data + " inserted successfully into the deque");
}
public Object frontDelete()
{ if(size == 0)
System.out.println("The deque is empty");
else
{ temp = front;
if( size == 1)
{ front = null;
rear = null;
27
}
else
{ front = front.right;
front.left = null;
}
size--;
}
return temp.info;
}
public Object rearDelete()
{ if(size == 0)
System.out.println("The deque is empty");
else
{ temp = rear;
if( size == 1)
{ front = null;
rear = null;
}
else
{ rear = rear.left;
rear.right = null;
}
size--;
}
return temp.info;
}
public void frontDeque()
{ if(size == 0)
System.out.println("Deque is empty");
else
System.out.println("Element at front of the queue is : "+ front.info);
}
public void rearDeque()
{ if(size == 0)
System.out.println("Deque is empty");
else
System.out.println("Element at rear of the queue is : "+ rear.info);
}
public void traverse()
{ if(size == 0)
System.out.println("The Deque is empty");
else
{ p = front;
System.out.println("Elements in the deque are: ");
while ( p!= null)
{
System.out.println(p.info);
p = p.right;
}
28
}
}
}
class DequeDemo2
{
public static void main(String args[])
{ Scanner sc = new Scanner(System.in);
int ch;
DequeDLL ob = new DequeDLL( );
do
{ System.out.println("1.Creating an empty deque");
System.out.println("2.Checking whether deque is empty or not");
System.out.println("3.Displaying number of elements in the deque");
System.out.println("4.Inserting an element at front of the deque");
System.out.println("5.Inserting an element at rear of the deque");
System.out.println("6.Deleting an element at front of the deque");
System.out.println("7.Deleting an element at rear of the deque");
System.out.println("8.Displaying element at front of the deque");
System.out.println("9.Displaying element at rear of the deque");
System.out.println("10.Traversing elements in a deque");
System.out.println("11.Exit");
System.out.print("\nEnter your choice:");
ch = sc.nextInt();
switch(ch)
{ case 1 :ob.create();
break;
case 2 :System.out.println("Deque is empty : " +
ob.isEmpty());
break;
case 3 :System.out.println("Number of elements in the Deque are deque are: " + ob.count() );
break;
case 4 :
System.out.println("Enter the Element to insert into the
deque:");
int element = sc.nextInt();
ob.frontInsert(element);
break;
case 5 :
System.out.println("Enter the Element to insert into the
deque:");
element = sc.nextInt();
ob.rearInsert(element);
break;
case 6 :System.out.println("The element deleted from front of the
deque is : " + ob.frontDelete());
break;
case 7 :System.out.println("The element deleted from rear of the
deque is : " + ob.rearDelete());
break;
29
case 8 :ob.frontDeque();
break;
case 9: ob.rearDeque();
break;
case 10 :ob.traverse();
break;
}
}while(ch>=1 && ch<=10);
}
}
30
7. a) Aim: To write a java program to convert the given infix expression into postfix expression.
Program:
import java.util.Stack;
class Test
{ static int Prec(char ch)
{ switch (ch)
{
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
case '^':
return 3;
}
return -1;
}
static String infixToPostfix(String exp)
{
String result = new String("");
Stack<Character> stack = new Stack<>();
for (int i = 0; i<exp.length(); ++i)
{
char c = exp.charAt(i);
if (Character.isLetterOrDigit(c))
result += c;
else if (c == '(')
stack.push(c);
else if (c == ')')
{
while (!stack.isEmpty() && stack.peek() != '(')
result += stack.pop();
31
if (!stack.isEmpty() && stack.peek() != '(')
return "Invalid Expression";
else
stack.pop();
}
else
{
while (!stack.isEmpty() && Prec(c) <= Prec(stack.peek()))
result += stack.pop();
stack.push(c);
}
}
while (!stack.isEmpty())
result += stack.pop();
return result;
}
public static void main(String[] args)
{
String exp = "a+b*(c^d-e)^(f+g*h)-i";
System.out.println(infixToPostfix(exp));
}
}
32
7. b) Aim: To write a java program to evaluate the given postfix expression.
Program:
import java.util.Stack;
public class Test
{
static int evaluatePostfix(String exp)
{
Stack<Integer> stack=new Stack<>();
for(int i=0;i<exp.length();i++)
{
char c=exp.charAt(i);
if(Character.isDigit(c))
stack.push(c - '0');
else
{
int val1 = stack.pop();
int val2 = stack.pop();
switch(c)
{
case '+':
stack.push(val2+val1);
break;
case '-':
stack.push(val2- val1);
break;
case '/':
stack.push(val2/val1);
break;
case '*':
stack.push(val2*val1);
break;
}
}
}
return stack.pop();
}
public static void main(String[] args)
{
String exp="231*+9-";
33
System.out.println(evaluatePostfix(exp));
}
}
34
8. Aim: To write a program on Binary Search Tree operations (insertion, deletion and
traversals).
Algorithm:
Algorithm insert_BST(data)
1. start
2. node =new Node( ) // allocate memory dynamically for new node
3. node.info = data
4. node.left = null
5. node.right = null
6. if (root = = null) then
6.1 root = node
7. else
7.1 cur = root
7.2 while (cur != null) do
7.2.1 par = cur
7.2.2 if (data <= cur.info) then
7.2.2.1 cur = cur.left
7.2.3 else
7.2.3.1 cur = cur.right
7.4 if(data <= par.info)
7.4.1 par.left = node
7.5 else
7.5.1 par.right = node
8. print "The element inserted into BST successfully"
9. return
Algorithm delete_BST(data)
1. start
2. if (root == null) then
2.1 print "Tree is empty, hence deletion cannot be performed"
3. else
3.1 cur = root
3.2 while(cur != null && cur.info !=data) do
3.2.1 par = cur
3.2.2 if (data <= (cur.info) ) then
3.2.2.1 cur = cur.left
3.2.3 else
3.2.3.1 cur = cur.right
3.3 if (cur == null) then
3.3.1 print "Given element is not found in BST and hence it cannot be deleted"
3.4 else
3.4.1 if ( cur == root ) then
3.4.1.1 if (cur.left == null && cur.right == null) then
3.4.1.1.1 root = null
3.4.1.2 else if (cur.left != null && cur.right == null) then
3.4.1.2.1 root = root.left
3.4.1.3 else if (cur.left == null && cur.right != null) then
35
3.4.1.3.1 root = root.right
3.4.1.4 else
3.4.1.4.1 temp = (root.left)
3.4.1.4.2 root = root.right
3.4.1.4.3 cur = root
3.4.1.4.4 while ( cur.left != null)
cur = cur.left
3.4.1.4.5 cur.left = temp
3.4.2 else
3.4.2.1 if ( cur.left == null && cur.right == null) then
3.4.2.1.1 if( (cur.info) <= (par.info)) then
par.left = null
3.4.2.1.2 else
par.right = null
3.4.2.2 else if ( cur.left != null && cur.right == null) then
3.4.2.2.1 if( (cur.info) <= (par.info)) then
par.left = cur.left
3.4.2.2.2 else
par.right = cur.left
3.4.2.3 else if ( cur.left == null && cur.right != null) then
3.4.2.3.1 if( (cur.info) <= (par.info)) then
par.left = cur.right;
3.4.2.3.2 else
par.right = cur.right;
3.4.2.4 else
3.4.2.4.1 temp = (cur.left)
3.4.2.4.2 if( (cur.info) <= (par.info)) then
par.left = cur.right
3.4.2.4.3 else
par.right = cur.right
3.4.2.4.4 cur = (cur.right)
3.4.2.4.5 while( cur.left != null) do
cur = cur.left
3.4.2.4.6 cur.left = temp
3.5 print "The element deleted from BST successfully"
4. return
Program:
import java.util.*;
class Node
{ int info;
Node left, right;
}
interface BSTADT
{
void insert_BST(int data);
void delete_BST(int data);
void inorder(Node root);
void preorder(Node root);
void postorder(Node root);
}
class BST implements BSTADT
{
Node root=null, par, cur, node;
public void insert_BST(int data)
{
node=new Node( );
node.info=data;
node.left=null;
node.right=null;
if(root == null)
root = node;
else
{
cur = root;
while(cur !=null)
{
par=cur;
if(data <= cur.info)
cur=cur.left;
else
cur=cur.right;
}
if(data <= par.info)
37
par.left=node;
else
par.right=node;
}
System.out.println("The element inserted into BST successfully");
}
public void delete_BST(int data)
{
Node temp;
if (root == null)
{
System.out.println("Tree is empty, hence deletion cannot be performed");
}
else
{
cur = root;
while(cur != null && cur.info !=data)
{
par = cur;
if (data <= (cur.info) )
cur = cur.left;
else
cur = cur.right;
}
if (cur == null)
{
System.out.println("\nGiven element is not found in BST and
hence it cannot be deleted");
}
else
{
if ( cur == root )
{
if (cur.left == null && cur.right == null)
root = null;
else if (cur.left != null && cur.right == null)
root = root.left;
else if (cur.left == null && cur.right != null)
root = root.right;
else
{
temp = (root.left);
root = root.right;
cur = root;
while ( cur.left != null)
cur = cur.left;
cur.left = temp;
}
}
38
else
{
if ( cur.left == null && cur.right == null)
{
if( (cur.info) <= (par.info))
par.left = null;
else
par.right = null;
}
else if ( cur.left != null && cur.right == null)
{
if( (cur.info) <= (par.info))
par.left = cur.left;
else
par.right = cur.left;
}
else if ( cur.left == null && cur.right != null)
{
if( (cur.info) <= (par.info))
par.left = cur.right;
else
par.right = cur.right;
}
else
{
temp = (cur.left);
if( (cur.info) <= (par.info))
par.left = cur.right;
else
par.right = cur.right;
cur = (cur.right);
while( cur.left != null)
cur = cur.left;
cur.left = temp;
}
}
System.out.println("The element deleted from BST successfully");
}
}
}
public void inorder(Node root)
{ if(root!=null)
{
inorder(root.left);
System.out.println(root.info);
inorder(root.right);
}
}
public void preorder(Node root)
39
{ if(root!=null)
{
System.out.println(root.info);
preorder(root.left);
preorder(root.right);
}
}
public void postorder(Node root)
{ if(root!=null)
{
postorder(root.left);
postorder(root.right);
System.out.println(root.info);
}
}
}
class BSTDemo
{
public static void main(String args[])
{
int choice, element;
Scanner sc = new Scanner(System.in);
BST ob = new BST( );
do
{
System.out.println("\n\t\tBinary Search Tree operations");
System.out.println("1.Inserting an element into BST");
System.out.println("2.Deleting an element from BST");
System.out.println("3.Inorder Traversal");
System.out.println("4.Preorder Traversal");
System.out.println("5.Postorder Traversal");
System.out.println("6.Exit");
System.out.println("Enter your choice : ");
choice = sc.nextInt();
switch(choice)
{ case 1 : System.out.println("Enter element to insert into BST : ");
element = sc.nextInt();
ob.insert_BST(element);
break;
case 2 : System.out.println("Enter element to delete from BST : ");
element = sc.nextInt();
ob.delete_BST(element);
break;
case 3 : ob.inorder(ob.root);
break;
case 4 : ob.preorder(ob.root);
break;
case 5 : ob.postorder(ob.root);
break;
40
default : System.out.println("Program Terminated");
}
}while(choice>=1 && choice<=5);
}
}
41
9. a) Aim: To write a program to search an element in a given list using Linear Search
Technique.
Algorithm linearSearch( ):
1. Start
2. Repeat for i=0 to n-1 step by 1 do
2.1 if (key == a[i]) then
2.1.1 return (i)
3. return (-1)
Program:
import java.util.*;
class LinearSearchDemo
{ static int a[ ], key, n, loc ;
static int linearSearch()
{ for( int i=0; i<n; i++)
{
if(key == a[i])
return (i);
}
return (-1);
}
public static void main(String args[])
{ Scanner sc = new Scanner(System.in);
System.out.println("Enter number of elements : ");
n = sc.nextInt( );
a = new int[n];
System.out.println("Enter elements : ");
for(int i=0;i<n;i++)
a[i] = sc.nextInt();
System.out.println("Enter key element to search : ");
key = sc.nextInt();
loc = linearSearch();
if( loc == -1 )
System.out.println(key + " is not found in the list");
else
System.out.println(key + " is found in the list at location " + (loc+1) );
}
}
42
9. b) Aim: To write a program to search an element in a given list using Binary Search
Technique.
Program:
import java.util.*;
class BinarySearchDemo
{ static int a[ ], key, n, loc;
static int binarySearch(int low, int high)
{
if( low > high )
return (-1);
int mid = (low + high)/2;
if( key < a[mid] )
return binarySearch(low, mid-1);
else if( key > a[mid])
return binarySearch(mid+1, high);
else
return mid;
}
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter number of elements : ");
n = sc.nextInt( );
a = new int[n];
System.out.println("Enter elements : ");
for(int i=0;i<n;i++)
a[i] = sc.nextInt();
System.out.println("Enter key element to search:");
key = sc.nextInt();
loc = binarySearch(0, n-1);
if( loc== -1 )
System.out.println(key + " is not found in the list");
else
System.out.println(key + " is found in the list " + (loc+1) );
}
}
43
10. Aim: To write a program to sort the given elements in ascending order using quick sort.
Program:
import java.util.*;
class QSort
{
void quick_sort(int a[],int lb,int ub)
{
int i,j,key,temp;
boolean flag = true;
if(lb<ub)
{
key=a[lb];
i=lb;
j=ub+1;
while(flag)
{
i=i+1;
while(a[i]<key&&i<ub)
i=i+1;
j--;
while((a[j]>key)&&(j>lb))
44
{
j=j-1;
}
if(i<j)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
else
{
flag=false;
}
}
temp=a[lb];
a[lb]=a[j];
a[j]=temp;
quick_sort(a,lb,j-1);
quick_sort(a,j+1,ub);
}
}
}
class QSDemo
{ public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter number of elements : ");
int n = sc.nextInt();
int arr[ ] = new int[n];
System.out.println("Enter elements : ");
for(int i=0;i<n;i++)
arr[i] = sc.nextInt();
QSort ob = new QSort();
ob.quick_sort(arr,0,n-1);
System.out.println("The sorted elements are:");
for(int i=0;i<n;i++)
System.out.println( arr[i]);
}
}
45
11. Aim: To write a program to sort the given elements in ascending order using merge sort.
Program:
import java.util.*;
class MSort
{ void merge_sort(int a[],int start,int finish)
{ int size,mid;
size=finish-start+1;
if(size>1)
{
mid=(start+finish)/2;
merge_sort(a,start,mid);
merge_sort(a,mid+1,finish);
46
merge(a,start,mid+1,finish);
}
}
void merge(int a[],int s,int m,int f)
{ int i,j,k;
I nt temp[] = new int [20];
i=s;
j=m;
k=1;
while(i<m&&j<=f)
{
if(a[i]<=a[j])
{
temp[k]=a[i];
i=i+1;
k=k+1;
}
else
{
temp[k]=a[j];
j=j+1;
k=k+1;
}
}
if(i>=m)
{
while(j<=f)
{
temp[k]=a[j];
j=j+1;
k=k+1;
}
}
else
{
while(i<m)
{
temp[k]=a[i];
i=i+1;
k=k+1;
}
}
for(i=1;i<=k-1;i++)
{
a[s+i-1]=temp[i];
}
}
}
class MSDemo
47
{ public static void main(String args[])
{ Scanner sc = new Scanner(System.in);
System.out.println("Enter number of elements : ");
int n = sc.nextInt();
int arr[ ] = new int[n];
System.out.println("Enter elements : ");
for(int i=0;i<n;i++)
arr[i] = sc.nextInt();
MSort ob = new MSort();
ob.merge_sort(arr,0,n-1);
System.out.println("The sorted elements are:");
for(int i=0;i<n;i++)
System.out.println( arr[i]);
}
}
48