M.E DS LabManual.docx

  • View
    4

  • Download
    0

Embed Size (px)

Transcript

Ex.No:1IMPLEMENTATION OF MINHEAPDATE:

AIM To write a java program to implement MinHeap

ALGORITHM

STEP1- Start the program.

STEP2- Create an empty heap and enter the size of the heap

STEP3- Insertion of a new element into the heap

STEP4- Add the element to the bottom level of the heap.

STEP5- Compare the added element with its parent; if they are in the correct order, Stop the process.

STEP6- If not, swap the element with its parent and return to the previous step.

STEP7- Deletion of the smallest element from the heap

STEP8- Replace the root of the heap with the last element on the last level.

STEP9- Compare the new root with its children; if they are in the correct order, stop.

STEP10- If not, swap the element with one of its children and return to the previous step. (Swap with its smaller child in a min-heap and its larger child in a max- heap.)

PROGRAM

import java.io.*;public class MinHeap{private int[] Heap;private int maxsize;private int size;public MinHeap(int max){maxsize = max;Heap = new int[maxsize];size = 0 ;Heap[0] = Integer.MIN_VALUE;}private int leftchild(int pos){return 2*pos;}private int rightchild(int pos){return 2*pos + 1;}private int parent(int pos){return pos / 2;}private boolean isleaf(int pos){return ((pos > size/2) && (pos n)return (pos+offset)/2;return pos + offset;}private int minPartner(int pos){int offset = 1;int i = pos;while (i > 3){i /= 2;offset *= 2;}return pos - offset;}private void minInsert(int at, int key){

for (int parent; (parent = at / 2) != 1 && key < deap[parent]; deap[at] = deap[parent], at = parent) ;deap[at] = key;}private void maxInsert(int at, int key){for (int parent; (parent = at / 2) != 1 && key > deap[parent]; deap[at] = deap[parent], at = parent) ;deap[at] = key;}public int deleteMax(){int i, j;int key;if (n >= 3){ // if more than 2 elementskey = deap[3];}else{n--;return deap[2];}int x = deap[n--];// while i has child, move larger to ifor (i = 3; 2*i u.left.npl){node dummy=u.right;

u.right=u.left;u.left=dummy;}if(u.right==null)u.npl=0;elseu.npl=min(u.left.npl,u.right.npl)+1;return u;}static int min(int x,int y){if(xtemp.data){prev.lchild=temp;prev.lchild.parent=prev;prev.bf+=1;}else{prev.rchild=temp;prev.rchild.parent=prev;prev.bf-=1;}while((prev!=null) && prev.bf!=2 && prev.bf!=-2){p=prev;

prev=prev.parent;if(prev!=null){if(p==p.lchild)prev.bf+=1;elseprev.bf-=1;}}System.out.println("Tree Before Rotation");dispre(root);if(prev!=null && prev.bf==2){switch(p.bf){case 1:System.out.println("LL Rotation");ll_rotate();dispre(root);break;case -1:System.out.println("LR Rotate");lr_rotate();dispre(root);break;}}if(prev!=null && prev.bf==-2){switch(p.bf){case 1:System.out.println("RL Rotation");rl_rotate();dispre(root);break;case -1:System.out.println("RR Rotate");rr_rotate();dispre(root);break;}}}}public void ll_rotate(){p.parent=prev.parent;if(prev.parent!=null)prev.parent.lchild=p;p.rchild=prev;prev.lchild=null;if(prev==root)

root=p;prev.bf=0;p.bf=0;}public void rr_rotate(){p.parent=prev.parent;if(prev.parent!=null)prev.parent.rchild=p;p.lchild=prev;prev.rchild=null;if(prev==root)root=p;prev.bf=0;p.bf=0;}public void lr_rotate(){avlnode t=p.rchild;if(t.lchild!=null)p.rchild=t.lchild;elsep.rchild=null;if(t.rchild!=null)prev.lchild=t.rchild;elseprev.lchild=null;if(prev.parent!=null){t.parent=prev.parent;prev.parent.lchild=t;}else{t.parent=null;root=t;}t.rchild=prev;t.lchild=p;p.bf=0;prev.bf=0;t.bf=0;}public void rl_rotate(){avlnode t=p.lchild;if(t.rchild!=null)p.lchild=t.rchild;elsep.lchild=null;

if(t.lchild!=null)prev.rchild=t.lchild;elseprev.rchild=null;if(prev.parent!=null){t.parent=prev.parent;prev.parent.rchild=t;}else{t.parent=null;root=t;}t.lchild=prev;t.rchild=p;p.bf=0;prev.bf=0;t.bf=0;}public void dispre(avlnode cnode){if(cnode!=null){dispre(cnode.lchild);System.out.println(cnode.data+"bf="+cnode.bf);dispre(cnode.rchild);}}}public class avltree{public static void main(String args[])throws IOException{int cho,ele;BufferedReader br=new BufferedReader(new InputStreamReader(System.in));avl tree=new avl();System.out.println("1.Insert,2.Exit");while(true){System.out.println("Enter the choice");cho=Integer.parseInt(br.readLine());switch(cho){case 1:System.out.println("Enter data");ele=Integer.parseInt(br.readLine());tree.insert(ele);break;case 2:System.exit(0);}}}}

