Ex No: 1 MIN HEAP Date

:

AIM To implement the min heap structure with insert and delete minimum operation using Java program

ALGORITHM Step 1: Step 2: Step 3: start the program by creating function with min heap property two functions namely insert() and deletemin() are created The insert () is used to insert new element in the tree structure with heap property. The deletemin() is used to delete the minimum element which is usually a root node. The two operations are performed satisfying heapness and completeness property. End of the program.

Step 4:

Step 5:

Step 6:

1

PROGRAM:
MIN HEAP

import java.io.*; class heapalg { int maxsize=100,size; int[] h=new int[maxsize]; public int leftchild(int i) { return 2*i; } public int rightchild(int i) { return 2*i + 1; } public int parent(int i) { return i/2; } public boolean isleaf(int i) { return ((i<=size) && (i>size/2)); } public void swap(int i,int j) { int t; t=h[i];h[i]=h[j];h[j]=t; } public void display() { System.out.println("The heap elements are:"+"\n"); for(int i=1;i<=size;i++) System.out.println("\n"+h[i]); } public void insert() { size++; if(size>maxsize) System.out.println("Heapfull"); else { try { System.out.println("Enter the element:"); DataInputStream din=new DataInputStream(System.in); h[size]=Integer.parseInt(din.readLine()); } catch(Exception e){} insertelt(size); } } public void insertelt(int i) { while ((h[parent(i)]>h[i])) { int par=parent(i); swap(par,i); i=par;

2

} public void delete() } { if(size==0) System.out.println("Heapempty"); else { System.out.println("The deleted min elt:"+h[1]); h[1]=h[size--]; if(size!=0) percolate(1); } } public void percolate(int i) { while(!isleaf(i)) { int small=leftchild(i); if( (small<size)&& h[small] > h[small+1]) small+=1; if(h[small] < h[i]) swap(small,i); i=small; } } class minheap } { public static void main(String args[]) throws IOException { int ch=0,cont=0; heapalg h1=new heapalg(); do { System.out.println("MIN HEAP 1.Insert 2.Delete Min"); DataInputStream din=new DataInputStream(System.in); try { ch=Integer.parseInt(din.readLine()); } catch(Exception e){} if(ch==1) { h1.insert(); h1.display(); } else if(ch==2) { h1.delete(); h1.display(); } else { System.out.println("Enter the correct choice"); } System.out.println("press 1 to continue:"); try { cont=Integer.parseInt(din.readLine());

3

} catch(Exception e){} }while(cont==1). } } 4 .

Insert 2.OUTPUT: -------MIN HEAP 1.Insert 2. 5 .Delete Min 2 The deleted min elt:2 The heap elements are: 3 42 86 press 1 to continue: 12 .Delete Min 1 Enter the element: 3 The heap elements are: 3 42 press 1 to continue: 1 MIN HEAP 1.Insert 2.Insert 2.Delete Min 1 Enter the element: 42 The heap elements are: 42 press 1 to continue: 1 MIN HEAP 1.Delete Min 1 Enter the element: 2 The heap elements are: 2 3 86 42 press 1 to continue: 1 MIN HEAP 1.Insert 2.Delete Min 1 Enter the element: 86 The heap elements are: 3 42 86 press 1 to continue: 1 MIN HEAP 1.

After the insert and delete operation Deap elements are displayed.Ex No: 2 DEAPS Date: AIM To implement program for deaps structure with insert and delete operations using Java. The delete() is done with 2 methods namely deletemax() and deletemin() The leftChild and rightChild are compared and the appropriate element is placed in the root node. Perform insert and delete functions. The insert() is done with 2 methods namely maxinsert() and mininsert(). ALGORITHM Step 1: Step 2: Step 3: Step 4: Step 5: Start the program by creating Deap Structure. Step 6: Step 7: 6 . Stop of the program.

