Question

In: Computer Science

In Java Write a method countOddInternalNodes that returns the number of internal nodes in a binary...

In Java

Write a method countOddInternalNodes that returns the number of internal nodes in a binary tree that contain odd numbers.

You are essentially writing a method that will become part of the IntegerTree class.

You may define private helper methods to solve this problem.

Make your own trees in the main method of the code. I specifically kept vague the way the nodes are being added to the tree so you can practice building your own trees from scratch and testing your code on them.

Example

If a variable tree stores a reference to the following tree:

          +---+
          | 2 |
          +---+
         /     \
     +---+     +---+
     | 8 |     | 1 |
     +---+     +---+
    /         /     \
+---+     +---+     +---+
| 3 |     | 7 |     | 6 |
+---+     +---+     +---+
         /               \
     +---+               +---+
     | 4 |               | 9 |
     +---+               +---+

Then the call tree.countOddInternalNodes(); should return 2 because there are two branch nodes with even values (1 and 7).

Notice that the leaf nodes with odd values are not included (the nodes storing 3 and 9).

Resources

Watch this video on how to solve a similar problem.

Testing

As part of the exercise, you should construct the main function to test your code.

Add the nodes to the tree manually in the main method.

In doing so, you can test examples like the tree given in the example above.

A default array like the one below is given for you to test your tree on, but you have no way of knowing how the elements in that array are going to be inserting in the tree. Therefore, you should make your own trees and test your code on them.

Integer Tree class(No change needed)

// This class represents a tree of integers
public class IntegerTree {
private IntegerTreeNode root;

// Constructs a tree
public IntegerTree() {
root = null;
}
// Getters
public IntegerTreeNode getRoot() {
   return root;
}
// Setters
public void setRoot(IntegerTreeNode node) {
   root = node;
}
}

Integer Tree Node class(No change needed)

// Class that represents a single node in the tree
public class IntegerTreeNode {
public int data; // data stored at this node
public IntegerTreeNode left; // reference to left subtree
public IntegerTreeNode right; // reference to right subtree

// Constructs a leaf node with the given data.
public IntegerTreeNode(int data) {
this(data, null, null);
}

// Constructs a leaf or branch node with the given data and links.
public IntegerTreeNode(int data, IntegerTreeNode left, IntegerTreeNode right) {
this.data = data;
this.left = left;
this.right = right;
}
}

Main class

import java.util.*;

public class Main {
public static void main (String [] args) {
//Variables
int [] defaultArr = {33,45,2,21,46,3,1,32,11,211,10};
  
/*
* Enter code here to build your own trees manually and test the countOddInternalNodes() method on them
*/

}
}

OddNodeCounter class

public class OddNodeCounter extends IntegerTree {
public OddNodeCounter(){
super();
}
public int countOddInternalNodes(IntegerTreeNode root){
//Enter code here

}
}

Solutions

Expert Solution

HI,

Please find updated code below.

Main.java:

import java.util.*;

public class Main
{
public static void main (String [] args)
{
//Variables
int [] defaultArr = {33,45,2,21,46,3,1,32,11,211,10};
IntegerTree myTree = new IntegerTree();
OddNodeCounter oddCOunter = new OddNodeCounter();
/*
* Enter code here to build your own trees manually and test the countOddInternalNodes() method on them
*/
//IntegerTreeNode myTree = myTree.getRoot();
IntegerTreeNode root = myTree.getRoot();
root = myTree.insert(root, defaultArr[0]);
myTree.setRoot(root);
  
for(int i =1;i<defaultArr.length;i++)
{
root = myTree.insert(myTree.getRoot(), defaultArr[i]);
}
myTree.inOrder(myTree.getRoot());
myTree.printinTreeFormat(myTree.getRoot());
int noOfOddInternalNodes = oddCOunter.countOddInternalNodes(root);
System.out.print("No of odd internal nodes are : " + noOfOddInternalNodes);
}
}

IntegerTree.java:

public class IntegerTree
{
private IntegerTreeNode root;
  
// Constructs a tree
public IntegerTree()
{
root = null;
}
// Getters
public IntegerTreeNode getRoot()
{
return root;
}
// Setters
public void setRoot(IntegerTreeNode node)
{
root = node;
}
static IntegerTreeNode newNode(int item)
{
IntegerTreeNode temp = new IntegerTreeNode(item);
temp.data = item;
temp.left = null;
temp.right = null;
return temp;
}
static IntegerTreeNode insert(IntegerTreeNode node, int key)
{
/* If the tree is empty, return a new node */
if (node == null)
{
System.out.println("first time");
return newNode(key);
}
  
/* Otherwise, recur down the tree */
if (key < node.data)
node.left = insert(node.left, key);
else
node.right = insert(node.right, key);
  
/* return the (unchanged) node pointer */
return node;
}
static void printinTreeFormatUtil(IntegerTreeNode root, int mySpace)
{
int COUNT = 11;
// Base case
if (root == null)
return;
  
mySpace += COUNT;
printinTreeFormatUtil(root.right, mySpace);
  
  
System.out.print("\n");
for (int i = COUNT; i < mySpace; i++)
System.out.print(" ");
System.out.print(root.data + "\n");
  
printinTreeFormatUtil(root.left, mySpace);
}
  
// Wrapper over printinTreeFormatUtil()
static void printinTreeFormat(IntegerTreeNode root)
{
// Pass initial space count as 0
printinTreeFormatUtil(root, 0);
}
  
public void inOrder(IntegerTreeNode root)
{
if (root != null)
{
//System.out.print("Im here");
inOrder(root.left);
System.out.print(root.data + " ");
inOrder(root.right);
}
}
}

