Question: complete this: Please keep all the existing codes and imeplement the functions that are not completed. import java.util.LinkedList; import java.util.Queue; import java.util.function.Consumer; docs.oracle.com / javase

complete this:
Please keep all the existing codes and imeplement the functions that are not completed.
import java.util.LinkedList;
import java.util.Queue;
import java.util.function.Consumer; docs.oracle.com/javase/8/docs/api/java/util/function/Consumer.html
public class BinaryTree>{
protected class Node>{
T data;
Node leftChild;
Node rightChild;
Node parent;
Node(){
leftChild = null;
rightChild = null;
parent = null;
}
Node(Node theParent){
leftChild = null;
rightChild = null;
parent = theParent;
}
}
protected Node root;
protected Boolean verbose;
BinaryTree(){
root = null;
verbose = false;
}
public void setVerbose(){
verbose = true;
}
public void unsetVerbose(){
verbose = false;
}
public int numberOfNodes(){
// note that root.left and root.right
return numberOfNodes(root);
}
int numberOfNodes(Node t){
if (null == t){
return 0;
}
return 1+ numberOfNodes(t.leftChild)+ numberOfNodes(t.rightChild);
}
public int height(){
return height(root);
}
int height(Node t){
if (null == t){
return -1;
}
return 1+ Math.max(height(t.leftChild), height(t.rightChild));
}
public boolean isEmpty(){
return (null == root);
}
public void clear(){
// yay for garbage collection
root = null;
}
enum DfsType {
PREFIX,
INFIX,
POSTFIX
}
void recursiveDfs(DfsType dfs, Node t, Consumer actionClass){
if (null == t){
return;
}
if (dfs.equals(DfsType.PREFIX)){
actionClass.accept(t.data);
}
recursiveDfs(dfs, t.leftChild, actionClass);
if (dfs.equals(DfsType.INFIX)){
actionClass.accept(t.data);
}
recursiveDfs(dfs, t.rightChild, actionClass);
if (dfs.equals(DfsType.POSTFIX)){
actionClass.accept(t.data);
}
}
public void dfsPrefix(Consumer actionObject){
recursiveDfs(DfsType.PREFIX, root, actionObject);
}
public void dfsInfix(Consumer actionObject){
recursiveDfs(DfsType.INFIX, root, actionObject);
}
public void dfsPostfix(Consumer actionObject){
recursiveDfs(DfsType.POSTFIX, root, actionObject);
}
public void bfs(Consumer actionObject){
if (root == null){
return;
}
Queue> queue = new LinkedList<>();
queue.add(root);
while (!queue.isEmpty()){
Node current = queue.poll();
actionObject.accept(current.data);
if (current.leftChild != null){
queue.add(current.leftChild);
}
if (current.rightChild != null){
queue.add(current.rightChild);
}
}
}
StringBuilder reversedInorder(Node nd, int indent){
var rv = new StringBuilder();
if (null != nd){
if (null != nd.rightChild){
rv.append(reversedInorder(nd.rightChild, indent +4));
}
for (int i =0; i < indent +5; ++i){
rv.append("");
}
rv.append("("+(indent /4)+")");
rv.append(nd.data);
rv.append("
");
if (null != nd.leftChild){
rv.append(reversedInorder(nd.leftChild, indent +4));
}
}
return rv;
}
@Override
public String toString(){
return reversedInorder(root,0).toString();
}
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Accounting Questions!