Results 1 to 16 of 16

Thread: anyone help me

  1. #1
    menir2412 is offline Member
    Join Date
    Mar 2011
    Location
    Indonesia, Jakarta
    Posts
    8
    Rep Power
    0

    Default anyone help me

    i wanna make visualization oh heap tree in java programming...
    but i don't know how to make

    thank you
    Attached Thumbnails Attached Thumbnails anyone help me-untitled.jpg  

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  3. #3
    menir2412 is offline Member
    Join Date
    Mar 2011
    Location
    Indonesia, Jakarta
    Posts
    8
    Rep Power
    0

    Default

    both of them..the design including code of that program...can you help me?

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  5. #5
    menir2412 is offline Member
    Join Date
    Mar 2011
    Location
    Indonesia, Jakarta
    Posts
    8
    Rep Power
    0

    Default

    i just find the simple code..but i don't know to visualization that code..
    Attached Files Attached Files
    • File Type: zip h.zip (21.8 KB, 0 views)

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    First of all separate the things. Do you have done any UI related stuff with Java, like Swing/SWT related.

  7. #7
    menir2412 is offline Member
    Join Date
    Mar 2011
    Location
    Indonesia, Jakarta
    Posts
    8
    Rep Power
    0

    Default

    no i don't because i don't know the step to make it

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Okay fine.

    Did you complete the computation part of your application? If so try to workout something about drawing shapes with Java, like circles, lines, etc.

  9. #9
    menir2412 is offline Member
    Join Date
    Mar 2011
    Location
    Indonesia, Jakarta
    Posts
    8
    Rep Power
    0

    Default

    i don't know how

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,786
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by menir2412 View Post
    i don't know how
    First define what a heap exactly is: a heap is a complete binary tree such that for every node the node is larger than its children (if any). If you add a node to the heap it stays a complete binary tree but you have to 'reheap' it by bubbling the newly inserted node (see your own illustration for an example). Work from there: start with the visualization of a complete binary tree.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    menir2412 is offline Member
    Join Date
    Mar 2011
    Location
    Indonesia, Jakarta
    Posts
    8
    Rep Power
    0

    Default

    first of all,

    i know what the meaning of heap..

    but to make the visualization like connect node to another i can't..because i don't know the code..

    thank you sir

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    What exactly do you know?

  13. #13
    menir2412 is offline Member
    Join Date
    Mar 2011
    Location
    Indonesia, Jakarta
    Posts
    8
    Rep Power
    0

    Default

    i have a heap code from book..the output not like the picture what i mean..just text...

    this is the code what i've got..

    import java.io.*;

    public class HeapSortApp {
    public static void main(String[] args) throws IOException
    {
    int size, j;
    //System.out.print("Enter number of items: ");
    size = getInt();
    Heap theHeap = new Heap(size);
    for(j=0; j<size; j++) // fill array with
    { // random nodes
    int random = (int)(java.lang.Math.random()*100);
    Node newNode = new Node(random);
    theHeap.insertAt(j, newNode);
    theHeap.incrementSize();
    }

    //System.out.print("Random: ");
    theHeap.displayArray(); // display random array

    for(j=size/2-1; j>=0; j--) // make random array into heap
    theHeap.trickleDown(j);
    //System.out.print("Heap: ");
    theHeap.displayArray(); // display heap array
    theHeap.displayHeap(); // display heap

    for(j=size-1; j>=0; j--) // remove from heap and
    { // store at array end
    Node biggestNode = theHeap.remove();
    theHeap.insertAt(j, biggestNode);
    }
    //System.out.print("Sorted: ");
    theHeap.displayArray(); // display sorted array
    } // end main()
    // -------------------------------------------------------------

    public static String getString() throws IOException
    {
    InputStreamReader isr = new InputStreamReader(System.in);
    BufferedReader br = new BufferedReader(isr);
    String s = br.readLine();
    return s;
    }
    //-------------------------------------------------------------
    public static int getInt() throws IOException
    {
    String s = getString();
    return Integer.parseInt(s);
    }
    // -------------------------------------------------------------
    } // end class HeapSortApp

    public class Heap {
    private Node[] heapArray;
    private int maxSize; // size of array
    private int currentSize; // number of items in array
    // -------------------------------------------------------------

    public Heap(int mx) // constructor
    {
    maxSize = mx;
    currentSize = 0;
    heapArray = new Node[maxSize];
    }
    // -------------------------------------------------------------

    public Node remove() // delete item with max key
    { // (assumes non-empty list)
    Node root = heapArray[0];
    heapArray[0] = heapArray[--currentSize];
    trickleDown(0);
    return root;
    } // end remove()
    // -------------------------------------------------------------

    public void trickleDown(int index)
    {
    int largerChild;
    Node top = heapArray[index]; // save root
    while(index < currentSize/2) // not on bottom row
    {
    int leftChild = 2*index+1;
    int rightChild = leftChild+1;
    // find larger child
    if(rightChild < currentSize && // right ch exists?
    heapArray[leftChild].getKey() <
    heapArray[rightChild].getKey())
    largerChild = rightChild;
    else
    largerChild = leftChild;
    // top >= largerChild?
    if(top.getKey() >= heapArray[largerChild].getKey())
    break;
    // shift child up
    heapArray[index] = heapArray[largerChild];
    index = largerChild; // go down
    } // end while
    heapArray[index] = top; // root to index
    } // end trickleDown()
    // -------------------------------------------------------------

    public void displayHeap()
    {
    int nBlanks = 32;
    int itemsPerRow = 1;
    int column = 0;
    int j = 0; // current item
    String dots = "...............................";
    System.out.println(dots+dots); // dotted top line
    while(currentSize > 0) // for each heap item
    {
    if(column == 0) // first item in row?
    for(int k=0; k<nBlanks; k++) // preceding blanks

    System.out.print(' ');
    // display item
    System.out.print(heapArray[j].getKey());
    if(++j == currentSize) // done?
    break;
    if(++column==itemsPerRow) // end of row?
    {
    nBlanks /= 2; // half the blanks
    itemsPerRow *= 2; // twice the items
    column = 0; // start over on
    System.out.println(); // new row
    }
    else // next item on row
    for(int k=0; k<nBlanks*2-2; k++)
    System.out.print(' '); // interim blanks
    } // end for
    System.out.println("\n" +dots+dots); // dotted bottom line
    } // end displayHeap()
    // -------------------------------------------------------------

    public void displayArray()
    {
    for(int j=0; j<maxSize; j++)
    System.out.print(heapArray[j].getKey() + " ");
    System.out.println("");
    }
    // -------------------------------------------------------------

    public void insertAt(int index, Node newNode)
    {
    heapArray[index] = newNode;
    }
    // -------------------------------------------------------------

    public void incrementSize()
    {
    currentSize++;
    }
    // -------------------------------------------------------------
    } // end class Heap


    public class Node {
    private int iData;

    public Node (int key) {
    iData = key;
    }
    public int getKey() {
    return iData;
    }
    }

    what about you guys???

  14. #14
    menir2412 is offline Member
    Join Date
    Mar 2011
    Location
    Indonesia, Jakarta
    Posts
    8
    Rep Power
    0

    Default

    Sir i wanna ask..
    do you know make a node with number inside the node like picture that i want??

  15. #15
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by menir2412 View Post
    Sir i wanna ask..
    do you know make a node with number inside the node like picture that i want??
    Start here: 2D Graphics Tutorial

  16. #16
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,786
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by menir2412 View Post
    Sir i wanna ask..
    do you know make a node with number inside the node like picture that i want??
    I'd find a nice little image in the shape of an oval and draw a number on top of it. Read the API documentation for the Graphics class for details how to draw images and Strings.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •