Answered step by step
Verified Expert Solution
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 countGreaterThanValueBinaryNode 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 BinaryNodeE element
thiselement null, null;
constructor for internal nodes
public BinaryNodeE element, BinaryNode left, BinaryNode right
this.left left;
this.right right;
this.element element;
@Override
public String toString
ifleft null && right nullbase case
return element ;
return element left right ;
public int height
ifleft null && right null
return ;
ifleft null
return right.height;
ifright null
return left.height;
return Math.maxleftheight right.height;
public int size
int size ;counting root
ifleft nullcounting left subtree nodes
size left.size;
ifright nullcounting right subtree nodes
size right.size;
return size;
public void printPreOrder
System.out.printelement ;
ifleft null
left.printPreOrder;
ifright null
right.printPreOrder;
public void printPostOrder
ifleft null
left.printPostOrder;
ifright null
right.printPostOrder;
System.out.printelement ;
public void printInOrder
ifleft null
left.printInOrder;
System.out.printelement ;
ifright null
right.printInOrder;
public void printBFSbreadth first search traversal nonrecursive method
Queue q new LinkedList;
qaddthis;
whileqisEmpty
BinaryNode cur qremove;
System.out.printcurelement ;
ifcurleft null
qaddcurleft;
ifcurright null
qaddcurright;
public void printDFSdepth first search traversal equivalent to preorder traversal
Stack stack new Stack;
stack.addthis;
whilestack.empty
BinaryNode cur stack.pop;
System.out.printcurelement ;
ifcurright null
stack.pushcurright;
ifcurleft null
stack.pushcurleft;
public String toString
ifleft null && right null && element null
return ;
Queue list new LinkedList;
String result ;
list.addthis;
list.addnull;
int level int Math.pow height;
BinaryNode dummy new BinaryNodenull;
whilelist.isEmpty
boolean allDummies true;
forBinaryNode b: list
ifb dummy && b null
allDummies false;
break;
BinaryNode cur list.remove;
ifcur null allDummies
break;
forint i ; i level ;i
result t;
ifcur dummy
result cur.element;
forint i ; i level ;i
result t;
ifcurleft null
list.addcurleft;
else
list.adddummy;
ifcurright null
list.addcurright;
else
list.adddummy;
iflistpeek null
forint i ; i height;i
result
;
list.remove;
list.addnull;
level;
return result
;
:
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started