static void g_tree_node_check (GTreeNode *node) { gint left_height; gint right_height; gint balance; if (node) { left_height = 0; right_height = 0; if (node->left) left_height = g_tree_node_height (node->left); if (node->right) right_height = g_tree_node_height (node->right); balance = right_height - left_height; if (balance != node->balance) g_log (g_log_domain_glib, G_LOG_LEVEL_INFO, "g_tree_node_check: failed: %d ( %d )\n", balance, node->balance); if (node->left) g_tree_node_check (node->left); if (node->right) g_tree_node_check (node->right); } }
/** * g_tree_height: * @tree: a #GTree. * * Gets the height of a #GTree. * * If the #GTree contains no nodes, the height is 0. * If the #GTree contains only one root node the height is 1. * If the root node has children the height is 2, etc. * * Return value: the height of the #GTree. **/ gint g_tree_height (GTree *tree) { g_return_val_if_fail (tree != NULL, 0); if (tree->root) return g_tree_node_height (tree->root); else return 0; }
static void g_tree_node_check (GTreeNode *node) { gint left_height; gint right_height; gint balance; GTreeNode *tmp; if (node) { if (node->left_child) { tmp = g_tree_node_previous (node); g_assert (tmp->right == node); } if (node->right_child) { tmp = g_tree_node_next (node); g_assert (tmp->left == node); } left_height = 0; right_height = 0; if (node->left_child) left_height = g_tree_node_height (node->left); if (node->right_child) right_height = g_tree_node_height (node->right); balance = right_height - left_height; g_assert (balance == node->balance); if (node->left_child) g_tree_node_check (node->left); if (node->right_child) g_tree_node_check (node->right); } }
static gint g_tree_node_height (GTreeNode *node) { gint left_height; gint right_height; if (node) { left_height = 0; right_height = 0; if (node->left_child) left_height = g_tree_node_height (node->left); if (node->right_child) right_height = g_tree_node_height (node->right); return MAX (left_height, right_height) + 1; } return 0; }