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
You are given a reference to the root node of a binary search tree, that implements...
You are given a reference to the root node of a binary search tree, that implements a dictionary data structure. Please print all the elements in depths 500 through 510, all in sorted order. A node in a binary search tree is at depth x, if it takes x hops to get from the root. So the root is at depth 0, the children of the root are at depth 1, and so on. The class TreeNode defines a single...
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...
import java.util.ArrayList; /* Rules:         1. Allow Tester to iterate through all nodes using the...
import java.util.ArrayList; /* Rules:         1. Allow Tester to iterate through all nodes using the in-order traversal as the default.             This means, in Tester the following code should work for an instance of this class             called bst that is storing Student objects for the data:                 BinarySearchTree_Lab08<String> bst = new BinarySearchTree_Lab08<String>();                 bst.add("Man");       bst.add("Soda");   bst.add("Flag");                 bst.add("Home");   bst.add("Today");   bst.add("Jack");                ...
Problem Definition: Problem: Given an array of integers find all pairs of integers, a and b,...
Problem Definition: Problem: Given an array of integers find all pairs of integers, a and b, where a – b is equal to a given number. For example, consider the following array and suppose we want to find all pairs of integers a and b where a – b = 3 A = [10, 4, 6, 16, 1, 6, 12, 13] Then your method should return the following pairs: 4, 1 15, 12 13, 10 A poor solution: There are...
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...
Delta airlines case study Global strategy. Describe the current global strategy and provide evidence about how...
Delta airlines case study Global strategy. Describe the current global strategy and provide evidence about how the firms resources incompetencies support the given pressures regarding costs and local responsiveness. Describe entry modes have they usually used, and whether they are appropriate for the given strategy. Any key issues in their global strategy? casestudy: Atlanta, June 17, 2014. Sea of Delta employees and their families swarmed between food trucks, amusement park booths, and entertainment venues that were scattered throughout what would...