Java 中的二叉搜索树

发布于 2024-08-27 11:13:31 字数 1439 浏览 8 评论 0原文

我想制作一个通用的 BST,它可以由任何数据类型组成,但是如果我的 BST 是通用的,我不确定如何将内容添加到树中。我需要的所有代码如下。我希望我的 BST 由位置组成,并按 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);
    }


}

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);
    }


}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

脸赞 2024-09-03 11:13:31

您可以限制您的类型来实现Comparable

public class BinarySearchTree<E extends Comparable<? super E>>

然后你可以调用 compareTo:

// Instead of if (element < root)
if (element.compareTo(root) < 0)

(etc)

或者,您可以在构建搜索树时强制调用者传入 Comparator,并且然后用它来比较元素。在我看来,这实际上是一个更灵活的解决方案 - 这意味着您可以为相同的元素类型创建不同的二叉搜索树,但以不同的方式对它们进行排序。

You can constrain your type to implement Comparable<? super E>:

public class BinarySearchTree<E extends Comparable<? super E>>

Then you can call compareTo:

// Instead of if (element < root)
if (element.compareTo(root) < 0)

(etc)

Alternatively, you could force the caller to pass in a Comparator<E> when the search tree is constructed, and then use that to compare elements. That's actually a more flexible solution in my view - it means you could create different binary search trees for the same element type, but ordering them in different ways.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文