Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何让我的BST add方法通过前两个子节点添加节点?_Java_Binary Tree_Binary Search Tree - Fatal编程技术网

Java 如何让我的BST add方法通过前两个子节点添加节点?

Java 如何让我的BST add方法通过前两个子节点添加节点?,java,binary-tree,binary-search-tree,Java,Binary Tree,Binary Search Tree,我对二叉树有点陌生,正在为一个简单的游戏开发一个add方法。当前,每次添加新节点时,该方法都会替换根节点的子节点。如何让它在整个树中添加新节点,而不是仅仅替换根节点的前两个子节点?这就是我到目前为止所做的: public boolean add(User friend) { User node = friend; if (root == null) { root = node; return true; } if (node.key

我对二叉树有点陌生,正在为一个简单的游戏开发一个add方法。当前,每次添加新节点时,该方法都会替换根节点的子节点。如何让它在整个树中添加新节点,而不是仅仅替换根节点的前两个子节点?这就是我到目前为止所做的:

public boolean add(User friend) {
    User node = friend;
    if (root == null) {
        root = node;
        return true;
    }
    if (node.key() < root.key()) {
        if(root.left == null) {
            root.setLeft(node);
        }
    } else if (node.key() > root.key()) {
        if(root.getRight() == null) {
            root.setRight(node);
        }
    }
    return true;
}
公共布尔添加(用户朋友){
用户节点=朋友;
if(root==null){
根=节点;
返回true;
}
if(node.key()root.key()){
if(root.getRight()==null){
root.setRight(节点);
}
}
返回true;
}

通常,对于二叉树,您将编写递归函数。您可以找到节点所属的那一侧,并将插入转交给该节点一侧的递归函数调用,而不是从一个函数调用中管理插入

假设此函数属于类
用户
,则应该可以使用

public boolean beFriend(User friend) {
    User node = friend;
    if (root == null) {
        root = node;
        return true;
    }
    if (node.getKey() < root.getKey()) {
        if(root.getLeft() == null) {
            root.setLeft(node);
        } else {
            // recursively call beFriend handing insertion to the child
            root.getLeft().beFriend(node);
        }
    } else if (node.getKey() > root.getKey()) {
        if(root.getRight() == null) {
            root.setRight(node);
        } else {
            // recursively call beFriend handing insertion to the child
            root.getRight().beFriend(node); 
        }
    } else { // node.getKey() == root.getKey() so we replace the previous root
        node.setLeft(root.getLeft());
        node.setRight(root.getRight());
        root = node;
    }
    return true;
}
public boolean好友(用户好友){
用户节点=朋友;
if(root==null){
根=节点;
返回true;
}
if(node.getKey()root.getKey()){
if(root.getRight()==null){
root.setRight(节点);
}否则{
//递归调用beFriend,将插入操作交给孩子
root.getRight().beFriend(节点);
}
}else{//node.getKey()==root.getKey(),因此我们替换上一个根
node.setLeft(root.getLeft());
node.setRight(root.getRight());
根=节点;
}
返回true;
}

用作指南。此树似乎是
用户
类的一部分。最好创建一个单独的
BinarySearchTree
类,并使您的
User
类具有一个字段
私有BinarySearchTree朋友
<代码>用户::beFriend然后只需呼叫
朋友。添加
@VladK。该指南很好地解释了树。如果键相等,为什么要替换根,而不是让它保持相等?你的答案似乎遗漏了这一点:这是你在没有激励的情况下所做的改变。我不确定钥匙是如何产生的,也不知道钥匙是在哪里产生的,所以对我来说,这似乎是最安全的赌注。最糟糕的情况是,我们做了一些额外的工作。最好的情况是,它使用所需的用户正确更新。很抱歉,我应该提到它所属的类,它有自己的BinaryTree类和user类,分别保存键
getKey()