t=h[i]. class deapsalg { int maxsize=100. } else { return 1. return partner.floor(Math.log(p)/Math. 7 . } public void swap(int i. int partner=p-(int)(Math.int j) { int t.PROGRAM DEAPS import java. } public boolean isleaf(int i) { return ((i<=size) && (i>size/2)).log(p)/Math. } public void display() { System.log(2)))-1).out. } public int rightchild(int i) { return 2*i + 1. } } public int MinPartner(int p) { int powvalue=(int) ((Math.powvalue)).io. for(int i=1.h[i]=h[j].i++) System. int[] h=new int[maxsize+1]. public int leftchild(int i) { return 2*i. } public int MaxPartner(int p) { int powvalue=(int) ((Math. } public int parent(int i) { return i/2.size.println("The deaps elements are:").out. while(t!=2 && t!=3) t=t/2.*.log(2)))-1).floor(Math.i<=size+1.println("\n"+h[i]).h[j]=t. } public int MaxHeap(int i) { int t=i. if(t==2) { return 0.pow(2.

newelt=Integer. swap(par.partner). switch(MaxHeap(p)) { case 1: int partner=MinPartner(p). } public void MaxInsert(int i) } { while (parent(i) !=1 && (h[parent(i)]<h[i])) { int par=parent(i). return partner.pow(2. } int p=size+1. 8 . } catch(Exception e){} if(size==1) { h[2]=newelt. i=par.out. i=par. MinInsert(partner). if(h[partner]>h[p]) { swap(p. else { try { System. return. public void MinInsert(int i) } { while (parent(i)!=1 && (h[parent(i)]>h[i])) { int par=parent(i). size++. } else MaxInsert(p).readLine()). case 0: partner=MaxPartner(p).println("Deap full").i). } public void insert() } { int newelt=0. swap(par. if(size>maxsize) System. DataInputStream din=new DataInputStream(System.println("Enter the element:").parseInt(din.powvalue)).i). if(partner>size+1) partner/=2. h[p]=newelt.out. break.in).int partner=p+(int)(Math.

} else MaxInsert(i).if(h[partner]<h[p]) { swap(p. break.out. size--. default: 9 . int small. int p=size+1.2*i<=size+1.i=small) { if(h[rightchild(i)]<h[leftchild(i)]) small=rightchild(i). } else MinInsert(p).println("Deap empty"). break.partner). break. if(h[partner]<h[i]) { swap(i. MaxInsert(partner). t h[p]=t. int t=h[p]. for(i=2.out.i<=size+1.println("The deleted min elt:"+ h[2]). } else MinInsert(i). else small=leftchild(i). } } public void deletemin() } { if(size==0) System. MaxInsert(partner). int i. for( i=2.out.println("ERROR").i++) { switch(MaxHeap(i)) { case 1: int partner=MinPartner(i).partner). h[i]=h[small]. if(h[partner]>h[i]) { swap(i.partner). MinInsert(partner). case 0: partner=MaxPartner(i). } p=i. else { System. default: System.

2*i<=size+1. case 0: partner=MaxPartner(i). for( i=3.i=big) { if(h[rightchild(i)]>h[leftchild(i)]) big=rightchild(i). int p=size+1. else big=leftchild(i). } } } public void deletemax() } { if(size==0) System.out.out. } } } } public class deaps } { 10 . if(h[partner]>h[i]) { swap(i.System. int t=h[p]. default: System.println("The deleted max elt:"+ h[3]). break. MinInsert(partner).println("Deap empty"). h[i]=h[big].println("ERROR"). } else MinInsert(i). } p=i. h[p]=t. for(i=2. size--.out.partner). int i. else { System.println("ERROR"). break. MaxInsert(partner). if(h[partner]<h[i]) { swap(i. int big. } else MaxInsert(i).out.i++) { switch(MaxHeap(i)) { case 1: int partner=MinPartner(i).i<=size+1.partner).

h1. do { System.display().println("Enter the correct choice").insert().out.deletemin(). try { cont=Integer. } } 11 .readLine()).out. } catch(Exception e){} }while(cont==1).Delete Max"). deapsalg h1=new deapsalg().println("press 1 to continue:").public static void main(String args[]) throws IOException { int ch=0.parseInt(din. h1. } System.readLine()).Insert 2.println("DEAPs 1.in). } else { System.deletemax().cont=0.parseInt(din.display().Delete Min 3. } else if(ch==3) { h1.display(). h1. } else if(ch==2) { h1. try { ch=Integer.out. } catch(Exception e){} if(ch==1) { h1. DataInputStream din=new DataInputStream(System.

Insert 2.Delete Max 1 Enter the element: 3 The deaps elements are: 0 3 20 5 press 1 to continue: 1 DEAPs 1.Delete Min 3.Insert 2.OUTPUT: -------DEAPs 1.Insert 2.Delete Max 1 Enter the element: 7 The deaps elements are: 0 3 20 5 7 press 1 to continue: 1 DEAPs 1.Delete Max 1 Enter the element: 5 The deaps elements are: 0 5 20 press 1 to continue: 1 DEAPs 1.Delete Min 3.Delete Max 1 Enter the element: 20 The deaps elements are: 0 20 press 1 to continue: 1 DEAPs 1.Delete Min 3.Insert 2.Delete Min 3.Delete Max 1 Enter the element: 11 The deaps elements are: 0 3 20 5 7 11 press 1 to continue: 1 12 .Delete Min 3.Insert 2.

Insert 2.Insert 2.Delete Min 3.Delete Min 3.Delete Min 3.Delete Max 1 Enter the element: 88 The deaps elements are: 0 3 88 5 7 77 20 11 55 press 1 to continue: 1 DEAPs 1.Delete Max 1 Enter the element: 77 The deaps elements are: 0 3 77 5 7 55 20 11 press 1 to continue: 1 DEAPs 1.Delete Min 3.Delete Max 1 Enter the element: 17 The deaps elements are: 0 3 88 5 7 77 20 11 55 13 .Delete Max 1 Enter the element: 55 The deaps elements are: 0 3 55 5 7 11 20 press 1 to continue: 1 DEAPs 1.Insert 2.Insert 2.DEAPs 1.

Delete Min 3.Delete Max 1 Enter the element: 1 The deaps elements are: 0 1 88 5 3 77 20 11 55 17 7 press 1 to continue: 1 DEAPs 1.Insert 2.Insert 2.Delete Max 2 The deleted min elt:1 The deaps elements are: 0 3 100 5 7 88 77 11 55 17 20 press 1 to continue: 14 .Delete Max 1 Enter the element: 100 The deaps elements are: 0 1 100 5 3 88 20 11 55 17 7 77 press 1 to continue: 1 DEAPs 1.Insert 2.Delete Min 3.Delete Min 3.17 press 1 to continue: 1 DEAPs 1.

RESULT Thus the program for Deaps using Java has been implemented. 15 .

No:3 LEFTIST HEAP Date: AIM To implement the leftist heap with insert and deletemin operation using Java. We know heap as the root node with minimum element.Ex. The deletemin() is used to delete the minimum element in the heap. Stop the program. ALGORITHM Step 1: Step 2: Step 3: Start the program by defining function. The insert operation is performed by combining the two leftist trees. After the insert and delete operations leftist heap elements are displayed. Step 4: Step 5: Step 6: Step 7: 16 . The insert and delete operations are performed with the help of combining 2 trees.

node b) { if(a.RC.RC==null) a.b). temp. t=a. } if(a. temp. } catch(Exception e){} node temp = new node().RC. a=b.RC=t. class node { public int data.data) { node t.PROGRAM LEFTIST HEAP import java.println("Enter the element:").RC.RC.parseInt(din.RC=meld(a. a. else a.in).RC=b.RC==null) a.io.data > b. if(root==null) root=temp.RC. public void insert() { int newelt=0.RC=null.temp).data=newelt. t=a. newelt=Integer.LC.shortest < a.LC==null) || (a. DataInputStream din=new DataInputStream(System. public node LC. a.LC=temp.LC.readLine()). } public node meld(node a.shortest=1. } if(a. return a. } public void remove() { 17 . temp.out. public int shortest.LC=a. else a. } class minleftist { node root = null.shortest)) { node t=new node(). else root=meld(root. if((a.shortest+1. try { System.shortest=1.*. b=t.shortest=a.

out.println(currentnode. } System. Delete"). dispin(currentnode. } else if(ch==2) { m. try { cont=Integer. m.parseInt(din.out. } catch(Exception e){} 18 .System.RC).data+"\n").println("Enter the correct choice"). Insert 2. } else { System.shortest). public void display() } { if(root==null) System. System. do { System.LC. minleftist m = new minleftist().parseInt(din. dispin(root).RC).println("EMPTY").out. } } public void dispin(node currentnode) { if(currentnode!=null) { dispin(currentnode.display().LC).root.readLine()).display().println("Deleted element is "+root.out.println("LEFTIST TREE 1. m. } catch(Exception e){} if(ch==1) { m. DataInputStream din = new DataInputStream(System.remove(). else { System.readLine()). root=meld(root.println("\nIn Order").out.out.out. } } }.in).println("press 1 to continue:").insert().data+" "+"SHORTEST "+currentnode. try { ch=Integer.cont=0. class LeftistTree { public static void main(String args[ ])throws IOException { int ch=0.

} OUTPUT: } ------LEFTIST TREE 1. Delete 1 Enter the element: 13 In Order 50 SHORTEST 1 8 SHORTEST 2 13 SHORTEST 1 press 1 to continue: 1 LEFTIST TREE 1. Delete 1 Enter the element: 11 In Order 50 SHORTEST 1 8 SHORTEST 2 13 SHORTEST 1 11 SHORTEST 1 press 1 to continue: 1 LEFTIST TREE 1. Insert 2. Delete 1 Enter the element: 50 In Order 50 SHORTEST 1 press 1 to continue: 1 LEFTIST TREE 1. Delete 1 Enter the element: 20 In Order 13 SHORTEST 1 11 SHORTEST 2 20 SHORTEST 1 8 SHORTEST 2 50 SHORTEST 1 press 1 to continue: 1 LEFTIST TREE 1. Insert 2. Insert 2. Insert 2.}while(cont==1). Insert 2. Delete 1 Enter the element: 8 In Order 50 SHORTEST 1 8 SHORTEST 1 press 1 to continue: 1 LEFTIST TREE 1. Insert 2. Delete 1 Enter the element: 19 .

Delete 1 Enter the element: 2 In Order 13 SHORTEST 1 11 SHORTEST 2 20 SHORTEST 1 8 SHORTEST 2 50 SHORTEST 1 18 SHORTEST 1 2 SHORTEST 1 press 1 to continue: 1 LEFTIST TREE 1. Delete 2 Deleted element is 2 In Order 13 SHORTEST 1 11 SHORTEST 2 20 SHORTEST 1 8 SHORTEST 2 50 SHORTEST 1 18 SHORTEST 1 20 . Insert 2. Insert 2.18 In Order 13 SHORTEST 1 11 SHORTEST 2 20 SHORTEST 1 8 SHORTEST 2 50 SHORTEST 1 18 SHORTEST 1 press 1 to continue: 1 LEFTIST TREE 1. Insert 2. Delete 1 Enter the element: 7 In Order 13 SHORTEST 1 11 SHORTEST 2 20 SHORTEST 1 8 SHORTEST 2 50 SHORTEST 1 18 SHORTEST 1 2 SHORTEST 2 7 SHORTEST 1 press 1 to continue: 1 LEFTIST TREE 1.

RESULT Thus the program for leftist heap using Java has been implemented. 21 .

Step 6: Step 7: 22 . Insert the elements to the AVL tree. Check the tree if it is balanced or not.Ex no: 4 AVL TREE Date: AIM To implement the AVL tree with insert & delete operations using Java. 1 and -1. ALGORITHM Step 1: Step 2: Step 3: Step 4: Step 5: Start the program by defining the functions. If it is not balanced. The balance factor is one of 0. balance the tree using (i) Left-left (ii) Left-right (iii) Right-left (iv) Right-right Balancing And if the tree is balanced and then the insert() and the delete() operations are performed. Stop the program.

p=p. DataInputStream din=new DataInputStream(System.q. a=root.parseInt(din.} if(newelt<p.data){q=p.LC=null. found=false.in).f=q.} else {y=p. f=null.println("Enter the element:").p=p. q=null. y. while (p!=null && found!=true) { if(p. p=root.p. return true.LC=null.out.*.PROGRAM AVL TREE import java.RC=null. } node f.} else if(newelt>p.io.RC.c.RC. y. } class avltree { node root = null.data) q. unbalanced.} } if(found==false) { y.a. try { System.RC=null.data=newelt. public node LC.data=newelt. boolean found. int d. node b. newelt=Integer.LC=y. public int bf. y.found=true.data){q=p. } catch(Exception e){} if(root==null) { node y=new node().bf=0.bf!=0) {a=p. else 23 .LC. class node { public int data. y. if(newelt<q. public boolean insert() { int newelt=0. y.bf=0. root=y. y. node y=new node(). y.readLine()).

out.RC=c.bf==1) { System.d=1.bf==-1) { System. case 0: a.LC.data) {p=a.bf) { case 1: a.out.bf=-1. a.LC.RC=a. } else { System. b.} if(unbalanced==true) { if(d==1) { if(b. } } else { if(b.b=p.println("RR imbalance"). if(newelt >a.RC. break.out. break. b.bf=0.bf+=d.LC=b. c=b. break. switch(c.bf=0.data) {p. c.LC.unbalanced=false.bf=-1.RC=a. b. 24 .RC.} while(p!=y) { if(newelt > p. case -1: a.bf=1.d=-1.} } unbalanced=true. if((a. c.bf=0.} else {p=a.p=p.RC.bf+d)==0)) {a.println("LR imbalance").RC. a.bf==0)||((a. b.bf=0.LC=b. b=c.LC=c.bf=1.bf=0.RC. b.q.bf=0. b.} else {p.p=p. a.bf=0.RC=y. } c.println("LL imbalance").b=p.

case -1: a.LC=a.RC). } else { System.out.bf=0. System.LC=a. } } public void dispin(node currentnode) { if(currentnode!=null) { dispin(currentnode. } return false.LC. b.bf=1.a.RC. switch(c. b. else if(a==f. case 0: a. b=c. c=b.LC). } c.println("\nIn Order"). else if(a==f. } } if(f==null) root=b. a.bf=0.RC=c.LC.println("RL imbalance"). break.LC) f.RC=b.bf=0.LC.println("EMPTY").RC=b.bf=0.LC=c. } public void display() { if(root==null) System. a. b.out.bf=0. b.bf).bf=0. c. b.bf=0.println(currentnode.bf) { case 1: a. else { System. 25 .RC=b. c.out. break. } return true. b.data+" "+"BF "+currentnode. dispin(root). dispin(currentnode.out.bf=-1.LC=b.RC) f. break.

cont=0. } catch(Exception e){} if(ch==1) { boolean y=true. { public static void main(String args[ ])throws IOException { int ch=0.} } class AVLTreeImp }.println("Data already exists").println("press 1 to continue:"). } catch(Exception e){} }while(cont==1). Insert "). } else { System. } System. try { cont=Integer.insert().readLine()).in).readLine()). y=a.println("AVLTREES 1.println("Enter the correct choice"). a.parseInt(din. do { System. DataInputStream din = new DataInputStream(System. try { ch=Integer. } } 26 . avltree a = new avltree().out.display(). if(y==false) System.out.parseInt(din.out.out.

Insert 1 Enter the element: 3 In Order 3 BF 0 press 1 to continue: 1 AVLTREES 1. Insert 1 Enter the element: 1 LL imbalance In Order 1 BF 0 2 BF 0 3 BF 0 press 1 to continue: 1 AVLTREES 1. Insert 1 Enter the element: 2 In Order 2 BF 0 3 BF 1 press 1 to continue: 1 AVLTREES 1.OUTPUT: ------AVLTREES 1. Insert 1 Enter the element: 6 RR imbalance 27 . Insert 1 Enter the element: 4 In Order 1 BF 0 2 BF -1 3 BF -1 4 BF 0 press 1 to continue: 1 AVLTREES 1. Insert 1 Enter the element: 5 RR imbalance In Order 1 BF 0 2 BF -1 3 BF 0 4 BF 0 5 BF 0 press 1 to continue: 1 AVLTREES 1.

Insert 1 Enter the element: 7 RR imbalance In Order 1 BF 0 2 BF 0 3 BF 0 4 BF 0 5 BF 0 6 BF 0 7 BF 0 press 1 to continue: 1 AVLTREES 1. Insert 1 Enter the element: 14 28 .In Order 1 BF 0 2 BF 0 3 BF 0 4 BF 0 5 BF -1 6 BF 0 press 1 to continue: 1 AVLTREES 1. Insert 1 Enter the element: 16 In Order 1 BF 0 2 BF 0 3 BF 0 4 BF -1 5 BF 0 6 BF -1 7 BF -1 16 BF 0 press 1 to continue: 1 AVLTREES 1. Insert 1 Enter the element: 15 RL imbalance In Order 1 BF 0 2 BF 0 3 BF 0 4 BF -1 5 BF 0 6 BF -1 7 BF 0 15 BF 0 16 BF 0 press 1 to continue: 1 AVLTREES 1.

Insert 1 Enter the element: 11 29 . Insert 1 Enter the element: 12 LL imbalance In Order 1 BF 0 2 BF 0 3 BF 0 4 BF 0 5 BF 0 6 BF 1 7 BF 0 12 BF 0 13 BF 0 14 BF 0 15 BF 1 16 BF 0 press 1 to continue: 1 AVLTREES 1. Insert 1 Enter the element: 13 RR imbalance In Order 1 BF 0 2 BF 0 3 BF 0 4 BF 0 5 BF 0 6 BF 1 7 BF 0 13 BF 0 14 BF 1 15 BF 1 16 BF 0 press 1 to continue: 1 AVLTREES 1.RL imbalance In Order 1 BF 0 2 BF 0 3 BF 0 4 BF -1 5 BF 0 6 BF 1 7 BF 0 14 BF 0 15 BF 0 16 BF 0 press 1 to continue: 1 AVLTREES 1.

LL imbalance In Order 1 BF 0 2 BF 0 3 BF 0 4 BF 0 5 BF 0 6 BF 1 7 BF 0 11 BF 0 12 BF 1 13 BF 0 14 BF 0 15 BF 0 16 BF 0 press 1 to continue: 1 AVLTREES 1. Insert 1 Enter the element: 8 In Order 1 BF 0 2 BF 0 3 BF 0 4 BF 0 5 BF 0 6 BF 1 7 BF -1 8 BF 0 10 BF 1 11 BF 1 12 BF 0 30 . Insert 1 Enter the element: 10 LL imbalance In Order 1 BF 0 2 BF 0 3 BF 0 4 BF 0 5 BF 0 6 BF 1 7 BF 0 10 BF 0 11 BF 0 12 BF 0 13 BF 0 14 BF 0 15 BF 0 16 BF 0 press 1 to continue: 1 AVLTREES 1.

13 BF 1 14 BF 0 15 BF 0 16 BF 0 press 1 to continue: 1 AVLTREES 1. Insert 1 Enter the element: 16 In Order 1 BF 0 2 BF 0 3 BF 0 4 BF 0 5 BF 0 6 BF 1 7 BF -1 8 BF 0 9 BF 0 10 BF 0 11 BF 1 12 BF 0 13 BF 1 14 BF 0 15 BF 0 16 BF 0 Data already exists press 1 to continue: 12 31 . Insert 1 Enter the element: 9 LR imbalance In Order 1 BF 0 2 BF 0 3 BF 0 4 BF 0 5 BF 0 6 BF 1 7 BF -1 8 BF 0 9 BF 0 10 BF 0 11 BF 1 12 BF 0 13 BF 1 14 BF 0 15 BF 0 16 BF 0 press 1 to continue: 1 AVLTREES 1.

RESULT Thus the program for AVL tree using Java has been implemented 32 .

No:5 B-TREE Date: AIM To implement the b-tree with insert and delete operations using Java. If it is greater insert it into right sub tree.data2. Otherwise. to split the nodes Call the function display to display data1. Declare the class btree The insert and delete operations are performed To insert. ALGORITHM Step 1: Step 2: Step 3: Step 4: Start the program by defining function. if it is empty insert the element as root. check if root is empty.Ex.address and parent End of the program Step 5: Step 6: Step 7: Step 8: Step 9: 33 . insert it into left sub tree Use the function split.

rptr!=null) temp. else if((temp.data2=0. bnode prev.data2. if(temp. while(p1!=null) { prev=p1. } p1=prev.io.data2==0) { if(temp. void insert(int ele) { bnode temp=new bnode().*. if(root==null) { root=temp.data1<p1.rptr. if(temp.data1=this.data1<p1.parent.rptr.lptr.mptr=temp. p1.data1.rptr=this.p1. } else { p1=root.data1=ele. temp.PROGRAM B-TREE import java.lptr=temp. public void bnode() { this.rptr. else p1=p1.data1>p1. while(p1!=null) { if(p1.lptr=this.parent=p1.lptr.data2)) p1=p1. } } class btree { bnode root=null.mptr=this. bnode p.data1.lptr.parent=p1. p1.data1=temp.mptr.data1) &&(temp. p1. } else 34 .data1<p1.rptr.data1) p1=p1.parent=null. this. p1. bnode lptr. class bnode { int data1.data2=t.mptr.data1) { int t=p1.lptr!=null) temp. if(temp.

parent=p.lptr.mptr=null.data2!=0)) } { p1=split(temp.data2)) { if(p. p.rptr.lptr. p.data1.parent=p1.rptr. if(n2!=null) p. p.rptr. p.p1). 35 .lptr.data1) { if(p. } else if((t. } } } display(root).lptr!=null) n1=p.rptr.rptr!=null) n2=p.parent=p1. p1.rptr=temp.lptr=new bnode().mptr.data1=t. bnode n2=null.rptr.mptr=temp.parent.data1<p.parent=p.rptr.rptr.rptr. p.mptr!=null) n1=p.data1<p. if(n1!=null) p.data1=p. p. break.lptr!=null) temp. p. p.data2. temp=p1.parent=p.rptr. p1=p1. p.lptr=t.lptr=new bnode().parent=p.data1=p.rptr!=null) n2=p. else if((p1.rptr=new bnode().rptr.rptr. if(temp.data1.data2=temp. p.lptr.lptr.data1. if(p.parent=p1.rptr.parent.rptr.data1!=0) && (p1.data2=0.rptr=n2.lptr. p.rptr!=null) temp. if(t. if(p. p1. if(temp. p. t.data1>p. p.data1) && (t.lptr=n1. bnode split(bnode t.parent=p.bnode p) } { bnode n1=null.{ p1. } temp.

rptr=t. p.rptr. p.rptr.lptr!=null) p.lptr. if(t. p.mptr!=null) n2=p. display(temp.lptr=new bnode().data2. p. } } } class btrees { public static void main(String[] args)throws IOException { 36 .parent=p.rptr.lptr!=null) n1=p.rptr.parent=p.lptr.lptr).lptr.lptr. data2+" Address::"+temp+" parent::"+temp.lptr. p.rptr=n2.lptr.parent=p. p.data1=p. p. p.mptr=null. p.lptr.rptr.lptr=t. display(temp.mptr=null.rptr=new bnode().out. } return p.p.rptr. p.rptr).rptr.rptr=n2.data1+" data2::"+temp. p.lptr. } else { if(p.lptr.data1=p.rptr.rptr. p.data2.data2=0. if(n2!=null) p.rptr=t. } void display(bnode temp) { if(temp!=null) { display(temp.lptr=n1.parent).rptr.mptr).parent=p. p.lptr. if(n1!=null) p. if(n2!=null) p. p. p. System.rptr=new bnode(). p.lptr. p.rptr!=null) p.lptr.lptr=n1.rptr.parent=p.rptr. if(t.println("data1::"+temp.parent=p. p.lptr. if(p.lptr.lptr.lptr.data2=0. p.parent=p.mptr.lptr.data1.lptr.lptr.data1=p.parent=p. if(n1!=null) p.parent=p.rptr.

parseInt(in.out. int x. System.insert(x).out.println("To continue.. } } 37 .println("Enter the element").parseInt(in. }while(ch==1). btree bt=new btree(). x=Integer. bt.press 1")..System.readLine()).println("B-Trees"). DataInputStream in=new DataInputStream(System. ch=Integer. do { System.ch.in).out.readLine()).

.press 1 1 Enter the element 56 data1::12 data2::45 Address::bnode@130c19b parent::bnode@923e30 data1::56 data2::89 Address::bnode@1f6a7b9 parent::bnode@923e30 data1::52 data2::0 Address::bnode@923e30 parent::null To continue....OUTPUT: -------B-Trees Enter the element 52 data1::52 data2::0 Address::bnode@923e30 parent::null To continue.....press 1 1 Enter the element 32 data1::1 data2::0 Address::bnode@7d772e parent::bnode@923e30 data1::32 data2::45 Address::bnode@11b86e7 parent::bnode@923e30 data1::56 data2::89 Address::bnode@1f6a7b9 parent::bnode@923e30 data1::12 data2::52 Address::bnode@923e30 parent::null To continue...press 1 1 Enter the element 45 data1::45 data2::52 Address::bnode@923e30 parent::null To continue..press 1 1 Enter the element 12 data1::12 data2::45 Address::bnode@130c19b parent::bnode@923e30 data1::89 data2::0 Address::bnode@1f6a7b9 parent::bnode@923e30 data1::52 data2::0 Address::bnode@923e30 parent::null To continue....press 1 1 Enter the element 1 data1::1 data2::0 Address::bnode@7d772e parent::bnode@923e30 data1::45 data2::0 Address::bnode@11b86e7 parent::bnode@923e30 data1::56 data2::89 Address::bnode@1f6a7b9 parent::bnode@923e30 data1::12 data2::52 Address::bnode@923e30 parent::null To continue.press 1 1 Enter the element 89 data1::45 data2::0 Address::bnode@130c19b parent::bnode@923e30 data1::89 data2::0 Address::bnode@1f6a7b9 parent::bnode@923e30 data1::52 data2::0 Address::bnode@923e30 parent::null To continue..press 1 38 ..press 1 1 Enter the element 25 data1::1 data2::0 Address::bnode@7d772e parent::bnode@35ce36 data1::25 data2::0 Address::bnode@757aef parent::bnode@35ce36 data1::12 data2::0 Address::bnode@35ce36 parent::bnode@923e30 data1::45 data2::0 Address::bnode@d9f9c3 parent::bnode@9cab16 data1::56 data2::89 Address::bnode@1f6a7b9 parent::bnode@9cab16 data1::52 data2::0 Address::bnode@9cab16 parent::bnode@923e30 data1::32 data2::0 Address::bnode@923e30 parent::null To continue.

1 Enter the element 60 data1::1 data2::0 Address::bnode@7d772e parent::bnode@35ce36 data1::25 data2::0 Address::bnode@757aef parent::bnode@35ce36 data1::12 data2::0 Address::bnode@35ce36 parent::bnode@923e30 data1::45 data2::0 Address::bnode@d9f9c3 parent::bnode@9cab16 data1::56 data2::0 Address::bnode@1a46e30 parent::bnode@9cab16 data1::89 data2::0 Address::bnode@3e25a5 parent::bnode@9cab16 data1::52 data2::60 Address::bnode@9cab16 parent::bnode@923e30 data1::32 data2::0 Address::bnode@923e30 parent::null To continue..press 1 1 Enter the element 83 data1::1 data2::0 Address::bnode@7d772e parent::bnode@35ce36 data1::25 data2::0 Address::bnode@757aef parent::bnode@35ce36 data1::12 data2::0 Address::bnode@35ce36 parent::bnode@923e30 data1::45 data2::0 Address::bnode@d9f9c3 parent::bnode@9cab16 data1::56 data2::0 Address::bnode@1a46e30 parent::bnode@9cab16 data1::83 data2::89 Address::bnode@3e25a5 parent::bnode@9cab16 data1::52 data2::60 Address::bnode@9cab16 parent::bnode@923e30 data1::32 data2::0 Address::bnode@923e30 parent::null To continue...press 1 12 RESULT: Thus the program for b-tree using Java has been implemented.. 39 .

if it is not found check for left or right side of the root. otherwise call empty print the current node in left sub tre in the format as currentnode.data + level and tag. If its found return the element to insert the particular element read that elemnt and insert the element with tag as 0 and level as 1. ALGORITHM Step 1: Step 2: Step 3: Start the program by defining the functions.display if root as null. display current node in the right sub tree end of the program Step 4: Step 5: Step 6: Step 7: Step 8: 40 . First initialize the node to null to find the particular element use function find check the element to root node. to display the elements . print as empty.Ex no: 6 TRIES Date: AIM To implement the tries with insert & delete operations using Java.

} class trie { public node cptr.*.PROGRAM TRIES import java. key=Integer. } else break. } public void insert() { int key=0.in). 41 . try { System.LC.par. class node { public int tag. item=item >> 1.tag==1) { if((item & 1)==0) { temp=temp. DataInputStream din=new DataInputStream(System. while(temp!=null) { cptr=temp.RC. } else { temp=temp. public node root=null. node temp=root.level. item=item >> 1. public node find(int key) { int item=key.data) { return temp.out. } } return null. } } else { if(key==temp. if(temp. } catch(Exception e){} if(root==null) { root=new node().readLine()).RC. starts with 1 public int data.io.parseInt(din.println("Enter the element:"). public node LC.

tag=1. 42 . int lv=1.level=temp. root.tag=0. temp. temp.level+1. } if( (k1 & 1)==0) { temp. root. k1=k1 >> 1. n3. n2.par=null.tag==0) { node n1=new node(). } else { temp.data=k1.RC=null. node n2=new node(). if(temp==null) { temp=cptr. lv++. n3.root. n1.par=temp.tag=0.tag=1. k2=k2 >> 1. n1.level=1.level=n2. if(temp.data=key. int kk1.temp.level=temp. while ( (k1 & 1 ) ==(k2 & 1 )) { kk1=k1.LC=null.data=k2. root. temp.data=0. temp. if(lv>=temp.tag=0.LC=n1. int k1=temp.data.level) { node n3=new node().LC=n2. n1. root.n2.RC=n2.level+1.level=temp.LC=n3.RC=n3. int k2=key. temp=n3. root. } else { temp. } else lv++.LC=null.RC=null. } else { node temp=find(key). if ( (kk1 & 1)==0) { temp. } n3.level+1. n3.

level=n2. else } public void display() } { if(root==null) System.out. n1.println("Element already exists").data=key.RC=n1. } } System.LC=n1.out. do { System. } } public void dispin(node currentnode) { if(currentnode!=null) { dispin(currentnode. try { ch=Integer. 43 } "+currentnode.out.level+" "+"LEVEL- . dispin(currentnode. n1.println("\nIn Order").println("TRIES 1. dispin(root).RC=n1.par=temp.level=temp.readLine()).LC).tag).par=temp. DataInputStream din = new DataInputStream(System. else { System. } } class TrieImp }. Insert "). n1. if(temp.println(currentnode. trie t = new trie().parseInt(din.LC==null) temp. } } else { n2. n1. System.par=temp.level+1.data+" "+"TAG-"+currentnode.level+1. node n1=new node().level=temp.out. n1.temp.RC).tag=0.println("EMPTY").out.cont=0. { public static void main(String args[ ])throws IOException { int ch=0. else temp. n1.in).

} } 44 . } else { System.println("Enter the correct choice").insert(). } System. try { cont=Integer.println("press 1 to continue:").parseInt(din.out.out. t.readLine()). } catch(Exception e){} }while(cont==1).display().catch(Exception e){} if(ch==1) { t.

3 1232 press 1 TAG-0 to continue: 1 0 LEVEL.1 0 LEVEL.2 4451 LEVEL. Insert 1 Enter the element: 1232 In Order 1232 LEVEL.5 TAG-0 0 LEVEL.5 TAG-0 0 LEVEL.5 TAG-0 0 LEVEL.3 1015 In Order LEVEL. Insert 1 Enter the element: 4451 In Order 1232 LEVEL. Insert 1 Enter the element: 1243 In Order 1232 LEVEL.3 press 1 to continue: 1 TRIES 1.2 TRIES 1.2 TAG-0 0 LEVEL. Insert 1 Enter the element: 1015 In Order 1232 LEVEL.OUTPUT: ------TRIES 1.4 1243 LEVEL.2 TAG-0 0 LEVEL.2 TAG-0 0 LEVEL.5 TAG-0 0 LEVEL.2 TAG-0 press 1 to continue: 1 TRIES 1.1 4451 LEVEL.2 4451 LEVEL. Insert 1 Enter the element: 1942 TAG-1 TAG-1 TAG-1 TAG-1 TAG-1 TAG-1 TAG-1 TAG-1 TAG-1 TAG-1 45 .1 TAG-0 press 1 to continue: 1 TRIES 1.4 TAG-0 LEVEL.4 1243 LEVEL.1 0 LEVEL.

1 TAG-1 0 LEVEL.5 TAG-0 0 LEVEL.1 TAG-1 1941 LEVEL.5 TAG-0 press 1 to continue: 1 TRIES 1.5 TAG-0 0 LEVEL.3 .2 TAG-1 4451 LEVEL.1942 LEVEL.2 TAG-1 1942 LEVEL.4 TAG-0 press 1 to continue: 1 TRIES 1.4 TAG-1 1243 LEVEL. Insert 1 Enter the element: 1055 In Order 1232 LEVEL.5 TAG-0 0 LEVEL.3 TAG-0 0 LEVEL.3 TAG-1 1015 LEVEL.5 TAG-0 0 LEVEL.3 TAG-0 0 LEVEL.3 TAG-0 0 LEVEL.3 TAG-0 0 LEVEL.4 TAG-1 1055 LEVEL. Insert 1 Enter the element: 1243 Element already exists 46 In Order 1232 LEVEL.2 TAG-1 1942 LEVEL.4 TAG-1 1243 LEVEL.5 TAG-0 0 LEVEL.2 TAG-1 4451 LEVEL.5 TAG-0 0 LEVEL.3 TAG-0 0 LEVEL.3 TAG-0 0 LEVEL.1 TAG-1 1941 LEVEL.4 TAG-0 press 1 to continue: 1 TRIES 1.3 TAG-1 1015 LEVEL.4 TAG-1 1243 LEVEL.2 TAG-1 4451 LEVEL.5 TAG-0 0 LEVEL. Insert 1 Enter the element: 1941 In Order 1232 LEVEL.3 TAG-1 1015 LEVEL.3 TAG-0 0 LEVEL.

0 LEVEL.3 1015 LEVEL.5 TAG-0 0 LEVEL.5 TAG-0 0 LEVEL.4 1243 LEVEL.5 TAG-0 0 LEVEL.4 1055 LEVEL.5 TAG-0 press 1 to continue: 12 TAG-1 TAG-1 TAG-1 TAG-1 RESULT Thus the program for tries using Java has been implemented 47 .2 4451 LEVEL.

Ex No: 7 Quick Sort Date:

AIM To write a Java program for the implementation the quick sort ALGORITHM Step1 :start the program

Step2: declare and initialize the array size Step3: enter the number of elements to be quick sorted. Step4: enter the elements using for loop Step5: call the function quick(1,noe) Void quick(int first,int last) Step6: if the first element is less than the last (a) then the first element is taken as the pivot &i=first, &j=last (b) the condition is checked for i<j if true Step7: set a loop to check the elements (a)while (a[pivot]>=a[i]&&i<last)i++; (b)while (a[pivot]>=a[j]&&j>first)j--; Step8: if (i>j) Swap(i,j) Step9: sort the elements and display the sorted values.

48

PROGRAM Quick Sort import java.io.*; class quicksortalg { int noe; int[] a=new int[100]; public void sort() { try { System.out.println("Enter the number of elements:"); DataInputStream din=new DataInputStream(System.in); noe=Integer.parseInt(din.readLine()); System.out.println("Enter the elements:"); for(int i=1;i<=noe;i++) a[i]=Integer.parseInt(din.readLine()); System.out.println("The array:"); display(); } catch(Exception e){} quick(1,noe); } public void swap(int i,int j) { int t; t=a[i];a[i]=a[j];a[j]=t; } public void quick(int first,int last) { if(first<last) { int pivot=first; int i=first; int j=last; while(i<j) { while(a[pivot]>=a[i] && i<last) i++; while(a[pivot]<=a[j] && j>first) j--; if(i<j) swap(i,j); } swap(pivot,j); quick(first,j-1); quick(j+1,last); } } public void display() { for(int i=1;i<=noe;i++)

49

System.out.println(a[i]+"\n"); } class quicksort } { public static void main(String args[])throws IOException { quicksortalg q1=new quicksortalg(); q1.sort(); System.out.println("The sorted array:"); q1.display(); }

50

OUTPUT Enter the number of elements: 5 Enter the elements: 2 96 1 45 63 The array: 2 96 1 45 63 The sorted array: 1 2 45 63 96 RESULT: Thus the program for quick sort has been implemented using Java and the output is verified. 51 .

Ex No: 8 CONVEX HULL Date: AIM To write a Java program for the implementation of convex hull ALGORITHM Step1: Start the program Step2: Create a class convexhullalg Step3: Read the number of points Step4: Get the x and y co-ordinate values Step5: Sort the values using sort function Step6: To sort two values swap the values of i and j Step7: Call the function display to display the boundary points Step8: The function check id used to check whether the point is angular or not(180▫) Step9: End of the program 52 .

in). temp=y[i]. display().n.out.j++) if((x[i]>x[j]) || ((x[i]==x[j]) && (y[i]>y[j]))) swap(i. for(int i=0. y[i]=y[j].PROGRAM CONVEX HULL import java.i<n.readLine()).parseInt(in.out.out.i++) if(status[i]==true) System.'H').parseInt(in. check(0. System.int j) { int temp=x[i]. class convexhullalg { int x[].println("Boundary points are").i<n-1. check(0.println("("+x[i]+". } } catch(Exception e){} sort().println("Enter x and y coordinates for "). x[i]=Integer. n=Integer.println("point "+(i+1)). x[i]=x[j].*. x[j]=temp. } void sort() { for(int i=0. y[j]=temp. boolean status[]. "+y[i]+")"). j). y[i]=Integer.println("Enter number of points:"). } void display() { System. x=new int[n].out.i<n. System.readLine()).i++) { System.out. status[i]=false. } } void swap(int i.readLine()). } 53 . void insert() { try { DataInputStream in=new DataInputStream(System. y=new int[n].y[].'L').i++) { for(int j=i+1.j<n.parseInt(in.io. status=new boolean[n]. for(int i=0.

} if(i==p+1) { deg=degree. } } } if(next!=0) check(next.insert(). next=i.char c) { double slope=0. next=i. } else { if((c=='L' && deg>degree)||(c!='L' && deg<degree) ||(degree==deg && x[i]<x[next])) { deg=degree. for(int i=p+1.atan(slope)). status[p]=true. } catch(Exception e) { degree=90. } 54 . } class convexhull } { public static void main(String arg[]) throws IOException { convexhullalg c=new convexhullalg(). degree=Math.i++) { try { slope=(double)(x[i]-x[p])/(double)(y[i]-y[p]). if(degree < 0) degree+=180. c.toDegrees(Math.void check(int p. int next=0.i<n.c).deg=0.degree=0.

4) (2.OUTPUT: -------Enter number of points: 4 Enter x and y coordinates for point 1 2 6 point 2 7 8 point 3 1 4 point 4 2 10 Boundary points are (1. 55 . 10) (7. 8) RESULT: Thus the program for convex hull has been implemented using Java and the output is verified.

ALGORITHM Step 1: Step 2: Step 3: Step 4: Step 5: Step 6: Step 7: Step 8: Start the program and define the function. print the profit and weight Initializing is set to false. Read the number of objects that are given.Ex No: 9 0/1 KNAPSACK USING DYNAMIC PROGRAMMING Date: AIM To write a Java program for the implementation of 0/1 knapsack using dynamic programming. 56 . Initialize the weight and profit. For each objects. Display and print the item weight and profit Display the total cost End of the program.

W=Integer. int option2=-1.println("\nThe optimal solution is:").n<=N. } boolean take[]=new boolean[N+1]. sol[n][w]=(option2 > option1). for(int n=N.profit=Integer. System.profit+opt[n-1][w-st[n].weight<=w) option2=st[n].i<=N.profit=0.println("\nFor object "+i).println("Enter the maximum weight sack can take:").println(n+" \t "+st[n].weight.out.readLine()). prof+=st[n]. System.out. System.st[0]. for(int n=1. st[i]. int prof=0.io.profit.i++) { st[i]=new objects().parseInt(in.n<=N.println("Item \t weight \t profit"). public static void main(String args[])throws IOException { DataInputStream in=new DataInputStream(System.parseInt(in.out.parseInt(in.weight+" \t\t "+st[n].out.print("Enter profit: ").w++) { int option1=opt[n-1][w].PROGRAM 0/1 KNAPSACK USING DYNAMIC PROGRAMMING import java. System.w=W.n>0.out. option2). System. }} 57 . System. System.out.out. st[0]=new objects(). } public class knapsack { static int N. if(st[n].weight].n--) if(sol[n][w]) { take[n]=true. N=Integer.profit).readLine()). static objects st[]. opt[n][w]=Math.out. st[i].out. } int [][] opt=new int[N+1][W+1].print("Enter Weight: "). System. for(int i=1. boolean [][] sol= new boolean[N+1][W+1].n++) if(take[n]) System.n++) for(int w=1.*.weight=Integer.parseInt(in. w=w-st[n]. class objects { int weight.weight=st[0]. int profit.readLine()).in).W.println("\n Total profit:"+prof). for(int n=1.max(option1.readLine()). } else take[n]=false. st=new objects[N+1].println("Enter the number of objects:").w<=W.

58 .OUTPUT: -----Enter the number of objects: 3 Enter the maximum weight sack can take: 10 For object 1 Enter profit: 20 Enter Weight: 6 For object 2 Enter profit: 10 Enter Weight: 2 For object 3 Enter profit: 19 Enter Weight: 3 The optimal solution is: Item weight profit 1 6 20 3 3 19 Total profit:39 RESULT Thus the program for 0/1 knapsack using Java has been implemented.

Ex No: 10 GRAPH COLORING USING BACKTRACKING Date: AIM To write the Java program for the implementation of graph coloring ALGORITHM Step 1: Step 2: Step 3: Step 4: Step 5: Step 6: Step 7: Step 8: Step 9: Start the program and define the function Create a class coloring Get the number of vertices in the graph Enter one if there is an edge in the graph And enter zero if there is no edge in the graph. Get the adjacency matrix of the given values Perform all possible combinations that are given Display all the combination End of the program 59 .

if(x[k]==0) break.println("between "+i+" and "+j). for(int i=1. n. } void nextvalue(int k) { 60 . System.println("All possible combinations for m = "+i+" are "). for(int i=1. a[i][j]=Integer.i<=n.print(x[i]+"\t"). class gcoloring { int a[][]=new int[10][10].j++) { System. for(int i=2.out.readLine()).PROGRAM GRAPH COLORING USING BACKTRACKING import java. int m.out. } for(int i=1.i<=n.out.i<n. System.*. n=Integer. } } catch(Exception e){} System.print(a[i][j]+"\t"). }while(true).i<=n. int x[]=new int[10].out.i++) { for(int j=1.i++) x[i]=0.out.i++) System.println(). } } void mcoloring(int k) { do { nextvalue(k).out.println("Enter 1 if there is an edge Otherwise 0").out.io.out. void read() { DataInputStream in=new DataInputStream(System. System.i++) for(int j=1.i++) { m=i.readLine()).j<=n.i<=n.println(). } else mcoloring(k+1).out.println("Enter number of vertices in the graph"). if(k==n) { for(int i=1.j++) System.println("Given adjacency matrix is "). System.parseInt(in. try { System.j<=n.parseInt(in. mcoloring(1).in).

}while(true). if(x[k]==0) return. } } 61 .read().j<=n.j++) { if((a[k][j]==1) && (x[k]==x[j])) break. g. } class Graphcoloring } { public static void main(String args[ ])throws IOException { gcoloring g=new gcoloring(). } if(j==n+1) return. for(j=1.int j. do { x[k]=(x[k]+1)%(m+1).

OUTPUT: ------Enter number of vertices in the graph 4 Enter 1 if there is an edge Otherwise 0 between 1 and 1 0 between 1 and 2 1 between 1 and 3 0 between 1 and 4 1 between 2 and 1 1 between 2 and 2 0 between 2 and 3 1 between 2 and 4 0 between 3 and 1 0 between 3 and 2 1 between 3 and 3 0 between 3 and 4 1 between 4 and 1 1 between 4 and 2 0 between 4 and 3 1 between 4 and 4 0 Given adjacency matrix is 0 1 0 1 1 0 1 0 0 1 0 1 1 0 1 0 All possible combinations for m = 2 are 1 2 1 2 2 1 2 1 All possible combinations for m = 3 are 1 2 1 2 1 2 1 3 1 2 3 2 1 3 1 2 1 3 1 3 1 3 2 3 2 1 2 1 2 1 2 3 2 1 3 1 2 3 1 3 2 3 2 1 62 .

63 .2 3 3 3 3 3 3 3 1 1 1 2 2 2 2 2 3 3 1 3 3 3 1 1 2 2 1 2 RESULT Thus the program for graph coloring using Java has been implemented.

Sign up to vote on this title
UsefulNot useful