2016-03-31 40 views
1

我在寫一個Java方法,它採用二叉搜索樹(BST)T和一個關鍵字k,並返回大於k的第一個條目,該條目將出現在inorder遍歷。如果k不存在或者沒有大於k的密鑰,則返回null。例如,當應用於下圖中的BST時,如果k = 23,則應該返回29;如果k = 32,則應該返回null。BST:返回大於關鍵字的第一個條目

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

我寫uptil現在的代碼:

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

我想清楚我必須使用條件的幫助。

+0

那麼問題是什麼? – Guy

+0

@Guy他可能想要使用什麼條件代替'?' – TechSpellBound

+1

@TechSpellBound如果OP希望他/她需要說出什麼。 – Guy

回答

0

做一個遍歷,它將按升序打印樹鍵。同時繼續比較密鑰並打印第一個大於您的密鑰的密鑰..時間複雜度將小於O(n)

+0

我已經做了遍歷順序。但我無法弄清楚如何檢索比鍵大的第一個值。如果你看代碼,我有一個if語句來檢查樹中是否存在密鑰。但我不能找出else if語句來檢索比鍵大的第一個值。 –

+0

Integer x = null; 公共整數findFirstEntryLargerThanKey(BSTNode噸,INT鍵){// 掃描在一個空的子樹 終止如果(T!= NULL && X!= NULL){ //下降左 findFirstEntryLargerThanKey(t.left,鍵); //訪問節點 如果(t.nodeValue> key && found || t.nodeValue == key){ x = t.nodeValue; } //向右下降 findFirstEntryLargerThanKey(t.right,key); return x; } return null; } // print X –

0

由於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; 
} 
0
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; 
    } 
+0

我在調用方法時遇到問題。我創建了一個二叉樹,我正在使用'Integer s = bst.findFirstEntryLargerThanKey(null,key);'調用每次給我結果爲「null」的方法。你能plz幫我修復這個 –

+0

我編輯過它。請看看它是否工作謝謝 –

0

讓我們試着用兩個額外的步驟通常鍵搜索:

* 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; 
} 
相關問題