Given BinaryNode.java
package util;
import java.util.*;
/*
This class implements Binary Node for Binary Trees
Methods:
I) Two constructors
II) Two toString methods
III) height and size methods
IV) preorder, postorder, and inorder traversals
V) BFS and DFS traversal
*/
public class BinaryNode {
public E element;//data
public BinaryNode left;//left child
public BinaryNode right;//right child
//constructor for leaves
public BinaryNode(E element) {
this(element, null, null);
}
//constructor for internal nodes
public BinaryNode(E element, BinaryNode left, BinaryNode right) {
this.left = left;
this.right = right;
this.element = element;
}
public String toString2() {
if (left == null && right == null)//base case
return element + "";
return element + "(" + left + ", " + right + ")";
}
public int height() {
if (left == null && right == null)
return 0;
if (left == null)
return 1 + right.height();
if (right == null)
return 1 + left.height();
return 1 + Math.max(left.height(), right.height());
}
public int size() {
int size = 1;//counting root
if (left != null)//counting left subtree nodes
size += left.size();
if (right != null)//counting right subtree nodes
size += right.size();
return size;
}
public void printPreOrder() {
System.out.print(element + " ");
if (left != null)
left.printPreOrder();
if (right != null)
right.printPreOrder();
}
public void printPostOrder() {
if (left != null)
left.printPostOrder();
if (right != null)
right.printPostOrder();
System.out.print(element + " ");
}
public void printInOrder() {
if (left != null)
left.printInOrder();
System.out.print(element + " ");
if (right != null)
right.printInOrder();
}
public void printBFS() {//breadth first search traversal - non-recursive method
Queue q = new LinkedList<>();
q.add(this);
while (!q.isEmpty()) {
BinaryNode cur = q.remove();
System.out.print(cur.element + " ");
if (cur.left != null)
q.add(cur.left);
if (cur.right != null)
q.add(cur.right);
}
}
public void printDFS() {//depth first search traversal - equivalent to pre-order traversal
Stack stack = new Stack<>();
stack.add(this);
while (!stack.empty()) {
BinaryNode cur = stack.pop();
System.out.print(cur.element + " ");
if (cur.right != null)
stack.push(cur.right);
if (cur.left != null)
stack.push(cur.left);
}
}
public String toString() {
if (left == null && right == null && element == null)
return "";
Queue list = new LinkedList<>();
String result = "";
list.add(this);
list.add(null);
int level = (int) Math.pow(2, height());
BinaryNode dummy = new BinaryNode(null);
while (!list.isEmpty()) {
boolean allDummies = true;
for (BinaryNode b : list)
if (b != dummy && b != null) {
allDummies = false;
break;
}
BinaryNode cur = list.remove();
if (cur == null || allDummies)
break;
for (int i = 0; i < level - 1; i++)
result += '\t';
if (cur != dummy)
result += cur.element;
for (int i = 0; i < level + 1; i++)
result += '\t';
if (cur.left != null)
list.add(cur.left);
else
list.add(dummy);
if (cur.right != null)
list.add(cur.right);
else
list.add.
Major project report on Tata Motors and its marketing strategies
Given BinaryNode.javapackage util;import java.util.;T.pdf
1. Given BinaryNode.java
package util;
import java.util.*;
/*
This class implements Binary Node for Binary Trees
Methods:
I) Two constructors
II) Two toString methods
III) height and size methods
IV) preorder, postorder, and inorder traversals
V) BFS and DFS traversal
*/
public class BinaryNode {
public E element;//data
public BinaryNode left;//left child
public BinaryNode right;//right child
//constructor for leaves
public BinaryNode(E element) {
this(element, null, null);
}
//constructor for internal nodes
public BinaryNode(E element, BinaryNode left, BinaryNode right) {
this.left = left;
this.right = right;
this.element = element;
}
public String toString2() {
2. if (left == null && right == null)//base case
return element + "";
return element + "(" + left + ", " + right + ")";
}
public int height() {
if (left == null && right == null)
return 0;
if (left == null)
return 1 + right.height();
if (right == null)
return 1 + left.height();
return 1 + Math.max(left.height(), right.height());
}
public int size() {
int size = 1;//counting root
if (left != null)//counting left subtree nodes
size += left.size();
if (right != null)//counting right subtree nodes
size += right.size();
return size;
}
public void printPreOrder() {
System.out.print(element + " ");
if (left != null)
left.printPreOrder();
if (right != null)
right.printPreOrder();
}
public void printPostOrder() {
if (left != null)
left.printPostOrder();
if (right != null)
3. right.printPostOrder();
System.out.print(element + " ");
}
public void printInOrder() {
if (left != null)
left.printInOrder();
System.out.print(element + " ");
if (right != null)
right.printInOrder();
}
public void printBFS() {//breadth first search traversal - non-recursive method
Queue q = new LinkedList<>();
q.add(this);
while (!q.isEmpty()) {
BinaryNode cur = q.remove();
System.out.print(cur.element + " ");
if (cur.left != null)
q.add(cur.left);
if (cur.right != null)
q.add(cur.right);
}
}
public void printDFS() {//depth first search traversal - equivalent to pre-order traversal
Stack stack = new Stack<>();
stack.add(this);
while (!stack.empty()) {
BinaryNode cur = stack.pop();
System.out.print(cur.element + " ");
if (cur.right != null)
stack.push(cur.right);
if (cur.left != null)
stack.push(cur.left);
}
4. }
public String toString() {
if (left == null && right == null && element == null)
return "";
Queue list = new LinkedList<>();
String result = "";
list.add(this);
list.add(null);
int level = (int) Math.pow(2, height());
BinaryNode dummy = new BinaryNode(null);
while (!list.isEmpty()) {
boolean allDummies = true;
for (BinaryNode b : list)
if (b != dummy && b != null) {
allDummies = false;
break;
}
BinaryNode cur = list.remove();
if (cur == null || allDummies)
break;
for (int i = 0; i < level - 1; i++)
result += 't';
if (cur != dummy)
result += cur.element;
for (int i = 0; i < level + 1; i++)
result += 't';
if (cur.left != null)
list.add(cur.left);
else
list.add(dummy);
if (cur.right != null)
list.add(cur.right);
else
list.add(dummy);
if (list.peek() == null) {
5. for (int i = 0; i < height(); i++)
result += 'n';
list.remove();
list.add(null);
level /= 2;
}
}
return result + "n";
}
}
i need the main methods to test the following classes, countGreaterThanValue, isSymmetric, and
removeLeaves. i keep getting errors and cannot successfully run the program to test individually
package part;
import util.*
import java.util.*
import util.BinaryNode;
public class Question1 {
public static int countGreaterThanValue(BinaryNode root, int value) {
if (root == null) {
return 0;
}
int count = 0;
if (root.element > value) {
count = 1;
}
count += countGreaterThanValue(root.left, value);
6. count += countGreaterThanValue(root.right, value);
return count;
}
}
package part;
import util.BinaryNode;
public class Question2 {
public static boolean isSymmetric(BinaryNode root) {
if (root == null) {
return true;
}
return isMirror(root.left, root.right);
}
private static boolean isMirror(BinaryNode left, BinaryNode right) {
if (left == null && right == null) {
return true;
}
if (left == null || right == null) {
return false;
}
return (left.element.equals(right.element)) &&
isMirror(left.left, right.right) &&
isMirror(left.right, right.left);
}
}
package part;
7. import util.BinaryNode;
public class Question3 {
public static BinaryNode removeLeaves(BinaryNode root) {
if (root == null) {
return null;
}
if (root.left == null && root.right == null) {
return null;
}
root.left = removeLeaves(root.left);
root.right = removeLeaves(root.right);
return root;
}
}