Question
Creating a Tree-Traversal [100 pts] (a) Refer to the topic of traversing nodes of a tree. We had discussed several ways of traversing (node visitation)
Creating a Tree-Traversal [100 pts] (a) Refer to the topic of traversing nodes of a tree. We had discussed several ways of traversing (node visitation) of a tree, such as inorder, postorder, preorder, levelorder. You can use push and pop node(s) of a tree on a stack as a means to create a traversal order
(b) Refer to Figure 25-5 of the textbook that walks through the process that uses a stack to create an iterative inorder traversal of a binary tree, using a while loop. The section also shows Java method that implements the following algorithm to generate an inorder traversal of a binary tree with root as treeRoot
Algorithm doInorderTraversal(BinaryTreeNode treeRoot) {
Create Stack nodeStack = new Stack(); Set CurrentNode = treeRoot;
While(nodeStack notEmpty || currentNode is not null) { While(currentNode is not null) {
push current node on stack
currentNode = left child of current node }
// end inner while
If(nodeStack notEmpty) {
pop nodeStack and call it nextNode
print nextNode
set currentNode to right child of nextNode
} // end if
} // end outer while }
// end algorithm
(c) In this assignment, you will create preordertraversal sequence in two ways, one by using a stack object and second by a recursive method. Both ways should generate the same preordersequence of nodes of tree
Note: Recursive definition of preordertraversal (node visitation) is as follows: - Visit Root - Visit Left Child - Visit Right Child
2. Pre-Lab Planning
(a) Based on the note above, write recursive algorithm for preordertraversal of a binary tree. The format for the algorithm should be similar to 1(b) above. Refer to the diagrammatic process flow to create a preorder traversal of a binary tree as shown in Figure 25-6(a) of textbook to define the algorithm [20 pts]
(b) Load the Tree Sample Code, shared in Canvas after class session on ADT Tree
(c) Refer to the BinaryTree class in the sample code loaded in 2(b)
Sample code :
package com.mycompany.treesamplecode; /** A class that implements the ADT binary tree. */ public class BinaryTree
3. Writing Code (a) Since before you start to generate any traversal of a tree, that tree will have to be created first, the testBinaryTree() method of the BTDriver class in the Sample Code has been created for you, which you will need to run to build the tree. You will need to update testBinaryTree () method so it builds the following binary tree
For example, to build the subtree shown in the dotted square above, your code should be as follows:
BinaryTree leftSubTree = new BinaryTree(); leftSubTree.setTree("1", null, null);
BinaryTree rightSubTree = new BinaryTree(); rightSubTree.setTree("8", null, null);
BinaryTree subTree = new BinaryTree(); subTree.setTree("4", leftSubTree, rightSubTree);
Based on the algorithm you will have created in 2(a) above, write a recursive method recursivePreorderTraversal(root) that generates a preordertraversal of nodes of a binary tree, shown in the diagram in 3(a), such that [40 pts]
(i) This method must be called recursively (call itself)
(ii) The initial call of this method should be provided the root node of the tree as method argument
(iii) The final output of the method should be a string that shows tree data in preorder format
Note: - As discussed in class, each node of the binary tree will be a BinaryTreeNode object. Review structure of class BinaryTreeNode which is the inner class of BinaryTree. It has 3 attributes, data, leftChild and rightChild. You will be printing data attribute of each node you visit in a binary tree traversal - If a node does not have either right child, left child or both, the respective attribute value(s) will be null
Please answer all parts and put full code for the questions that require it and algorithm for the parts that require it
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started