Results 1 to 3 of 3
  1. #1
    hansmoolman is offline Member
    Join Date
    Oct 2010
    Posts
    5
    Rep Power
    0

    Default Binary search tree

    Can anyone help please

    I am writing a binary search tree to to add some names to my tree. The problem I am having is that although it seems that I have done everything correct, the names will not display when I run the program? Can anyone see where I am going wrong?

    When i call the prinSub method from my main method, I get an "non static variable root cannot be referenced from a static context". I know what I am passing into this method is incorrect but I cant seem to figure out where I am going wrong. :confused: This is prob a really obvious mistake but Ive been starring at it for an hour now and still cant figure it out. Please help!

    Here is my code:
    Java Code:
    import javax.swing.*;
    
    public class BinarySearchTree < Element extends Comparable< Element> > {
    
        public static void main(String[] args)
        {
            BinarySearchTree tree = new BinarySearchTree();
    
            tree.insert("Tom");
            tree.insert("Dick");
            tree.insert("Harry");
            tree.insert("Hans");
            tree.insert("Xavier");
            tree.insert("Aaron");
    
            //System.out.println("Why isnt this working " + tree);
    
            printSub(node.data);
        }
    
        private Node root;
    
        private class Node
        {
            public Element data;              
            public Node left;                   
            public Node right;                 
        }
    
    
    
        public BinarySearchTree()
        {
            root = null;
        }
    
    
        public void insert(Element e)
        {
            Node newNode = new Node();
            newNode.data = e;
            newNode.left = null;
            newNode.right = null;
    
            if (root == null)
                root = newNode;
            else
                insertSub(root, newNode);
        }
        
    
        private void insertSub(Node node, Node newNode)
        {
    
            if (newNode.data.compareTo(node.data) < 0)
            {
                if (node.left == null)
                    node.left = newNode;
                else
                    insertSub(node.left, newNode);
            }
    
            else
            {
                if (node.right == null)
                    node.right = newNode;
                else
                    insertSub(node.right, newNode);
            }
        }
    
        public void print()
        {
            if (root != null)
                printSub(root);
        }
    
    
        private void printSub(Node node)
        {
            if (node != null)
            {
                printSub(node.left);
                [B]JOptionPane.showMessageDialog(null, "The values are: " + node.data);[/B]           
                printSub(node.right);
            }
        }
    }

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,839
    Rep Power
    8

    Default

    The main method is a static method, which means it doesn't belong to any particular instance of the BinarySearchTree class. So you can't call the printSub method from main, because that method DOES belong to a particular instance of BinarySearchTree, so the compiler has no way of knowing which instance to use. It's just like you couldn't call the insert method without doing tree.insert(...). Does that make sense?

    The moral of the story is, you either need to make printSub static, or call printSub from a particular instance, like tree.printSub(...).

    And I'm not sure if this is what you meant by your comment, but simply calling System.out.println(tree) won't magically print the values of the tree in order.
    Last edited by KevinWorkman; 10-28-2010 at 02:18 PM.

  3. #3
    hansmoolman is offline Member
    Join Date
    Oct 2010
    Posts
    5
    Rep Power
    0

    Default

    Thanks, that seems to have sorted the problem. Im up and running again.

Similar Threads

  1. Replies: 0
    Last Post: 04-04-2010, 07:40 AM
  2. Binary search tree search method
    By chopo1980 in forum New To Java
    Replies: 2
    Last Post: 12-10-2009, 01:42 AM
  3. Binary Search Tree
    By anmadie in forum New To Java
    Replies: 5
    Last Post: 11-17-2009, 02:39 AM
  4. Binary Search Tree
    By Goo in forum New To Java
    Replies: 0
    Last Post: 03-06-2009, 04:46 PM
  5. Binary Search Tree
    By michael_mke in forum New To Java
    Replies: 3
    Last Post: 12-04-2008, 02:03 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •