Postorder Tree Traversal: Calculating the Result

  • Context: MHB 
  • Thread starter Thread starter barbara
  • Start date Start date
  • Tags Tags
    Tree
Click For Summary
SUMMARY

The discussion centers on implementing a postorder tree traversal algorithm to compute the value of a given expression tree. The algorithm follows a recursive approach where it first traverses the left subtree, then the right subtree, and finally processes the root node. The specific tree structure provided results in a final computed value of 8, derived from the traversal sequence of 2, 3, *, 4, 2, *, 1, 5, +, - and +. The discussion also clarifies a typographical error in the tree representation.

PREREQUISITES
  • Understanding of tree data structures
  • Familiarity with recursion algorithms
  • Basic knowledge of arithmetic operations in programming
  • Experience with algorithmic problem-solving
NEXT STEPS
  • Study the implementation of recursive algorithms in Python
  • Learn about tree traversal techniques, specifically postorder traversal
  • Explore expression trees and their evaluation methods
  • Investigate common data structure visualizations for better representation
USEFUL FOR

Students, software developers, and computer science enthusiasts interested in algorithms, particularly those focusing on tree data structures and recursive programming techniques.

barbara
Messages
10
Reaction score
0
The following algorithm describes a postorder tree traversal

Postorder(tree)

If left subtree exists then Postorder(left subtree)

If right subtree exists then Postorder(right subtree)

Print root

end



How can I apply that to the following tree





+

/ \

/ \

* -

/ \ / \

2 3 * +

/ \ / \

4 2 1 5



What is the result of the computation? What kind of algorithm is being used here?
 
Physics news on Phys.org
Firstly, the pointer shows to +. Then we notice that a left subtree exists , so we call the function Postorder(left subtree) and then the pointer shows to * . Can you continue?

P.S. : At the following part:

+

/ \

/ \

* -

this: / \ should appear only once. Is it a typo?
 
Last edited:
evinda said:
Firstly, the pointer shows to +. Then we notice that a left subtree exists , so we call the function Postorder(left subtree) and then the pointer shows to * . Can you continue?

P.S. : At the following part:

+

/ \

/ \

* -

this: / \ should appear only once. Is it a typo?
Yes its a typo
Thank you for your help and this is what the answer should look like. Please let me know if its correctThe is a representation of a recursion algorithm traverses the left subtree, the right subtree, and finally the root. That carries on the subtrees of the subtrees until a leaf is found. Thus, starting with +

/ \

/ \

* -

/ \ / \

2 3 * +

/ \ / \

4 2 1 5You start with the left subtree of the root, which is *

/ \

2 3The root of this tree is * , and it has a left subtree consisting of the leaf 2, and the right subtree consisting of the leaf 3. Going through the traversal, then this subtree is traversed as 2 3 *Now you expand the right subtree of the root of the tree. That is
-

/ \

* +

/ \ / \

4 2 1 5The left subtree is *

/ \

4 2 The expansion of this tree is 4 2 *The right subtree is is

+

/ \

1 5The expansion of this tree is 1 5 +The root of the right subtree is -so the expansion of the right subtree is (left) (right) (root) 4 3 * 1 5 + -Finallyyou put together the traversal of the entire tree is (left) (right) root2 3 * 4 3 * 1 5 + - +
The value is equal to 8
 
Last edited:
Barbara, in the future please use a text editor with a fixed-width font to type the tree and then copy and paste it between the [textdraw]...[/textdraw] tags, like this.

[textdraw]
+
/ \
/ \
/ \
* -
/ \ / \
2 3 * +
/ \ / \
4 2 1 5
[/textdraw]
barbara said:
this is what the answer should look like. Please let me know if its correct
How do you know that the answer should look like this if you are not sure whether it is correct?

barbara said:
2 3 * 4 3 * 1 5 + - +

According to the tree, it's 4 2 and not 4 3. Otherwise, it's correct.
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
Replies
14
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 15 ·
Replies
15
Views
3K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 1 ·
Replies
1
Views
9K
  • · Replies 15 ·
Replies
15
Views
2K