二进制搜索树继承人

时间:2015-09-08 23:03:42

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

如何更改以下功能,让我将整数k 的后继者返回到 h1 h2 之间的某个级别? h1和h2是范围,例如h1 = 0和h2 = 3,我想要k的后继者,其深度在0到3之间

//BST declaration
struct node
{
   int key;
   struct node *left;//pointer to left
   struct node *right;// pointer to right
};

struct node *successor(struct node *T, int k, int h1, int h2)
{
   struct node *z = T;
   struct node *y = NULL;//successor candidate
   if(T!=NULL)
   {
      while(z!=NULL && z->key!=NULL)
      {
        if(k > z->key)
            z = z->right;//If k is greater than the root then I move to the right
        else if(k < z->key)
        {
            y = z;//updates the successor of his father
            z = z->left;//If k is less than the root then I move to the left
        }
       }
      if(z!=NULL && z->right!=NULL)
            y = bstMinimum(z->right);
   }
   return y;
 }

//find the minimum of the right tree
struct node *bstMinimum(struct node *A)
{
   while(A!=NULL && A->left!=NULL)
   {
     A=A->left; 
   }
  return A;
}

0 个答案:

没有答案