File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Calculate a tree Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Calculate a tree" Watch "Calculate a tree" New topic
Author

Calculate a tree

Terence hiu
Ranch Hand

Joined: Oct 21, 2012
Posts: 36
Say i have a tree structure created in java

root= +
root left node = +
rood right node = 4
root left of left =2
root left of right =3

(2+3)+4

how would i calculate this tree?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18509
    
  40

Terence hiu wrote:Say i have a tree structure created in java

root= +
root left node = +
rood right node = 4
root left of left =2
root left of right =3

(2+3)+4

how would i calculate this tree?


If I were to do it, I would choose to do it recursively. Encountering a number would be the base condition, where the number is simply returned. Encountering an operator will trigger two recursive calls to get the operands which will be needed to calculate the value to be returned.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Terence hiu
Ranch Hand

Joined: Oct 21, 2012
Posts: 36
Henry Wong wrote:
Terence hiu wrote:Say i have a tree structure created in java

root= +
root left node = +
rood right node = 4
root left of left =2
root left of right =3

(2+3)+4

how would i calculate this tree?


If I were to do it, I would choose to do it recursively. Encountering a number would be the base condition, where the number is simply returned. Encountering an operator will trigger two recursive calls to get the operands which will be needed to calculate the value to be returned.

Henry


Thankyou, problem solved
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Calculate a tree
 
Similar Threads
When do i say that a binary search tree is unique?!
Algorithm to find Nodes having largest distance in Binary tree.
Trouble with recursion
recursive method to balance a binary tree
Tournament Tree help