In: Computer Science
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;
}
}
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