Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old March 5th, 2013, 06:00 AM
Mr_Bean Mr_Bean is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 6 Mr_Bean User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 3 m 56 sec
Reputation Power: 0
Homework - Threaded Tree Traversal

Hello,

As an assignment we have to write a method for a Threaded Binary Tree that traverses the tree in post-order. However, we aren't allowed to use recursion or stacks

This is what I have so far (after doing some Googling):

java Code:
Original - java Code
  1. public void postOrder() {
  2.     ThreadedTreeNode<T> temp = new ThreadedTreeNode<T>(null);
  3.     String action = "left";
  4.     ArrayList<ThreadedTreeNode<T>> visited = new ArrayList<ThreadedTreeNode<T>>();
  5.  
  6.     temp.left = root;
  7.    
  8.     while (temp != null) {
  9.         if (action == "left") {
  10.             if (temp.left != null && !visited.contains(temp.left))
  11.                 temp = temp.left;
  12.             else
  13.                 action = "right";
  14.         }
  15.         else if (action == "right") {
  16.             if (temp.left != null && !visited.contains(temp.left))
  17.                 action = "left";
  18.             else
  19.                 action = "visit";
  20.         }
  21.         else if (action == "visit") {
  22.             visit(temp);
  23.             visited.add(temp);
  24.  
  25.             if (temp.right.left == temp) {
  26.                 action = "right";
  27.                 temp = temp.right
  28.             }
  29.            
  30.             if (temp.right == null)
  31.                 return;
  32.         }
  33.     }
  34. }


Any ideas on what's wrong? It just goes into an infinite loop when visiting the root.

Thanks,

Mr_Bean

Reply With Quote
  #2  
Old March 5th, 2013, 06:22 AM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 2,958 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 3 h 7 m 10 sec
Reputation Power: 345
One problem is the use of == to compare Strings. The code should use the equals() method.

Reply With Quote
  #3  
Old March 6th, 2013, 12:32 AM
Mr_Bean Mr_Bean is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 6 Mr_Bean User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 3 m 56 sec
Reputation Power: 0
Thanks, but that didn't solve the problem.

Reply With Quote
  #4  
Old March 6th, 2013, 06:17 AM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 2,958 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 3 h 7 m 10 sec
Reputation Power: 345
Try debugging the code by adding some println statements that will print out the values of variables as the code executes and show you why it goes into an infinite loop.
It should show how the value of temp changes as the code executes.

Reply With Quote
  #5  
Old March 6th, 2013, 06:48 AM
tvc3mye's Avatar
tvc3mye tvc3mye is offline
Daniel Schildsky
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Mar 2004
Location: KL, Malaysia.
Posts: 1,534 tvc3mye User rank is General 10th Grade (Above 100000 Reputation Level)tvc3mye User rank is General 10th Grade (Above 100000 Reputation Level)tvc3mye User rank is General 10th Grade (Above 100000 Reputation Level)tvc3mye User rank is General 10th Grade (Above 100000 Reputation Level)tvc3mye User rank is General 10th Grade (Above 100000 Reputation Level)tvc3mye User rank is General 10th Grade (Above 100000 Reputation Level)tvc3mye User rank is General 10th Grade (Above 100000 Reputation Level)tvc3mye User rank is General 10th Grade (Above 100000 Reputation Level)tvc3mye User rank is General 10th Grade (Above 100000 Reputation Level)tvc3mye User rank is General 10th Grade (Above 100000 Reputation Level)tvc3mye User rank is General 10th Grade (Above 100000 Reputation Level)tvc3mye User rank is General 10th Grade (Above 100000 Reputation Level)tvc3mye User rank is General 10th Grade (Above 100000 Reputation Level)tvc3mye User rank is General 10th Grade (Above 100000 Reputation Level)tvc3mye User rank is General 10th Grade (Above 100000 Reputation Level)tvc3mye User rank is General 10th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Weeks 4 Days 2 h 27 m 57 sec
Reputation Power: 1620
Send a message via MSN to tvc3mye Send a message via Yahoo to tvc3mye Send a message via Google Talk to tvc3mye Send a message via Skype to tvc3mye
Facebook
infinite loop

Just by looking at the codes, there are 2 exit criteria to escape the while loop:
a) temp is null.
b) temp.right is null.

You have to find out why either of these 2 conditions are not reached.
__________________
When the programming world turns decent, the real world will turn upside down.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > Homework - Threaded Tree Traversal

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap