Question: Read through the IntTreeNode.java and IntTree.java files provided. Create a client program named IntTreeClient.java, you will create three trees using the class of IntTree with

Read through the IntTreeNode.java and IntTree.java files provided.

Create a client program named IntTreeClient.java, you will create three trees using the class of IntTree with maximum values as 14, 28, and 30.

Also in your client code, test the different methods to print each Tree.

The sample output of the tree with maximum value of 12 is displayed as following:

Tree structure:

7

3

6

12

1

11

5

10

2

9

4

8

preorder: 1 2 4 8 9 5 10 11 3 6 12 7

inorder: 8 4 9 2 10 5 11 1 12 6 3 7

postorder: 8 9 4 10 11 5 2 12 6 7 3 1

The output of running IntTreeClient.java should contain all three trees.

/*********************************************************************************

IntTree.java is below

import java.util.*;

public class IntTree { private IntTreeNode overallRoot;

// pre : max > 0 // post: constructs a sequential tree with given number of // nodes public IntTree(int max) { if (max <= 0) { throw new IllegalArgumentException("max: " + max); } overallRoot = buildTree(1, max); }

// post: returns a sequential tree with n as its root unless // n is greater than max, in which case it returns an // empty tree private IntTreeNode buildTree(int n, int max) { if (n > max) { return null; } else { return new IntTreeNode(n, buildTree(2 * n, max), buildTree(2 * n + 1, max)); } }

// 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 < level; i++) { System.out.print(" "); } System.out.println(root.data); printSideways(root.left, level + 1); } } }

/*******************************************************************************************

IntTreeNode.java is below

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; } }

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!