Can someone help me find the issue? I am trying to assemble a binary search tree from a list I am new to all of this sorry It trys to assemble A node list then makes a queue from the output and uses the queue to assemble the tree
Here is the code
Main.java
static List<Node> assembleNodeList(List<Double> items) {
ArrayList<Node> nodelist = new ArrayList<Node>();
for (int i = 0; i < items.size(); i++) {
nodelist.add(new Node(items.get(i)));
}
return nodelist;
}
static Queue assembleQueue(List<Node> nodelist) {
Queue<Node> queue = new LinkedList();
for (int i = 0;i<nodelist.size();i++) {
queue.add(nodelist.get(i));
}
return queue;
}
static BinaryTree assembleTree(Queue<Node> queue,BinaryTree tree,Node current) {
if (queue.isEmpty()) {
return tree;
}
if (tree.root == null) {
tree.root = queue.peek();
queue.remove();
}
Node item = queue.peek();
if (item.value < current.value) {
current.left = item;
queue.remove();
} else if (item.value >= current.value) {
current.right = item;
queue.remove();
}
return assembleTree(queue,tree,current);
}
public static void main(String[] args) {
BinaryTree tree = new BinaryTree();
double targetVal = 0.5;
tree.root = new Node(5);
tree.root.left = new Node(3);
tree.root.right = new Node(7);
tree.root.left.left = new Node(1);
tree.root.left.left.left = new Node(0.5);
tree.printInorder(tree.root);
try {
System.out.println(treeTraverse(targetVal,tree,tree.root));
} catch (NullPointerException e) {
System.out.println(false);
}
}
}