Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Issues with tree

  1. #1
    Member
    Join Date
    May 2011
    Posts
    61
    My Mood
    Busy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Issues with tree

    Hi, I'm trying to build a simple heap Swing applet and the problem I have is I cannot output the contents of the nodes. In my paintComponents method, I read in an array from main method and store each element into a Node array (data member) in my Heap class which is a JPanel with the paintComponents method. The drawing of the binary heap tree is fine, but I wanted to make sure the data actually inserted, but when I go print it out, it prints out null for each of the array indices of the data member: Node (instance variable of the Heap class), but I checked and I did read in data from the array (from main) into each indice of this Node array. So my Heap class constructor has a parameter to read in an array from main method I mean. Any suggestions appreciated?

    I have another Node class that stores a left and right "pointers'" (references) to other Nodes and stores a data value which is from the this.array_of_int array data member in the Heap class below.

    so basically I have:
    Heap class extends JPanel
    {
       private Node nodes_array[];
       private int array_of_int[];
     
      //constructor that has parameter for the nodes_array which stores a Node object and another array that 
      //   reads in each integer from an array passed into constructor in main 
     
       public void paintComponents()
       {
           //draw a binary heap with n Nodes basd on this.array (data member passed to Heap class's constructor instance)
           //and each time this.node_array[i] = new Node(null, null, this.array[i] );
     
       }
     
      public void output_heap()
      {
              //for each item in node_arrays:
                    if current index is null//PROBLEM: why is it ALWAYS null if I am creating a new Node in the paintComponents //method above, this is where I am stuck and need some help...
                           output: null
                    else
                          output: element at this node_arrays index                      
               //end for-loop
       }
    }

    EDIT: Would it be that paintComponents should only deal with drawing and NOT anything else...I'll look into this too but any suggestions appreciated


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Issues with tree

    //and each time this.node_array[i] = new Node(null, null, this.array[i] );
    I don't follow what you mean by that.
    Don't be shy to post the code you are working with.

    Suggestion to fix it yourself, verify the values passed to the constructor, verify the values when they are "read in from main method", verify the values at every point in the code until you find out why the values are not what was expected.

  3. #3
    Member
    Join Date
    May 2011
    Posts
    61
    My Mood
    Busy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Issues with tree

    That was a typo*** it should be:
    this.node_array[i] = new Node(xPosition, yPosition, this.array[i] )

    I have a separate Node class with constructor that has an int x and int y position because I would like to build my nodes to be separate objects to swap each node's value and the 3rd parameter of the Node constructor above which is the data value from the int array[] data member which is just a regular array with the values hardcoded (for now anyways).

    *So it's the problem I have with why in the output method above in the Heap class, it outputs null for each entry in the this.node_array if I am clearly inserting a new Node objet in the code snippet in this post and OP. Appreciate any help.

    At this stage, I just want to be able to output the nodes in the this.node_array array.

    import java.awt.*;
    import javax.swing.*;
     
    public class Heap extends JPanel
    {
      private int xPosition;//initialized in constructor
      private int yPosition;//same
      private int array[];//same
      private Node node_array[];//same
      private int nodes_to_draw;//same (in main a hard coded int array for now to use bottom-up build heap)
     
      //public Heap constructor
     
      public void paintComponent(Graphics g)
      {
              Graphics2d g2d = (Graphics2d)g;
               while ( nodes_to_draw > 0 )
                {
                      //if still nodes to draw at this level
                      g2d.setColor(Color.yellow);
    		 g2d.fillOval( first_node_startX, first_node_startY, this.width, this.height );
     
    	//add this position to cur Heap_Node obj (most important is the data and color to change during heap sort)
    	this.node_arr[i] = new Heap_Node( xPosition, yPosition, width , height, this.array[i] );
            System.out.println("Node " + i + ": " + this.node_arr[i].get_data());//I can output the data at each node, but then it's always null in the output
            //   each node's data in the output method below which is where I am stuck
     
                //if maximum number of nodes reached to draw at current level
                        //update total nodes to draw at next level
               }
      }
     
      public void output_heap_data()//Problem here, I am unsure why it outputs null if I am inserting into this.node_arr in the paintComponents method above
      {
    		for ( int i = 1; i < this.node_arr.length; ++i )
    		{
    			if ( this.node_arr[i] != null )
    				System.out.println("Data: " + this.node_arr[i].get_data());
    			else
    				System.out.println("Data: " + null);//PROBLEM I have is here: why is it null if I'm inserting into this.node_arr array in the paintComponents method above??
    		}
     }
    }
     
    }

    ALso, the reason I want to store the xPosition and yPosition in each Node object is to be able to find the graphic nodes drawn so that I can find the coordinates to change the value if I need to swap (for heap sort) by updating what the g.drawString method displays but if there is a simpler way I'd appreciate since I am just getting into Swing applets but I find it interesting

Similar Threads

  1. Issues with my ordering program - if statement issues?
    By Shenaniganizer in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 31st, 2012, 10:17 PM
  2. B+ tree
    By cms in forum Algorithms & Recursion
    Replies: 2
    Last Post: December 15th, 2011, 07:58 AM
  3. Data Structures(Binary Search Tree to AVL Tree)ASAP
    By jfAdik in forum Algorithms & Recursion
    Replies: 2
    Last Post: April 5th, 2010, 03:58 AM
  4. t:tree 2 an
    By smackdown90 in forum Web Frameworks
    Replies: 0
    Last Post: January 27th, 2010, 12:56 PM
  5. B+ Tree
    By mikesir87 in forum Java Theory & Questions
    Replies: 0
    Last Post: November 20th, 2009, 10:52 AM