Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a toString method for a binary tree of integers. The method should return empty for an empty tree. For a leaf node, it should

Write a toString method for a binary tree of integers. The method should return "empty" for an empty tree. For a leaf node, it should return the data in the node as a string. For a branch node, it should return a parenthesized String that has three elements separated by commas: the data at the root, a string representation of the left subtree, and then a string representation of the right subtree. For example, if a variable t refers to reference tree #2, then the call t.toString() should return the following String (without the surrounding quotes): "(2, (8, 0, empty), (1, (7, 4, empty), (6, empty, 9)))"

Please also create a Main Program to test the code.

image text in transcribed

Provided Java Files

**************

IntTree.java

**************

} }

// post: prints the tree contents using a preorder traversal public void printPreorder() { System.out.print("preorder:"); printPreorder(overallRoot); System.out.println(); }

// post: prints the tree contents using a preorder traversal // post: prints in preorder the tree with given root private void printPreorder(IntTreeNode root) { if (root != null) { System.out.print(" " + root.data); printPreorder(root.left); printPreorder(root.right); } }

// post: prints the tree contents using a inorder traversal public void printInorder() { System.out.print("inorder:"); printInorder(overallRoot); System.out.println(); }

// post: prints in inorder the tree with given root private void printInorder(IntTreeNode root) { if (root != null) { printInorder(root.left); System.out.print(" " + root.data); printInorder(root.right); } }

// post: prints the tree contents using a postorder traversal public void printPostorder() { System.out.print("postorder:"); printPostorder(overallRoot); System.out.println(); }

// post: prints in postorder the tree with given root private void printPostorder(IntTreeNode root) { if (root != null) { printPostorder(root.left); printPostorder(root.right); System.out.print(" " + root.data); } }

// post: prints the tree contents, one per line, following an // inorder traversal and using indentation to indicate // node depth; prints right to left so that it looks // correct when the output is rotated. public void printSideways() { printSideways(overallRoot, 0); }

// post: prints in reversed preorder the tree with given // root, indenting each line to the given level private void printSideways(IntTreeNode root, int level) { if (root != null) { printSideways(root.right, level + 1); for (int i = 0; i

}

********************

IntTreeNode.java

********************

// Class for storing a single node of a binary tree of ints

public class IntTreeNode { public int data; public IntTreeNode left; public IntTreeNode right; // constructs a leaf node with given data public IntTreeNode(int data) { this(data, null, null); } // constructs a branch node with given data, left subtree, // right subtree public IntTreeNode(int data, IntTreeNode left, IntTreeNode right) { this.data = data; this.left = left; this.right = right; } }

Reference Tree #2

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Systems For Advanced Applications 17th International Conference Dasfaa 2012 Busan South Korea April 2012 Proceedings Part 1 Lncs 7238

Authors: Sang-goo Lee ,Zhiyong Peng ,Xiaofang Zhou ,Yang-Sae Moon ,Rainer Unland ,Jaesoo Yoo

2012 Edition

364229037X, 978-3642290374

More Books

Students also viewed these Databases questions

Question

Solve for the variable indicated. y = a + bx for x

Answered: 1 week ago

Question

305 mg of C6H12O6 in 55.2 mL of solution whats the molarity

Answered: 1 week ago