Binary Search Tree in Java

Posted by John R on Stack Overflow See other posts from Stack Overflow or by John R
Published on 2010-04-04T17:43:33Z Indexed on 2010/04/04 18:03 UTC
Read the original article Hit count: 469

I want to make a generic BST, that can be made up of any data type, but i'm not sure how I could add things to the tree, if my BST is generic. All of my needed code is below. I want my BST made up of Locations, and sorted by the x variable. Any help is appreciated.

Major thanks for looking.

public void add(E element)
{
    if (root == null)
         root = element;
    if (element < root)
         add(element, root.leftChild);
    if (element > root)
         add(element, root.rightChild);
    else
         System.out.println("Element Already Exists");
}

private void add(E element, E currLoc)
{
    if (currLoc == null)
         currLoc = element;
    if (element < root)
         add(element, currLoc.leftChild);
    if (element > root)
         add(element, currLoc.rightChild);
    else
         System.out.println("Element Already Exists);
}

Other Code

public class BinaryNode<E>
{
    E BinaryNode;
    BinaryNode nextBinaryNode;
    BinaryNode prevBinaryNode;

    public BinaryNode()
    {
        BinaryNode = null;
        nextBinaryNode = null;
        prevBinaryNode = null;
    }

}


public class Location<AnyType> extends BinaryNode
{
    String name;
    int x,y;

    public Location()
    {
        name = null;
        x = 0;
        y = 0;
    }

    public Location(String newName, int xCord, int yCord)
    {
        name = newName;
        x = xCord;
        y = yCord;
    }

    public int equals(Location otherScene)
    {
        return name.compareToIgnoreCase(otherScene.name);
    }


}

© Stack Overflow or respective owner

Related posts about java

Related posts about binary-trees