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.

Dani AI

Generated

Short answer: don’t construct a brand‑new Node just to call the minimum method. Either call the method against the tree’s actual root (or expose the root), or provide a no‑argument wrapper that looks up the minimum starting from root. Also fix the small bug in your current minimumVal(Node n) — it checks root == null instead of n == null.

A compact, safe pattern is to keep the subtree routine private and expose a public no‑arg method that uses root. Example implementation (uses recursion and throws an exception for an empty tree):

public int minimumVal() {
    if (root == null)
        throw new java.util.NoSuchElementException("Tree is empty");
    return minimumVal(root);
}

private int minimumVal(Node n) {
    if (n.left == null) return n.dataNode;
    return minimumVal(n.left);
}

To test from main:

BinaryTree tree = new BinaryTree();
tree.addNode(6);
tree.addNode(12);
tree.addNode(8);
System.out.println("Minimum: " + tree.minimumVal()); // should print 6

Notes and troubleshooting:

  • Creating new Node(x) and passing that to minimumVal only inspects that isolated node (or its subtree) — it won’t reflect the values already inserted via addNode. That’s why a wrapper that uses root is the right approach. ’s suggestion to pass a Node is technically valid only if that Node is already part of the tree.
  • Returning 0 for an empty tree is brittle (0 might be a valid value). Prefer throwing an exception or returning an Optional/OptionalInt.
  • Keep root private and provide the public no‑arg API so callers don’t have to know your internal representation.

Good call by to add a helper — that’s the cleanest, safest fix.

Recommended Answers

All 5 Replies

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.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.