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
- 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.
- 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.)
- 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.)
- 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.)
- 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.)
- 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.)
- 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
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started