Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In this assignment you are free to use any Binary Tree reference implementation you like (cite the source). The methods I am asking you to

In this assignment you are free to use any Binary Tree reference implementation you like (cite the source). The methods I am asking you to write apply to a Binary Tree ( for this assignment you can also use a binary search tree; a BST is a specialization of a BT) and you should add them to the class BinaryTree. This assignment requires you to modify/extend a BinaryTree class.

This program will require modifications to the BinaryTree class

  1. Write the method nodeCount that takes as a a parameter a reference to the root node of a binary tree and returns the number of nodes in a binary tree. Add this method to class BinaryTree (5 pts.) Note: DO NOT USE a count variable that keeps track of nodes added to the tree as it is built to do this, that solution that would work but is not how you will do it. The whole point of these questions are for you to get comfortable traversing trees.
  2. Write a method leavesCount that takes as a a parameter a reference to the root node of a binary tree and returns the number of leaves and prints their values. Add this method to class BinaryTree (5 pts.)
  3. Write a method swapTrees that takes as a a parameter a reference to the root node of a binary tree and swaps all of the left and right subtrees of a binary tree. Add this method to class BinaryTree (20 pts.)
  4. Write a method singleParent that returns the number of nodes in a binary tree that have only one child. Add this method to class BinaryTree (20 pts.)
  5. Question: Let T be an ordered tree with more than one node. Is it possible that the preorder traversal of T visits the nodes in the same order as the postorder traversal of T? If so, give an example; otherwise explain why this cannot occur. Likewise, is it possible that the preorder traversal of T visits the nodes in the reverse order of the postorder traversal of T? If so, give an example; otherwise explain why this cannot occur. (20 pts.)
  6. Question: Draw a binary tree that simultaneously satisfies the following: a. Each internal node of T stores a single character. b. A preorder traversal of T yields EXAMFUN c. An inorder traversal of T yields MAFXUEN (15 pts.)
  7. Question: Write a method that computes and prints, for every position p/node of a tree T, the element of p followed by the height of p's subtree. Add this method to class BinaryTree (15 pts.)

You must also create a program to test the various operations on a binary search tree on each of the methods above.

Help in java please

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

Automating Access Databases With Macros

Authors: Fish Davis

1st Edition

1797816349, 978-1797816340

More Books

Students also viewed these Databases questions

Question

What are the three most common sources of equity funding?

Answered: 1 week ago

Question

Describe Table Structures in RDMSs.

Answered: 1 week ago