Please help me with the following: package binaryTree; import iterators.Predicat
ID: 3705846 • Letter: P
Question
Please help me with the following:
package binaryTree;
import iterators.Predicate;
import iterators.ReduceFunction;
import java.util.*;
import static javafx.scene.input.KeyCode.T;
public class BinaryTree<T> {
// the root of the tree
private TreeNode<T> root;
// the queue of TreeNodes in the tree that have 0 or 1 children
private final List<TreeNode<T>> nodesThatNeedChildren;
// number of TreeNodes in the tree
public int size;
public BinaryTree() {
root = null;
nodesThatNeedChildren = new LinkedList<>();
size = 0;
}
/*
Insert the element d into the BinaryTree at the
"next available location" starting with the left
*/
public void insertNode(T d){
/*
Insert the element d into the BinaryTree at the
"next available location" starting with the left
*/
}
Not sure how to complete the "insertNode"
Explanation / Answer
import iterators.Predicate;
import iterators.ReduceFunction;
import java.util.*;
import static javafx.scene.input.KeyCode.T;
public class BinaryTree<T> {
// the root of the tree
private TreeNode<T> root;
// the queue of TreeNodes in the tree that have 0 or 1 children
private final List<TreeNode<T>> nodesThatNeedChildren;
// number of TreeNodes in the tree
public int size;
public BinaryTree() {
root = null;
nodesThatNeedChildren = new LinkedList<TreeNode<T>>();
size = 0;
}
/*
Insert the element d into the BinaryTree at the
"next available location" starting with the left
*/
public void insertNode(T d){
/*
Insert the element d into the BinaryTree at the
"next available location" starting with the left
*/
TreeNode<T> temp;
Queue<TreeNode<T>> q = new LinkedList<TreeNode<T>();//creating a queue
q.add(root);//adding tree
//level order traversal until we find
// next avialable location
while (!q.isEmpty()) //runs upto q is not empty
{
temp = q.peek();
q.remove();
//finding next available location
if (temp.left == null) {
temp.left = new TreeNode<T>(d);//inserting..in location
break;
} else
q.add(temp.left);
if (temp.right == null) {
temp.right = new TreeNode<T>(d);//inserting in location...
break;
} else
}