Question: I'm supposed to write a program that implements a binary tree. The program has to use an array and fill the elements from the array

I'm supposed to write a program that implements a binary tree. The program has to use an array and fill the elements from the array into a tree structure.

This is what I have so far:

Class 1:

public class Node { private int value; private Node left; private Node right; private boolean isRoot=false; public Node(int value){ this.value=value; } public int getValue() { return value; } public void setValue(int value) { this.value = value; } public Node getLeft() { return left; } public void setLeft(Node left) { this.left = left; } public Node getRight() { return right; } public void setRight(Node right) { this.right = right; } public boolean isRoot() { return isRoot; } public void setRoot(boolean isRoot) { this.isRoot = isRoot; } }

Class 2:

public class Tree { public Tree(int[] array){ Node root= new Node(array[0]); root.setRoot(true); Node current=root; int level=0; for(int i=1;i<=array.length;i++){ if(array[i]>current.getValue()){ current.setLeft(new Node(array[i])); current=current.getLeft(); level++; } else{ current.setRight(new Node(array[i])); current=current.getRight(); level++; } } } }

I'm not too sure what to do after this and I need some help.

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 Databases Questions!