Question

Suppose a binary tree stores integers. Write efficient methods (and give their Big-Oh running times) that...

Suppose a binary tree stores integers. Write efficient methods (and give their Big-Oh running times) that take a reference to a binary tree root T and compute

a. the number of nodes with two children that contain the same value

**JAVA**

Homework Answers

Answer #1

/* recursive method returns numbers of nodes with two children that contain the same value */

public int numberOfNodes(BtreeNode T)
{
   //check if T is null or both children are null then return 0
   if(T==null || T.leftChild==null && T.rightChild==null)
       return 0;
  
   //check if left child is null then call recursively with right child
   if(T.leftChild==null)
       return numberOfNodes(T.rightChild);
      
   //check if right child is null then call recursively with left child
   if(T.rightChild==null)
       return numberOfNodes(T.leftChild);
  
   //check if both two children contain the same value
   if(T.leftChild.data == T.rightChild.data)
       return (1 + numberOfNodes(T.leftChild) + numberOfNodes(T.rightChild));
   else
       return (numberOfNodes(T.leftChild) + numberOfNodes(T.rightChild));  
}

Since all the node in the binary tree need to check whether its two children contain the same value or not. Therefore, the running time of this method in big O notation is O(n), where n is the number of nodes in the binary tree.

Note: Whether you need any modification then share with me in the comment section, I'll happy to help you.

In this solution, I have checking whether the left and right child are null or not. When I will compare the data part of the children, and if any of children is null then JVM arises an Exception. Therefore, we should check beforehand, whether any node is null or not.

There are four possibilities: (1) both children are null, (2) left child is null, (3) right child is null, (4) both are not null
(1)When both children are null, that means it is a leaf node, so it returns 0
(2)When left child is null then we can process with the right subtree (since right child is not null)
(3)When right child is null then can process with the left subtree (since left child is not null)
(4)When both are not null then we can check their data parts, there are two possibilities
(a) data parts are equals (b) data parts are unequal
(a) When data parts are equals then returns 1 plus the counts of left-subtree and right-subtree (which is called recursively)
(b) data parts are unequal then returns the counts of left-subtree and right-subtree (which is called recursively) only

Note: Whether you need any modification then share with me in the comment section, I'll happy to help you.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
This is in java and you are not allowed to use Java API classes for queues,...
This is in java and you are not allowed to use Java API classes for queues, stacks, arrays, arraylists and linkedlists. You have to write your own implementations for them. You should construct a BST by inserting node values starting with a null tree. You can re-use the code for the insert method given in the sample code from the textbook. -insert method is provided below Your code should have a menu driven user interface at the command line with...
This assignment involves using a binary search tree (BST) to keep track of all words in...
This assignment involves using a binary search tree (BST) to keep track of all words in a text document. It produces a cross-reference, or a concordance. This is very much like assignment 4, except that you must use a different data structure. You may use some of the code you wrote for that assignment, such as input parsing, for this one. Remember that in a binary search tree, the value to the left of the root is less than the...
The main goal is to implement two recursive methods, each is built to manipulate linked data...
The main goal is to implement two recursive methods, each is built to manipulate linked data structures. To host these methods you also have to define two utterly simplified node classes. 1.) Add a class named BinaryNode to the project. This class supports the linked representation of binary trees. However, for the BinaryNode class Generic implementation not needed, the nodes will store integer values The standard methods will not be needed in this exercise except the constructor 2.) Add a...
JAVA please Arrays are a very powerful data structure with which you must become very familiar....
JAVA please Arrays are a very powerful data structure with which you must become very familiar. Arrays hold more than one object. The objects must be of the same type. If the array is an integer array then all the objects in the array must be integers. The object in the array is associated with an integer index which can be used to locate the object. The first object of the array has index 0. There are many problems where...
What tools could AA leaders have used to increase their awareness of internal and external issues?...
What tools could AA leaders have used to increase their awareness of internal and external issues? ???ALASKA AIRLINES: NAVIGATING CHANGE In the autumn of 2007, Alaska Airlines executives adjourned at the end of a long and stressful day in the midst of a multi-day strategic planning session. Most headed outside to relax, unwind and enjoy a bonfire on the shore of Semiahmoo Spit, outside the meeting venue in Blaine, a seaport town in northwest Washington state. Meanwhile, several members of...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT