//Create a tree with the given data, left subtree, and right subtree then return the tree. //O(1) struct tree *createTree(int data, struct tree *left, struct tree *right){ //create the root struct tree *root; root=malloc(sizeof(struct tree)); //Assign that data and pointers of the new tree root->data=data; root->right=right; root->left=left; root->parent=NULL; //If the right and left parents are not NULL, the left and right nodes came from another tree, and need to be detatched before adding to our tree if(right!=NULL){ detatch(right); root->right->parent=root; } if(left!=NULL){ detatch(left); root->left->parent=root; } return root; }
void irept::move_to_sub(irept &irep) { #ifdef SHARING detatch(); #endif get_sub().push_back(get_nil_irep()); get_sub().back().swap(irep); }
void irept::move_to_named_sub(const irep_namet &name, irept &irep) { #ifdef SHARING detatch(); #endif add(name).swap(irep); irep.clear(); }
//Assings the right subtree of the given node to the given right subtree //O(1) void setRight(struct tree *root, struct tree *right){ assert(root!=NULL); //If the root's right is not null, we need to set it's parent pointer to null so it can be reassigned if(root->right!=NULL) root->right->parent=NULL; //Now we can set the root's left subtree to the given left, and make sure it is not attatched to some other tree root->right=right; detatch(right); }
//moves person of id x to to district i, j int gw_move(GW g, int x, int i, int j) { if(x >= g->pop) { return 0; } PERSON *p = &(g->people[x]); DISCT *from = &(g->grid[p->r][p->c]); DISCT *to = &(g->grid[i][j]); if(p->isAlive == 0) { return 0; } detatch(from, p); //remove from current district p->r = i; p->c = j; add(to, p); // add to new district to->pop++; // increase population of new district return 1; }
//kill the person with id x int gw_kill(GW g, int x) { PERSON *p = &(g->people[x]); if(p->isAlive == 0) { return 0; } detatch(&(g->grid[p->r][p->c]), p); if(g->dead == NULL) { p->next = NULL; } else { p->next = g->dead; } p->prev = NULL; g->dead = p; p->isAlive = 0; g->pop--; return 1; }