Question: BinaryTree Implementation (java) Please check this code and help with 2 last parts (private void postorderToString and private void inorderToString) Thanks /* */ //package KW.CH06;
BinaryTree Implementation (java)
Please check this code and help with 2 last parts (private void postorderToString and private void inorderToString) Thanks
/*
import java.io.BufferedReader; import java.io.IOException; import java.io.Serializable;
/** * Class for a binary tree that stores type E objects. **/
public class BinaryTree
/** Class to encapsulate a tree node. */ protected static class Node
/** The information stored in this node. */ public E data; /** Reference to the left child. */ public Node
// Constructors -------------- Missing code 1 /** * Construct a node with given data and no children. * @param data The data to store in this node */ public Node(E data) { this.data = data; left = null; right = null; }
// Methods /** * Returns a string representation of the node. * @return A string representation of the data fields */ @Override public String toString() { return data.toString(); } } /*
/** Construct an empty BinaryTree */ public BinaryTree() { root = null; }
/** * Construct a BinaryTree with a specified root. * Should only be used by subclasses. * @param root The node that is the root of the tree. */ protected BinaryTree(Node
/** * Constructs a new binary tree with data in its root,leftTree ---Missing code 2 * as its left subtree and rightTree as its right subtree. */ public BinaryTree(E data, BinaryTree
/** * Return the left subtree. --- Missing code 3 * @return The left subtree or null if either the root or * the left subtree is null */ public BinaryTree
if(root != null && root.left != null) return new BinaryTree(root.left); else return null; }
/** * Return the right sub-tree ---- Missing code 4 * @return the right sub-tree or * null if either the root or the * right subtree is null. */ public BinaryTree
if(root != null && root.right != null) return new BinaryTree(root.right); else return null; }
/** * Return the data field of the root * @return the data field of the root * or null if the root is null */ public E getData() { if (root != null) { return root.data; } else { return null; } }
/** * Determine whether this tree is a leaf. ----- Missing code 5 * @return true if the root has no children */ public boolean isLeaf() { return root == null || (root.left == null && root.right == null);
}
@Override public String toString() { StringBuilder sb = new StringBuilder(); preOrderTraverse(root, 1, sb); return sb.toString(); }
/** * Perform a pre order traversal. * @param node The local root * @param depth The depth * @param sb The string buffer to save the output */ private void preOrderTraverse(Node
/*
/* private void preorderToString(StringBuilder stb, Node stb.append(root); if (root.left != null) { stb.append(" "); preorderToString(stb, root.left); } if (root.right != null) { stb.append(" "); preorderToString(stb, root.right); } } /*
/* private void postorderToString(StringBuilder stb, Node } /*
/* private void inorderToString(StringBuilder stb, Node } /*
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
