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

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

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

ALGORITHM Step 1: Step 2: Step 3: Step 4: Step 5: Start the program by creating Deap Structure. Stop of the program.Ex No: 2 DEAPS Date: AIM To implement program for deaps structure with insert and delete operations using Java. Perform insert and delete functions. Step 6: Step 7: 6 . After the insert and delete operation Deap elements are displayed. The insert() is done with 2 methods namely maxinsert() and mininsert(). 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.

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

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

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

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

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

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

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

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

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

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

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

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. Delete 1 Enter the element: 19 . 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.}while(cont==1). Delete 1 Enter the element: 50 In Order 50 SHORTEST 1 press 1 to continue: 1 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. Insert 2. 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. } OUTPUT: } ------LEFTIST TREE 1. 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. Insert 2. Insert 2. 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 . 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.

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

Step 6: Step 7: 22 . 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. If it is not balanced.Ex no: 4 AVL TREE Date: AIM To implement the AVL tree with insert & delete operations using Java. Check the tree if it is balanced or not. Insert the elements to the AVL tree. The balance factor is one of 0. Stop the program. ALGORITHM Step 1: Step 2: Step 3: Step 4: Step 5: Start the program by defining the functions. 1 and -1.

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

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

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

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

OUTPUT: ------AVLTREES 1. Insert 1 Enter the element: 6 RR imbalance 27 . Insert 1 Enter the element: 3 In Order 3 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: 2 In Order 2 BF 0 3 BF 1 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: 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: 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.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: 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: 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: 14 28 .

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. 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.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. Insert 1 Enter the element: 11 29 .

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: 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. 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 .

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

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

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

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

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

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

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 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.OUTPUT: -------B-Trees Enter the element 52 data1::52 data2::0 Address::bnode@923e30 parent::null To continue...........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..press 1 38 .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 1 Enter the element 45 data1::45 data2::52 Address::bnode@923e30 parent::null To continue..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.

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

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

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

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

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

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

1 0 LEVEL. Insert 1 Enter the element: 1232 In Order 1232 LEVEL.2 TAG-0 press 1 to continue: 1 TRIES 1.2 TAG-0 0 LEVEL. Insert 1 Enter the element: 4451 In Order 1232 LEVEL.3 1015 In Order LEVEL.2 4451 LEVEL.3 press 1 to continue: 1 TRIES 1.1 4451 LEVEL.2 4451 LEVEL.2 TAG-0 0 LEVEL. Insert 1 Enter the element: 1243 In Order 1232 LEVEL.OUTPUT: ------TRIES 1.3 1232 press 1 TAG-0 to continue: 1 0 LEVEL.2 TRIES 1.5 TAG-0 0 LEVEL.5 TAG-0 0 LEVEL.2 TAG-0 0 LEVEL.4 1243 LEVEL.5 TAG-0 0 LEVEL.4 1243 LEVEL.1 0 LEVEL.1 TAG-0 press 1 to continue: 1 TRIES 1.4 TAG-0 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 . Insert 1 Enter the element: 1015 In Order 1232 LEVEL.5 TAG-0 0 LEVEL.

3 TAG-0 0 LEVEL. Insert 1 Enter the element: 1941 In Order 1232 LEVEL.3 TAG-0 0 LEVEL.5 TAG-0 0 LEVEL.3 TAG-1 1015 LEVEL.4 TAG-1 1243 LEVEL.3 TAG-0 0 LEVEL.3 TAG-0 0 LEVEL.1 TAG-1 1941 LEVEL.4 TAG-1 1243 LEVEL. Insert 1 Enter the element: 1055 In Order 1232 LEVEL.4 TAG-1 1243 LEVEL.1 TAG-1 0 LEVEL.5 TAG-0 press 1 to continue: 1 TRIES 1.5 TAG-0 0 LEVEL. Insert 1 Enter the element: 1243 Element already exists 46 In Order 1232 LEVEL.3 TAG-1 1015 LEVEL.3 TAG-0 0 LEVEL.3 TAG-0 0 LEVEL.4 TAG-1 1055 LEVEL.2 TAG-1 4451 LEVEL.3 TAG-0 0 LEVEL.4 TAG-0 press 1 to continue: 1 TRIES 1.5 TAG-0 0 LEVEL.2 TAG-1 1942 LEVEL.5 TAG-0 0 LEVEL.2 TAG-1 1942 LEVEL.3 .1 TAG-1 1941 LEVEL.5 TAG-0 0 LEVEL.2 TAG-1 4451 LEVEL.3 TAG-1 1015 LEVEL.4 TAG-0 press 1 to continue: 1 TRIES 1.1942 LEVEL.2 TAG-1 4451 LEVEL.5 TAG-0 0 LEVEL.5 TAG-0 0 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 .0 LEVEL.5 TAG-0 0 LEVEL.3 1015 LEVEL.4 1243 LEVEL.5 TAG-0 0 LEVEL.4 1055 LEVEL.5 TAG-0 0 LEVEL.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 .

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

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

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.

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. print the profit and weight Initializing is set to false. Display and print the item weight and profit Display the total cost End of the program. 56 . For each objects. Read the number of objects that are given. ALGORITHM Step 1: Step 2: Step 3: Step 4: Step 5: Step 6: Step 7: Step 8: Start the program and define the function. Initialize the weight and profit.

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

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.

Get the adjacency matrix of the given values Perform all possible combinations that are given Display all the combination End of the program 59 .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.

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

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

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. 63 .

Sign up to vote on this title
UsefulNot useful

Master Your Semester with Scribd & The New York Times

Special offer: Get 4 months of Scribd and The New York Times for just $1.87 per week!

Master Your Semester with a Special Offer from Scribd & The New York Times