View the step-by-step solution to:

Complete the implementation of getRoot and toString. Complete the implementation of size and isEmpty operations of a binary tree, assuming that...

1. Complete the implementation of getRoot and toString.
2. Complete the implementation of size and isEmpty operations of a binary tree, assuming that there is not a count variable.
3. Create boolean methods for our BinaryTreeNode class to determine if the node is a leaf or a an internal node.
4. Create a method called depth that will return an int representing the level or depth of the given node from the root.
5. Complete the implementation of the contains method for a binary tree.

Similar to the prior projects your program should provide a command-line user interface that accepts the following commands. These commands will operate on a binary tree whose nodes contain integer values.

1. print

This command should print the contents of the binary tree using the following output format:
a. A leaf node containing the integer value, x, should be displayed as:

( x )
b. A non-leaf node containing the integer value, x, should be displayed as:

(x subtree1 subtree2)

where subtree1 and subtree2 represents the display generated by recursively applying rules a. and b. If either subtree1 or subtree2 is empty the empty subtree will be displayed as:

()

Example: A binary tree of integers of depth 3 with the structure:

1
11 12
111 112 122

would display as:

( 1 (11 (111) (112)) (12 () (122)))

2. size
This command should print the number of nodes in the tree.
3. depth val_integer
This command should determine if the tree contains the value val_integer. If so, then the command should print the depth of that node in the tree. If the value is not present, print an error message:

The tree does not contain the value val_integer.

4. contains val_integer
This command should determine if the tree contains the value val_integer. If so, then the command should print:

The tree contains the value val_integer.
The node is node_type.

where node_type is the phrase: “the root”, “a leaf”, or “an internal node” depending on whether the found node is one of those types of nodes.
If the tree does not contain the node, print the error message:

The tree does not contain the value val_integer.

5. set tree
This command sets the binary tree to be the tree tree, where tree is sequence of characters that describe a binary tree of integers using the same syntax as the display syntax for the print command. More precisely the rules that describe the syntax are as follows. ( | separates different options):

Tree  ( ) | ( integer ) | (integer Tree Tree)

Examples:

set (1 (11 (111) (112)) (12 () (122)))
set ( 1 )
set ( 1 () () )
set ( 1 () (2 (3) () ) )

Program.zip - Preview not available

Why Join Course Hero?

Course Hero has all the homework and study help you need to succeed! We’ve got course-specific notes, study guides, and practice tests along with expert tutors.

-

Educational Resources
• -

Study Documents

Find the best study resources around, tagged to your specific courses. Share your own to gain free Course Hero access.

Browse Documents