OUTPUT

RESULT Thus the java program to implement avl tree has been executed and output is verified successfully.

Ex.No:5IMPLEMENTATION OF B-TREEDATE:

AIM To write a java program to implement the B-Tree.

ALGORITHM

STEP1- Start the program by defining function.

STEP2- Declare the class btree

STEP3- To insert, check if root is empty, if it is empty insert the element as root.

STEP4- If it is greater insert it into right sub tree. Otherwise, insert it into left sub tree

STEP5- Use the function split, to split the nodes

STEP6- Call the function display to display data1,data2, and parent

STEP7- End of the program

PROGRAM

import java.io.*;class bnode{int data1,data2;bnode lptr,mptr,rptr,parent;public void bnode(){this.data1=this.data2=0;this.lptr=this.mptr=this.rptr=this.parent=null;}}class btree{bnode root=null;bnode p,p1;bnode prev;void insert(int ele){bnode temp=new bnode();temp.data1=ele;if(root==null){root=temp;}else{p1=root;while(p1!=null){prev=p1;if(temp.data1p1.data1) &&(temp.data1> 1; } } else { if(key==temp.data) { return temp; } else break; } } return null; } public void insert() { int key=0; try {

System.out.println("Enter the element:"); DataInputStream din=new DataInputStream(System.in); key=Integer.parseInt(din.readLine()); } catch(Exception e){} if(root==null) { root=new node();root.data=key; root.tag=0; root.level=1; root.par=null; root.LC=null; root.RC=null; }else{node temp=find(key); if(temp==null) {temp=cptr; if(temp.tag==0) {node n1=new node(); node n2=new node(); temp.tag=1; n1.tag=0;n2.tag=0; int k1=temp.data;temp.data=0; int k2=key; int kk1; n1.data=k1; n2.data=k2; int lv=1; while ( (k1 & 1 ) ==(k2 & 1 )) { kk1=k1; k1=k1 >> 1; k2=k2 >> 1; if(lv>=temp.level) { node n3=new node(); n3.tag=1; if ( (kk1 & 1)==0) { temp.LC=n3; temp.RC=null; n3.level=temp.level+1; } else { temp.RC=n3;

temp.LC=null; n3.level=temp.level+1; } n3.par=temp; temp=n3; lv++; } else lv++; } if( (k1 & 1)==0) { temp.LC=n1; temp.RC=n2; n1.level=n2.level=temp.level+1; } else { temp.LC=n2; temp.RC=n1; n1.level=n2.level=temp.level+1; n1.par=temp; } n2.par=temp; }else{node n1=new node(); n1.tag=0; n1.data=key; if(temp.LC==null) temp.LC=n1; else temp.RC=n1; n1.level=temp.level+1; n1.par=temp; }}elseSystem.out.println("Element already exists"); } }public void display() { if(root==null) System.out.println("EMPTY"); else { System.out.println("\nIn Order");

dispin(root); } } public void dispin(node currentnode) { if(currentnode!=null) { dispin(currentnode.LC); System.out.println(currentnode.data+" "+"LEVEL"+currentnode.level+" "+"TAG-"+currentnode.tag); dispin(currentnode.RC); } } };class TrieImp { public static void main(String args[ ])throws IOException { int ch=0,cont=0; trie t = new trie(); do { System.out.println("TRIES 1. Insert "); DataInputStream din = new DataInputStream(System.in); try{ ch=Integer.parseInt(din.readLine()); } catch(Exception e){} if(ch==1) { t.insert(); t.display(); } else { System.out.println("Enter the correct choice"); } System.out.println("press 1 to continue:"); try { cont=Integer.parseInt(din.readLine()); } catch(Exception e){} }while(cont==1); }}

OUTPUT

RESULT Thus the java program to implement trie has been executed and output is verified successfully.

Ex.No: 7IMPLEMENTATION OF QUICK SORTDATE:

AIM To write a java program to implement Quick sort.

ALGORITHM

STEP1- Start the Program

STEP2- Choose an array value (first) to use as the pivot

STEP3- Starting from the left end, find the first element that is greater than or equal to the pivot

STEP4- Searching backward from the right end, find the first element that is less than the pivot

STEP5- Interchange (swap) these two elements

STEP6- Repeat the steps until i