【问题标题】:BST: How to find the successor key given a key?BST:如何找到给定密钥的后继密钥?
【发布时间】:2016-03-31 03:06:45
【问题描述】:

我正在使用 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) {

【问题讨论】:

    标签: java parent-child binary-search-tree nodes


    【解决方案1】:
    1. 您需要一个私有方法来获取密钥节点
    2. 然后你得到那个节点的后继节点并返回它的值
    3. 您还应该更新“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;
        }       
    

    【讨论】:

    • 这是一个愚蠢的问题,但它说它找不到符号父级。我会在我的节点构造函数中添加它吗?如果是这样,如何?到目前为止,我有 public Node(K key, V value) { this.key = key; this.value = value;
    • 说它找不到父级的“它”是什么。这发生在代码的什么地方。方法 next() 是 BST 类的一部分,对吧?...内部类 Node 定义了一个父字段...所以 next() 方法应该能够访问父字段
    • 啊是的,这是我的问题我没有定义父字段,但我不太确定如何
    • 我只是简单地添加“节点父级;”在构造函数类中?
    • 类节点 { 左节点;节点对;节点父节点; KVPair kv; K键; V值; public Node(K key, V value) { this.key = key; this.value = 值; }
    猜你喜欢
    • 2015-09-19
    • 2011-05-02
    • 1970-01-01
    • 2017-12-25
    • 1970-01-01
    • 2016-04-01
    • 2017-10-08
    • 1970-01-01
    • 2022-01-24
    相关资源
    最近更新 更多