Results 1 to 5 of 5
Thread: Help me understand this method
- 04-11-2009, 12:44 AM #1
Member
- Join Date
- Jan 2008
- Posts
- 36
- Rep Power
- 0
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; }
- 04-11-2009, 12:53 AM #2
You should check out Queues and Nodes if you don't already know what they are.
Hope that helps,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; }
Mr. Beans
-
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.
- 04-11-2009, 12:59 AM #4
Member
- Join Date
- Jan 2008
- Posts
- 36
- Rep Power
- 0
Thank you very much, I understand. Sorry for the cross post.
- 04-11-2009, 01:00 AM #5
Member
- Join Date
- Jan 2008
- Posts
- 36
- Rep Power
- 0
Similar Threads
-
Help me to understand the problem
By ron87 in forum New To JavaReplies: 1Last Post: 03-25-2009, 04:34 PM -
I don´t understand
By Manikyr in forum New To JavaReplies: 6Last Post: 02-22-2009, 11:22 PM -
Understand my logic errors and better understanding method and class creation
By freethinker89 in forum New To JavaReplies: 3Last Post: 10-06-2008, 11:03 PM -
help me need to understand queries
By hossainsadd in forum JDBCReplies: 1Last Post: 05-26-2008, 12:02 AM -
[SOLVED] Please help me understand...
By Master Zero in forum Java AppletsReplies: 6Last Post: 05-05-2008, 07:41 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks