Results 1 to 3 of 3
Thread: Binary search tree
 10282010, 12:45 PM #1Member
 Join Date
 Oct 2010
 Posts
 5
 Rep Power
 0
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); } } }
 10282010, 12:59 PM #2
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; 10282010 at 02:18 PM.
 10282010, 01:59 PM #3Member
 Join Date
 Oct 2010
 Posts
 5
 Rep Power
 0
Similar Threads

Data Structures(Binary Search Tree to AVL Tree)ASAP pls
By jfAdik in forum Forum LobbyReplies: 0Last Post: 04042010, 07:40 AM 
Binary search tree search method
By chopo1980 in forum New To JavaReplies: 2Last Post: 12102009, 02:42 AM 
Binary Search Tree
By anmadie in forum New To JavaReplies: 5Last Post: 11172009, 03:39 AM 
Binary Search Tree
By Goo in forum New To JavaReplies: 0Last Post: 03062009, 05:46 PM 
Binary Search Tree
By michael_mke in forum New To JavaReplies: 3Last Post: 12042008, 03:03 AM
Bookmarks