Question

In: Computer Science

This is a java program I am trying to figure out how to add a couple...

This is a java program

I am trying to figure out how to add a couple methods to a program I am working on.

I need to be able to:

1. Remove elements from a binary tree

2. Print them in breadth first order

Any help would appreciated.

//start BinaryTree class

/**
*
* @author Reilly
* @param <E>
*/
public class BinaryTree {

TreeNode root = null;
TreeNode current, parent;
private int size = 0, counter = 0;

public int getSize() {
return this.size;
}

public boolean insert(int el) {
current = root;

if (current == null) {
root = new TreeNode(el);
} else {
parent = null;
current = root;

while (current != null) {
if (el < current.element) {
parent = current;
current = current.left;
} else if (el > current.element) {
parent = current;
current = current.right;
} else {
return false;
}
}

if (el < parent.element) {
parent.left = new TreeNode(el);
}
if (el > parent.element) {
parent.right = new TreeNode(el);
}
}

this.size++;
return true;
}

public boolean search(int el) {
current = root;
while (current != null) {
if (el < current.element) {
current = current.left;
} else if (el > current.element) {
current = current.right;
} else {
return true;
}
}
return false;
}

public void inorder() {
inorder(root);
System.out.print("\n");
}

public void inorder(TreeNode curRoot) {
if (curRoot == null) {
return;
}
inorder(curRoot.left);
System.out.print(curRoot.element + " ");
inorder(curRoot.right);
}

public void postorder() {
postorder(root);
System.out.print("\n");
}

public void postorder(TreeNode curRoot) {
if (curRoot == null) {
return;
}
postorder(curRoot.left);
postorder(curRoot.right);
System.out.print(curRoot.element + " ");
}

public void getNumberOfLeaves() {
if (oddOrEven(getNumberOfLeaves(root))) {
System.out.println("Even");
} else {
System.out.println("Odd");
}
}

int getNumberOfLeaves(TreeNode curRoot) {
if (curRoot == null) {
return 0;
}
if (curRoot.left == null && curRoot.right == null) {
return 1;
} else {
return getNumberOfLeaves(curRoot.left) + getNumberOfLeaves(curRoot.right);
}
}

public boolean oddOrEven(int x) {

return (x % 2) == 0;

}

}

//start Driver class

public class Driver {

public static void main(String[] args) {
BinaryTree tree = new BinaryTree();

tree.insert(10);
tree.insert(5);
tree.insert(15);
tree.insert(6);
tree.insert(4);
tree.insert(20);
  
tree.inorder();
tree.getNumberOfLeaves();

tree.inorder();
  
}
  
}

//start TreeNode class

public class TreeNode {
int element;
TreeNode left;
TreeNode right;
  
public TreeNode(int el)
{
this.element = el;
}
}

Solutions

Expert Solution

package myList;

import java.util.LinkedList;

/**

*

* @author Reilly

* @param <E>

*/

public class BinaryTree {

TreeNode root = null;

TreeNode current, parent;

private int size = 0, counter = 0;

public int getSize() {

return this.size;

}

public boolean insert(int el) {

current = root;

if (current == null) {

root = new TreeNode(el);

} else {

parent = null;

current = root;

while (current != null) {

if (el < current.element) {

parent = current;

current = current.left;

} else if (el > current.element) {

parent = current;

current = current.right;

} else {

return false;

}

}

if (el < parent.element) {

parent.left = new TreeNode(el);

}

if (el > parent.element) {

parent.right = new TreeNode(el);

}

}

this.size++;

return true;

}

public boolean search(int el) {

current = root;

while (current != null) {

if (el < current.element) {

current = current.left;

} else if (el > current.element) {

current = current.right;

} else {

return true;

}

}

return false;

}

public void inorder() {

inorder(root);

System.out.print("\n");

}

public void inorder(TreeNode curRoot) {

if (curRoot == null) {

return;

}

inorder(curRoot.left);

System.out.print(curRoot.element + " ");

inorder(curRoot.right);

}

public void postorder() {

postorder(root);

System.out.print("\n");

}

public void postorder(TreeNode curRoot) {

if (curRoot == null) {

return;

}

postorder(curRoot.left);

postorder(curRoot.right);

System.out.print(curRoot.element + " ");

}

public void getNumberOfLeaves() {

if (oddOrEven(getNumberOfLeaves(root))) {

System.out.println("Even");

} else {

System.out.println("Odd");

}

}

int getNumberOfLeaves(TreeNode curRoot) {

if (curRoot == null) {

return 0;

}

if (curRoot.left == null && curRoot.right == null) {

return 1;

} else {

return getNumberOfLeaves(curRoot.left) + getNumberOfLeaves(curRoot.right);

}

}

public boolean oddOrEven(int x) {

return (x % 2) == 0;

}

public TreeNode delete(TreeNode root, int key) {

if (root == null)

return null;

if (key < root.element)

root.left = delete(root.left, key);

else if (key > root.element)

root.right = delete(root.right, key);

else {

if (root.left == null)

return root.right;

if (root.right == null)

return root.left;

TreeNode x = root;

root = findMin(root.right);

root.right = delete(x.right, root.element);

root.left = x.left;

}

return root;

}

public static TreeNode findMin(TreeNode root) {

if (root == null)

return root;

while (root.left != null)

root = root.left;

return root;

}

public void delete(int key) {

if (root == null)

return;

delete(root, key);

}

public void levelOrderTraversal(TreeNode node) {

if (node == null)

return ;

LinkedList<TreeNode> queue = new LinkedList<TreeNode>();

queue.add(node);

while (!queue.isEmpty()) {

TreeNode tempNode = queue.poll();

System.out.println(tempNode.element + " ");

if (tempNode.left != null)

queue.add(tempNode.left);

if (tempNode.right != null)

queue.add(tempNode.right);

}

}

public void levelOrderTraversal() {

levelOrderTraversal(root);

}

}

