1
[Link] (Computer Sc ience) Object Oriente d Programming with Java and Data Structures Lab Programs- Data Structures
1. Program to impleme nt Bubble Sort. import [Link].*; class Prog37 { public static void main(String[] args) throws IOException { [Link]("How many values do u want:"); BufferedReader br=new BufferedReader(new InputStreamReader([Link])); int n=[Link]([Link]()); int arr[]=new int[n]; [Link]("Enter " + n + " numbers:"); int temp; for(int i=0;i<n;i++) arr[i]=[Link]([Link]()); for(int j=n-1;j>1;j--) for(int k=0;k<j;k++) if(arr[k]>arr[k+1]) { temp=arr[k]; arr[k]=arr[k+1]; arr[k+1]=temp; } [Link]("Bubble Sorted List:"); for(int i=0;i<n;i++) [Link](arr[i]); } } 2. Program to impleme nt Selection Sort. import [Link].*; class Prog36 { public static void main(String[] args) throws IOException { [Link]("How many values do u want:"); BufferedReader br=new BufferedReader(new InputStreamReader([Link])); int n=[Link]([Link]()); int arr[]=new int[n]; [Link]("Enter " + n + " numbers:"); for(int i=0;i<n;i++)
Prasanth Kumar K (Head-Dept of Computers, IIMC)
2
arr[i]=[Link]([Link]()); int min; int temp; for(int j=0;j<n-1;j++) { min=j; for(int k=j+1;k<n;k++) if(arr[k]<arr[min]) min=k; temp=arr[j]; arr[j]=arr[min]; arr[min]=temp; } [Link]("Selection Sorted List:"); for(int i=0;i<n;i++) [Link](arr[i]); } } 3. Program to impleme nt Insertion Sort. import [Link].*; class Prog26 { public static void main(String[] args) throws IOException { [Link]("How many values do u want:"); BufferedReader br=new BufferedReader(new InputStreamReader([Link])); int n=[Link]([Link]()); int arr[]=new int[n]; [Link]("Enter " + n + " numbers:"); for(int i=0;i<n;i++) arr[i]=[Link]([Link]()); int temp,k; for(int j=1;j<n;j++) { temp=arr[j]; k=j; while(k>0 && arr[k-1]>=temp) { arr[k]=arr[k-1]; --k; } arr[k]=temp; } [Link]("Selection Sorted List:"); for(int i=0;i<n;i++) [Link](arr[i]); }
Prasanth Kumar K (Head-Dept of Computers, IIMC)
3
4. Program to impleme nt Quic k Sort. import [Link].*; class Prog39 { public static void main(String[] args) throws IOException { [Link]("How many values do u want:"); BufferedReader br=new BufferedReader(new InputStreamReader([Link])); int n=[Link]([Link]()); int arr[]=new int[n]; [Link]("Enter " + n + " numbers:"); for(int i=0;i<n;i++) arr[i]=[Link]([Link]()); quickSort(arr,0,[Link]-1); [Link]("Quick Sorted List:"); for(int i=0;i<n;i++) [Link](arr[i]); } public static void quickSort(int arr[], int left, int right) { int index = partition(arr, left, right); if (left < index - 1) quickSort(arr, left, index - 1); if (index < right) quickSort(arr, index, right); } public static int partition(int arr[], int left, int right) { int i = left, j = right; int tmp; int pivot = arr[(left + right) / 2]; while (i <= j) { while (arr[i] < pivot) i++; while (arr[j] > pivot) j--; if (i <= j) { tmp = arr[i]; arr[i] = arr[j]; arr[j] = t mp; i++; j--; } } return i; } } }
Prasanth Kumar K (Head-Dept of Computers, IIMC)
5. Program to impleme nt PUSH a nd POP ope rations on Stac k using array method. import [Link].*; class stack { int [ ]a; int top; public stack(int n) { a=new int[n]; top=-1; }
public void push(int val) { if(top==[Link]-1) { [Link]("Stack Overf low "); } else { top++; a[top]=val; } }
public void pop() { if(top==-1) { [Link]("Stack Underf low "); } else { [Link]("Popped element is "+a[top]); top--; } }
public void display() { if(top==-1) { [Link]("Stack is Empty ");
Prasanth Kumar K (Head-Dept of Computers, IIMC)
5
} else { for(int i=top;i>=0;i--) { [Link]("stack element:"+a[i]); } } } } public class Prog27 { public static void main(String args[]) throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader([Link])); stack s; [Link]("enter the size of stack" ); int n=[Link]([Link]()); s=new stack(n); int choice; do { [Link]("--------\nenter your choice\[Link]\[Link]\[Link]\[Link]:\n--------\n"); choice=[Link]([Link]()); switch(choice) { case 1: [Link]("enter element to push:"); int value=[Link]([Link]()); [Link](value); break; case 2: [Link](); break; case 3: [Link](); break; case 4: break; default:[Link]("Invalid Choice"); } }while(choice!=4); } }
Prasanth Kumar K (Head-Dept of Computers, IIMC)
6. Program to impleme nt inse rt a nd delete ope rations on Queue using array method.
import [Link].*; class queue { int maxSize; int [ ]a; int front; int rear; int nItems; public queue(int n) { maxSize = n; a = new int[maxSize]; front = -1; rear = -1; nItems = 0; } public void insert(int val) { if(rear == maxSize-1) { [Link]("Queue is Full"); } else { a[++rear] = val; front =0; } } public void remove() { if(front ==- 1 && rear==-1) { [Link]("Queue is Empty "); } else if(front ==rear) { front = -1; rear=-1; } else front ++; } public void display()
Prasanth Kumar K (Head-Dept of Computers, IIMC)
7
{ if(front ==- 1 || rear==-1) { [Link]("Queue is Empty "); } else { for(int i=f ront;i<=rear;i++) { if(front!=-1 && rear!=-1) [Link]("Queue Element:"+ a[i]); } } } } public class Prog29 { public static void main(String args[]) throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader([Link])); queue q; [Link]("enter the size of Queue" ); int n=[Link]([Link]()); q=new queue(n); int choice; do { [Link]("--------\nenter your choice\[Link]\[Link]\[Link]\[Link]:\n--------\n"); choice=[Link]([Link]()); switch(choice) { case 1: [Link]("enter element to Insert:"); int value=[Link]([Link]()); [Link](value); break; case 2: [Link](); break; case 3: [Link](); break; case 4: break; default:[Link]("Invalid Choice"); }
Prasanth Kumar K (Head-Dept of Computers, IIMC)
8
}while(choice!=4); } }
7. Program to create, inse rt, delete a nd display operations on Single Linked List. import [Link].*; class Link { int data; Link link; } class Prog21 { int count=0; Link start,temp,pre; public void insertFirst()throws Exception { Link newLink = new Link(); BufferedReader br=new BufferedReader(new InputStreamReader([Link])); [Link]("Enter a value:"); [Link]=[Link]([Link]()); [Link] = start; start = new Link; count++; } public void deleteLast()throws Exception { if(start==null) { [Link]("List is Empty"); } else if([Link]==null) { [Link]([Link]+" deleted from the list"); start=null; } else { temp=start; for(int i=1;i<count-1;i++) temp=[Link]; pre=[Link]; [Link]([Link]+" deleted from the list"); [Link]=null; count--;
Prasanth Kumar K (Head-Dept of Computers, IIMC)
9
} } public void showList() { if(start==null) [Link]("List is Empty"); else { temp=start; [Link]("Linked List is"); for(;temp!=null;temp=[Link]) [Link]([Link]+"- >"); [Link]("Null"); } } public static void main(String args[])throws Exception { int ch; BufferedReader br=new BufferedReader(new InputStreamReader([Link])); Prog21 s=new Prog21(); do { [Link]("-------Enter your choice\[Link]\[Link]\[Link]\[Link]:"); ch=[Link]([Link]()); switch(ch) { case 1: [Link](); break; case 2: [Link](); break; case 3: [Link](); break; case 4: [Link](0); break; default: [Link]("Invalid Choice"); } }while(ch!=4); } }
Prasanth Kumar K (Head-Dept of Computers, IIMC)
10
8. Program to impleme nt PUSH a nd POP ope rations on Stac k using Linke d List method. import [Link].*; class Link { int data; Link link; } class stack1 { int top,size; Link start,temp,pre; public stack1(int n) { size=n; top=-1; } public void push(int val) { if(top==size-1) { [Link]("Stack Overf low "); } else { Link newLink = new Link(); top++; [Link]=val; [Link] = start; start = newLink; } }
public void pop() { if(top==-1) { [Link]("Stack Underf low "); } else { temp=start;
Prasanth Kumar K (Head-Dept of Computers, IIMC)
11
[Link]([Link]+" deleted from the list"); start=[Link]; top--; } }
public void display() { if(top==-1) { [Link]("Stack is Empty "); } else { [Link]("Linked List is"); for(temp=start;temp!=null;temp=[Link]) [Link]([Link]+"- >"); [Link]("Null"); } } } public class Prog28 { public static void main(String args[]) throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader([Link])); stack1 s; [Link]("enter the size of stack" ); int n=[Link]([Link]()); s=new stack1(n); int choice; do { [Link]("--------\nenter your choice\[Link]\[Link]\[Link]\[Link]:\n--------\n"); choice=[Link]([Link]()); switch(choice) { case 1: [Link]("enter element to push:"); int value=[Link]([Link]()); [Link](value); break; case 2: [Link](); break; case 3: [Link](); break;
Prasanth Kumar K (Head-Dept of Computers, IIMC)
12
case 4: break; default:[Link]("Invalid Choice"); } }while(choice!=4); }}
9. Program to impleme nt inse rt a nd delete ope rations on Queue using Linked List method.
import [Link].*; class Link { int data; Link link; } class queue1 { int front,rear,size,count=0; Link start,temp,pre; public queue1(int n) { size=n; front =-1; rear=- 1; } public void insert(int val) { if(rear==size-1) { [Link]("Queue is Full"); } else { Link newLink = new Link(); ++rear; [Link]=val; [Link] = start; start = newLink; front =0; count++; } } public void delete() { if(front ==- 1 && rear==-1) { [Link]("Queue is Empty "); } else if(front ==rear)
Prasanth Kumar K (Head-Dept of Computers, IIMC)
13
{ [Link]([Link]+" deleted from the list "); start=null; front =-1; rear=- 1; count--; } else { temp=start; for(int i=1;i<count-1;i++) temp=[Link]; pre=[Link]; [Link]([Link]+" deleted from the list"); [Link]=null; front ++; count--; } } public void display() { if(front ==- 1 && rear==-1) { [Link]("Queue is Empty "); } else { [Link]("Queue in terms of Linked List is"); for(temp=start;temp!=null;temp=[Link]) [Link]([Link]+"- >"); [Link]("Null"); } } } public class Prog30 { public static void main(String args[]) throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader([Link])); queue1 q; [Link]("enter the size of Queue" ); int n=[Link]([Link]()); q=new queue1(n); int choice; do { [Link]("--------\nenter your choice\[Link]\[Link]\[Link]\[Link]:\n--------\n"); choice=[Link]([Link]()); switch(choice) { case 1: [Link]("enter element to push:");
Prasanth Kumar K (Head-Dept of Computers, IIMC)
14
int value=[Link]([Link]()); [Link](value); break; case 2: [Link](); break; case 3: [Link](); break; case 4: break; default:[Link]("Invalid Choice"); } }while(choice!=4); } }
10. Program to reverse a single linked list.
import [Link].*; class Link { int data; Link link; } class RevList { int count=0; Link start,temp,pre; int a[]=new int[50]; public void insertFirst()throws Exception { Link newLink = new Link(); BufferedReader br=new BufferedReader(new InputStreamReader([Link])); [Link]("Enter a value:"); [Link]=[Link]([Link]()); [Link] = start; start = newLink; count++; } public void reverseList()throws Exception { if(start==null) [Link]("List is Empty ");
Prasanth Kumar K (Head-Dept of Computers, IIMC)
15
else { int i=0; for(temp=start;temp!=null;temp=[Link]) { a[i++]=[Link]; } int j; [Link]("Reversed List:"); for(temp=start,j=i-1;temp!=null;temp=[Link],j--) [Link]=a[j]; } } public void showList() { if(start==null) [Link]("List is Empty"); else { [Link]("Linked List is"); for(temp=start;temp!=null;temp=[Link]) [Link]([Link]+"- >"); [Link]("Null"); } } } class Prog25a { public static void main(String args[])throws Exception { int ch; BufferedReader br=new BufferedReader(new InputStreamReader([Link])); RevList s=new RevList(); do { [Link]("-------Enter your choice\[Link]\[Link]\[Link]\[Link]:"); ch=[Link]([Link]()); switch(ch) { case 1: [Link](); break; case 2: [Link](); break; case 3: [Link](); break; case 4: [Link](0); break; default: [Link]("Invalid Choice"); } }while(ch!=4);
Prasanth Kumar K (Head-Dept of Computers, IIMC)
16
} }
11. Program to split a single linked list.
import [Link].*; class Link { int data; Link link; } class SplitList { int count=0; Link start,temp,pre; int a[]=new int[50]; public void insertFirst()throws Exception { Link newLink = new Link(); BufferedReader br=new BufferedReader(new InputStreamReader([Link])); [Link]("Enter a value:"); [Link]=[Link]([Link]()); [Link] = start; start = newLink; count++; } public void split() { if(start==null) [Link]("List is Empty "); else { Link l1=new Link(); Link l2=new Link(); int k=count/2; int i=0;
Prasanth Kumar K (Head-Dept of Computers, IIMC)
17
for(temp=start;i<k;temp=[Link],i++) { [Link]=[Link]; [Link]([Link] + "- >"); } [Link]("Null"); for(;temp!=null;temp=[Link]) { [Link]=[Link]; [Link]([Link] + "- >"); } [Link]("Null"); } } public void showList() { if(start==null) [Link]("List is Empty"); else { [Link]("Linked List is"); for(temp=start;temp!=null;temp=[Link]) [Link]([Link]+"- >"); [Link]("Null"); } } public void deleteLast()throws Exception { if(start==null) { [Link]("List is Empty"); } else if([Link]==null) { [Link]([Link]+" deleted from the list"); start=null; } else { temp=start; for(int i=1;i<count-1;i++) temp=[Link]; pre=[Link]; [Link]([Link]+" deleted from the list"); [Link]=null; count--; } }
Prasanth Kumar K (Head-Dept of Computers, IIMC)
18
} class Prog24 { public static void main(String args[])throws Exception { int ch; BufferedReader br=new BufferedReader(new InputStreamReader([Link])); SplitList s=new SplitList(); do { [Link]("-------Enter your choice\[Link]\[Link]\[Link]\[Link]\[Link]:"); ch=[Link]([Link]()); switch(ch) { case 1: [Link](); break; case 2: [Link](); break; case 3: [Link](); break; case 4: [Link](); break; case 5: [Link](0); break; default: [Link]("Invalid Choice"); } }while(ch!=5); } }
12. Program to c reate, insert, delete and display ope rations on circular single linked list. import [Link].*; class Link { int data; Link link; } class CList { int count=0; Link start,temp,pre;
Prasanth Kumar K (Head-Dept of Computers, IIMC)
19
public void insertFirst()throws Exception { Link newLink = new Link(); BufferedReader br=new BufferedReader(new InputStreamReader([Link])); [Link]("Enter a value:"); [Link]=[Link]([Link]()); [Link] = start; start = new Link; if([Link]==null) [Link]=start; count++; } public void deleteLast()throws Exception { if(start==null) { [Link]("List is Empty"); } else if([Link]==null) { [Link]([Link]+" deleted from the list"); start=null; } else { temp=start; for(int i=1;i<count-1;i++) temp=[Link]; pre=[Link]; [Link]([Link]+" deleted from the list"); [Link]=start; count--; } } public void showList() { if(start==null) [Link]("List is Empty"); else { int c=0; [Link]("Linked List is"); for(temp=start;c<count;temp=[Link],c ++) [Link]([Link]+"- >"); [Link]("F irstElement(" + [Link] + ")"); } } } class Prog23 {
Prasanth Kumar K (Head-Dept of Computers, IIMC)
20
public static void main(String args[])throws Exception { int ch; BufferedReader br=new BufferedReader(new InputStreamReader([Link])); CList c=new CList(); do { [Link]("-------Enter your choice\[Link]\[Link]\[Link]\[Link]:"); ch=[Link]([Link]()); switch(ch) { case 1: [Link](); break; case 2: [Link](); break; case 3: [Link](); break; case 4: [Link](0); break; default: [Link]("Invalid Choice"); } }while(ch!=4); } }
13. Program to c reate, insert, delete and display ope rations on double linked list. import [Link].*; class Link { int data; Link next; Link previous; public Link(int d) { data = d; } public void displayLink(){ [Link](data + "- >"); }
Prasanth Kumar K (Head-Dept of Computers, IIMC)
21
} class DLList { Link first; Link last; public DLList() { first = null; last = null; } public boolean isEmpty() { return first == null; } public void insertFirst(int val){ Link new Link = new Link(val); if (isEmpty()) last = newLink; else [Link] = newLink; [Link] = f irst; first = newLink; } public void deleteLast()throws Exception { if(first==null) { [Link]("List is Empty"); } else if([Link] ==null) { [Link](f [Link]+" deleted from the list"); first=null; } else { [Link]([Link] + " is deleted"); [Link] = null; last = [Link]; } } public void showList() { if(first==null) [Link]("List is Empty"); else { displayForward();
Prasanth Kumar K (Head-Dept of Computers, IIMC)
22
display Backward(); } } public void displayForward() { [Link]("List(first to last): "); Link current = first; while (current != null) { [Link] Link(); current = [Link]; } [Link]("Null"); } public void display Backward() { [Link]("List (last to first) : "); Link current = last; while (current != null){ [Link] Link(); current = [Link] ious; } [Link]("Null"); }
} class Prog22 { public static void main(String args[])throws Exception { int ch; BufferedReader br=new BufferedReader(new InputStreamReader([Link])); DLList c=new DLList(); do { [Link]("-------Enter your choice\[Link]\[Link]\[Link]\[Link]:"); ch=[Link]([Link]()); switch(ch) { case 1: [Link]("Enter a value:"); int val=[Link]([Link]()); [Link](val); break; case 2: [Link](); break; case 3: [Link](); break; case 4: [Link](0); break;
Prasanth Kumar K (Head-Dept of Computers, IIMC)
23
default: [Link]("Invalid Choice"); } }while(ch!=4); } }
14. Program to implement insert and delete on Priority Queue.
import [Link].*; class queue { int maxSize; int [ ]a; int front; int rear; int nval; public queue(int n) { maxSize = n; a = new int[maxSize]; front = -1; rear = -1; nval = 0; } public void insert(int val) { if(rear == [Link]-1) { [Link]("Queue is Full"); } else if(nval==0) a[nval++]=val; else { int j; for(j=nval-1;j>=0;j--) { if(val>a[j]) a[j+1]=a[j]; else break; } a[j+1]=val; nval++; } ++rear; front =0;
Prasanth Kumar K (Head-Dept of Computers, IIMC)
24
} public void remove() { if(front ==- 1 && rear==-1) { [Link]("Queue is Empty "); } else if(front ==rear) { front = -1; rear=-1; nval--; [Link](a[nval] + "is deleted"); } else { nval--; [Link](a[nval] + "is deleted"); front ++; } } public void display() { if(front ==- 1 && rear==-1) { [Link]("Queue is Empty "); } else { [Link]("Queue Elements are:"); for(int i=0;i<nval;i++) { if(front!=-1 && rear!=-1) [Link](a[i] + " "); } } } } public class Prog31 { public static void main(String args[]) throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader([Link])); queue q; [Link]("enter the size of Queue" ); int n=[Link]([Link]()); q=new queue(n); int choice; do {
Prasanth Kumar K (Head-Dept of Computers, IIMC)
25
[Link]("--------\nenter your choice\[Link]\[Link]\[Link]\[Link]:\n--------\n"); choice=[Link]([Link]()); switch(choice) { case 1: [Link]("Enter element to Insert:"); int value=[Link]([Link]()); [Link](value); break; case 2: [Link](); break; case 3: [Link](); break; case 4: break; default:[Link]("Invalid Choice"); } }while(choice!=4); } }
15. Program to implement insert and delete Double Ended Queue.
import [Link].*; class queue { int maxSize; int [ ]a; int front; int rear; public queue(int n) { maxSize = n; a = new int[maxSize]; front = -1; rear = -1; }
public void insertL(int val) { if(rear == maxSize-1) { [Link]("Queue is Full");
Prasanth Kumar K (Head-Dept of Computers, IIMC)
26
} else if(rear==-1 && front ==- 1) { front =0; rear=0; a[front]=val; } else { int temp; for(int i=rear;i>=0;i--) a[i+1]=a[i]; rear++; a[front]=val; } } public void insert R(int val) { if(rear == maxSize-1) { [Link]("Queue is Full"); } else { a[++rear] = val; front =0; } }
public void removeL() { if(front ==- 1 && rear==-1) { [Link]("Queue is Empty "); } else if(front ==rear) { [Link](a[front] + " is deleted"); front = -1; rear=-1; } else { [Link](a[0] + " is deleted"); for(int i=0;i<rear;i++) a[i]=a[i+1]; rear--; } }
Prasanth Kumar K (Head-Dept of Computers, IIMC)
27
public void removeR() { if(front ==- 1 && rear==-1) { [Link]("Queue is Empty "); } else if(front ==rear) { [Link](a[rear] + " is deleted"); front = -1; rear=-1; } else { [Link](a[rear] + " is deleted"); rear--; } } public void display() { if(front ==- 1 || rear==-1) { [Link]("Queue is Empty "); } else { [Link]("Queue Elements:"); for(int i=f ront;i<=rear;i++) { if(front!=-1 && rear!=-1) [Link](a[i] + " "); } } } } public class Prog32 { public static void main(String args[]) throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader([Link])); queue q; [Link]("enter the size of Queue" ); int n=[Link]([Link]()); q=new queue(n); int choice,value; do { [Link]("--------\nenter your choice\[Link]\[Link]\[Link]\[Link]\[Link]\[Link] IT:\n--------\n"); choice=[Link]([Link]()); switch(choice) {
Prasanth Kumar K (Head-Dept of Computers, IIMC)
28
case 1: [Link]("enter element to Insert:"); value=[Link]([Link]()); [Link](value); break; case 2: [Link]("enter element to Insert:"); value=[Link]([Link]()); [Link](value); break; case 3: [Link](); break; case 4: [Link](); break; case 5: [Link](); break; case 6: break; default:[Link]("Invalid Choice"); } }while(choice!=6); 16. Program to evaluate postfix expression by using stac k. } } import [Link].*; class stack { int [ ]a; int top; public stack(int n) { a=new int[n]; top=-1; }
public void push(int val) { a[++top]=val; }
Prasanth Kumar K (Head-Dept of Computers, IIMC)
29
public int pop() { return a[top--]; } }
class Postfix { stack s; String input; public Postfix(String str) { input =str; } public int doParse() { s=new stack(20); char ch; int j; int num1,num2,ans; for(j=0;j<[Link]();j++) { ch=[Link](j); if(ch>='0' && ch<='9') [Link]((int) (ch-'0')); else { num2=[Link](); num1=[Link](); switch(ch) { case '+': ans=num1+num2; break; case '-': ans=num1-num2; break; case '*': ans=num1* num2; break; case '/': ans=num1/num2; break; default: ans=0; } [Link](ans); } } ans=[Link](); return ans; } }
Prasanth Kumar K (Head-Dept of Computers, IIMC)
30
public class Prog33 { public static void main(String args[]) throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader([Link])); [Link]("Enter the Postfix expression" ); String s=[Link](); Postfix p=new Postfix(s); int res=[Link](); [Link]("Evaluates to " + res); } }
17. Program to construct Binary Searc h Tree a nd implement tree traversing techniques.
import [Link].*; public class Prog34 { public static void main(String[] args) throws IOException { Prog34 p=new Prog34(); [Link](); } static class Node { Node left; Node right; int value; public Node(int value) { [Link] = value; } } public void run() throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader([Link])); [Link]("Enter the root element"); int n=[Link]([Link]()); Node root = new Node(n); [Link]("Building tree with root value " + [Link]); int choice; int val; do { [Link]("Enter the element to insert "); val=[Link]([Link]()); insert(root,val);
Prasanth Kumar K (Head-Dept of Computers, IIMC)
31
[Link]("To stop Enter 0"); choice=[Link]([Link]()); }while (choice!=0); [Link]("Traversing tree(PREORDER)"); printPreOrder(root); [Link]("Traversing tree(INORDER)"); printInOrder(root); [Link]("Traversing tree(POSTORDER)"); printPostOrder(root); } public void insert(Node node, int value) { if (value <= [Link]) { if ([Link] != null) { insert([Link], value); } else { [Link](" Inserted " + value + " to left of " + [Link]); [Link] = new Node(value); } } else if (value >= [Link]) { if ([Link] != null) { insert([Link], value); } else { [Link](" Inserted " + value + " to right of " + [Link]); [Link] = new Node(value); } } } public void printPostOrder(Node node) { if (node != null) { printInOrder([Link]); printInOrder([Link]); [Link](" Traversed " + [Link]); } } public void printPreOrder(Node node) { if (node != null) { [Link](" Traversed " + [Link]); printInOrder([Link]); printInOrder([Link]); } }
public void printInOrder(Node node) { if (node != null) { printInOrder([Link]); [Link](" Traversed " + [Link]); printInOrder([Link]); } } }
Prasanth Kumar K (Head-Dept of Computers, IIMC)