// Retrun the balance a of BinarySearchTreeNode. int tree_balance(BinarySearchTreeNode *node, int *height) { if (NULL == node) { *height = 0; return 0; } int balance = 0; int left_height = 0; int left_balance = tree_balance(node->left, &left_height); int right_height = 0; int right_balance = tree_balance(node->right, &right_height); if (left_height >= right_height) { *height = left_height+1; balance = left_height-(right_height-right_balance); } else { *height = right_height+1; balance = right_height-(left_height-left_balance); } return balance; }
/** * Rebalance the dictionary tree. * This recomputes the tree depth wayy too often, but so what.. this * only wastes cpu time during the initial dictinary read. */ static Dict_node *rebalance(Dict_node *root) { int bal = tree_balance(root); if (2 == bal) { bal = tree_balance(root->right); if (-1 == bal) { root->right = rotate_right (root->right); } return rotate_left(root); } else if (-2 == bal) { bal = tree_balance(root->left); if (1 == bal) { root->left = rotate_left (root->left); } return rotate_right(root); } return root; }
int balance_areatree(int cycle) { grid_update( grid, n_grid_units, m_grid_units, grid_unit_sz_x, grid_unit_sz_y, 0, thread_stats); //balance if( !atree ) { atree = (tree_t*)malloc(sizeof(tree_t)); if (atree == NULL) return 0; tree_init(atree, NULL, tm_wm.map_r, DIR_UP, tm_wm.depth); } grid_set_owner_no_tid( grid, n_grid_units, m_grid_units, thread_stats ); tree_balance(atree); grid_assign_players( grid, grid_unit_sz_x, grid_unit_sz_y ); return 1; }
struct node *tree_delete(struct tree *t, char *a) { struct node *n, *m, **s; int c; if (t == 0 || a == 0) { fputs("Null pointer.\n", stderr); exit(EXIT_FAILURE); } /* locate node m with name a */ m = t->root; while (m != 0) { c = strcmp(a, m->name); if (c < 0) m = m->left; else if (c > 0) m = m->rght; else break; } if (m == 0) return 0; /* set s to point to place where m is saved */ if (m->root == 0) s = &t->root; else s = m == m->root->left ? &m->root->left : &m->root->rght; /* remove node m from list next, prev */ if (m->prev != 0) m->prev->next = m->next; else t->frst = m->next; if (m->next != 0) m->next->prev = m->prev; else t->last = m->prev; /* remove node from tree */ if (m->left == 0) { *s = m->rght; if (m->rght != 0) m->rght->root = m->root; if (m->root != 0) tree_balance(t, m->root); } else if (m->rght == 0) { *s = m->left; m->left->root = m->root; if (m->root != 0) tree_balance(t, m->root); } else { /* replace node with righmost node of left subtree */ *s = m->prev; if (m->prev == m->left) { n = m->prev; } else { n = m->prev->root; n->rght = m->prev->left; if (n->rght != 0) n->rght->root = n; m->prev->left = m->left; m->left->root = m->prev; } m->prev->rght = m->rght; m->prev->root = m->root; m->rght->root = m->prev; tree_balance(t, n); } return m; }
struct node *tree_insert(struct tree *t, char *a) { struct node *n, *m; int c; if (t == 0 || a == 0) { fputs("Null pointer.\n", stderr); exit(EXIT_FAILURE); } m = GC_malloc(sizeof(struct node)); if (m == 0) { fputs("Out of memory.\n", stderr); exit(EXIT_FAILURE); } m->name = a; m->vlue = 0; m->tree = t; m->left = m->rght = 0; m->dpth = 1; if (t->root == 0) { m->prev = m->next = m->root = 0; t->frst = t->last = t->root = m; return m; } n = t->root; for (;;) { c = strcmp(m->name, n->name); if (c < 0) { if (n->left != 0) { n = n->left; } else { m->root = n; if (n->prev != 0) { m->prev = n->prev; n->prev->next = m; } else { m->prev = 0; t->frst = m; } m->next = n; n->prev = m; n->left = m; break; } } else if (c > 0) { if (n->rght != 0) { n = n->rght; } else { m->root = n; m->prev = n; if (n->next != 0) { m->next = n->next; n->next->prev = m; } else { m->next = 0; t->last = m; } n->next = m; n->rght = m; break; } } else { return 0; } } tree_balance(t, n); return m; }
// Tell if a BinarySearchTree is balanced. // @return: true if the BinarySearchTree is balanced. false otherwise. bool tree_balanced(BinarySearchTree *tree) { int height = 0; if (2 > tree_balance(tree->root, &height)) return true; else return false; }