Ds Record Final 2010

download Ds Record Final 2010

of 76

Transcript of Ds Record Final 2010

  • 7/26/2019 Ds Record Final 2010

    1/76

    Ex.No.1

    Date.

    Implementation of Min Heap

    Aim:

    To implement Minheap using java.

    Algorithm:

    Step 1: Start the program.

    Step 2: Include the necessary packages.

    Step 3: Declare the sie o! heap as private and other mem"er !unctions to

    insert#delete as pu"lic.

    Step $: To insert the element into heap check %hether the heap is !ull or not.i!

    the keep is !ull report that heap is !ull.

    Step &: I! the heap is not !ull then to insert an element ' into a heap#create a

    hole in the ne't availa"le positioin#other%ise the tree %ill not complete.

    Step (: I! ' can "e placed in the hole %ithout violating the heap order then %e

    can insert in that position.

    Step ): *ther%ise slide the element that is in the holes parent node into the

    hole#thus "u""ling yhe hole up to%ard the root continue this process

    until ' can "e placed into the hole.this is a strategy called percolate up.Step +: To delete an element#%e have to check %hether the heap is empty#or

    not.I! the heap is empty report that the heap is empty.

    Step,: I! the heap is not empty delete#have to delete the minimum#%hen the

    minimum is removed# a hole is created at the root. It !ollo%s that the last

    element ' in the heap must move some%here in the heap. I! ' can "e

    placed in the hole then it is done.

    Step 1-: *ther%ise slide the smaller o! the holes children into the hole# thus

    pushing the hole do%n one level. epeat the step until ' can "e placed in

    the hole. This strategy is called percolate do%n.

    Step 11: Display the nodes in the Minheap.

    Step 12: Stop the program.

    1

  • 7/26/2019 Ds Record Final 2010

    2/76

    Program:

    import java.io./0

    pu"lic class Mineap

    private int4 eap0

    private int ma'sie0

    private int sie0

    pu"lic Mineap5int ma'6

    ma'sie 7 ma'0

    eap 7 ne% intma'sie40

    sie 7 - 0

    eap-4 7 Integer.MI89;0

    ?

    private int le!tchild5int pos6

    return 2/pos0

    ?

    private int rightchild5int pos6

    return 2/pos @ 10

    ?

    private int parent5int pos6

    return pos A 20 ?

    private "oolean islea!5int pos6

    return 55pos B sieA26 CC 5pos 7 sie660

    ?

    private void s%ap5int pos1# int pos26

    int tmp0

    tmp 7 eappos140

    eappos14 7 eappos240eappos24 7 tmp0

    ?

    pu"lic void insert5int elem6

    sie@@0

    eapsie4 7 elem0

    int current 7 sie0

    %hile 5eapcurrent4 eapparent5current646

    s%ap5current# parent5current660current 7 parent5current60

    2

  • 7/26/2019 Ds Record Final 2010

    3/76

    ?

    ?

    pu"lic void print56

    int i0!or 5i710 i7sie0i@@6

    System.out.print5eapi4 @ E E60

    System.out.println560

    ?

    pu"lic int removemin56

    s%ap51#sie60

    sieFF0

    i! 5sie G7 -6

    pushdo%n5160

    return eapsie@140

    ?

    private void pushdo%n5int position6

    int smallestchild0

    %hile 5Gislea!5position66

    smallestchild 7 le!tchild5position60

    i! 55smallestchild sie6 CC 5eapsmallestchild4 B

    eapsmallestchild@1466smallestchild 7 smallestchild @ 10

    i! 5eapposition4 7 eapsmallestchild46 return0 s%ap5position#smallestchild60

    position 7 smallestchild0

    ?

    ?

    pu"lic static void main5String4 args6

    InputStreameader '' 7 ne% InputStreameader5System.in6 0

    Hu!!eredeader "u!ead 7 ne% Hu!!eredeader5''6 0try

    Mineap m7ne% Mineap5&-60

    int ans710

    %hile5ans7716

    System.out.println5E1.I8S>T E60

    System.out.println5E2.>M*> E60

    System.out.println5E3.>'it E60

    System.out.println5EnE60System.out.println5E>nter ur choice:E60

    3

  • 7/26/2019 Ds Record Final 2010

    4/76

    System.out.println5EnE60

    String ch7"u!ead.read8T> T> SIJ> E60

    String ms7"u!ead.read8T> T> ; T*

    I8S>T E60

    !or5int i7-0imss0i@@6

    String ins7"u!ead.read ;S ;>

    I8S>T>D S=KK>SSL='itE60

    System.e'it5-60de!ault:

    System.out.println5E>nter the valid choiceE60

    ?

    ?

    ?

    catch5I*>'ception err6

    System.out.println5err60

    ?

    ??

    $

  • 7/26/2019 Ds Record Final 2010

    5/76

    Output:

    K:j2sdk1.$.-"inBjavac Mineap.java

    K:j2sdk1.$.-"inBjava Mineap1.I8S>T

    2.>M*>

    3.>'it

    >nter ur choice:

    1

    1.>8T> T> SIJ>

    &

    1.>8T> T> ; T* I8S>T

    3

    23

    $

    1(

    ,

    T> ;S ;> I8S>T>D S=KK>SSL=M*>

    3.>'it>nter ur choice:

    2the min val536is deleted success!ully

    $ , 1( 23

    1.I8S>T

    2.>M*>

    3.>'it

    >nter ur choice:

    3

    >'it

    &

  • 7/26/2019 Ds Record Final 2010

    6/76

    Result:

    Thus the program to implement the Minheap %as completed

    success!ully.

    (

  • 7/26/2019 Ds Record Final 2010

    7/76

    Ex.No.2

    Date:

    Implementation of Deaps

    Aim:

    To implement the "asic operation on an Deaps using java.

    Algorithm:

    Step 1: Start the program.

    Step 2: Include the necessary packages.

    Step 3: Declare the sie o! Deaps as private and other mem"er !unctions to

    insert#delete as pu"lic.Step $: To insert the element into heap check %hether the heap is !ull or not.i!

    the keep is !ull report that Deap is !ull.

    Step &: I! the heap is not !ull then to insert an element ' into a Deap# create a

    hole in the ne't availa"le position# other%ise the tree %ill not

    complete.

    Step (: I! ' can "e placed in the hole %ithout violating the Deap order then %e

    can insert in that position.

    Step ): *ther%ise slide the element that is in the holes parent node into the

    hole#thus "u""ling the hole up to%ard the root continue this process

    until ' can "e placed into the hole.this is a strategy called percolate up.

    Step +: To delete an element#%e have to check %hether the Deap is empty#or

    not.I! the Deap is empty report that the Deap is empty.

    Step,: I! the Deap is not empty delete# have to delete the minimum# %hen the

    minimum is removed# a hole is created at the root. It !ollo%s that the last

    element ' in the Deap must move some%here in the Deap. I! ' can "e

    placed in the hole then it is done.

    Step 1-: *ther%ise slide the smaller o! the holes children into the hole# thus

    pushing the hole do%n one level. epeat the step until ' can "e placed in

    the hole. This strategy is called percolate do%n.

    Step 11: Display the nodes in the Deaps.

    Step 12: Stop the program.

    )

  • 7/26/2019 Ds Record Final 2010

    8/76

    Program:

    import java.io./0

    class deapsalg

    int ma'sie71--#sie0

    int4 h7ne% intma'sie@140 AAdeaps array representation

    pu"lic int le!tchild5int i6

    return 2/i0

    ?

    pu"lic int rightchild5int i6

    return 2/i @ 10

    ?

    pu"lic int parent5int i6

    return iA20

    ?

    pu"lic "oolean islea!5int i6

    return 55i7sie6 CC 5iBsieA2660

    ?

    pu"lic void s%ap5int i#int j6

    int t0 t7hi40hi47hj40hj47t0

    ?

    pu"lic void display56

    System.out.println5EThe deaps elements are:E60

    !or5int i710i7sie@10i@@6

    System.out.println5EnE@hi460

    ?

    pu"lic int Ma'eap5int i6

    int t7i0

    %hile5tG72 CC tG736

    t7tA20

    i!5t7726

    return -0 AAmin heap

    ?

    else

    return 10AAma' heap ?

    +

  • 7/26/2019 Ds Record Final 2010

    9/76

    ?

    pu"lic int MinNartner5int p6

    int po%value75int6 55Math.!loor5Math.log5p6AMath.log52666F160

    int partner7pF5int65Math.po%52#po%value660return partner0

    ?

    pu"lic int Ma'Nartner5int p6

    int po%value75int6 55Math.!loor5Math.log5p6AMath.log52666F160

    int [email protected]%52#po%value660

    i!5partnerBsie@16

    partnerA720

    return partner0

    ?

    pu"lic void MinInsert5int i6 AA inserting the ne% element 0 "ottom up

    approach

    %hile 5parent5i6G71 CC 5hparent5i64Bhi466

    int par7parent5i60

    s%ap5par#i60

    i7par0

    ?

    ? pu"lic void Ma'Insert5int i6 AA inserting the ne% element 0 "ottom up

    approach

    %hile 5parent5i6 G71 CC 5hparent5i64hi466

    int par7parent5i60

    s%ap5par#i60 i7par0

    ?

    ?

    pu"lic void insert56 AA getting the ne% element

    int ne%elt7-0

    sie@@0 AAsie denotes the num"er o! elements

    i!5sieBma'sie6

    System.out.println5EDeap !ullE60

    else

    try

    System.out.println5E>nter the element:E60

    ,

  • 7/26/2019 Ds Record Final 2010

    10/76

    DataInputStream din7ne%

    DataInputStream5System.in60

    ne%elt7Integer.parseInt5din.read'ception e6? i!5sie7716

    h247ne%elt0AAinsertion into a empty deap0

    AAroot should "e empty0 start %ith 2

    return0

    ?

    int p7sie@10 AAp is the ne% last position in deap

    hp47ne%elt0

    s%itch5Ma'eap5p66

    case 1: AAp is a position in ma' heap

    int partner7MinNartner5p60

    i!5hpartner4Bhp46 AAcompare %ith partner in

    minheap

    s%ap5p#partner60 AA i! greater s%ap

    MinInsert5partner60

    ?

    else

    Ma'Insert5p60 "reak0

    case -: AA p is a postion in min heap partner7Ma'Nartner5p60

    i!5hpartner4hp46 AAcompare %ith partner in

    ma'heap

    s%ap5p#partner60 AA i! lesser s%ap

    Ma'Insert5partner60

    ?

    else MinInsert5p60

    "reak0

    de!ault:

    System.out.println5E>*E60

    ?AAend o! s%itch

    ?AAend o! else

    ?AAend o! !unction

    pu"lic void deletemin56

    i!5sie77-6 System.out.println5EDeap emptyE60

    1-

  • 7/26/2019 Ds Record Final 2010

    11/76

    else

    System.out.println5EThe deleted min elt:E@ h2460

    int i0

    int p7sie@10 int t7hp40

    sieFF0

    int small0

    !or5 i7202/i7sie@10i7small6

    i!5hrightchild5i64hle!tchild5i646

    small7rightchild5i60

    else

    small7le!tchild5i60

    hi47hsmall40

    ?

    p7i0 AAp is the position o! the element t

    hp47t0 AAdisplay560

    !or5i720i7sie@10i@@6

    s%itch5Ma'eap5i66

    case 1: AAi is a position in ma' heap

    int partner7MinNartner5i60

    i!5hpartner4Bhi46 AAcompare %ith partner inminheap

    s%ap5i#partner60 AA i! greater s%ap

    MinInsert5partner60

    ?

    else

    Ma'Insert5i60

    "reak0

    case -: AA i is a postion in min heap

    partner7Ma'Nartner5i60 i!5hpartner4hi46 AAcompare %ith partner in

    ma'heap

    s%ap5i#partner60 AA i! lesser s%ap

    Ma'Insert5partner60

    ?

    else

    MinInsert5i60

    "reak0

    de!ault: System.out.println5E>*E60

    11

  • 7/26/2019 Ds Record Final 2010

    12/76

    ?AAend o! s%itch

    ?AAend o! !or

    ?AAend o! else

    ?AAend o! !n deletemin

    pu"lic void deletema'56

    i!5sie77-6

    System.out.println5EDeap emptyE60

    else

    System.out.println5EThe deleted ma' elt:E@ h3460

    int i0

    int p7sie@10

    int t7hp40

    sieFF0

    int "ig0

    !or5 i7302/i7sie@10i7"ig6

    i!5hrightchild5i64Bhle!tchild5i646

    "ig7rightchild5i60

    else

    "ig7le!tchild5i60

    hi47h"ig40

    ?

    p7i0 AAp is the position o! the element t hp47t0

    !or5i720i7sie@10i@@6

    s%itch5Ma'eap5i66

    case 1: AAi is a position in ma' heap

    int partner7MinNartner5i60

    i!5hpartner4Bhi46 AAcompare %ith partner in

    minheap

    s%ap5i#partner60 AA i! greater s%ap

    MinInsert5partner60

    ?

    else

    Ma'Insert5i60

    "reak0

    case -: AA i is a postion in min heap

    partner7Ma'Nartner5i60

    i!5hpartner4hi46 AAcompare %ith partner in

    ma'heap

    12

  • 7/26/2019 Ds Record Final 2010

    13/76

    s%ap5i#partner60 AA i! lesser s%ap

    Ma'Insert5partner60

    ?

    else

    MinInsert5i60 "reak0

    de!ault:

    System.out.println5E>*E60

    ?AAend o! s%itch

    ?AAend o! !or

    ?AAend o! else

    ?AAend o! !n deletema'

    ?AAend o! class

    pu"lic class deaps

    pu"lic static void main5String args46 thro%s I*>'ception

    int ch7-#cont7-0

    deapsalg h17ne% deapsalg560

    do

    System.out.println5ED>;Ns 1.Insert 2.Delete Min 3.Delete Ma'E60

    DataInputStream din7ne% DataInputStream5System.in60

    try

    ch7Integer.parseInt5din.read'ception e6?

    i!5ch7716

    h1.insert560

    h1.display560

    ?

    else i!5ch7726

    h1.deletemin560

    h1.display560

    ?

    else i!5ch7736

    h1.deletema'560

    h1.display560

    ?

    else

    System.out.println5E>nter the correct choiceE60

    13

  • 7/26/2019 Ds Record Final 2010

    14/76

    ?

    System.out.println5Epress 1 to continue:E60

    try

    cont7Integer.parseInt5din.read'ception e6?

    ?%hile5cont77160

    ?

    ?

    1$

  • 7/26/2019 Ds Record Final 2010

    15/76

    Output:

    K:j2sdk1.$.-"inBjavac deaps.java

    K:j2sdk1.$.-"inBjava deaps

    D>;NS 1.Insert 2.Delete Min 3.Delete Ma'

    1

    >nter the element:

    &

    The deaps elements are:

    -

    &

    press 1 to continue:

    1

    D>;Ns 1.Insert 2.Delete Min 3.Delete Ma'

    1

    >nter the element:

    1-

    The deaps elements are:

    -

    &

    1-

    press 1 to continue:

    1D>;Ns 1.Insert 2.Delete Min 3.Delete Ma'

    1>nter the element:

    $&

    The deaps elements are:

    -

    &

    $&

    1-

    press 1 to continue:1

    D>;Ns 1.Insert 2.Delete Min 3.Delete Ma'

    1

    >nter the element:

    +

    The deaps elements are:

    -

    &

    $&

    1-+

    1&

  • 7/26/2019 Ds Record Final 2010

    16/76

    press 1 to continue:

    1

    D>;Ns 1.Insert 2.Delete Min 3.Delete Ma'

    1

    >nter the element:2&

    The deaps elements are:

    -

    &

    $&

    1-

    +

    2&

    press 1 to continue:

    1

    D>;Ns 1.Insert 2.Delete Min 3.Delete Ma'

    2

    The deleted min elt:&

    The deaps elements are:

    -

    +

    $&

    1-

    2&

    press 1 to continue:1

    D>;Ns 1.Insert 2.Delete Min 3.Delete Ma'3

    The deleted ma' elt:$&

    The deaps elements are:

    +

    2&

    1-

    press 1 to continue:

    12

    1(

  • 7/26/2019 Ds Record Final 2010

    17/76

    Result:

    Thus the program to implement the Deaps %as completed

    success!ully.

    1)

  • 7/26/2019 Ds Record Final 2010

    18/76

    Ex.No.

    Date:

    Implementation of !eftist "ree

    Aim:

    To implement

  • 7/26/2019 Ds Record Final 2010

    19/76

    Program:

    import java.io./0

    class node

    pu"lic int data0

    pu"lic node nter the element:E60

    DataInputStream din7ne% DataInputStream5System.in60

    ne%elt7Integer.parseInt5din.read'ception e6?

    node temp 7 ne% node560

    temp.data7ne%elt0

    temp.

  • 7/26/2019 Ds Record Final 2010

    20/76

    t7a.MNTE60

    else

    System.out.println5EnIn *rderE60

    dispin5root60

    ?

    ? pu"lic void dispin5node currentnode6

    i!5currentnodeG7null6

    dispin5currentnode.ST

    [email protected]

    dispin5currentnode.K60

    ?

    ??0

    class

  • 7/26/2019 Ds Record Final 2010

    21/76

    try

    ch7Integer.parseInt5din.read'ception e6? i!5ch7716

    m.insert560

    m.display560

    ?

    else i!5ch7726

    m.remove560

    m.display560

    ?

    else

    System.out.println5E>nter the correct choiceE60

    ?

    System.out.println5Epress 1 to continue:E60

    try

    cont7Integer.parseInt5din.read'ception e6? ?%hile5cont77160

    ??

    Output:

    21

  • 7/26/2019 Ds Record Final 2010

    22/76

    K:j2sdk1.$.-"inBjavac

  • 7/26/2019 Ds Record Final 2010

    23/76

    1

    >nter the element:

    2-

    In *rder13 S*T>ST 1

    11 S*T>ST 2

    2- S*T>ST 1

    + S*T>ST 2

    &- S*T>ST 1

    press 1 to continue:

    1

    LTIST T>> 1. Insert 2. Delete

    1

    >nter the element:

    1+

    In *rder

    13 S*T>ST 1

    11 S*T>ST 2

    2- S*T>ST 1

    + S*T>ST 2

    &- S*T>ST 1

    1+ S*T>ST 1

    press 1 to continue:1

    LTIST T>> 1. Insert 2. Delete1

    >nter the element:

    2

    In *rder

    13 S*T>ST 1

    11 S*T>ST 2

    2- S*T>ST 1+ S*T>ST 2

    &- S*T>ST 1

    1+ S*T>ST 1

    2 S*T>ST 1

    press 1 to continue:

    1

    LTIST T>> 1. Insert 2. Delete

    1

    >nter the element:

    )

    23

  • 7/26/2019 Ds Record Final 2010

    24/76

    In *rder

    13 S*T>ST 1

    11 S*T>ST 2

    2- S*T>ST 1

    + S*T>ST 2&- S*T>ST 1

    1+ S*T>ST 1

    2 S*T>ST 2

    ) S*T>ST 1

    press 1 to continue:

    1

    LTIST T>> 1. Insert 2. Delete

    2

    Deleted element is 2

    In *rder

    13 S*T>ST 1

    11 S*T>ST 2

    2- S*T>ST 1

    + S*T>ST 2

    &- S*T>ST 1

    1+ S*T>ST 1

    ) S*T>ST 1

    press 1 to continue:12

    2$

  • 7/26/2019 Ds Record Final 2010

    25/76

    Result:

    Thus the program to implement the

  • 7/26/2019 Ds Record Final 2010

    26/76

    Implementation of A$! "ree

    Aim:

    To implement the "asic operations on an ;< tree.

    Algorithm:

    Step 1: Lind the place to insert the ne% node in the tree. Traverse the node !rom

    the inserted position# until %e end %ith a "alance !actor @1 or

    F1.Denote this node#as '. I! a duplicate key is !ound terminate the

    process.

    Step 2: I! node ' %as not !ound#make another pass !rom the root# updating the

    "alance !actors as you go and terminate the process.Step 3: I! HL5P6 7 1 and ne% node %as inserted in PQs right su" tree or HL5P6

    7F1 and ne% node %as inserted in P0s le!t su"tree#HL5P67-# update the

    "alance !actor on path to P and terminate the process.

    Step $: *ther%ise# classi!y the im"alance at P and per!orm any o! the $

    rotations.

    "%pes of rotation in an A$! tree:

  • 7/26/2019 Ds Record Final 2010

    27/76

    import java.io./0

    class node

    pu"lic int data0

    pu"lic node nter the element:E60

    DataInputStream din7ne% DataInputStream5System.in60

    ne%elt7Integer.parseInt5din.read'ception e6?

    i!5root77null6

    node y7ne% node560

    y.data7ne%elt0y."!7-0

    y.TI*8 N*I8T

    node !#a#R#p0 AA!FBaFBRFBp

    node "#c0 AA!FBas parent0aFBnode %ith@AF 1

    int d0 AARFB!ollo%s p0pFBinsertion pointnode y7ne% node560

    "oolean !ound# un"alanced0

    !7null0

    a7root0

    p7root0

    R7null0

    !ound7!alse0

    %hile 5pG7null CC !oundG7true6

    i!5p."!G7-6 a7p0!7R0?i!5ne%eltp.data6R7p0p7p.

  • 7/26/2019 Ds Record Final 2010

    28/76

    else i!5ne%eltBp.data6R7p0p7p.K0?

    else y7p0!ound7true0?

    ?

    A/ !inally in the last iteration o! the %hile loop

    R %ill "e the node to %hich p has to "e attached p %ill "e null in the last iteration /A

    AAI8S>T C >H;

    i!5!ound77!alse6

    y.data7ne%elt0

    y."!7-0

    y.H; T* R TI >;K T> 8> 8*D>

    %hile5pG7y6

    i!5ne%elt B p.data6

    p."!7F10p7p.K0?else

    p."!710p7p.

  • 7/26/2019 Ds Record Final 2010

    29/76

    System.out.println5E

  • 7/26/2019 Ds Record Final 2010

    30/76

    case 1:

    a."!7-0

    "."!7F10

    "reak0

    case F1:a."!710

    "."!7-0

    "reak0

    case -:

    a."!7-0

    "."!7-0

    "reak0

    ?

    c."!7-0

    "7c0

    ?

    ?

    i!5!77null6

    root7"0

    else i!5a77!.MNTE60

    else

    System.out.println5EnIn *rderE60 dispin5root60

    ?

    ?

    pu"lic void dispin5node currentnode6

    i!5currentnodeG7null6

    dispin5currentnode.

  • 7/26/2019 Ds Record Final 2010

    31/76

    ?

    ?0

    class ;

  • 7/26/2019 Ds Record Final 2010

    32/76

    K:j2sdk1.$.-"inBjavac ;S 1. Insert

    1>nter the element:

    $

    In *rder

    1 HL -

    2 HL F1

    3 HL F1

    $ HL -

    press 1 to continue:

    1

    ;>S 1. Insert1

    32

  • 7/26/2019 Ds Record Final 2010

    33/76

    >nter the element:

    &

    im"alance

    In *rder

    1 HL -2 HL F1

    3 HL -

    $ HL -

    & HL -

    press 1 to continue:

    1

    ;>S 1. Insert

    1

    >nter the element:

    (

    im"alance

    In *rder

    1 HL -

    2 HL -

    3 HL -

    $ HL -

    & HL F1

    ( HL -

    press 1 to continue:

    1;>S 1. Insert

    1>nter the element:

    )

    im"alance

    In *rder

    1 HL -

    2 HL -

    3 HL -

    $ HL -& HL -

    ( HL -

    ) HL -

    press 1 to continue:

    1

    ;>S 1. Insert

    1

    >nter the element:

    1(

    In *rder1 HL -

    33

  • 7/26/2019 Ds Record Final 2010

    34/76

    2 HL -

    3 HL -

    $ HL F1

    & HL -

    ( HL F1) HL F1

    1( HL -

    press 1 to continue:

    1

    ;>S 1. Insert

    1

    >nter the element:

    1&

    < im"alance

    In *rder

    1 HL -

    2 HL -

    3 HL -

    $ HL F1

    & HL -

    ( HL F1

    ) HL -

    1& HL -

    1( HL -

    press 1 to continue:1

    ;>S 1. Insert1

    >nter the element:

    1$

    < im"alance

    In *rder

    1 HL -

    2 HL -

    3 HL -$ HL F1

    & HL -

    ( HL 1

    ) HL -

    1$ HL -

    1& HL -

    1( HL -

    press 1 to continue:

    1

    ;>S 1. Insert1

    3$

  • 7/26/2019 Ds Record Final 2010

    35/76

    >nter the element:

    13

    im"alance

    In *rder

    1 HL -2 HL -

    3 HL -

    $ HL -

    & HL -

    ( HL 1

    ) HL -

    13 HL -

    1$ HL 1

    1& HL 1

    1( HL -

    press 1 to continue:

    1

    ;>S 1. Insert

    1

    >nter the element:

    12

  • 7/26/2019 Ds Record Final 2010

    36/76

    $ HL -

    & HL -

    ( HL 1

    ) HL -

    11 HL -12 HL 1

    13 HL -

    1$ HL -

    1& HL -

    1( HL -

    press 1 to continue:

    1

    ;>S 1. Insert

    1

    >nter the element:

    1(

    Data already e'ists

    press 1 to continue:

    12

    3(

  • 7/26/2019 Ds Record Final 2010

    37/76

    Result:

    Thus the ;< Tree has "een implemented success!ully.

    Ex.No.'

    3)

  • 7/26/2019 Ds Record Final 2010

    38/76

    Date:

    Implementation of ()"ree

    Aim:

    To implement the "asic operations in a "inary tree.

    Algorithm:

    Algorithm for Insert:

    Step 1: Kheck %hether the root node o! the "inary tree is 8=

  • 7/26/2019 Ds Record Final 2010

    39/76

    Step 2: ;ssign the link o! the parent node to the child o! the node to "e deleted0

    Step 3: elease the memory !or the deleted node.

    Deleting the no&e *ith t*o +hil&ren:

    Step 1: Search the parent o! the node to "e deleted 5%ith t%o children6

    Step 2: Kopy the content o! the inorder successor to the node to "e deleted5%ith t%o children6

    Step 3: Delete the inorder successor node. I! the inorder successor node has no

    child# !ollo% the steps given in the deleting the lea! node. I! the inorder

    successor node has only one child# !ollo% the steps given in the

    deleting the node %ith one child.

    Step $: elease the memory !or the inorder successor node.

    Step &: eplace the contents o! the node to "e deleted %ith the copy o! the

    content o! the inorder successor node.

    Program:import java.io./0

    3,

  • 7/26/2019 Ds Record Final 2010

    40/76

    class "node

    int data1#data20

    "node lptr#mptr#rptr#parent0

    pu"lic void "node56

    this.data17this.data27-0

    this.lptr7this.mptr7this.rptr7this.parent7null0

    ?

    ?

    class "tree

    "node root7null0

    "node p#p10

    "node prev0

    void insert5int ele6

    "node temp7ne% "node560

    temp.data17ele0

    i!5root77null6

    root7temp0

    ?

    else

    p17root0

    %hile5p1G7null6

    prev7p10

    i!5temp.data1p1.data16

    p17p1.lptr0

    else i!55temp.data1Bp1.data16 CC 5temp.data1p1.data266

    p17p1.mptr0

    else

    p17p1.rptr0 ?

    p17prev0

    %hile5p1G7null6

    i!5p1.data277-6

    i!5temp.data1p1.data16

    int t7p1.data10

    p1.data17temp.data10 p1.data27t0

    $-

  • 7/26/2019 Ds Record Final 2010

    41/76

    p1.lptr7temp.lptr0

    i!5temp.lptrG7null6

    temp.lptr.parent7p10

    p1.mptr7temp.rptr0

    i!5temp.rptrG7null6 temp.rptr.parent7p10

    ?

    else

    p1.data27temp.data10

    p1.mptr7temp.lptr0

    i!5temp.lptrG7null6

    temp.lptr.parent7p10

    p1.rptr7temp.rptr0

    i!5temp.rptrG7null6

    temp.rptr.parent7p10

    ?

    temp.parent7p1.parent0

    "reak0

    ?

    else i!55p1.data1G7-6 CC 5p1.data2G7-66

    p17split5temp#p160

    temp7p10

    p17p1.parent0 ?

    ? ?

    display5root60

    ?

    "node split5"node t#"node p6

    "node n17null0

    "node n27null0

    i!5t.data1p.data16

    i!5p.mptrG7null6

    n17p.mptr0

    i!5p.rptrG7null6

    n27p.rptr0

    p.lptr7ne% "node560

    p.lptr7t0

    t.parent7p0

    p.mptr7null0

    p.rptr7ne% "node560 p.rptr.data17p.data20

    $1

  • 7/26/2019 Ds Record Final 2010

    42/76

    p.rptr.lptr7n10

    i!5n1G7null6

    p.rptr.lptr.parent7p.rptr0

    p.rptr.rptr7n20

    i!5n2G7null6 p.rptr.rptr.parent7p.rptr0

    p.rptr.parent7p0

    p.data27-0

    ?

    else i!55t.data1Bp.data16 CC 5t.data1p.data266

    i!5p.lptrG7null6

    n17p.lptr0

    i!5p.rptrG7null6

    n27p.rptr0

    p.lptr7ne% "node560

    p.lptr.data17p.data10

    p.lptr.parent7p0

    p.data17t.data10

    p.lptr.lptr7n10

    i!5n1G7null6

    p.lptr.lptr.parent7p.lptr0

    p.lptr.rptr7t.lptr0

    i!5t.lptrG7null6

    p.lptr.rptr.parent7p.lptr0 p.rptr7ne% "node560

    p.rptr.data17p.data20 p.rptr.rptr7n20

    i!5n2G7null6

    p.rptr.rptr.parent7p.rptr0

    p.rptr.lptr7t.rptr0

    i!5t.rptrG7null6

    p.rptr.lptr.parent7p.rptr0

    p.rptr.parent7p0

    p.data27-0 p.mptr7null0

    ?

    else

    i!5p.lptrG7null6

    n17p.lptr0

    i!5p.mptrG7null6

    n27p.mptr0

    p.lptr7ne% "node560

    p.lptr.data17p.data10 p.lptr.parent7p0

    $2

  • 7/26/2019 Ds Record Final 2010

    43/76

    p.mptr7null0

    p.lptr.lptr7n10

    i!5n1G7null6

    p.lptr.lptr.parent7p.lptr0

    p.lptr.rptr7n20 i!5n2G7null6

    p.lptr.rptr.parent7p.lptr0

    p.data17p.data20

    p.data27-0

    p.rptr7ne% "node560

    p.rptr7t0

    p.rptr.parent7p0

    ?

    return p0

    ?

    void display5"node temp6

    i!5tempG7null6

    display5temp.lptr60

    display5temp.mptr60

    display5temp.rptr60

    System.out.println5Edata1::[email protected]@E data2::E@temp.

    data2@E ;ddress::E@temp@E parent::[email protected]

    ? ?

    ?class "trees

    pu"lic static void main5String4 args6thro%s I*>'ception

    System.out.println5EHFTreesE60

    DataInputStream in7ne% DataInputStream5System.in60

    "tree "t7ne% "tree560

    int '#ch0 do

    System.out.println5E>nter the elementE60

    '7Integer.parseInt5in.read

  • 7/26/2019 Ds Record Final 2010

    44/76

    K:j2sdk1.$.-"inBjava "trees

    HFTrees

    >nter the element

    &2data1::&2 data2::- ;ddress::"nodeU,23e3- parent::null

    To continue...press 1

    1

    >nter the element

    $&

    data1::$& data2::&2 ;ddress::"nodeU,23e3- parent::null

    To continue...press 1

    1

    >nter the element

    +,

    data1::$& data2::- ;ddress::"nodeU13-c1," parent::"nodeU,23e3-

    data1::+, data2::- ;ddress::"nodeU1!(a)", parent::"nodeU,23e3-

    data1::&2 data2::- ;ddress::"nodeU,23e3- parent::null

    To continue...press 1

    1

    >nter the element

    12

    data1::12 data2::$& ;ddress::"nodeU13-c1," parent::"nodeU,23e3-

    data1::+, data2::- ;ddress::"nodeU1!(a)", parent::"nodeU,23e3-

    data1::&2 data2::- ;ddress::"nodeU,23e3- parent::nullTo continue...press 1

    1>nter the element

    &(

    data1::12 data2::$& ;ddress::"nodeU13-c1," parent::"nodeU,23e3-

    data1::&( data2::+, ;ddress::"nodeU1!(a)", parent::"nodeU,23e3-

    data1::&2 data2::- ;ddress::"nodeU,23e3- parent::null

    To continue...press 1

    1

    >nter the element1

    data1::1 data2::- ;ddress::"nodeU)d))2e parent::"nodeU,23e3-

    data1::$& data2::- ;ddress::"nodeU11"+(e) parent::"nodeU,23e3-

    data1::&( data2::+, ;ddress::"nodeU1!(a)", parent::"nodeU,23e3-

    data1::12 data2::&2 ;ddress::"nodeU,23e3- parent::null

    To continue...press 1

    1

    >nter the element

    32

    data1::1 data2::- ;ddress::"nodeU)d))2e parent::"nodeU,23e3-data1::32 data2::$& ;ddress::"nodeU11"+(e) parent::"nodeU,23e3-

    $$

  • 7/26/2019 Ds Record Final 2010

    45/76

    data1::&( data2::+, ;ddress::"nodeU1!(a)", parent::"nodeU,23e3-

    data1::12 data2::&2 ;ddress::"nodeU,23e3- parent::null

    To continue...press 1

    1

    >nter the element2&

    data1::1 data2::- ;ddress::"nodeU)d))2e parent::"nodeU3&ce3(

    data1::2& data2::- ;ddress::"nodeU)&)ae! parent::"nodeU3&ce3(

    data1::12 data2::- ;ddress::"nodeU3&ce3( parent::"nodeU,23e3-

    data1::$& data2::- ;ddress::"nodeUd,!,c3 parent::"nodeU,ca"1(

    data1::&( data2::+, ;ddress::"nodeU1!(a)", parent::"nodeU,ca"1(

    data1::&2 data2::- ;ddress::"nodeU,ca"1( parent::"nodeU,23e3-

    data1::32 data2::- ;ddress::"nodeU,23e3- parent::null

    To continue...press 1

    1

    >nter the element

    (-

    data1::1 data2::- ;ddress::"nodeU)d))2e parent::"nodeU3&ce3(

    data1::2& data2::- ;ddress::"nodeU)&)ae! parent::"nodeU3&ce3(

    data1::12 data2::- ;ddress::"nodeU3&ce3( parent::"nodeU,23e3-

    data1::$& data2::- ;ddress::"nodeUd,!,c3 parent::"nodeU,ca"1(

    data1::&( data2::- ;ddress::"nodeU1a$(e3- parent::"nodeU,ca"1(

    data1::+, data2::- ;ddress::"nodeU3e2&a& parent::"nodeU,ca"1(

    data1::&2 data2::(- ;ddress::"nodeU,ca"1( parent::"nodeU,23e3-

    data1::32 data2::- ;ddress::"nodeU,23e3- parent::nullTo continue...press 1

    1>nter the element

    +3

    data1::1 data2::- ;ddress::"nodeU)d))2e parent::"nodeU3&ce3(

    data1::2& data2::- ;ddress::"nodeU)&)ae! parent::"nodeU3&ce3(

    data1::12 data2::- ;ddress::"nodeU3&ce3( parent::"nodeU,23e3-

    data1::$& data2::- ;ddress::"nodeUd,!,c3 parent::"nodeU,ca"1(

    data1::&( data2::- ;ddress::"nodeU1a$(e3- parent::"nodeU,ca"1(

    data1::+3 data2::+, ;ddress::"nodeU3e2&a& parent::"nodeU,ca"1(data1::&2 data2::(- ;ddress::"nodeU,ca"1( parent::"nodeU,23e3-

    data1::32 data2::- ;ddress::"nodeU,23e3- parent::null

    To continue...press 1

    12

    $&

  • 7/26/2019 Ds Record Final 2010

    46/76

    Result:

    Thus the "asic operation o! a Hinary Tree has "een implemented

    success!ully.

    Ex.No.,

    Date:Implementation of "ries

    $(

  • 7/26/2019 Ds Record Final 2010

    47/76

    Aim:

    To implement Tries using java.

    Algorithm:

    Algorithm for Insert:

    Step 1: Start the program.

    Step 2: Kheck %hether the root node o! the tree is 8=

  • 7/26/2019 Ds Record Final 2010

    48/76

    pu"lic int tag#level0 AAtag !or ptr node 10data node -0 level starts %ith 1

    pu"lic int data0 AA!or data node

    pu"lic node

  • 7/26/2019 Ds Record Final 2010

    49/76

    DataInputStream din7ne% DataInputStream5System.in60

    key7Integer.parseInt5din.read'ception e6?

    i!5root77null6

    root7ne% node560

    root.data7key0

    root.tag7-0

    root.level710

    root.par7null0 root.

  • 7/26/2019 Ds Record Final 2010

    50/76

    temp.K7n30

    temp.

  • 7/26/2019 Ds Record Final 2010

    51/76

    i!5root77null6

    System.out.println5E>MNTE60

    else

    System.out.println5EnIn *rderE60 dispin5root60

    ?

    ?

    pu"lic void dispin5node currentnode6

    i!5currentnodeG7null6

    dispin5currentnode.

  • 7/26/2019 Ds Record Final 2010

    52/76

    cont7Integer.parseInt5din.read'ception e6?

    ?%hile5cont77160

    ??

    Output:

    K:j2sdk1.$.-"inBjavac TrieImp.javaK:j2sdk1.$.-"inBjava TrieImp

    &2

  • 7/26/2019 Ds Record Final 2010

    53/76

    TI>S 1. Insert

    1

    >nter the element:

    1232

    In *rder

    1232 >S 1. Insert

    1

    >nter the element:

    $$&1

    In *rder

    1232 >S 1. Insert

    1

    >nter the element:

    12$3

    In *rder

    1232 >S 1. Insert

    1

    >nter the element:

    1-1&

    In *rder

    1232 >

  • 7/26/2019 Ds Record Final 2010

    54/76

    - >S 1. Insert

    1

    >nter the element:

    1,$2

    In *rder

    1232 >S 1. Insert1

    >nter the element:1,$1

    In *rder

    1232 >S 1. Insert

    1>nter the element:

    &$

  • 7/26/2019 Ds Record Final 2010

    55/76

    1-&&

    In *rder

    1232 >S 1. Insert

    1

    >nter the element:

    12$3

    >lement already e'ists

    In *rder1232 >

  • 7/26/2019 Ds Record Final 2010

    56/76

    Result:

    Thus the implementation o! Tries has "een completed success!ully.

    Ex.No.-

    Date:

    Implementation of ui+/ 0ort

    &(

  • 7/26/2019 Ds Record Final 2010

    57/76

    Aim:

    To sort the given num"er in ascending order using Ruick sort

    Algorithm:

    Step 1: Start the program.

    Step 2:Include the necessary packages.

    Step 3: Declare the num"er o! elements that is to "e sorted.

    Step $: I! the num"er o! elements is - or 1 then return.

    Step &: Divide the given array into t%o and take middle element P as the pivotin the array ;.

    Step (: Nartition ;FP4 5the remaining elements in S6 into t%o disjoint groups

    ;1 7 5y "elongs to ;FP4Ay7P6 and ;275y "elongs to ;FP4AyB7P6.

    Step ): eturn 5Ruicksort5;16 !ollo%ed "y P !ollo%ed "y Ruicksort5;266

    Step +: Display the sorted list.

    Step ,: Stop the program.

    Program:

    import java.io./0

    pu"lic class Wuicksort

    &)

  • 7/26/2019 Ds Record Final 2010

    58/76

    pu"lic static void main5String args46 thro%s I*>'ception

    int 4 array7ne% int&-40

    int i0

    InputStreameader istream 7 ne% InputStreameader5System.in60

    Hu!!eredeader "u!ead 7 ne% Hu!!eredeader5istream6 0

    System.out.println5EWuick SortnE60

    System.out.println5E >nter The Sie *! array:E60

    String nn7"u!ead.readnter The alues *! array:nE60

    !or5i7-0in0i@@6

    String ms7"u!ead.read

  • 7/26/2019 Ds Record Final 2010

    59/76

    ?

    i! 5lo hi6

    int T 7 arraylo40

    arraylo4 7 arrayhi40arrayhi4 7 T0

    ?

    ?

    i! 5hi lo6

    int T 7 hi0

    hi 7 lo0

    lo 7 T0

    ?

    Ruick9srt5array# lo%# lo60

    Ruick9srt5array# lo 77 lo% X lo@1 : lo# n60

    ?

    ?

    Output:

    K:j2sdk1.$.-"inBjavac Wuicksort.java

    K:j2sdk1.$.-"inBjava Wuicksort

    &,

  • 7/26/2019 Ds Record Final 2010

    60/76

    Wuick Sort

    >nter The Sie *! array:

    &

    >nter The alues *! array:

    (

    23

    )

    1

    ,-

    alues He!ore the sort:

    ( 23 ) 1 ,-

    alues a!ter the sort:

    1 ( ) 23 ,-

    (-

  • 7/26/2019 Ds Record Final 2010

    61/76

    Result:

    Thus the Ruick sort algorithm has "een implemented success!ully.

    Ex.No.

    Date:

    Implementation of on3ex Hull

    Aim:

    (1

  • 7/26/2019 Ds Record Final 2010

    62/76

    To implement Konve' hull algorithm using java.

    Algorithm:

    Step 1: Start the program.

    Step 2: Include the necessary packages.

    Step 3: Declare the num"er o! points that polygon containing all the points S.

    Step $: I! 'i4 is point out the element and 'j4 is the ne't element.

    Step &: the point p in element o! lo%est yFcoordinates.

    Step (: i!55'i4B'j46 OO 55'i477'j46 CC 5yi4Byj4666 s%ap5i# j60

    Step ): The points according to the angle made %ith p and the 'Fa'is.

    Step +: Display the conve' polygon points .

    Step ,: Stop the program.

    Program:

    import java.io./0

    class conve'hullalg

    int '4#y4#n0

    (2

  • 7/26/2019 Ds Record Final 2010

    63/76

    "oolean status40

    void insert56

    try

    DataInputStream in7ne% DataInputStream5System.in60

    System.out.println5E>nter num"er o! points:E60

    n7Integer.parseInt5in.read

  • 7/26/2019 Ds Record Final 2010

    64/76

    System.out.println5E5E@'i4@E# E@yi4@E6E60

    ?

    void check5int p#char c6

    dou"le slope7-#degree7-#deg7-0 int ne't7-0

    statusp47true0

    !or5int i7p@10in0i@@6

    try

    slope75dou"le65'i4F'p46A5dou"le65yi4Fyp460

    degree7Math.toDegrees5Math.atan5slope660

    i!5degree -6

    degree@71+-0

    ?

    catch5>'ception e6

    degree7,-0

    ?

    i!5i77p@16

    deg7degree0

    ne't7i0

    ? else

    i!55c77

  • 7/26/2019 Ds Record Final 2010

    65/76

    ?

    Output:

    K:j2sdk1.$.-"inBjavac conve'hull.java

    K:j2sdk1.$.-"inBjava conve'hull

    >nter num"er o! points:

    $

    >nter ' and y coordinates !or

    point 1

    2

    (

    point 2

    )

    +

    point 3

    1

    $

    point $

    2

    1-

    Houndary points are

    51# $652# 1-6

    5)# +6

    (&

  • 7/26/2019 Ds Record Final 2010

    66/76

    Result:

    Thus the Konve' ull algorithm has "een e'ecuted success!ully.

    Ex.No.4

    Date:

    561 7napsa+/ using D%nami+ Programming

    ((

  • 7/26/2019 Ds Record Final 2010

    67/76

    Aim:

    To implement Ynapsack pro"lem using java.

    Algorithm:

    Step 1: Start the program.

    Step 2: Include the necessary packages.

    Step 3: Declare m is the sie o! knapsack and n is num"er o! %eights and

    pro!its.

    Step $: %4 and p4 are the %eights and pro!its.

    Step &: pi4A%i4 Z pi@14A%i@14. !% is the !inal %eight o! knapsack.

    Step (: !p is the !inal ma'imum pro!it.

    Step ): 'k47- i! %k4 is not in the knapsack0else 'k471.

    Step +: Nrint the %eight and !inal pro!it.

    Step ,: Stop the program.

    Program:

    import java.util./0

    import java.io./0import java.lang./0

    ()

  • 7/26/2019 Ds Record Final 2010

    68/76

    pu"lic class Ynapsack-1

    static int n7 0

    static o"j st40

    pu"lic static Hu!!eredeader "r 7ne% Hu!!eredeader5ne%InputStreameader5System.in660

    static class o"j

    int %eight0

    int pro!it0

    ?

    pu"lic static void main5String args46 thro%s I*>'ception

    int i7-0

    System.out.println5EYnap Sack Nro"lemnFFFFFFFFFFFFFFFFFFnE60

    System.out.print5E>nter total num"er o! o"jects: E60

    n 7 Integer.parseInt5 "r.readnter the ma'imum %eight sack can take: E60

    7 Integer.parseInt5 "r.read

  • 7/26/2019 Ds Record Final 2010

    69/76

    si4%4 7 siF14%40

    int i 7 n0

    int k 7 0

    int pro! 7 -0

    %hile55iB-6CC5kB-66

    i! 5si4k4 G7 siF14k46

    System.out.print5i@E:5E@sti4.%eight@E#

    [email protected]!it@E6# E60

    k 7 k F sti4.%eight0

    pro! @7 sti4.pro!it0

    ?

    iFF0

    ?

    System.out.print5E ?nIt %ill yield a pro!it o! s.E@ pro!60

    ?

    ?

    Output:

    K:j2sdk1.$.-"inBjavac Ynapsack-1.java

    K:j2sdk1.$.-"inBjava Ynapsack-1

    (,

  • 7/26/2019 Ds Record Final 2010

    70/76

    Ynap Sack Nro"lem

    FFFFFFFFFFFFFFFFFF

    >nter total num"er o! o"jects: 3>nter the ma'imum %eight sack can take: 3-

    Lor *"ject 1 :F

    eight: (

    Nro!it: 12

    Lor *"ject 2 :F

    eight: )

    Nro!it: 21

    Lor *"ject 3 :F

    eight: &

    Nro!it: 23

    *ptimal Solution is : 3:5 s.236# 2:5)# s.216# 1:5(# s.126# ?

    It %ill yield a pro!it o! s.&(

    )-

  • 7/26/2019 Ds Record Final 2010

    71/76

    Result:

    Thus the knapsack pro"lem has "een implemented success!ully.

    Ex.No.15

    Date:

    8raph +oloring using (a+/tra+/ing

    )1

  • 7/26/2019 Ds Record Final 2010

    72/76

    Aim:

    To implement Vraph coloring using java.

    Algorithm:

    Step 1: Start the program.

    Step 2: Include the necessary packages.

    Step 3: To get the num"er o! vertices and colors.

    Step $: 'i4 is the num"er o! vertices and k is the inde' o! the ne't verte' to

    color.

    Step &: Venerate all legal assignment !or 'k4 ne'tvalue5k6.

    Step (: i!5'k47-6 then no ne% color possi"le

    Step ): i!5k7n6 then almost m colors have "een used to color then n vertices.

    Step +: Display all the nodes and possi"le colors .

    Step ,: Stop the program.

    Program:

    import java.io./0

    pu"lic class VraphKoloring

    static int 44 V0

    )2

  • 7/26/2019 Ds Record Final 2010

    73/76

    static int 4 '0

    static int n# m0

    static "oolean !ound 7 !alse0

    pu"lic static Hu!!eredeader "r 7ne% Hu!!eredeader5ne%

    InputStreameader5System.in660pu"lic static void main5String4 args6 thro%s I*>'ception

    System.out.println5EttttV;N K*nter the num"er o! the vertices: E60

    n 7 Integer.parseInt5"r.read

  • 7/26/2019 Ds Record Final 2010

    74/76

    return0

    ?

    else

    mKoloring5k@160

    ??

    static void 8e'talue5int k6

    int j0

    %hile5true6

    'k4 7 5'k4@16[5m@160

    i!5'k477-6

    return0

    !or5j710 j7n0 j@@6

    i!5 5Vk4j4 G7 -6 CC 5'k4 77 'j46 6

    "reak0

    i!5j 77 n@16

    return0

    ?

    ?

    ?

    Output:

    K:j2sdk1.$.-"inBjavac VraphKoloring.java

    K:j2sdk1.$.-"inBjava VraphKoloring

    )$

  • 7/26/2019 Ds Record Final 2010

    75/76

    V;N K*nter the num"er o! the vertices: (

    I! edge "et%een the !ollo%ing vertices enter 1 else -:

    1 and 2: 1

    1 and 3: -

    1 and $: -

    1 and &: -

    1 and (: 1

    2 and 3: 1

    2 and $: -

    2 and &: -

    2 and (: 1

    3 and $: 1

    3 and &: &

    3 and (: -

    $ and &: 1

    $ and (: -

    & and (: 1

    >nter the num"er o! colors availa"le: 3

    Solution:1 2 1 3 2 3

    1 2 3 2 1 32 1 3 1 2 3

    2 3 1 2 3 1

    2 3 2 1 3 1

    3 2 1 3 2 1

    )&

  • 7/26/2019 Ds Record Final 2010

    76/76

    Result:

    Thus the Vraph coloring has "een implemented success!ully.