TreeConstruction.java [40 pts] 1. Fill in the method postorder that does the following: 1) take the root
Question:
TreeConstruction.java [40 pts]
1. 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);
}
}
Java How To Program Late Objects Version
ISBN: 9780136123712
8th Edition
Authors: Paul Deitel, Deitel & Associates