Results 1 to 5 of 5
  1. #1
    VinceGuad is offline Member
    Join Date
    Jan 2008
    Posts
    36
    Rep Power
    0

    Default Help me understand this method

    Can someone please provide inline comments for this method? I am having trouble understanding/recognizing whats going on...

    Java Code:
    	private Node openParent() {
    		Queue<Node> queue = new LinkedList<Node>();
    		queue.add(root);
    		Node temp;
    		while (queue.peek() != null) {
    			if (queue.peek().leftChild != null
    					&& queue.peek().rightChild != null) {
    				temp = queue.remove();
    				queue.add(temp.leftChild);
    				queue.add(temp.rightChild);
    			} else
    				return queue.peek();
    		}
    		return null;
    	}

  2. #2
    Mr.Beans's Avatar
    Mr.Beans is offline Senior Member
    Join Date
    Apr 2009
    Location
    Virginia Beach, Virginia
    Posts
    149
    Rep Power
    0

    Default

    You should check out Queues and Nodes if you don't already know what they are.

    Java Code:
    private Node openParent() 
            {
                // Create a new Queue object which holds data of type Node
                Queue<Node> queue = new LinkedList<Node>(); 
                // add an object with the variable name 'root' to the Queue
                queue.add(root);
                Node temp;  // Declare a variable called 'temp' of type Node
                // while the next item in the queue is not null
                while (queue.peek() != null) {
                    /*
                     * Check if the Node's data field 'leftChild' and 'rightChild'
                     * from the instance of Node that is next in the Queue are
                     * not null
                     */
                    if (queue.peek().leftChild != null
                            && queue.peek().rightChild != null) {
                        // Remove an the object at the front of the Queue
                        // and reference temp to it
                        temp = queue.remove(); 
                        // ad temps field 'leftChilde' to the Queue
                        queue.add(temp.leftChild);  
                        // ad temps field 'leftChilde' to the Queue
                        queue.add(temp.rightChild);
                    } else
                        // the children of the Node are null, return the Node itself
                        return queue.peek();
                }
                return null;
            }
    Hope that helps,

    Mr. Beans

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

    Default

    To the original poster, cross-posting can frustrate anyone who tries to help you only to find out later that the same answer was given hours ago in a cross-posted thread. No one likes wasting their time, especially a volunteer. The polite thing to do would be to not do this, but if you feel that you absolutely must, to at least provide links in both cross-posts to each other.

  4. #4
    VinceGuad is offline Member
    Join Date
    Jan 2008
    Posts
    36
    Rep Power
    0

    Default

    Thank you very much, I understand. Sorry for the cross post.

  5. #5
    VinceGuad is offline Member
    Join Date
    Jan 2008
    Posts
    36
    Rep Power
    0

    Default

    I have posted in the other forum that a solution has been found. Thank you

Similar Threads

  1. Help me to understand the problem
    By ron87 in forum New To Java
    Replies: 1
    Last Post: 03-25-2009, 04:34 PM
  2. I don´t understand
    By Manikyr in forum New To Java
    Replies: 6
    Last Post: 02-22-2009, 11:22 PM
  3. Replies: 3
    Last Post: 10-06-2008, 11:03 PM
  4. help me need to understand queries
    By hossainsadd in forum JDBC
    Replies: 1
    Last Post: 05-26-2008, 12:02 AM
  5. [SOLVED] Please help me understand...
    By Master Zero in forum Java Applets
    Replies: 6
    Last Post: 05-05-2008, 07:41 PM

Posting Permissions

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