Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 强平衡树改进_C_Algorithm_Binary Tree - Fatal编程技术网

C 强平衡树改进

C 强平衡树改进,c,algorithm,binary-tree,C,Algorithm,Binary Tree,我使用以下结构来表示二叉树: typedef struct node *pnode; typedef struct node { int val; pnode left; pnode right; } snode; 树的权重是树的所有节点的val参数的总和。当树是空的或者左的权重等于右子树的权重,并且每个子树(左和右)都是强平衡的时,我们说树是强平衡的。我必须写一个函数来判断树是否是强平衡的 我写的代码是: int getWeight(pnode tree) { if(

我使用以下结构来表示二叉树:

typedef struct node *pnode;
typedef struct node {
   int val;
   pnode left;
   pnode right;
} snode;
树的权重是树的所有节点的val参数的总和。当树是空的或者左的权重等于右子树的权重,并且每个子树(左和右)都是强平衡的时,我们说树是强平衡的。我必须写一个函数来判断树是否是强平衡的

我写的代码是:

int getWeight(pnode tree)
{
   if(! tree)
      return 0;
   return getWeight(tree->left) + getWeight(tree->right)
            + tree->val;
} 

bool isStronglyBalanced(pnode tree)
{
   if(! tree)
      return true;

   int wL, wR;
   wL = getWeight(tree->left);
   wR = getWeight(tree->right);

   if(wL != wR)
      return false;
   return (isStronglyBalanced(tree->left) && isStronglyBalanced(tree->right));
}

上面的函数做得很好,但我注意到它不止一次访问树的节点。如果
getweight
还告诉您子树是否是强平衡的,您可以将代码简化为一次扫描

有几种方法可以从函数调用返回两条信息。如果有一些你知道永远不会是子树权重的返回值(可能是负数),你可以用它作为子树不是强平衡的信号。或者您可以简单地返回两个值:一个
bool
和一个
int
。(在C中,您可以为其中一个使用“out”参数。)或者您可以使用两个值定义一个复合对象,在C中,它将是
struct{bool balanced;int weight;}

不管你怎么做,逻辑都是一样的。在下面的伪代码中,我假设您可以像在某些语言中一样返回值对(例如,C++,但尽管有任何偶然的相似之处,它仍然是伪代码):

成对获取权重(树){
如果(!tree)返回{true,0};
平衡,权重左=获取权重(树->左);
如果(!balanced)返回{false,0};/*返回的权重无关紧要*/
平衡,权重\右=获取权重(树->右);
返回{balanced&&weight\u left==weight\u right,
2*weight\u right+tree->val};/*weight\u left==weight\u right*/
}

这是一个很好的建议,但是返回值不应该是
{balanced&&weight\u left==weight\u right,weight\u right+weight\u left+tree->val}
?@DougCurrie:确实如此。谢谢修正了,注意到weight\u left==weight\u right。
pair<bool, int> get_weight(tree) {
  if (!tree) return {true, 0};
  balanced, weight_left = get_weight(tree->left);
  if (!balanced) return {false, 0};      /* Returned weight doesn't matter */
  balanced, weight_right = get_weight(tree->right);
  return {balanced && weight_left == weight_right,
          2 * weight_right + tree->val}; /* weight_left == weight_right */
}