| | |
inorder traverse
![]() |
I have a binary tree which I would like to get printed out inorder.
Right now I have this recursice loop:
This get the right order printed. The problem is that I want this function to return a String with the right order.
Right now I have this recursice loop:
Java Syntax (Toggle Plain Text)
public void inorder() { if(leftChild != null ) leftChild.inorder(); System.out.print(" " + data); if(rightChild != null ) rightChild.inorder(); }
•
•
Join Date: Aug 2008
Posts: 77
Reputation:
Solved Threads: 16
Since you want to return a string, just save the string returned from each recursive call, plus the string of the data in the node you are looking at and add them all together:
Java Syntax (Toggle Plain Text)
public void inorder() { if(leftChild != null ) String s1 = leftChild.inorder(); String s2 = " " + data; if(rightChild != null ) String s3 = rightChild.inorder(); return s1 + s2 + s3; }
•
•
Join Date: Nov 2008
Posts: 328
Reputation:
Solved Threads: 51
Hello efus.
in inorder() function you have a line
Succesive collect nascent data in external buffer.
buffer can be placed in own singleton class.
0. Initialize external buffer;
---------------------------------
1. Invoke root.inorder();
2. Get external buffer as String
3. Clear external buffer
quuba
in inorder() function you have a line
System.out.print(" " + data); Replace this static method with own.Succesive collect nascent data in external buffer.
buffer can be placed in own singleton class.
0. Initialize external buffer;
---------------------------------
1. Invoke root.inorder();
2. Get external buffer as String
3. Clear external buffer
public void inorder() {
if (leftChild != null) {
leftChild.inorder();
}
Buffer.print(data.toString());
System.out.print(" " + data);
if (rightChild != null) {
rightChild.inorder();
}
}![]() |
Similar Threads
- Tree Traversal (C++)
- Binary Tree (C++)
- Database manipulation with AVL tree.. please help (Java)
- Trouble shooting help (C++)
- Don't know where to begon (C)
- searching and inserting node in a binary search tree (C)
Other Threads in the Java Forum
- Previous Thread: Postal code program, need help to get in right direction
- Next Thread: i need help understanding how to sort
| Thread Tools | Search this Thread |
-xlint actionlistener android api applet application array automation bi binary blackberry block bluetooth character class client code compile compiler component consumer database desktop developmenthelp eclipse error fractal freeze ftp functiontesting game gameprogramming givemetehcodez graphics gui health html hyper ide image int j2me j2seprojects java javac javaee javaprojects jetbrains jni jpanel jtable julia learningresources lego linked linux mac main map method mobile myregfun netbeans nonstatic notdisplaying number online pearl printf problem program project qt researchinmotion rotatetext rsa scanner screen server set singleton sms sort spamblocker sql string swing system textfields thread threads time title tree tutorial-sample update variablebinding windows working xor





