在二叉搜索树中找到第二个最小元素

时间:2017-02-09 04:38:23

标签: data-structures tree binary-tree binary-search-tree

int secondSmallestInBST(struct node * tNode) {
if( tNode==NULL || (tNode->left==NULL && tNode->right==NULL) )  // case 1 and 2
    exit;
if(tNode->left == NULL){                     // case 3
    tNode=tNode->right;
    while(tNode->left!=NULL){
        tNode=tNode->left;
    }
    return tNode->data;
}                                                     // general case.
node * parent=tNode,* child = tNode->left;
while(child->left!=NULL){
    parent = child;
    child = child->left;
}
return parent->data;

}

并非我的代码都传递了每个测试用例。建议我,如果我的代码中缺少任何测试用例。我只是在二叉搜索树中找到第二个最小的元素。

int secondSmallestInBST(struct node * tNode) {
if( tNode==NULL || (tNode->left==NULL && tNode->right==NULL) )  // case 1 and 2
    exit;
if(tNode->left == NULL){                     // case 3
    tNode=tNode->right;                     // find smallest in right bst.
    while(tNode->left!=NULL){
        tNode=tNode->left;
    }
    return tNode->data;
}                                                     // general case.
if(tNode->left->left==NULL && tNode->left->right!=NULL){   //missed case.
    tNode=tNode->left->right;
    while(tNode->left!=NULL){
        tNode=tNode->left;
    }
    return tNode->data;
}
node * parent= tNode;
node * child = tNode->left;
while(child->left!=NULL){
    parent = child;
    child = child->left;
}
return parent->data;

}

//在此代码中仍然缺少一些测试用例。

3 个答案:

答案 0 :(得分:1)

测试此案例 - 3 6 2 3。 树将如下所示:

    6
   /
  2
   \
    3

你的方式,答案将是6,而它是3。

答案 1 :(得分:0)

`

int Successor(Node* root){
  while(root->left){
    root = root->left;
  }
  return root->data;
}

int Second_Minimum(Node* root){
  //  make sure tree is not empty
  if(!root)
    return -1;
  // previous node takes before the last left node
  Node* previous = root;
  // check left node first for smallest key
  if(root->left){
    while(root->left){
      previous = root;
      root = root->left;        //    6
    }                           //   /
    // checks for the case ---->    2
    if(!root->right)            //   \
      return previous->data;    //    3 
  }
  // Go for minimum successor if exists 
  if(root->right)
    return Successor(root->right);
  // checked left and right branch root is on his own 
  return -1;
}

`

答案 2 :(得分:0)

BST有序遍历将元素按顺序排列(排序)。因此,我们的想法是返回遍历中的第二个元素(如果树上的元素少于两个,那么它将没有第二个最小值,并且应该返回null(未找到)。)

以下代码实现了该算法。请注意,可以更改算法以轻松返回第K个最小元素。

代码已用C#编写(很容易可以用其他语言编写:-)享受吧!

public static int? FindSecondMimimum(Node node)
{
    int current = 0;
    return FindKthMinimum(node, 2, ref current);
}

private static int? FindKthMinimum(Node node, int k, ref int current)
{
    int? result = null;
    if (node == null)
        return null;

    if (node.Left != null)
    {
        result = FindKthMinimum(node.Left, k, ref current);
        if (result != null)
            return result;
    }

    current++;
    if (current == k)
        return node.Value;

    if (node.Right != null)
    {
        result = FindKthMinimum(node.Right, k, ref current);
    }

    return result;
}
相关问题