Java BST:返回比键大的第一个条目

Java BST:返回比键大的第一个条目,java,binary-search-tree,Java,Binary Search Tree,我写这篇文章的目的是编写一个Java方法,它接受一个二进制搜索树(BST)T和一个键k,并返回比按序遍历中出现的k大的第一个条目。如果k不存在或没有大于k的键存在,则返回null。例如,当应用于下图中的BST时,如果k=23,则应返回29;如果k=32,则应返回null 伪代码是: findFirstEntryLargerThanKey(BSTNode t, int key) // go left findFirstEntryLargerThanKey(t.left, key); // vis

我写这篇文章的目的是编写一个Java方法,它接受一个二进制搜索树(BST)T和一个键k,并返回比按序遍历中出现的k大的第一个条目。如果k不存在或没有大于k的键存在,则返回null。例如,当应用于下图中的BST时,如果k=23,则应返回29;如果k=32,则应返回null

伪代码是:

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

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

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

按顺序遍历,这将按升序打印树键。同时保持比较密钥并打印第一个大于密钥的密钥。。时间复杂度将小于O(n)

您可以在找到答案时直接返回答案,因为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;
}

您可以在找到答案时直接返回答案,因为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;
}

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

 * 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;
}
*每当我们从父母向左走到孩子,
记住父母是一个潜在的答案。
*如果找到了密钥并且它有一个正确的子树,
然后答案是该子树最左边(最小)的节点。
FindFirstEntryLargerTankey(BSTNode t,int键){
BSTNode result\u so\u far=null;
对于(BSTNode cur=t;cur;){
如果(键<当前->键){
结果迄今为止=当前;
cur=cur->left;
}
否则如果(键==当前->键){
如果(当前->右侧){
result\u so\u far=LeftMostNode(cur->right);
}
打破
}
否则{
cur=cur->right;
}
}
返回迄今为止的结果;
}

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

 * 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;
}
*每当我们从父母向左走到孩子,
记住父母是一个潜在的答案。
*如果找到了密钥并且它有一个正确的子树,
然后答案是该子树最左边(最小)的节点。
FindFirstEntryLargerTankey(BSTNode t,int键){
BSTNode result\u so\u far=null;
对于(BSTNode cur=t;cur;){
如果(键<当前->键){
结果迄今为止=当前;
cur=cur->left;
}
否则如果(键==当前->键){
如果(当前->右侧){
result\u so\u far=LeftMostNode(cur->right);
}
打破
}
否则{
cur=cur->right;
}
}
返回迄今为止的结果;
}

那么问题是什么呢?@他可能想知道应该用什么条件来代替“?”s@TechSpellBound如果那是OP想要的,他/她需要说出来。@盖伊同意了!Sry伙计们,我需要条件方面的帮助。那个么问题是什么呢?@伙计,他可能想知道应该用什么条件来代替“?”s@TechSpellBound如果那是OP想要的,他/她需要说出来。@盖伊同意了!Sry的伙计们,我需要条件方面的帮助。我已经按顺序完成了。但我不知道如何检索大于key的第一个值。如果您查看代码,我有一个If语句来检查树中是否存在键。但是我无法理解else if语句,它检索的第一个值大于键;public Integer findfirstentyrargerthankey(BSTNode t,int key){//如果(t!=null&&x!=null){//下降左findfirstentyrargerthankey(t.left,key);//如果(t.nodeValue>key&&found | | t.nodeValue==key)访问节点{x=t.nodeValue;}//右下findFirstEntryLargerTankey(t.right,key);返回x;}返回null;}/XI打印已经在遍历中完成了。但是我不能计算如何检索比键大的第一个值。如果你看代码,我有一个if语句来检查树中是否存在密钥。但是我不能计算出检索大于键的第一个值的另席语句。ntrylargertankey(BSTNode t,int key){//如果(t!=null&&x!=null){//下降左findfirstentrylargertankey(t.left,key);//如果(t.nodeValue>key&&found | t.nodeValue==key){x=t.nodeValue;}访问节点//下行右FiFrdStReCythEngReleReTykey(TyTror,Key);返回x;} //席Xi调用方法有问题。我创建了一个二叉树,并且我使用了<代码>整型s= BST.FiffIdStReCyListReleSKey(null,key);< /Cord>调用方法,它给了我结果“null”。每次。你能帮我解决这个问题吗?我调用这个方法时遇到问题。我已经创建了一个二叉树,我正在使用
Integer s=bst.findfirstentrylargertankey(null,key);
调用每次都给我结果为“null”的方法。你能帮我解决这个问题吗