Postorder Tree Traversal: Calculating the Result

  • MHB
  • Thread starter barbara
  • Start date
  • Tags
    Tree
In summary, the algorithm described a postorder tree traversal that recursively traverses the left subtree, then the right subtree, and finally the root. In the given example tree, the result of the computation is 8 and the algorithm used is a type of recursion.
  • #1
barbara
10
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
  • #2
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:
  • #3
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:
  • #4
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.
 

Related to Postorder Tree Traversal: Calculating the Result

1. How does postorder tree traversal work?

Postorder tree traversal is a method used to traverse a tree data structure in a specific order. In this method, the left subtree is visited first, then the right subtree, and finally the root node. This process is repeated recursively until all nodes have been visited.

2. What is the purpose of postorder tree traversal?

The purpose of postorder tree traversal is to visit all nodes in a tree in a specific order. This can be useful for performing various operations on the tree, such as calculating the result of a mathematical expression represented by the tree.

3. How do you calculate the result using postorder tree traversal?

To calculate the result using postorder tree traversal, you first need to traverse the tree in postorder. As you visit each node, you can perform the appropriate operation (such as addition, subtraction, etc.) on the values of the left and right subtrees and the current node. This process is repeated until you reach the root node, which will hold the final result.

4. Can postorder tree traversal be used for all types of trees?

Yes, postorder tree traversal can be used for all types of trees, including binary trees, n-ary trees, and balanced trees. This method ensures that all nodes are visited and processed in the same order, regardless of the type of tree.

5. Are there any advantages of using postorder tree traversal over other traversal methods?

One advantage of using postorder tree traversal is that it can be used to calculate the result of a mathematical expression represented by a tree. This method also ensures that all nodes are visited and processed efficiently, making it a useful technique for various tree-related operations.

Similar threads

  • Set Theory, Logic, Probability, Statistics
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
  • Programming and Computer Science
Replies
14
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Set Theory, Logic, Probability, Statistics
Replies
1
Views
903
  • Programming and Computer Science
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Programming and Computer Science
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
15
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
9K
Back
Top