New Ds Lab Manual

Embed Size (px)

Citation preview

  • 7/30/2019 New Ds Lab Manual

    1/87

    Ex no: 1Date: MIN HEAP

    AIM

    To implement the min heap structure with insert and delete minimum operationusing 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 heapproperty.

    The deletemin() is used to delete the minimum element which is usually arootnode.

    The two operations are performed satisfying heapness and completenessproperty.

    End of the program.

    Step 4:

    Step 5:

    Step 6:

  • 7/30/2019 New Ds Lab Manual

    2/87

    import java.io.*;

    class heapalg{

    int maxsize=100,size;

    int[] h=newint[maxsize];

    publicint leftchild(int i)

    {

    return 2*i;

    }

    publicint rightchild(int i)

    {

    return 2*i + 1;}

    publicint parent(int i)

    {return i/2;

    }

    publicboolean isleaf(int i)

    {

    return ((isize/2));

    }

    publicvoid swap(int i,int j){

    int t;

    t=h[i];h[i]=h[j];h[j]=t;

    }

    publicvoid display()

    {

    System.out.println("The heap elements are:"+"\n");

    for(int i=1;imaxsize)

    System.out.println("Heapfull");else

    {

    try

    {

    System.out.println("Enter the element:");DataInputStream din=newDataInputStream(System.in);

    h[size]=Integer parseInt(din readLine());

    PROGRAM:

  • 7/30/2019 New Ds Lab Manual

    3/87

    insertelt(size);

    }

    }

    publicvoid insertelt(int i)

    {while ((h[parent(i)]>h[i]))

    {

    int par=parent(i);

    swap(par,i);

    i=par;

    }

    }

    publicvoid delet()

    {

    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);

    }

    }

    publicvoid percolate(int i){

    while(!isleaf(i))

    {

    int small=leftchild(i);

    if( (small h[small+1])

    small+=1;

    if(h[small] < h[i])

    swap(small,i);

    i=small;

    }}

    };

    class minheap

    {

    publicstaticvoid main(String args[]) throwsIOException

    {

    int ch=0,cont=0;

    heapalg h1=new heapalg();

    do

    {

    System.out.println("MIN HEAP 1.Insert 2.Delete Min");

  • 7/30/2019 New Ds Lab Manual

    4/87

    {

    ch=Integer.parseInt(din.readLine());

    }

    catch(Exception e){}

    if(ch==1)

    {h1.insert();

    h1.display();

    }

    elseif(ch==2)

    {

    h1.delet();

    h1.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);

    }}

  • 7/30/2019 New Ds Lab Manual

    5/87

    OUTPUT

    -------MIN HEAP 1.Insert 2.Delete Min1Enter the element:42The heap elements are:42

    press 1 to continue:1MIN HEAP 1.Insert 2.Delete Min

    1Enter the element:3The heap elements are:342

    press 1 to continue:1MIN HEAP 1.Insert 2.Delete Min1Enter the element:86The heap elements are:

    34286

    press 1 to continue:1MIN HEAP 1.Insert 2.Delete Min1Enter the element:2The heap elements are:2386

    42press 1 to continue:1MIN HEAP 1.Insert 2.Delete Min2The deleted min elt:2The heap elements are:34286

    press 1 to continue:12

  • 7/30/2019 New Ds Lab Manual

    6/87

    RESULT:

    Thus the program f or Minheap using Java has been implemented and executed

    Successfully.

  • 7/30/2019 New Ds Lab Manual

    7/87

    Ex No: 2Date:

    Step 6:

    Step 7:

  • 7/30/2019 New Ds Lab Manual

    8/87

    PROGRAM:

    import java.io.*;

    class deapsalg

    {

    int maxsize=100,size;

    int[] h=newint[maxsize+1];

    publicint leftchild(int i)

    {

    return 2*i;

    }

    publicint rightchild(int i)

    {

    return 2*i + 1;

    }

    publicint parent(int i)

    {

    return i/2;

    }

    publicboolean isleaf(int i)

    {

    DEAPS

    AIM

    To implement program for deaps structure with insert and delete operations

    using java.

    ALGORITHM

    Step 1:

    Step 2:

    Step 3:

    Step 4:

    Step 5:

    Start the program by creating Deap Structure.

    Perform insert and delete functions.

    The insert() is done with 2 methods namely maxinsert() andmininsert().

    The delete() is done with 2 methods namely deletemax() anddeletemin()

    The leftChild and rightChild are compared and the appropriate elementis placed in the root node.

    After the insert and delete operation Deap elements are displayed.

    Stop of the program.

  • 7/30/2019 New Ds Lab Manual

    9/87

    publicvoid swap(int i,int j)

    {

    int t;

    t=h[i];h[i]=h[j];h[j]=t;

    }

    publicvoid display()

    {

    System.out.println("The deaps elements are:");for(int i=1;isize+1)partner/=2;

    return partner;

    }

    publicvoid MinInsert(int i)

    {

    while (parent(i)!=1 && (h[parent(i)]>h[i]))

    {

    int par=parent(i);

    swap(par,i);

    i=par;

    }

  • 7/30/2019 New Ds Lab Manual

    10/87

    {

    while (parent(i) !=1 && (h[parent(i)]maxsize)

    System.out.println("Deap full");

    else

    {

    try

    {System.out.println("Enter the element:");

    DataInputStream din=newDataInputStream(System.in);

    newelt=Integer.parseInt(din.readLine());

    }

    catch(Exception e){}

    if(size==1)

    {h[2]=newelt;

    return;

    }

    int p=size+1;

    h[p]=newelt;

    switch(MaxHeap(p))

    {

    case 1:

    int partner=MinPartner(p);

    if(h[partner]>h[p]){

    swap(p,partner);

    MinInsert(partner);

    }

    else

    MaxInsert(p);

    break;

    case 0:

    partner=MaxPartner(p);

    if(h[partner]

  • 7/30/2019 New Ds Lab Manual

    11/87

    }

    else

    MinInsert(p);

    break;

    default:

    System.out.println("ERROR");

    }

    }}

    publicvoid deletemin()

    {

    if(size==0)

    System.out.println("Deap empty");

    else

    {

    System.out.println("The deleted min elt:"+ h[2]);

    int i;

    int p=size+1;int t=h[p];

    size--;

    int small;

    for( i=2;2*i

  • 7/30/2019 New Ds Lab Manual

    12/87

    partner=MaxPartner(i);

    if(h[partner]

  • 7/30/2019 New Ds Lab Manual

    13/87

    MaxInsert(i);

    break;

    case 0:

    partner=MaxPartner(i);

    if(h[partner]

  • 7/30/2019 New Ds Lab Manual

    14/87

    {

    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);

    }

    }

  • 7/30/2019 New Ds Lab Manual

    15/87

    OUTPUT:

    DEAPs 1.Insert 2.Delete Min 3.Delete Max1Enter the element:20

    The deaps elements are:020

    press 1 to continue: 1DEAPs 1.Insert 2.Delete Min 3.Delete Max1Enter the element:5The deaps elements are:0520

    press 1 to continue: 1DEAPs 1.Insert 2.Delete Min 3.Delete Max1Enter the element: 3The deaps elements are:03205Press 1 to continue: 1DEAPs 1.Insert 2.Delete Min 3.Delete Max1Enter the element: 7The deaps elements are:032057

    press 1 to continue:1DEAPs 1.Insert 2.Delete Min 3.Delete Max1Enter the element: 11

  • 7/30/2019 New Ds Lab Manual

    16/87

    DEAPs 1.Insert 2.Delete Min 3.Delete Max1

    Enter the element:55

    The deaps elements are:0355571120

    press 1 to continue:1DEAPs 1.Insert 2.Delete Min 3.Delete Max1

    Enter the element:77The deaps elements are:0377575520

    The deaps elements are:03205711

    press 1 to continue:1

  • 7/30/2019 New Ds Lab Manual

    17/87

    1DEAPs 1.Insert 2.Delete Min 3.Delete Max1Enter the element:88The deaps elements are:03885

    777201155

    press 1 to continue:1DEAPs 1.Insert 2.Delete Min 3.Delete Max1

    Enter the element:17The deaps elements are:

    0388

    press 1 to continue:1

    DEAPs 1.Insert 2.Delete Min 3.Delete Max2

    The deleted min elt:1

    The deaps elements are:

    03100578877115517

    20press 1 to continue:

  • 7/30/2019 New Ds Lab Manual

    18/87

    5

    7

    77

    2011

    55press 1 to continue:1

    DEAPs 1.Insert 2.Delete Min 3.Delete Max1Enter the element:1

    The deaps elements are:0

    1885377201155177

    press 1 to continue:1DEAPs 1.Insert 2.Delete Min 3.Delete Max

    1

  • 7/30/2019 New Ds Lab Manual

    19/87

    Enter the element:100The deaps elements are:01100538820115517777

  • 7/30/2019 New Ds Lab Manual

    20/87

    RESULT

    Thus the program for Deaps using Java has been implemented and

    executed successfully.

  • 7/30/2019 New Ds Lab Manual

    21/87

  • 7/30/2019 New Ds Lab Manual

    22/87

    PROGRAM

    import java.io.*;

    class node

    {

    publicint data;

    public node LC,RC;publicint shortest;

    }

    class minleftist

    {

    node root = null;

    publicvoid insert()

    {

    int newelt=0;

    try

    {System.out.println("Enter the element:");

    DataInputStream din=new DataInputStream(System.in);

    Ex no: 3Date:

    LEFTIST HEAP

    AIM

    To implement the leftist heap with insert and deletemin operation using

    Java.

    ALGORITHM

    Step 1:

    Step 2:

    Step 3:

    Step 4:

    Step 5:

    Step 6:

    Step 7:

    Start the program by defining function.

    We know heap as the root node with minimum element.

    The insert and delete operations are performed with the help of combining2 trees.

    The insert operation is performed by combining the two leftist trees.

    The deletemin() is used to delete the minimum element in the heap.

    After the insert and delete operations leftist heap elements are displayed.

    Stop the program.

  • 7/30/2019 New Ds Lab Manual

    23/87

    catch(Exception e){}

    node temp = new node();

    temp.data=newelt;

    temp.LC=temp.RC=null;

    temp.shortest=1;

    if(root==null)

    root=temp;

    elseroot=meld(root,temp);

    }

    public node meld(node a, node b)

    {

    if(a.data > b.data)

    {

    node t;

    t=a;

    a=b;

    b=t;}

    if(a.RC==null)

    a.RC=b;

    else

    a.RC=meld(a.RC,b);

    if((a.LC==null) || (a.LC.shortest < a.RC.shortest))

    {

    node t=new node();

    t=a.LC;

    a.LC=a.RC;a.RC=t;

    }

    if(a.RC==null)

    a.shortest=1;

    else

    a.shortest=a.RC.shortest+1;

    return a;

    }publicvoid remove()

    {

    System.out.println("Deleted element is "+root.data+"\n");

    root=meld(root.LC,root.RC);

    }

    publicvoid display()

    {

    if(root==null)

    System.out.println("EMPTY");

    else

  • 7/30/2019 New Ds Lab Manual

    24/87

    dispin(root);

    }

    }

    publicvoid dispin(node currentnode)

    {

    if(currentnode!=null)

    {

    dispin(currentnode.LC);System.out.println(currentnode.data+" "+"SHORTEST "+currentnode.shortest);

    dispin(currentnode.RC);

    }

    }

    };

    class LeftistTree

    {

    publicstaticvoid main(String args[ ])throwsIOException

    {

    int ch=0,cont=0;minleftist m = new minleftist();

    do

    {

    System.out.println("LEFTIST TREE 1. Insert 2. Delete");

    DataInputStream din = newDataInputStream(System.in);

    try

    {

    ch=Integer.parseInt(din.readLine());

    }

    catch(Exception e){}

    if(ch==1)

    {

    m.insert();

    m.display();

    }

    elseif(ch==2)

    {

    m.remove();m.display();

    }

    else

    {

    System.out.println("Enter the correct choice");

    }

    System.out.println("press 1 to continue:");

    try

    {

    cont=Integer.parseInt(din.readLine());

    }

  • 7/30/2019 New Ds Lab Manual

    25/87

    }

    }

  • 7/30/2019 New Ds Lab Manual

    26/87

    OUTPUT:

    LEFTIST TREE 1. Insert2. Delete1

    Enter the element:50In Order50 SHORTEST 1

    press 1 to continue:1LEFTIST TREE 1. Insert2. Delete1Enter the element:8In Order50 SHORTEST 18 SHORTEST 1

    press 1 to continue:1LEFTIST TREE 1. Insert2. Delete1Enter the element:13In Order50 SHORTEST 18 SHORTEST 213 SHORTEST 1

    press 1 to continue:1LEFTIST TREE 1. Insert2. Delete1Enter the element:11In Order50 SHORTEST 18 SHORTEST 213 SHORTEST 111 SHORTEST 1Press 1 to continue:

  • 7/30/2019 New Ds Lab Manual

    27/87

    1LEFTIST TREE 1. Insert 2. Delete1Enter the element:20In Order13 SHORTEST 111 SHORTEST 2

    20 SHORTEST 18 SHORTEST 250 SHORTEST 1

    press 1 to continue:1LEFTIST TREE 1. Insert 2. Delete1

    Enter the element:18In Order13 SHORTEST 111 SHORTEST 220 SHORTEST 18 SHORTEST 250 SHORTEST 118 SHORTEST 1

    press 1 to continue:1LEFTIST TREE 1. Insert 2. Delete1Enter the element:2In Order

  • 7/30/2019 New Ds Lab Manual

    28/87

    8 SHORTEST 250 SHORTEST 118 SHORTEST 12 SHORTEST 1

    press 1 to continue:1LEFTIST TREE 1. Insert 2. Delete1Enter the element:

    7In Order13 SHORTEST 111 SHORTEST 220 SHORTEST 18 SHORTEST 250 SHORTEST 118 SHORTEST 12 SHORTEST 27 SHORTEST 1

    press 1 to continue:1LEFTIST TREE 1. Insert 2. Delete

    2Deleted element is 2In Order13 SHORTEST 111 SHORTEST 220 SHORTEST 18 SHORTEST 250 SHORTEST 118 SHORTEST 1

  • 7/30/2019 New Ds Lab Manual

    29/87

    RESULT:

    Thus the program for leftist heap using Java has been implemented and

    executed successfully.

  • 7/30/2019 New Ds Lab Manual

    30/87

    Left-leftLeft-right

    (i)(ii)

  • 7/30/2019 New Ds Lab Manual

    31/87

    (iii) Right-left(iv) Right-right

    Balancing

    And if the tree is balanced and then the insert() and the delete() operationsare

    performed.

    Stop the program.

    EX NO: 4Date:

    AVL TREE

    Step 1:

    Step 2:

    Step 3:

    Step 4:

    Step 5:

    AIM

    To implement the AVL tree with insert & delete operations usingJava.

    ALGORITHM

    Start the program by defining the functions.

    Insert the elements to the AVL tree.

    Check the tree if it is balanced or not.

    The balance factor is one of 0, 1 and -1.

    If it is not balanced, balance the tree using

    Step 6:

    Step 7:

  • 7/30/2019 New Ds Lab Manual

    32/87

    PROGRAM:

    import java.io.*;

    class node{

    publicint data;

    public node LC,RC;

    publicint bf;

    }

    class avltree

    {

    node root = null;

    publicboolean insert()

    {int newelt=0;

    try

    {

    System.out.println("Enter the element:");

    DataInputStream din=newDataInputStream(System.in);

    newelt=Integer.parseInt(din.readLine());

    }

    catch(Exception e){}

    if(root==null)

    {node y=new node();

    y.data=newelt;

    y.bf=0;

    y.LC=null;

    y.RC=null;

    root=y;

    returntrue;

    }

    node f,a,q,p;

    node b,c;

    int d;

  • 7/30/2019 New Ds Lab Manual

    33/87

    f=null;

    a=root;

    p=root;

    q=null;

    found=false;

    while (p!=null && found!=true)

    {

    if(p.bf!=0) {a=p;f=q;}if(neweltp.data){q=p;p=p.RC;}

    else {y=p;found=true;}

    }

    if(found==false)

    {

    y.data=newelt;

    y.bf=0;y.LC=null;

    y.RC=null;

    if(newelta.data)

    {p=a.RC;b=p;d=-1;}

    else

    {p=a.LC;b=p;d=1;}while(p!=y)

    {

    if(newelt > p.data)

    {p.bf=-1;p=p.RC;}

    else

    {p.bf=1;p=p.LC;}

    }

    unbalanced=true;

    if((a.bf==0)||((a.bf+d)==0))

    {a.bf+=d;unbalanced=false;}if(unbalanced==true)

    {

    if(d==1)

    {

    if(b.bf==1)

    {

    System.out.println("LL imbalance");

    a.LC=b.RC;

    b.RC=a;

    a.bf=0;

    b.bf=0;

  • 7/30/2019 New Ds Lab Manual

    34/87

    {

    System.out.println("LR imbalance");

    c=b.RC;

    b.RC=c.LC;

    a.LC=c.RC;

    c.LC=b;

    c.RC=a;

    switch(c.bf){

    case 1:

    a.bf=-1;

    b.bf=0;

    break;

    case -1:

    a.bf=0;

    b.bf=1;break;

    case 0:

    a.bf=0;

    b.bf=0;

    break;

    }

    c.bf=0;

    b=c;

    }

    }else

    {

    if(b.bf==-1)

    {

    System.out.println("RR imbalance");

    a.RC=b.LC;

    b.LC=a;

    a.bf=0;

    b.bf=0;

    }else

    {

    System.out.println("RL imbalance");

    c=b.LC;

    b.LC=c.RC;

    a.RC=c.LC;

    c.RC=b;

    c.LC=a;

    switch(c.bf)

    {

    case 1:

  • 7/30/2019 New Ds Lab Manual

    35/87

    break;

    case -1:

    a.bf=1;

    b.bf=0;

    break;

    case 0:

    a.bf=0;

    b.bf=0;break;

    }

    c.bf=0;

    b=c;

    }

    }

    if(f==null)

    root=b;elseif(a==f.LC)

    f.LC=b;

    elseif(a==f.RC)

    f.RC=b;

    }

    returntrue;

    }

    returnfalse;

    }

    publicvoid display(){

    if(root==null)

    System.out.println("EMPTY");

    else

    {

    System.out.println("\nIn Order");

    dispin(root);

    }

    }

    publicvoid dispin(node currentnode){

    if(currentnode!=null)

    {

    dispin(currentnode.LC);

    System.out.println(currentnode.data+" "+"BF "+currentnode.bf);

    dispin(currentnode.RC);

    }

    }

    };

    class AVLTreeImp

    {

  • 7/30/2019 New Ds Lab Manual

    36/87

    int ch=0,cont=0;

    avltree a = new avltree();

    do

    {

    System.out.println("AVLTREES 1. Insert ");

    DataInputStream din = newDataInputStream(System.in);

    try

    {ch=Integer.parseInt(din.readLine());

    }

    catch(Exception e){}

    if(ch==1)

    {

    boolean y=true;

    y=a.insert();

    a.display();if(y==false)

    System.out.println("Data already exists");

    }

    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);

    }

    }

  • 7/30/2019 New Ds Lab Manual

    37/87

    OUTPUT:

    AVLTREES 1. Insert1Enter the element:3In Order3 BF 0

    press 1 to continue:1AVLTREES 1. Insert1Enter the element:2

    In Order2 BF 03 BF 1

    press 1 to continue:1AVLTREES 1. Insert1Enter the element:1LL imbalanceIn Order1 BF 02 BF 0

    3 BF 0press 1 to continue:1AVLTREES 1. Insert1Enter the element:4In Order1 BF 02 BF -13 BF -14 BF 0

    press 1 to continue:

    1AVLTREES 1. Insert1

  • 7/30/2019 New Ds Lab Manual

    38/87

    In Order1 BF 02 BF -13 BF 04 BF 05 BF 0

    press 1 to continue:1AVLTREES 1. Insert

    1Enter the element:6RR imbalance

    In Order1 BF 02 BF 03 BF 04 BF 0

    5 BF -16 BF 0

    press 1 to continue:1AVLTREES 1. Insert1Enter the element:7RR imbalanceIn Order1 BF 02 BF 03 BF 0

    4 BF 05 BF 06 BF 07 BF 0

    press 1 to continue:1AVLTREES 1. Insert1Enter the element:16In Order1 BF 02 BF 03 BF 04 BF -15 BF 06 BF -17 BF -116 BF 0

    press 1 to continue:1AVLTREES 1. Insert1Enter the element:15RL imbalanceIn Order1 BF 02 BF 0

  • 7/30/2019 New Ds Lab Manual

    39/87

    6 BF -17 BF 015 BF 016 BF 0

    press 1 to continue:1AVLTREES 1. Insert1Enter the element:

    14

    imbalance

    In Order1 BF 02 BF 03 BF 04 BF -15 BF 06 BF 17 BF 014 BF 015 BF 016 BF 0

    press 1 to continue:

    1AVLTREES 1. Insert1Enter the element:13RR imbalance

    In Order1 BF 02 BF 03 BF 04 BF 05 BF 0

    6 BF 17 BF 013 BF 014 BF 115 BF 116 BF 0

    press 1 to continue:1AVLTREES 1. Insert1Enter the element:12LL imbalance

    In Order1 BF 02 BF 0

  • 7/30/2019 New Ds Lab Manual

    40/87

    5 BF 06 BF 17 BF 012 BF 013 BF 014 BF 015 BF 116 BF 0

    press 1 to continue:

    1AVLTREES 1. Insert1Enter the element:11

  • 7/30/2019 New Ds Lab Manual

    41/87

    LL imbalance

    In Order1 BF 02 BF 03 BF 04 BF 0

    5 BF 06 BF 17 BF 011 BF 012 BF 113 BF 014 BF 015 BF 016 BF 0

    press 1 tocontinue:1AVLTREES 1.

    Insert1Enter theelement:10LL imbalance

    In Order1 BF 02 BF 03 BF 04 BF 05 BF 0

    6 BF 17 BF 010 BF 011 BF 012 BF 013 BF 014 BF 015 BF 016 BF 0

    press 1 tocontinue:1AVLTREES 1.

    Insert1Enter theelement:8

  • 7/30/2019 New Ds Lab Manual

    42/87

    13 BF 1

    In Order1 BF 02 BF 03 BF 04 BF 05 BF 06 BF 17 BF -18 BF 010 BF 111 BF 1

  • 7/30/2019 New Ds Lab Manual

    43/87

    16 BF 0press 1 to continue:1AVLTREES 1. Insert1Enter the element:9LR imbalance

    In Order1 BF 02 BF 03 BF 04 BF 05 BF 06 BF 17 BF -18 BF 09 BF 010 BF 011 BF 112 BF 0

    13 BF 114 BF 015 BF 016 BF 0

    press 1 to continue:1AVLTREES 1. Insert1Enter the element:16In Order1 BF 02 BF 0

    3 BF 04 BF 05 BF 06 BF 17 BF -18 BF 09 BF 010 BF 011 BF 112 BF 013 BF 114 BF 015 BF 0

    16 BF 0Data already existspress 1 to continue:12

  • 7/30/2019 New Ds Lab Manual

    44/87

    EX No: 5Date:

    B-TREE

    AIM

    To implement the b-tree with insert and delete operations usingJava.

    ALGORITHM

    Step 1:

    Step 2:

    Step 3:

    Step 4:

    Start the program by defining function.

    Declare the class btree

    The insert and delete operations are performed

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

    If it is greater insert it into right sub tree.

    Otherwise, insert it into left sub tree

    Use the function split, to split the nodes

    Call the function display to displaydata1, data2, address and parent

    End of the program

    Step 5:

    Step 6:

    Step 7:

    Step 8:

    Step 9:

  • 7/30/2019 New Ds Lab Manual

    45/87

    PROGRAM:

    import java.io.*;class bnode

    {

    int data1,data2;

    bnode lptr,mptr,rptr,parent;

    publicvoid 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

  • 7/30/2019 New Ds Lab Manual

    46/87

    int t=p1.data1;

    p1.data1=temp.data1;

    p1.data2=t;

    p1.lptr=temp.lptr;

    if(temp.lptr!=null)

    temp.lptr.parent=p1;p1.mptr=temp.rptr;

    if(temp.rptr!=null)

    temp.rptr.parent=p1;

    }

    else

    {

    p1.data2=temp.data1;

    p1.mptr=temp.lptr;

    if(temp.lptr!=null)

    temp.lptr.parent=p1;p1.rptr=temp.rptr;

    if(temp.rptr!=null)

    temp.rptr.parent=p1;

    }

    temp.parent=p1.parent;

    break;

    }

    elseif((p1.data1!=0) && (p1.data2!=0))

    {

    p1=split(temp,p1);temp=p1;

    p1=p1.parent;

    }

    }

    }

    display(root);

    }

    bnode split(bnode t,bnode p)

    {

    bnode n1=null;bnode n2=null;

    if(t.data1

  • 7/30/2019 New Ds Lab Manual

    47/87

    p.rptr.lptr=n1;

    if(n1!=null)

    p.rptr.lptr.parent=p.rptr;

    p.rptr.rptr=n2;

    if(n2!=null)

    p.rptr.rptr.parent=p.rptr;p.rptr.parent=p;

    p.data2=0;

    }

    elseif((t.data1>p.data1) && (t.data1

  • 7/30/2019 New Ds Lab Manual

    48/87

    p.lptr.lptr.parent=p.lptr;

    p.lptr.rptr=n2;

    if(n2!=null)

    p.lptr.rptr.parent=p.lptr;

    p.data1=p.data2;

    p.data2=0;p.rptr=new bnode();

    p.rptr=t;

    p.rptr.parent=p;

    }

    return p;

    }

    void display(bnode temp)

    {

    if(temp!=null)

    {display(temp.lptr);

    display(temp.mptr);

    display(temp.rptr);

    System.out.println("data1::"+temp.data1+" data2::"+temp.

    data2+" Address::"+temp+" parent::"+temp.parent);

    }

    }

    };

    class btrees

    {publicstaticvoid main(String[] args)throwsIOException

    {

    System.out.println("B-Trees");

    DataInputStream in=newDataInputStream(System.in);

    btree bt=new btree();

    int x,ch;

    do

    {

    System.out.println("Enter the element");

    x=Integer.parseInt(in.readLine());bt.insert(x);

    System.out.println("To continue...press 1");

    ch=Integer.parseInt(in.readLine());

    }while(ch==1);

    }

    }

  • 7/30/2019 New Ds Lab Manual

    49/87

    OUTPUT:

    B-TreesEnter the element52data1::52 data2::0 Address::bnode@923e30 parent::nullTo continue...press 11Enter the element45data1::45 data2::52 Address::bnode@923e30 parent::null

    To continue...press 11Enter the element89data1::45 data2::0 Address::bnode@130c19b parent::bnode@923e30data1::89 data2::0 Address::bnode@1f6a7b9 parent::bnode@923e30data1::52 data2::0 Address::bnode@923e30 parent::nullTo continue...press 11Enter the element12data1::12 data2::45 Address::bnode@130c19b parent::bnode@923e30data1::89 data2::0 Address::bnode@1f6a7b9 parent::bnode@923e30

    data1::52 data2::0 Address::bnode@923e30 parent::nullTo continue...press 11Enter the element56data1::12 data2::45 Address::bnode@130c19b parent::bnode@923e30data1::56 data2::89 Address::bnode@1f6a7b9 parent::bnode@923e30data1::52 data2::0 Address::bnode@923e30 parent::nullTo continue...press 11Enter the element1data1::1 data2::0 Address::bnode@7d772e parent::bnode@923e30

    data1::45 data2::0 Address::bnode@11b86e7 parent::bnode@923e30data1::56 data2::89 Address::bnode@1f6a7b9 parent::bnode@923e30data1::12 data2::52 Address::bnode@923e30 parent::nullTo continue...press 11Enter the element32data1::1 data2::0 Address::bnode@7d772e parent::bnode@923e30data1::32 data2::45 Address::bnode@11b86e7 parent::bnode@923e30data1::56 data2::89 Address::bnode@1f6a7b9 parent::bnode@923e30data1::12 data2::52 Address::bnode@923e30 parent::nullTo continue...press 11

    Enter the element25data1::1 data2::0 Address::bnode@7d772e parent::bnode@35ce36d 1 25 d 2 0 Add b d @757 f b d @35 36

  • 7/30/2019 New Ds Lab Manual

    50/87

    data1::56 data2::89 Address::bnode@1f6a7b9 parent::bnode@9cab16data1::52 data2::0 Address::bnode@9cab16 parent::bnode@923e30data1::32 data2::0 Address::bnode@923e30 parent::nullTo continue...press 1

    Enter the element60data1::1 data2::0 Address::bnode@7d772e parent::bnode@35ce36data1::25 data2::0 Address::bnode@757aef parent::bnode@35ce36data1::12 data2::0 Address::bnode@35ce36 parent::bnode@923e30data1::45 data2::0 Address::bnode@d9f9c3 parent::bnode@9cab16data1::56 data2::0 Address::bnode@1a46e30 parent::bnode@9cab16data1::89 data2::0 Address::bnode@3e25a5 parent::bnode@9cab16data1::52 data2::60 Address::bnode@9cab16 parent::bnode@923e30data1::32 data2::0 Address::bnode@923e30 parent::nullTo continue...press 11Enter the element83data1::1 data2::0 Address::bnode@7d772e parent::bnode@35ce36data1::25 data2::0 Address::bnode@757aef parent::bnode@35ce36data1::12 data2::0 Address::bnode@35ce36 parent::bnode@923e30data1::45 data2::0 Address::bnode@d9f9c3 parent::bnode@9cab16data1::56 data2::0 Address::bnode@1a46e30 parent::bnode@9cab16data1::83 data2::89 Address::bnode@3e25a5 parent::bnode@9cab16data1::52 data2::60 Address::bnode@9cab16 parent::bnode@923e30data1::32 data2::0 Address::bnode@923e30 parent::nullTo continue...press 112

  • 7/30/2019 New Ds Lab Manual

    51/87

    RESULT:

    Thus the program for b-tree using Java has been implemented.

  • 7/30/2019 New Ds Lab Manual

    52/87

    EX NO:6Date: TRIES

    AIM

    To implement the tries with insert & delete operations using

    Java.

    ALGORITHM

    Step 1:

    Step 2:

    Step 3:

    Step 4:

    Step 5:

    Step 6:

    Step 7:

    Step 8:

    Start the program by defining the functions.

    First initialize the node to null

    To find the particular element use function findsCheck the element to root node, if it is not found check for left or right sideof the root. If its found return the element

    To insert the particular element read that element andInsert the element with tag as 0 and level as 1.

    To display the elements, display if root as null, print asempty, otherwise call empty

    Print the current node in left sub tree in the format as currentnode.data +level and tag.

    Display current node in the right sub tree

    End of the program

  • 7/30/2019 New Ds Lab Manual

    53/87

    PROGRAM:

    import java.io.*;

    class node

    {

    publicint tag,level;

    publicint data;public node LC,RC,par;

    }

    class trie

    {

    public node cptr;

    public node root=null;

    public node find(int key)

    {

    int item=key;

    node temp=root;while(temp!=null)

    {

    cptr=temp;

    if(temp.tag==1)

    {

    if((item & 1)==0)

    {

    temp=temp.LC;

    item=item >> 1;

    }

    else

  • 7/30/2019 New Ds Lab Manual

    54/87

    item=item >> 1;

    }

    }

    else

    {

    if(key==temp.data)

    {

    return temp;}

    elsebreak;

    }

    }

    returnnull;

    }

    publicvoid insert()

    {

    int key=0;

    try{

    System.out.println("Enter the element:");

    DataInputStream din=newDataInputStream(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;

  • 7/30/2019 New Ds Lab Manual

    55/87

    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;

  • 7/30/2019 New Ds Lab Manual

    56/87

    else

    temp.RC=n1;

    n1.level=temp.level+1;

    n1.par=temp;

    }

    }

    System.out.println("Element already exists");

    }}

    publicvoid display()

    {

    if(root==null)

    System.out.println("EMPTY");

    else

    {

    System.out.println("\nIn Order");

    dispin(root);

    }

    }

    publicvoid 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

    {

    publicstaticvoid main(String args[ ])throwsIOException

    {

    int ch=0,cont=0;

    trie t = new trie();do

    {

    System.out.println("TRIES 1. Insert ");

    DataInputStream din = newDataInputStream(System.in);

    try

    {

    ch=Integer.parseInt(din.readLine());

    }

    catch(Exception e)

    {}

    if(ch==1)

  • 7/30/2019 New Ds Lab Manual

    57/87

    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:

    TRIES 1. Insert

    1Enter the element:1232

    In Order1232 LEVEL- 1 TAG-0

    press 1 to continue:1TRIES 1. Insert1Enter the element:4451In Order

    1232 LEVEL- 2 TAG-00 LEVEL- 1 TAG-14451 LEVEL- 2 TAG-0

    press 1 to continue:1TRIES 1. Insert1Enter the element:1243In Order1232 LEVEL- 2 TAG-00 LEVEL- 1 TAG-10 LEVEL- 2 TAG-1

    4451 LEVEL- 5 TAG-00 LEVEL- 4 TAG-11243 LEVEL- 5 TAG-00 LEVEL 3 TAG 1

  • 7/30/2019 New Ds Lab Manual

    58/87

    TRIES 1. Insert1Enter the element:1015In Order1232 LEVEL- 2 TAG-00 LEVEL- 1 TAG-10 LEVEL- 2 TAG-14451 LEVEL- 5 TAG-0

    0 LEVEL- 4 TAG-11243 LEVEL- 5 TAG-00 LEVEL- 3 TAG-11015 LEVEL- 4 TAG-0

    press 1 to continue:1TRIES 1. Insert1Enter the element:1942

    1942 LEVEL- 3 TAG-00 LEVEL- 1 TAG-10 LEVEL- 2 TAG-14451 LEVEL- 5 TAG-00 LEVEL- 4 TAG-11243 LEVEL- 5 TAG-00 LEVEL- 3 TAG-11015 LEVEL- 4 TAG-0

    press 1 to continue:1TRIES 1. Insert1Enter the element:1941In Order1232 LEVEL- 3 TAG-00 LEVEL- 2 TAG-11942 LEVEL- 3 TAG-00 LEVEL- 1 TAG-11941 LEVEL- 3 TAG-00 LEVEL- 2 TAG-14451 LEVEL- 5 TAG-00 LEVEL- 4 TAG-11243 LEVEL- 5 TAG-00 LEVEL- 3 TAG-11015 LEVEL- 4 TAG-0

    press 1 to continue:1TRIES 1. Insert1Enter the element:1055

  • 7/30/2019 New Ds Lab Manual

    59/87

    1942 LEVEL- 3 TAG-00 LEVEL- 1 TAG-11941 LEVEL- 3 TAG-00 LEVEL- 2 TAG-14451 LEVEL- 5 TAG-00 LEVEL- 4 TAG-11243 LEVEL- 5 TAG-00 LEVEL- 3 TAG-11015 LEVEL- 5 TAG-0

    0 LEVEL- 4 TAG-11055 LEVEL- 5 TAG-0press 1 to continue:1TRIES 1. Insert1Enter the element:1243Element already exists

    In Order1232 LEVEL- 3 TAG-0

    0 LEVEL- 2 TAG-14451 LEVEL- 5 TAG-0

    0 LEVEL- 4 TAG-11243 LEVEL- 5 TAG-00 LEVEL- 3 TAG-11015 LEVEL- 5 TAG-00 LEVEL- 4 TAG-11055 LEVEL- 5 TAG-0

    press 1 to continue:12

  • 7/30/2019 New Ds Lab Manual

    60/87

  • 7/30/2019 New Ds Lab Manual

    61/87

    RESULT:

    Thus the program for tries using Java has been implemented

  • 7/30/2019 New Ds Lab Manual

    62/87

    EX NO: 7Date: QUICK SORT

    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=a[i]&&i=a[j]&&j>first)j--;

    Step8: if (i>j)Swap (i,j)

    Step9: sort the elements and display the sorted values.

  • 7/30/2019 New Ds Lab Manual

    63/87

    import java.io.*;

    class quicksortalg

    {

    int noe;

    int[] a=newint[100];

    publicvoid sort()

    {

    try{

    System.out.println("Enter the number of elements:");

    DataInputStream din=newDataInputStream(System.in);

    noe=Integer.parseInt(din.readLine());

    System.out.println("Enter the elements:");

    for(int i=1;i

  • 7/30/2019 New Ds Lab Manual

    64/87

    publicvoid display()

    {

    for(int i=1;i

  • 7/30/2019 New Ds Lab Manual

    65/87

    OUTPUTEnter the number ofelements:5

    Enter the elements:29614563The array:29614563

    The sorted array:12456396

  • 7/30/2019 New Ds Lab Manual

    66/87

  • 7/30/2019 New Ds Lab Manual

    67/87

    RESULT:

    Thus the program for quick sort has been implemented using Java and the

    output is verified.

  • 7/30/2019 New Ds Lab Manual

    68/87

    PROGRAM:

    import java.io.*;

    class convexhullalg

    {

    int x[],y[],n;

    boolean status[];

    void insert(){

    try

    {

    DataInputStream in=newDataInputStream(System.in);

    System.out.println("Enter number of points:");

    n=Integer.parseInt(in.readLine());

    x=newint[n];

    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 angularor not(180)

    Step9: End of the program

    EX No: 8Date: CONVEX HULL

  • 7/30/2019 New Ds Lab Manual

    69/87

    System.out.println("Enter x and y coordinates for ");

    for(int i=0;i

  • 7/30/2019 New Ds Lab Manual

    70/87

    slope=(double)(x[i]-x[p])/(double)(y[i]-y[p]);

    degree=Math.toDegrees(Math.atan(slope));

    if(degree < 0)

    degree+=180;

    }

    catch(Exception e)

    {

    degree=90;}

    if(i==p+1)

    {

    deg=degree;

    next=i;

    }

    else

    {

    if((c=='L' && deg>degree)||(c!='L' && deg

  • 7/30/2019 New Ds Lab Manual

    71/87

  • 7/30/2019 New Ds Lab Manual

    72/87

    OUTPUT:

    Enter number ofpoints:

    4Enter x and ycoordinates for

    point 126

    point 278

    point 314

    point 4

    210Boundary points are(1, 4)(2, 10)(7,8)

  • 7/30/2019 New Ds Lab Manual

    73/87

  • 7/30/2019 New Ds Lab Manual

    74/87

    RESULT:

    Thus the program for convex hull has been implemented using Java and

    Executed successfully.

    0/1 KNAPSACK USING DYNAMIC

    PROGRAMMING

    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.

    Read the number of objects that are given.

    For each objects, 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.

  • 7/30/2019 New Ds Lab Manual

    75/87

    PROGRAM :

    import java.io.*;

    class objects

    {

    int weight;

    int profit;

    }

    publicclass knapsack

    {

    staticint N,W;

    static objects st[];publicstaticvoid main(String args[])throwsIOException

    {

    EX No: 9Date:

    AIM

    To write a Java program for the implementation of 0/1 knapsack usingdynamic programming.

    ALGORITHM

  • 7/30/2019 New Ds Lab Manual

    76/87

    N=Integer.parseInt(in.readLine());

    System.out.println("Enter the maximum weight sack can take:");

    W=Integer.parseInt(in.readLine());

    st=new objects[N+1];

    st[0]=new objects();st[0].weight=st[0].profit=0;

    for(int i=1;i

  • 7/30/2019 New Ds Lab Manual

    77/87

  • 7/30/2019 New Ds Lab Manual

    78/87

    Item Weight Profit

    1 6 20

    3 3 19

    Total profit=39

    OUTPUT:------

    Enter the number of objects:3Enter the maximum weight

    sack can take:10

    For object 1Enter profit: 20Enter Weight: 6

    For object 2Enter profit: 10Enter Weight: 2

    For object 3Enter profit: 19Enter Weight: 3

    The optimal solution is:

  • 7/30/2019 New Ds Lab Manual

    79/87

  • 7/30/2019 New Ds Lab Manual

    80/87

    PROGRAM

    EX No: 10Date: GRAPH COLORING USING

    BACKTRACKING

    AIM

    To write the Java program for the implementation of graphColoring.

    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

  • 7/30/2019 New Ds Lab Manual

    81/87

    import java.io.*;

    class gcoloring

    {

    int a[][]=newint[10][10];

    int x[]=newint[10];

    int m, n;

    void read()

    {DataInputStream in=newDataInputStream(System.in);

    try

    {

    System.out.println("Enter number of vertices in the graph");

    n=Integer.parseInt(in.readLine());

    System.out.println("Enter 1 if there is an edge Otherwise 0");

    for(int i=1;i

  • 7/30/2019 New Ds Lab Manual

    82/87

    System.out.println();

    }

    else

    mcoloring(k+1);

    }while(true);

    }

    void nextvalue(int k){

    int j;

    do

    {

    x[k]=(x[k]+1)%(m+1);

    if(x[k]==0) return;

    for(j=1;j

  • 7/30/2019 New Ds Lab Manual

    83/87

    Enter number of vertices in the graph

    4Enter 1 if there is an edge Otherwise 0

    between 1 and 10

    between 1 and 2

    1between 1 and 30

    between 1 and 41

    between 2 and 11

    between 2 and 20

    between 2 and 31

    between 2 and 40

    between 3 and 10between 3 and 21

    between 3 and 30

    between 3 and 41

    between 4 and 11

    between 4 and 20

    between 4 and 31

    between 4 and 40

    Given adjacency matrix is

    All possible combinations for m = 2 are

    0

    10

    1

    1

    01

    0

    0

    1

    0

    1

    1

    01

    0

  • 7/30/2019 New Ds Lab Manual

    84/87

    2 1 2 1

    All possible combinations for m = 3 are

    11111122222

    22233311133

    11311222312

    23223313131

  • 7/30/2019 New Ds Lab Manual

    85/87

  • 7/30/2019 New Ds Lab Manual

    86/87

    RESULT:

    Thus the program for graph coloring using Java has been implemented

    And executed successfully.

  • 7/30/2019 New Ds Lab Manual

    87/87