Exemple #1
0
static DirectNode *
tree_node_balance (DirectNode *node)
{
     if (node->balance < -1) {
          if (node->left->balance > 0)
               node->left = tree_node_rotate_left (node->left);
          node = tree_node_rotate_right (node);
     }
     else if (node->balance > 1) {
          if (node->right->balance < 0)
               node->right = tree_node_rotate_right (node->right);
          node = tree_node_rotate_left (node);
     }

     return node;
}
Exemple #2
0
/* Balance the tree with respect to node */
static int tree_node_balance(Edata_Tree *tree, Edata_Tree_Node *top_node)
{
	int balance;

	CHECK_PARAM_POINTER_RETURN("top_node", top_node, FALSE);

	/* Get the height of the left branch. */
	if (top_node->right_child)
		top_node->max_left = MAX_HEIGHT(top_node->right_child) + 1;
	else
		top_node->max_left = 0;

	/* Get the height of the right branch. */
	if (top_node->left_child)
		top_node->max_right = MAX_HEIGHT(top_node->left_child) + 1;
	else
		top_node->max_right = 0;

	/* Determine which side has a larger height. */
	balance = top_node->max_right - top_node->max_left;

	/* if the left side has a height advantage >1 rotate right */
	if (balance < -1)
		tree_node_rotate_right(tree, top_node);
	/* else if the left side has a height advantage >1 rotate left */
	else if (balance > 1)
		tree_node_rotate_left(tree, top_node);

	return TRUE;
}