Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/305.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:如何找到给定密钥的后续密钥?_Java_Parent Child_Binary Search Tree_Nodes - Fatal编程技术网

Java BST:如何找到给定密钥的后续密钥?

Java BST:如何找到给定密钥的后续密钥?,java,parent-child,binary-search-tree,nodes,Java,Parent Child,Binary Search Tree,Nodes,我正在使用BST。给定一个密钥,如何找到后续密钥?这是我目前掌握的代码。我已经成功地插入了一个新的键,并在给定键的情况下检索了一个值。现在,我需要完成下一个方法。我将如何处理这个问题 class BST<K extends Comparable<K>, V> implements RangeMap<K,V> { class Node { Node left; Node right; Node parent; KVPair<

我正在使用BST。给定一个密钥,如何找到后续密钥?这是我目前掌握的代码。我已经成功地插入了一个新的键,并在给定键的情况下检索了一个值。现在,我需要完成下一个方法。我将如何处理这个问题

class BST<K extends Comparable<K>, V> implements RangeMap<K,V> {
class Node {
    Node left;
    Node right;
    Node parent;
    KVPair<K,V> kv;
    K key;
    V value;
    public Node(K key, V value) {
        this.key = key;
        this.value = value;
        parent = left = right = sentinel;
    }
}

private Node root;


public void add(K key, V value) {
    // TODO: Implement me(basic score)
    root = add (root, key, value);
}

private Node add(Node x, K key, V value){
    if (x == null){
        return new Node(key, value); }
        int cmp = key.compareTo(x.key);
        if (cmp < 0){
            x.left = add(x.left, key, value);}
            else if (cmp > 0 ){
                x.right = add(x.right, key, value);}
                else if (cmp == 0){
                    x.value = value;} 
    return x;
}


public V get(K key) {
    Node x = root;
    while (x != null){
        int cmp = key.compareTo(x.key);
        if (cmp < 0){
            x = x.left;}
            else if (cmp > 0 ){
                x = x.right;}
               else if (cmp == 0){
                   return x.value;}
      }
    return null;
}


public K next(K key) {
类BST实现范围映射{
类节点{
左淋巴结;
节点权;
节点父节点;
千伏对千伏;
K键;
V值;
公共节点(K键,V值){
this.key=key;
这个值=值;
父=左=右=哨兵;
}
}
私有节点根;
公共无效添加(K键,V值){
//TODO:实施自我(基本分数)
根=添加(根、键、值);
}
专用节点添加(节点x、K密钥、V值){
如果(x==null){
返回新节点(键、值);}
int cmp=键。比较(x.key);
if(cmp<0){
x、 left=add(x.left,key,value);}
否则如果(cmp>0){
x、 右=添加(x.right,key,value);}
否则如果(cmp==0){
x、 value=value;}
返回x;
}
公共V get(K键){
节点x=根;
while(x!=null){
int cmp=键。比较(x.key);
if(cmp<0){
x=x.left;}
否则如果(cmp>0){
x=x.right;}
否则如果(cmp==0){
返回x.value;}
}
返回null;
}
公共K下一步(K键){
  • 您需要一个私有方法来获取密钥的节点
  • 然后获取该节点的后续节点并返回其值
  • 您还应该更新“V get(K key)”方法,以使用getNode(K key)方法来避免代码重复
  • 我已经写了下面所有3种方法

        private Node getNode(K key) {
            Node x = root;
            while (x != null){
                int cmp = key.compareTo(x.key);
                if (cmp < 0){
                    x = x.left;
                } else if (cmp > 0 ) {
                    x = x.right;
                } else if (cmp == 0){
                    return x;
                }
              }
            return null;
        }
    
        public K next(K key) {
            Node x = getNode(key);
            if (x.right != null) {
                x = x.right;
                while (x.left != null) {
                    x = x.left;
                }
                return x.key;
            }
            Node p = x.parent;
            while (p != null && p.right == x) {
                p = p.parent;
                x = x.parent;
            }
            return p.key;
        }
    
        public V get(K key) {
            Node x = getNode(key);
            return x==null?null:x.value;
        }       
    
    私有节点getNode(K键){
    节点x=根;
    while(x!=null){
    int cmp=键。比较(x.key);
    if(cmp<0){
    x=x.左;
    }否则如果(cmp>0){
    x=x,右;
    }否则如果(cmp==0){
    返回x;
    }
    }
    返回null;
    }
    公共K下一步(K键){
    Node x=getNode(键);
    如果(x.right!=null){
    x=x,右;
    while(x.left!=null){
    x=x.左;
    }
    返回x.key;
    }
    节点p=x.parent;
    while(p!=null&&p.right==x){
    p=p.parent;
    x=x.父母;
    }
    返回p.key;
    }
    公共V get(K键){
    Node x=getNode(键);
    返回x==null?null:x.value;
    }       
    
  • 您需要一个私有方法来获取密钥的节点
  • 然后获取该节点的后续节点并返回其值
  • 您还应该更新“V get(K key)”方法,以使用getNode(K key)方法来避免代码重复
  • 我已经写了下面所有3种方法

        private Node getNode(K key) {
            Node x = root;
            while (x != null){
                int cmp = key.compareTo(x.key);
                if (cmp < 0){
                    x = x.left;
                } else if (cmp > 0 ) {
                    x = x.right;
                } else if (cmp == 0){
                    return x;
                }
              }
            return null;
        }
    
        public K next(K key) {
            Node x = getNode(key);
            if (x.right != null) {
                x = x.right;
                while (x.left != null) {
                    x = x.left;
                }
                return x.key;
            }
            Node p = x.parent;
            while (p != null && p.right == x) {
                p = p.parent;
                x = x.parent;
            }
            return p.key;
        }
    
        public V get(K key) {
            Node x = getNode(key);
            return x==null?null:x.value;
        }       
    
    私有节点getNode(K键){
    节点x=根;
    while(x!=null){
    int cmp=键。比较(x.key);
    if(cmp<0){
    x=x.左;
    }否则如果(cmp>0){
    x=x,右;
    }否则如果(cmp==0){
    返回x;
    }
    }
    返回null;
    }
    公共K下一步(K键){
    Node x=getNode(键);
    如果(x.right!=null){
    x=x,右;
    while(x.left!=null){
    x=x.左;
    }
    返回x.key;
    }
    节点p=x.parent;
    while(p!=null&&p.right==x){
    p=p.parent;
    x=x.父母;
    }
    返回p.key;
    }
    公共V get(K键){
    Node x=getNode(键);
    返回x==null?null:x.value;
    }       
    
    这是一个愚蠢的问题,但它说它找不到符号父级。我会在我的节点构造函数中添加它吗?如果是,如何添加?到目前为止,我有一个公共节点(K key,V value){this.key=key;this.value=value;什么“it”表示它找不到父级。这在代码中的什么地方发生。方法next()是BST类的一部分吗?…内部类节点定义了父字段…因此next()方法应该能够访问父字段啊是的,这是我的问题我没有定义父字段,但我不太确定如何在构造函数类中添加“Node parent;”类节点{左节点;右节点;节点父节点;KVPair kv;K键;V值;公共节点(K键,V值){this.key=key;this.value=value;}这是一个愚蠢的问题,但它说它找不到符号父级。我会在我的节点构造函数中添加它吗?如果是,如何添加?到目前为止,我有公共节点(K key,V value){this.key=key;this.value=value;什么“it”表示它找不到父级。这在代码中的什么地方发生。方法next()是BST类的一部分吗?…内部类节点定义了父字段…因此next()方法应该能够访问父字段啊是的,这是我的问题我没有定义父字段,但我不太确定如何在构造函数类中添加“Node parent;”类节点{左节点;右节点;节点父节点;KVPair kv;K键;V值;公共节点(K键,V值){this.key=key;this.value=value;}