Question: Pg 216: 6.3: Write a postorder traversal function for general trees, similar to the preorder traversal function named preorder given in Section 6.1.2. 6.1.2 General

 Pg 216: 6.3: Write a postorder traversal function for general trees,
similar to the preorder traversal function named preorder given in Section 6.1.2.
6.1.2 General Tree Traversals In Section 5.2, three tree traversals were presented

Pg 216: 6.3: Write a postorder traversal function for general trees, similar to the preorder traversal function named preorder given in Section 6.1.2. 6.1.2 General Tree Traversals In Section 5.2, three tree traversals were presented for binary trees: preorder, pos- torder, and inorder. For general trees, preorder and postorder traversals are defined with meanings similar to their binary tree counterparts. Preorder traversal of a gen- eral tree first visits the root of the tree, then performs a preorder traversal of each subtree from left to right. A postorder traversal of a general tree performs a pos- torder traversal of the root's subtrees from left to right, then visits the root. Inorder 198 Chap. 6 Non-Binary Trees Figure 6.3 An example of a general tree. traversal does not have a natural definition for the general tree, because there is no particular number of children for an internal node. An arbitrary definition - such as visit the leftmost subtree in inorder, then the root, then visit the remaining sub- trees in inorder - can be invented. However, inorder traversals are generally not useful with general trees. Example 6.1 A preorder traversal of the tree in Figure 6.3 visits the nodes in order RACDEBF A postorder traversal of this tree visits the nodes in order CDEAFBR. To perform a preorder traversal, it is necessary to visit each of the children for a given node (say R) from left to right. This is accomplished by starting at R's leftmost child (call it T). From T. we can move to T's right sibling, and then to that node's right sibling, and so on. Using the ADT of Figure 6.2, here is a Java implementation to print the nodes of a general tree in preorder. Note the for loop at the end, which processes the list of children by beginning with the leftmost child, then repeatedly moving to the next child until calling next returns null /** Preorder traversal for general trees / static void preorder (GTNode It) { PrintNode (t); if (!rt.isLeaf()) { GTNode > temp = st. leftmost Child(); while (temp != null) { preorder (temp); temp-temp. right Sibling()

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!