【问题标题】:BST: return first entry larger than the keyBST:返回大于键的第一个条目
【发布时间】:2016-03-31 05:12:23
【问题描述】:

我正在编写一个 Java 方法,该方法采用二叉搜索树 (BST) T 和键 k,并返回大于 k 的第一个条目,该条目将出现在中序遍历中。如果 k 不存在或不存在大于 k 的键,则返回 null。例如,当应用于下图中的 BST 时,如果 k = 23,则应返回 29;如果 k = 32,则应返回 null。

http://imgur.com/fpNk9fT

伪代码为:

findFirstEntryLargerThanKey(BSTNode t, int key)
// go left
findFirstEntryLargerThanKey(t.left, key);
// visit the node
if t.nodeValue == key 
key exists, set a boolean value to true
else if t.nodeValue > key
check if this node value is the first entry larger than key
// go right
findFirstEntryLargerThanKey(t.right, key);

我写到现在的代码:

boolean found = false;
int x = 0;
public Integer findFirstEntryLargerThanKey(BSTNode t, int key) { 
    // the scan terminates on an empty subtree
    if (t != null) {


        // descend left 
        findFirstEntryLargerThanKey(t.left, key); 
        // visit the node
        if (t.nodeValue == key){
            found = true;
        }

        else if (t.nodeValue > key && found == true && ? && ?){
        x = t.nodeValue;

        }
        // descend right
        findFirstEntryLargerThanKey(t.right, key);
        return x;
    } 

    return null;
}

我需要有关我必须使用的条件的帮助。

【问题讨论】:

  • 那么问题是什么?
  • @Guy 他可能想用什么条件代替'?'s
  • @TechSpellBound 如果那是 OP 想要的,他/她需要说出来。
  • @Guy 同意了!
  • 对不起,我需要帮助。

标签: java binary-search-tree


【解决方案1】:

按顺序进行遍历,它将按升序打印树键。同时继续比较密钥并打印大于您的密钥的第一个密钥..时间复杂度将小于 O(n)

【讨论】:

  • 我已经按顺序遍历了。但我不知道如何检索大于键的第一个值。如果您查看代码,我有一个 if 语句来检查键是否存在于树中。但我不知道 else if 语句检索的第一个值大于键。
  • 整数 x = null; public Integer findFirstEntryLargerThanKey(BSTNode t, int key) { // 扫描在一个空子树上终止 if (t != null && x!=null) { // 向左下降 findFirstEntryLargerThanKey(t.left, key); // 访问节点 if (t.nodeValue > key && found ||t.nodeValue==key){ x = t.nodeValue; } // 向右下降 findFirstEntryLargerThanKey(t.right, key);返回 x; } 返回空值; } //打印X
【解决方案2】:

找到后可以直接返回答案,因为 x 是全局声明的。

你的函数应该是这样的:

boolean found = false;
int x = 0;
public Integer findFirstEntryLargerThanKey(BSTNode t, int key) { 
    // the scan terminates on an empty subtree
    if (t != null) {


        // descend left 
        findFirstEntryLargerThanKey(t.left, key); 
        // visit the node
        if (t.nodeValue == key){
            found = true;
        }

        else if (t.nodeValue > key && found == true){
            x = t.nodeValue;
            return x;
        }
        // descend right
        findFirstEntryLargerThanKey(t.right, key);
        return x;
    } 

    return null;
}

或者,做这样的事情:

int x = 0;
public Integer findFirstEntryLargerThanKey(BSTNode t, int key) { 
    // the scan terminates on an empty subtree
    if (t != null) {


        // descend left 
        findFirstEntryLargerThanKey(t.left, key); 
        // visit the node
        if (t.nodeValue > key){
            x = t.nodevalue;
            return x;
        }
        // descend right
        findFirstEntryLargerThanKey(t.right, key);
        return x;
    } 

    return null;
}

【讨论】:

    【解决方案3】:

    让我们通过两个额外的步骤尝试通常的键搜索:

     * Whenever we go left from a parent to a child, 
       remember the parent as a potential answer.
     * If the key is found and it has a right subtree, 
       then the answer is left-most (smallest) node of that subtree.
    
    FindFirstEntryLargerThanKey(BSTNode t, int key) {
      BSTNode result_so_far = null;
      for (BSTNode cur = t; cur; ) {
        if (key < cur->key) {
          result_so_far = cur;
          cur = cur->left;
        }
        else if (key == cur->key) {
          if (cur->right) {
            result_so_far = LeftMostNode(cur->right);
          }
          break;
        }
        else {
          cur = cur->right;
        }
      }
      return result_so_far;
    }
    

    【讨论】:

      【解决方案4】:
          try this out
      
      
          public Integer findFirstEntryLargerThanKey(BSTNode t, int key) { 
      Integer x =null;
              // the scan terminates on an empty subtree
              if (t != null && x!=null) {
      
      
                  // descend left 
                  findFirstEntryLargerThanKey(t.left, key); 
                  // visit the node
      
      
                   if (t.nodeValue > key  ||t.nodeValue==key){
                      x = t.nodeValue;
      
      
                  }
                  // descend right
                  findFirstEntryLargerThanKey(t.right, key);
      
              } 
      
              return x;
          }
      

      【讨论】:

      • 我在调用该方法时遇到问题。我已经创建了一棵二叉树,我正在使用Integer s = bst.findFirstEntryLargerThanKey(null, key); 调用每次都给我结果“null”的方法。你能帮我解决这个问题吗
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-08
      相关资源
      最近更新 更多