// start Driver class

public class Driver {

public static void main(String[] args) {

BinaryTree tree = new BinaryTree();

tree.insert(10);

tree.insert(5);

tree.insert(15);

tree.insert(6);

tree.insert(4);

tree.insert(20);

tree.inorder();

tree.getNumberOfLeaves();

tree.inorder();

System.out.println("Deleting 20");

tree.delete(20);

System.out.println("Breadth first Traversal: ");

tree.levelOrderTraversal();

}

}

// start TreeNode class

public class TreeNode {

int element;

TreeNode left;

TreeNode right;

public TreeNode(int el) {

this.element = el;

}

}

===========================

SEE OUTPUT

Thanks, PLEASE COMMENT if there is any concern. PLEASE UPVOTE


Related Solutions

I am trying to figure out how to properly execute a while loop and a do/while...
I am trying to figure out how to properly execute a while loop and a do/while loop in the same program using java. I am stuck at just writing the while loop. I made a condition but I can't figure out how to get it to loop if the condition is true?? Also I have to write a do/while loop in the same program. here are the details: This lab demonstrates the use of the While Loop and the Do...
I am trying to figure out the social security tax for the employees in this problem,...
I am trying to figure out the social security tax for the employees in this problem, becuase I have to post it all in a chart. I have tried multiplying by the 6.2 tax rate but my program says my answer is wrong. So, is there a different way to calculate social security tax or is there something that I am missing that the problem is asking me to do? I am not looking for answers, but just a clear...
I am trying to figure out the best way to solving a problem in the language...
I am trying to figure out the best way to solving a problem in the language python. I have some but have no clue if I am even going in the right direction. Here are the instructions: Write a program that calculates the shopping list for a birthday party with the minimum amount of leftovers. The program should ask the user for the number of kids attending the party. Assume each kid will cook (but not necessarily eat) 2 hot...
I am trying to figure out how to calculate the sustainable earnings: Permanent Versus Transitory Earnings...
I am trying to figure out how to calculate the sustainable earnings: Permanent Versus Transitory Earnings Entrust, Inc., is a global provider of security software; it operates in one business segment involving the design, production, and sale of software products for securing digital identities and information. The consolidated statements of operations for a three-year period (all values in thousands) follows. On January 1, Year 1, the Entrust common shares traded at $10.40 per share; by year end Year 3, the...
I am trying to write a java program that determines if an inputted year is a...
I am trying to write a java program that determines if an inputted year is a leap year or not, but I am not able to use if else statements how would I do it. I don't need the code just advice.
I am trying to figure out the probability, expected value, variance, and standard deviation for a...
I am trying to figure out the probability, expected value, variance, and standard deviation for a series of dice rolls. For example, if I roll a six-sided die in an attempt to roll a 1, and it takes me 7 rolls before a 1 appears, what are those answers? I have figured out the probability equation: P(P-1)^x where x is the number of rolls - 1 so for 7 rolls the probability would be: 1/6(1-1/6)^6 = 0.05581632... Further where I...
I am trying to figure out which test analysis to use for my research questions. I...
I am trying to figure out which test analysis to use for my research questions. I was thinking about think about multivariate because of the number of variable being addressed in the study but there is also the possibility to use univariate to address each question. What are the current levels of police satisfaction in CMPD jurisdictions? What is the public’s perception of crime in CMPD jurisdictions? Does “hot spot” policing reduce crime in CMPD jurisdictions? How does broken windows...
I am having problems trying to figure out what information to pull to prepare budgets
I am having problems trying to figure out what information to pull to prepare budgets
I am supposed to map out the following and can't figure out how to do it!...
I am supposed to map out the following and can't figure out how to do it! Can somebody help? The experiment has to do with determining the simplest formula of potassium chlorate and to determine the original amount of potassium chlorate in a potassium chlorate-potassium chloride mixture by measuring the oxygen lost from decomposition. The chemical reaction is 2KClO3(s) ------> 2KCL(s) + 3 O2(g) I am supposed to map out 1. Mass of oxygen lost in the first part 2....
I am trying to write a program in Java: Pick 4 cards and display the cards...
I am trying to write a program in Java: Pick 4 cards and display the cards and their total value (Ace = 1, King = 13, Queen = 12, and Jack 11...etc. , BUT you should check that no card is a duplicate... there is only one "Ace of Spades" per deck for example. I need to utilize the displayMenu so the program continues to run for the user without breaking. My program is not running correctly. import java.util.Scanner; import...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT