Question: QUICK QUESTION. I have only a problem for the System.out.println(st.retrieve()); on this code , itputs 1235[J@15db9742 . It should be 1235. Please help me fix
QUICK QUESTION. I have only a problem for the System.out.println(st.retrieve()); on this code , itputs 1235[J@15db9742 . It should be 1235. Please help me fix it!
package lab;
public class Node{
Node left;
Node right;
long val;
Node(long val_){ // constructor
val = val_;
}}
package lab;
public class SortTree{
Node root;
int treeSize;
public boolean isPresent(double value)
{
Node curr = root;
while(curr!=null)
{
if(curr.val==value)
{
return true;
}
else if(curr.val > value){ // look at left subtree
curr = curr.left;
}
else{
curr = curr.right; // go to right subtree
}}
return false;
}
public boolean insert(int value)
{
Node newNode = new Node(value);
if (root == null) {
root = newNode;
treeSize++; // tree is empty
return true;
}
Node curr = root;
Node parent = null;
while (true)
{
parent = curr;
if (value <= curr.val)
{
if (value == curr.val)
return false; // duplicate
curr = curr.left;
if (curr == null) {
parent.left = newNode;// left node
treeSize++;
return true;
}
} else {
curr = curr.right;
if (curr == null) {
parent.right = newNode;// right node
treeSize++;
return true;
}}}}
public int getCount()
{
return treeSize; // Return tree size
}
public long[] retrieve()
{
long[] array = new long[treeSize];
inorder(root, array, 0);
for(int i=0;i } return array; } public void inorder(Node root, long[] array, int index) { if(root!=null) { inorder(root.left, array, index); System.out.print(root.val); array[index++] = root.val; inorder(root.right, array, index); }}} package lab; public class Main { public static void main(String[] args) { SortTree st = new SortTree(); st.insert(2); st.insert(1); st.insert(3); st.insert(5); System.out.println(st.getCount()); System.out.println(st.isPresent(6)); System.out.println(st.isPresent(4)); st.insert(1); // duplicate System.out.println(st.getCount()); System.out.println(st.retrieve()); } } ------------------------------------------------------------------------ 4 false false 4 1235[J@15db9742
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
