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

时间:2016-03-31 05:12:24

标签: java binary-search-tree

我正在编写一个Java方法,该方法采用二叉搜索树(BST)T和密钥k,并返回大于k的第一个条目,该条目将出现在inorder遍历中。如果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;
}

我需要帮助解决我必须使用的条件。

4 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分: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;
}

答案 2 :(得分:0)

  OSStatus result = noErr;
    result = NewAUGraph(&audioGraph);
    if (noErr != result) {[self printErrorMessage: @"NewAUGraph" withStatus: result]; return;}

    // 2.add AUNode
    AUNode ioNode;

    // client format audio goes into the mixer
    clientFromat.SetCanonical(1, true);
    clientFromat.mSampleRate = kGraphSampleRate;
    clientFromat.Print();

    CAComponentDescription io_desc(kAudioUnitType_Output,kAudioUnitSubType_RemoteIO,kAudioUnitManufacturer_Apple);

    result = AUGraphAddNode(audioGraph, &io_desc, &ioNode);
    if (noErr != result) {[self printErrorMessage: @"AUGraphNewNode failed for Mixer unit" withStatus: result]; return;}

    // 3.open augraphic
    result = AUGraphOpen(audioGraph);
    if (noErr != result) {[self printErrorMessage: @"AUGraphOpen" withStatus: result]; return;}

    // 4.get audio unit instance from nodes
    result = AUGraphNodeInfo(audioGraph, ioNode, NULL, &ioUnit);
    if (result) { printf("AUGraphNodeInfo result %ld %08X %4.4s\n", result, (unsigned int)result, (char*)&result); return; }

    // 5. set audio unit property
    CAStreamBasicDescription outputFormat;
    // output format
    outputFormat.SetAUCanonical(1, false);
    outputFormat.mSampleRate = kGraphSampleRate;
    outputFormat.Print();

    AudioUnitElement inputBus = 1;
    UInt32 enableInput = 1;
    result = AudioUnitSetProperty(ioUnit, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Input, inputBus, &enableInput, sizeof(enableInput));

    // 6.connect input->eq->output node        
    result = AUGraphConnectNodeInput(audioGraph, ioNode, 1,ioNode, 0);
    if (result) { printf("AUGraphConnectNodeInput result %lu %4.4s\n", result, (char*)&result); return; }


    // 7. initialize graphic
    result = AUGraphInitialize(audioGraph);
    if (result) { printf("AUGraphInitialize result %ld %08X %4.4s\n", result, (unsigned int)result, (char*)&result); return; }
    CAShow(audioGraph);

答案 3 :(得分: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;
}