Hi all, I've just started out with programming and I'm working on a Binary Tree of integers. I have the Node and BinaryTree classes almost complete, but I'm having a derp moment trying to test my minimumVal method from the main and not sure on what to pass into the parenthesis.

Part of Node class (removed add method to save space);

class Node 
{
    Node left, right;
    int dataNode;

    public Node(int i) 
    {
        dataNode = i;
        right = left = null;
    }
}

BinaryTree class (removed some traversal methods, another add method);

public class BinaryTree 
{
    Node root;

    public int minimumVal(Node n) 
    {
        if (root == null)
        {
            System.out.println("Tree empty.");
            return 0;
        }
        else
        {   
            Node curr = n;

            while (curr.left != null)
                curr = curr.left;

            return curr.dataNode;
        }       
    }       
}

How would I test to see if minimumVal worked? What's confusing me is in the Node class the constructor is requiring an int. So in my main what would I pass in? At the minute I have...

public static void main(String[] args)
{
    BinaryTree tree = new BinaryTree();
    Node tNode = new Node(???); // what do I pass in here that'll help with finding the min value?
    tree.addNode(6);
    tree.addNode(12);
    tree.addNode(8);
    tree.minimumVal(???); // what do I pass into the parenthesis? it requires a Node reference
}

Stupid question probably but any help would be great.

well .. the first one requires an int, so you'll need to pass an int.
the second one requires a Node, so tNode should do.
but you don't have an addNode method anywhere, so remove those calls.

so:

Node tNode = new Node(15); // or any other int
tree.minimumVal(tNode);

I have an addNode method but I didn't include it in the post to save room. It's a basic method that checks if ins is less than dataNode and if so, insert it into the left of the tree, otherwise insert it into the right or make that value the root if the tree is empty. So I'd be using that method to insert values into the Binary Tree so I don't understand what to put in the parenthesis of Node that would then go into the parenthesis of minimumVal to test it out.

well, since you don't show the add method, I don't know what it's supposed to take as a parameter, but my guess is, it would be a new instance reference of Node

Node.java;

  public void add(int ins)
    {              
        if (ins < dataNode)
        {
            if (left == null)
                left = new Node(ins);
            else
                left.add(ins);
        }
        else
        {
            if (right == null)
                right = new Node(ins);
            else
                right.add(ins);
        }                   
    }

BinaryTree.java;

    public void addNode(int ins) 
    {
        if (root == null)
            root = new Node(ins);
        else
            root.add(ins);
    }

Then in the main it would be BinaryTree tree = new BinaryTree() and use tree to call addNode to insert values into the BinaryTree. So really, there's no need for a Node instance when inserting, etc. but I need a Node instance because the minimumVal method has a Node passed into the parenthesis. So I have to create an instance of Node which requires an int passed into it, but I can't see how adding a random int into the parenthesis of that will help with anything because I'm using the addNode method to populate the tree.

Edited 2 Years Ago by Datagram

Figured it out, I just added another method to act as a helper.

This question has already been answered. Start a new discussion instead.