IntegerTreeNode.java

public class IntegerTreeNode
{
public int data; // data stored at this node
public IntegerTreeNode left; // reference to left subtree
public IntegerTreeNode right; // reference to right subtree
  
// Constructs a leaf node with the given data.
public IntegerTreeNode(int data)
{
this(data, null, null);
}
  
// Constructs a leaf or branch node with the given data and links.
public IntegerTreeNode(int data, IntegerTreeNode left, IntegerTreeNode right)
{
this.data = data;
this.left = left;
this.right = right;
}
}

OddNodeCOunter.java

public class OddNodeCounter extends IntegerTree
{
int count =0;
public OddNodeCounter()
{
super();
}
boolean isLeaf(IntegerTreeNode node)
{
if (node == null)
return false;
if (node.right == null && node.left == null)
return true;
return false;
}
public int countOddInternalNodes(IntegerTreeNode root)
{
if (root != null)
{
countOddInternalNodes(root.left);
  
// if node is odd then print it
if (root.data % 2 != 0 && !isLeaf(root))
{
count++;
//System.out.println("Odd:" + root.data + " ");
}
countOddInternalNodes(root.right);
}
  
return count;
}
  
}

Output tree and output from program attached:

program output:

Thanks,

kindly upvote.


Related Solutions

java. Consider a binary tree with integer values in its nodes, implement a method that returns...
java. Consider a binary tree with integer values in its nodes, implement a method that returns the sum of the values contained in all of the nodes of the binary tree with root n.Hint: use recursion.
Write a O(n) method valuesInLevelOrder() that returns a list of the nodes of a binary tree...
Write a O(n) method valuesInLevelOrder() that returns a list of the nodes of a binary tree in level-order. That is, the method should return the root, then the nodes at depth 1, followed by the nodes at depth 2, and so on. Your algorithm should begin by putting the tree root on an initially empty queue. Then dequeue a node, add it to the output, and enqueue its left and right children (if they exist). Repeat until the queue is...
Assume that a given binary tree stores integer values in its nodes. Write a Java method...
Assume that a given binary tree stores integer values in its nodes. Write a Java method "smallest" that returns the smallest item in the tree.
3. Write a java method that accepts a binary number and converts it to decimal then...
3. Write a java method that accepts a binary number and converts it to decimal then display the result. For Example: (110)2 = (6)10 (2 2 *1)+ (21 *1) + (20*0) = 6 Additional task: write a method that accepts a decimal and converts it to binary. i need to solve it as soon as and i will upvote you directly
Write a solution to return the count of the number of nodes in a binary tree....
Write a solution to return the count of the number of nodes in a binary tree. Your method will be passed one parameter, a copy of a pointer to the root node of the tree (Node *) and will return an int that is the count of nodes. If the tree is empty, return 0 (this is the recursive base case).
Write a binary search tree and include the functions to find the number of nodes and...
Write a binary search tree and include the functions to find the number of nodes and the height of the tree. Test your code in main. Print the post-order, in-order and pre-order traversal. in c++ please.
Write a C++ program will read in the number of nodes and a binary relation representing...
Write a C++ program will read in the number of nodes and a binary relation representing a graph. The program will create an adjacency matrix from the binary relation. The program will then print the following : 1. The adjacency matrix 2. Determine if there are any isolated nodes and print them 3. Determine if an Euler path exists and said so. The sample run of the program is as follows. The output should just like this: It starts by...
Write a binary search tree with 10 nodes in Java, each node will have 3 attributes...
Write a binary search tree with 10 nodes in Java, each node will have 3 attributes (data, x, y). The binary tree need to have function "add()" to add new node into the tree. After added all 10 nodes, it will be sorted and turn into a balanced binary search tree.
IN JAVA Write a program with a method that returns an array. The method should accept...
IN JAVA Write a program with a method that returns an array. The method should accept as input a comma-delimited string with three values from a user. The array should store each value in a different element. Use Try..Catch error handling and print any failure messages, or print success from within method if the execution is successful (see Chapter 13 in the text). Call the method from the main method of the program to demonstrate its functionality by looping through...
(Binary Tree): Write a recursive implementation of the function, singleParent, that returns the number of the...
(Binary Tree): Write a recursive implementation of the function, singleParent, that returns the number of the nodes in a binary tree that have only one child. Convert it to an iterative version. in C++
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT