Question
Fill in the method postorder that does the following: 1) take the root node of the tree, an object of BinaryCharNode as an input, 2)
Fill in the method postorder that does the following: 1) take the root node of the tree, an object of
BinaryCharNode as an input, 2) print out the element of each BinaryCharNode in postorder traversing
order.
BinaryCharNode.postorder(root);
The method call above should print out:
D
B
E
F
C
A
2. Fill in the method reconstructTree that does the following: 1) take 2 strings as inputs (one containing
inorder traversal result of the tree, the other containing preorder traversal result of the tree), 2) reconstruct a
Binary Tree that results given inorder, preorder traversals, 3) return the root node object of BinaryCharTree.
(Hint: you can verify whether your tree is correctly constructed by using postorder method implemented
above).
public class TreeConstruction {
// DO NOT MODIFY BinaryNode CLASS.
public class BinaryCharNode {
public Character element;
public BinaryCharNode left;
public BinaryCharNode right;
BinaryCharNode() {
element = null;
}
BinaryCharNode(Character n) {
element = n;
}
// Update the code here
public static void postorder(BinaryCharNode root) {
// WRITE CODE HERE
}
}
// Update the code here
public BinaryCharNode reconstructTree(String inorder, String preorder) {
// WRITE CODE HERE
return new BinaryCharNode(); // Replace with correct return value
}
public static void main(String args[]) {
String inorder = "DBAECF";
String preorder = "ABDCEF";
BinaryCharNode root = new TreeConstruction().reconstructTree(inorder, preorder);
// The cost must print out
// D
// B
// E
// F
// C
// A
BinaryCharNode.postorder(root);
}
}
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