1

NOTE: This IS homework, so please do not give me the answer right out, but pointers would be helpful.

Assignment overview:

(Test each method to ensure that it works properly.)

  1. Generate a binary search with 100 elements added in the correct order - add the numbers 1 through 100 to the tree in the correct order add 1, then 2, then 3, etc.

  2. Generate a binary search with 100 elements add in revers order - add the numbers 100, 99, 98 and so on.

  3. Generate a binary search with 100 randomly generated elements.

The reason I said a linear array in the question is because the root has to be the first element in the array and then add the rest.

I do not have all the other methods required to properly test if the tree was made properly, and I am about to leave for work. However, I am posting below the start of the algorithm that I think will work, just not sure.

public BST(int[] numbers)
{
    node root;
    node currentParent; 
    node previousParent = null; 
    int i = 1; // skip the root, that will be handled manualy 

    root = new node(numbers[0]); // first element is the root
    currentParent = root;

    // handle right hand side of binary tree first
    while (i > root.getValue())
    {
        while (i > currentParent.getValue())
        {
            node newNode = new node (numbers[i]);
            currentParent.setRightChild(newNode);
            previousParent = currentParent;
            currentParent = newNode;
            i++;
        } // end inner while

        if (previousParent.leftChild == null)
        {
            node newNode = new node(numbers[i]);
            previousParent.leftChild =  newNode;
            currentParent = newNode;
            i++;
        } // end if branch
        else
        {
            System.out.println("Hmm, something seems to be wrong!");
        } // end else
    } // end right side binary tree while
} // end integer array constructor

This is pretty much the whole algorithm, I would have to do it again but reverse the < and > signs (if this is correct). Am I on the right path with this algorithm?

Adding the node class as requested.

public class node 
{
node leftChild; // left pointer
node rightChild; // right pointer
int value; // will be the int value inside the node
boolean isLeaf; // boolean for determing if a node is a left node or not

/*
    Null constructor for the class.  Sets the pointers to null 
*/
public node ()
{
    leftChild = null;
    rightChild = null; 
} // end null constructor


/*
    Intialzing constructor that will take a single integer as input
*/
public node (int number)
{
    leftChild = null;
    rightChild = null;
    value = number; 
} // end intialzing constructor with int input


/*
    The setValue method will take the int value inputted from the array into 
    a new node 
*/
public void setValue(int number)
{
    value = number;
} // end setValue()


/*
    The getValue method will return to the user the value that is stored 
    inside a specific node.
*/
public int getValue()
{
    return value; 
} // end getValue()


/*
    The setLeftChild method will set the pointers to the nodes left child 
    which will be a value that is equal or lower to the paret node
*/
public void setLeftChild (node leftChild)
{
    this.leftChild = leftChild;
} // end setLeftChild()


/*
    The setRightChild method will be the same as setLeftChild above but will
    handle setting the nodes right child.  The right child will be a value 
    that is greater than the parent node
*/
public void setRightChild(node rightChild)
{
    this.rightChild = rightChild; 
} // end setRightChild


/*
    the getLeftChild method will return to the user/calling method what the
    left child of a given node is. 
*/
public node getLeftChild (node currentNode)
{
    return leftChild;
} // end getLeftChild()


/*
    the getRightChild method is exactly the same as the getLeftChil method 
    above, except it will return the right child instead
*/
public node getRightChild(node currentNode)
{
    return rightChild; 
} // end getRightChild()

/*
    The setLeaf method will be in charge of manipulating a boolean value and
    setting it to true if the node is a left or not
*/
public void setLeaf(boolean leaf)
{
    if (leaf == true)
    {
        isLeaf = true;
    } // end if
    else
    {
        isLeaf = false;
    } // end else
} // end setLeaf

/*
    The getLeaft method will simply return a boolean value that indicates if
    the given node is a left node or not
*/
public boolean getLeaf()
{
    return isLeaf;
} // end getLeaf()
} // end node class
4
  • Could you paste the code for your node object? Also, the assignment sounds like you need to do something like tree.add(1); tree.add(2); instead of having a constructor make the tree from an int[]. Commented Apr 23, 2015 at 14:18
  • @Captain Man I added the node class. and although I do need an insert method I do require a constructor with int array as a parameter Commented Apr 23, 2015 at 14:32
  • @Jeffery Oh, I see. Does this tree need to be "balanced" or "self balancing"? That's something to consider. Since you have to have an insert method, I would suggest you constructor simply call the insert method for each int in the array, this way you aren't repeating your code. Something like `for(int i = 0; i < numbers.length; i++) { insert(numbers[i]); } Commented Apr 23, 2015 at 14:48
  • @Captain Man I will definitely look at that. We never really covered balancing and it is not mentioned in the assignment. The fact that he Commented Apr 23, 2015 at 14:52

1 Answer 1

3

No, sorry, this is wrong.

  1. You are comparing indices with data values. You should only compare the same things to the same things. Imagine the task is to put the numbers 1001 through 1100 into the tree, rather than 1 through 100. It will make it easier to distinguish between indices and data values (and the distinction is very subtle here because your indices are 0 through 99 and your data values 1 through 100).
  2. You cannot assume that you "handle the right hand part first". You have to decide whether the given item will go left or right based on its value, and whether it's less than or greater than the current node's value.
  3. Trying to add all high values first and then all low values will not work, if you have an unordered array.

One last tip: the class should be named Node, not node. Classes and all type names should start with a capital, variables, fields and method names should start with a lowercase letter, and constants should be in all-caps.

Sign up to request clarification or add additional context in comments.

3 Comments

I think I get what you are saying with the indicies, but wouldnt changing the the loop from i to numbers[i] resolve that? as for number 2) I am not writing three seperate methods, I was aiming for two different while loops in one method. Not sure if that changes the validity of your statement but figured I should clear that up. And yes, I realized I should name the class Node, but figured I could re-factor before I submit.
Yes, numbers[i] is more correct. Two while loops in the same method will certainly not work because of #3 above. But if you didn't mean that you will write three separate methods, I'll revise my answer.
@ RealSkeptic I see, thank you for your input. I am going to have to go back to the drawing board but I think I have a good idea on how to go about this

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.