我想制作一个通用的 BST,它可以由任何数据类型组成,但是如果我的 BST 是通用的,我不确定如何将内容添加到树中。我需要的所有代码都在下面。我希望我的 BST 由 Locations 组成,并按 x 变量排序。任何帮助表示赞赏。
主要感谢您的关注。
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);
}
其他代码
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);
}
}