Question

Using the attached files, TreeAssignment.java and IntTree.java Add to the methods in IntTree so that the...

Using the attached files, TreeAssignment.java and IntTree.java Add to the methods in IntTree so that the sumNodes methods returns the sum of all the integers in the tree. countLeftNodes should count up all the left children in the tree.

Provided in IntTree.java is an example to help you called countEvenBranches which counts all the branches (nodes with children) with even values, but not leaves (nodes with no children) with even values.

Both methods return an integer. The tree generated is random so each run will be slightly different.

TreeAssignment.java

public class TreeAssignment {

  
  
  
   // DO NOT CHANGE LINES IN MAIN- YOU MAY ADD AT BOTTOM BUT NOT REMOVE ANY LINES
   public static void main(String[] args) {

  
  
// create int tree with 10 random elements
       // you may make this smaller for testing
IntTree theTree = new IntTree(3);
      

// print the tree
theTree.printStructure();
  
// call already developed routine to count even branches - this
// counts branches with even nodes, not even leaf nodes
int evenCount = theTree.countEvenBranches();
System.out.println("There are " + evenCount + " even branches");
  
       // call user developed routine to count left Nodes
int leftNodes = theTree.countLeftNodes();
  
System.out.println("The number of left nodes is " + leftNodes);
  
// call second user developmed routine to sum the values of all the integers in the tree
int totalSum = theTree.sumNodes();
System.out.println("The total sum is " + totalSum);

   }

}

Homework Answers

Answer #1

public class IntTree

{ //we need to give refrence to the methods.Assuming theTree is root node and is global.

public int sumNodes(IntTree theTree)

{ if(theTree==NULL)

return 0;

return(theTree.key + (theTree.left) + (theTree.right);

}

public int countLeftNodes(IntTree theTree)

{

int count = 0;

if(theTree.left!=NULL)

count=count+1+countLeftNodes(theTree.left);

if(theTree.right ! =NULL)

count=count+1+countLeftNodes(theTree.right);

retrun count;

}

public int countEvenNodes(IntTree theTree)

{ if(theTree == NULL)

return 0;

else

{

int count = 0;

if((theTree.left != NULL || theTree.right !=NULL) && theTree.data%2==0)

count=count+1+countEvenNodes(theTree.left)+countEvenNodes(theTree.right);

return count;

}

}

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
Modify the CountByFives application so that the user enters the value to count by. Start each...
Modify the CountByFives application so that the user enters the value to count by. Start each new line after 10 values have been displayed. (Cengage Mindtap assignment). This is the java code we are given: public class CountByAnything { // Modify the code below public static void main (String args[]) { final int START = 5; final int STOP = 500; final int NUMBER_PER_LINE = 50; for(int i = START; i <= STOP; i += START) { System.out.print(i + "...
You are given the following Java files: EvaluateExprission that you will use to add a method....
You are given the following Java files: EvaluateExprission that you will use to add a method. In EvaluateExprission.java write a recursive method    public static int EvaluateE (int x, int y) The method should evaluate the expression bellow and take the values x, y from the user. Update the main to call EvaluateE method properly . E = xy +x* (x-1)(y-1) +x* (x-2)(y-2) + . . . +1
Task #4 Calculating the Mean Now we need to add lines to allow us to read...
Task #4 Calculating the Mean Now we need to add lines to allow us to read from the input file and calculate the mean. Create a FileReader object passing it the filename. Create a BufferedReader object passing it the FileReader object. Write a priming read to read the first line of the file. Write a loop that continues until you are at the end of the file. The body of the loop will: convert the line into a double value...
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");                ...
Java Program: You will be inserting values into a generic tree, then printing the values inorder,...
Java Program: You will be inserting values into a generic tree, then printing the values inorder, as well as printing the minimum and maximum values in the tree. Given main(), write the methods in the 'BSTree' class specified by the // TODO: sections. There are 5 TODOs in all to complete. Ex: If the input is like ferment bought tasty can making apples super improving juice wine -1 the output should be: Enter the words on separate lines to insert...
IN JAVA Complete the following program. Add codes in the main method to:1) call method average(double[]...
IN JAVA Complete the following program. Add codes in the main method to:1) call method average(double[] gpaarr ) to calculate the average of gpaArray; 2) add codes in the for loop to calculate sum 3) print the average . public class Test { public static void main (String args[]) { double[ ] gpaArray = { 2.5, 4.0, 3.8, 3.2, 2.9, 4.0 } ;   //add your codes here (you can put your codes in the Answer area with/without copying the original...
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...
1) Consider the following Java program. Which statement updates the appearance of a button? import java.awt.event.*;...
1) Consider the following Java program. Which statement updates the appearance of a button? import java.awt.event.*; import javax.swing.*; public class Clicker extends JFrame implements ActionListener {     int count;     JButton button;     Clicker() {         super("Click Me");         button = new JButton(String.valueOf(count));         add(button);         button.addActionListener(this);         setSize(200,100);         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);         setVisible(true);     }     public void actionPerformed(ActionEvent e) {         count++;         button.setText(String.valueOf(count));     }     public static void main(String[] args) { new Clicker(); } } a. add(button);...
1) Consider the following Java program, which one of the following best describes "setFlavor"? public class...
1) Consider the following Java program, which one of the following best describes "setFlavor"? public class Food {     static int count;     private String flavor = "sweet";     Food() { count++; }     void setFlavor(String s) { flavor = s; }     String getFlavor() { return flavor; }     static public void main(String[] args) {         Food pepper = new Food();         System.out.println(pepper.getFlavor());     } } a. a class variable b. a constructor c. a local object variable d....
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...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT