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

/*

*/ //package KW.CH06;

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 implements Serializable {

/** Class to encapsulate a tree node. */ protected static class Node implements Serializable { // Data Fields

/** The information stored in this node. */ public E data; /** Reference to the left child. */ public Node left; /** Reference to the right child. */ public Node right;

// 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(); } } /*

*/ // Data Field /** The root of the binary tree */ protected Node root;

/** 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 root) { this.root = root; }

/** * 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 leftTree, BinaryTree rightTree) { root = new Node(data); if(leftTree != null) root.left = leftTree.root; else root.left = null; if(rightTree != null) root.right = rightTree.root; else root.right = null; }

/** * 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 getLeftSubtree() {

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 getRightSubtree() {

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 node, int depth, StringBuilder sb) { for (int i = 1; i < depth; i++) { sb.append(" "); } if (node == null) { sb.append("null "); } else { sb.append(node.toString()); sb.append(" "); preOrderTraverse(node.left, depth + 1, sb); preOrderTraverse(node.right, depth + 1, sb); } }

/*

*/ /** * Method to read a binary tree. * @pre The input consists of a pre order traversal * of the binary tree. The line "null" indicates a null tree. * @param bR The input file * @return The binary tree * @throws IOException If there is an input error */ public static BinaryTree readBinaryTree(BufferedReader bR) throws IOException { // Read a line and trim leading and trailing spaces. String data = bR.readLine().trim(); if (data.equals("null")) { return null; } else { BinaryTree leftTree = readBinaryTree(bR); BinaryTree rightTree = readBinaryTree(bR); return new BinaryTree(data, leftTree, rightTree); } } /**/

/*

private void preorderToString(StringBuilder stb, Node root) {

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 root) {

} /**/

/*

private void inorderToString(StringBuilder stb, Node root) {

} /**/ } /**/

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!