Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a method with signature public static int countGreaterThanValue ( BinaryNode root, int value ) which gets a Binary Tree ( not BST necessarily! )

Write a method with signature public static int countGreaterThanValue(BinaryNode root, int value) which gets a Binary Tree (not BST necessarily!) and a value as input parameters. It counts the number of values greater than value in the tree and returns the counter.
Starter Codepackage 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;
}
@Override
public String toString(){
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 toString2(){
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){
for(int i =0; i < height();i++)
result +='
';
list.remove();
list.add(null);
level/=2;
}
}
return result +"
";
}
}:

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases Demystified

Authors: Andrew Oppel

1st Edition

0072253649, 9780072253641

More Books

Students also viewed these Databases questions

Question

Would you be willing to work with them?

Answered: 1 week ago

Question

5. Explain how to conduct an appraisal feedback interview.

Answered: 1 week ago

Question

2. Answer the question, Who should do the appraising?

Answered: 1 week ago