Question
algorithm from 2a : Recursive Algorithm for Preorder Traversal Algorithm recursivePreorderTraversal(BinaryTreeNode root) { if (root is not null) { // (i) Visit Root print root.getData()
algorithm from 2a :
- Recursive Algorithm for Preorder Traversal
Algorithm recursivePreorderTraversal(BinaryTreeNode root) {
if (root is not null) {
// (i) Visit Root
print root.getData()
// (ii) Visit Left Child
recursivePreorderTraversal(root.getLeftChild())
// (iii) Visit Right Child
recursivePreorderTraversal(root.getRightChild())
}
}
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
sample code BTDriver :
public class BTDriver { public static void main(String[] args) { testBinaryTree(); } public static void testBinaryTree() { BinaryTree
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