void delete_rec(struct node *root, unsigned int key) { if (root->key > key) { if (root->left == NULL) return; if (root->left->key == key) { root->left = delete_node(root->left); } else { delete_rec(root->left, key); set_height(root->left); root->left = rebalance(root->left); } } else { if (root->right == NULL) return; if (root->right->key == key) { root->right = delete_node(root->right); } else { delete_rec(root->right, key); set_height(root->right); root->right = rebalance(root->right); } } return; }
static gpr_avl_node *remove(const gpr_avl_vtable *vtable, gpr_avl_node *node, void *key) { long cmp; if (node == NULL) { return NULL; } cmp = vtable->compare_keys(node->key, key); if (cmp == 0) { if (node->left == NULL) { return ref_node(node->right); } else if (node->right == NULL) { return ref_node(node->left); } else if (node->left->height < node->right->height) { gpr_avl_node *h = in_order_head(node->right); return rebalance(vtable, vtable->copy_key(h->key), vtable->copy_value(h->value), ref_node(node->left), remove(vtable, node->right, h->key)); } else { gpr_avl_node *h = in_order_tail(node->left); return rebalance( vtable, vtable->copy_key(h->key), vtable->copy_value(h->value), remove(vtable, node->left, h->key), ref_node(node->right)); } } else if (cmp > 0) { return rebalance(vtable, vtable->copy_key(node->key), vtable->copy_value(node->value), remove(vtable, node->left, key), ref_node(node->right)); } else { return rebalance(vtable, vtable->copy_key(node->key), vtable->copy_value(node->value), ref_node(node->left), remove(vtable, node->right, key)); } }
Node* Node::insert ( Node* node ) { // We don't allow duplicate values right now assert(node->value_ != value_); if (node->value_ < value_) { // Go left if (left_) { left_ = left_->insert(node); } else { left_ = node; } return rebalance(); } // Go right if (right_) { right_ = right_->insert(node); } else { right_ = node; } return rebalance(); }
Node* Node::extract ( int value, Node** extracted ) { if (value < value_) { // Go left if (left_) { left_ = left_->extract(value, extracted); } else { *extracted = nullptr; } return rebalance(); } if (value > value_) { // Go right if (right_) { right_ = right_->extract(value, extracted); } else { *extracted = nullptr; } return rebalance(); } // We are the node to remove. Swap ourselves // with the in-order predecessor if there is // one, else just return the right subtree if (!left_) { Node* new_root = right_; *extracted = this; left_ = nullptr; right_ = nullptr; updateHeightAndCount(); return new_root; } // Find the in-order predecessor and swap Node* predecessor; left_ = left_->extractMaximum(&predecessor); predecessor->left_ = left_; predecessor->right_ = right_; predecessor->updateHeightAndCount(); *extracted = this; left_ = nullptr; right_ = nullptr; updateHeightAndCount(); return predecessor->rebalance(); }
void team_deathmatch::update(int dt, const plane_controls &player_controls) { deathmatch::update(dt, player_controls); if (!m_world.is_host()) return; if (m_last_planes_count != m_world.get_planes_count()) { m_last_planes_count = m_world.get_planes_count(); rebalance(); } else if (m_world.net_data_updated()) rebalance(); }
struct node *delete_node(struct node *root) { struct node *child, *aux; unsigned int key; if (root->left == NULL) { aux = root->right; free(root); return aux; } else if (root->right == NULL) { aux = root->left; free(root); return aux; } else { if (root->right->left == NULL) { child = root->right; root->value = child->value; root->key = child->key; root->right = child->right; free(child); } else { key = root->key; child = node_min(root->right); root->value = child->value; root->key = child->key; child->key = key; delete_rec(root, key); } set_height(root); return rebalance(root); } }
NodePtr AvlTree<T>::add_key(const NodePtr& new_node) { if(!m_root) { set_root(new_node); } else { auto node = m_root; while(node) { if(new_node->get_value() == node->get_value()) { return nullptr; } if(new_node->get_value() < node->get_value()) { if(!node->get_lson()) { node->set_lson(new_node); break; } node = node->get_lson(); } if(new_node->get_value() > node->get_value()) { if(!node->get_rson()) { node->set_rson(new_node); break; } node = node->get_rson(); } } new_node->set_parent(node); } m_size++; rebalance(new_node); return new_node; }
/** * Private method insert * Private method for inserting a given node in a given subtree * @param newNode the new node being inserted * @see Cormen, Leiserson, Rivest and Stein - Introduction to * Algorithms, 3rd Edition * @see http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-006-introduction-to-algorithms-fall-2011/lecture-videos/MIT6_006F11_lec06_orig.pdf */ void AVLTree::insert(node *newNode) { node *y = NULL; node *x = root; //Find insertion position while (x != NULL) { y = x; if (newNode->key < x->key) { x = x->leftChild; } else { x = x->rightChild; } } //Set position node y as new node's parent newNode->parent = y; if (y == NULL) { root = newNode; } else if (newNode->key < y->key) { y->leftChild = newNode; } else { y->rightChild = newNode; } x = newNode; rebalance(x); }
static int insert_ptr(NODE **rootaddr, NODE *node, int (*usrcmp)(void *, void *), int dup) { NODE *root = *rootaddr; int cmp; int ins; SET_PTRCMP(cmp, usrcmp, node->key.ptr, root->key.ptr) if (cmp < 0) { if (root->left) ins = insert_ptr(&root->left, node, usrcmp, dup); else { root->left = node; ins = DEEPER; } switch (ins) { CASE DEEPER : switch (root->bal) { CASE RIGHT : root->bal = BAL; return INS; CASE BAL : root->bal = LEFT; return DEEPER; CASE LEFT : root->bal = LEFTUNBAL; return rebalance(rootaddr) == LESS ? INS : DEEPER; } CASE INS : return INS; CASE NOTINS : return NOTINS; } } else if (cmp > 0 || dup) {
void bridge_docrange_rep::notify_remove (path p, int nr) { // cout << "Notify insert " << p << ", " << nr // << " [ " << begin << "--" << end << " ]\n"; ASSERT (!is_nil (p), "erroneous nil path"); ASSERT (p->item < end, "out of range"); if (p->item + nr > begin) { status= CORRUPTED; begin= min (begin , p->item); end = max (end-nr, p->item); } else { begin -= nr; end -= nr; } if (divide) { int i, n= N(acc); for (i=0; i<n; i++) if (p->item < mid[i+1]) break; for (; i<n; i++) { acc[i]->notify_remove (p, nr); mid[i+1]= max (mid[i+1]-nr, p->item); } // cout << "mid[rem,0]= " << mid << "\n"; rebalance (); // cout << "mid[rem,1]= " << mid << "\n"; } }
static void fixup_after_deletion(Ttree *ttree, TtreeNode *n, TtreeCursor *cursor) { TtreeNode *node = n->parent; int bfc_delta = get_bfc_delta(n); __remove_successor(n); /* * Unlike balance fixing after insertion, * deletion may require several rotations. */ while (node) { node->bfc -= bfc_delta; /* * If node's balance factor was 0 and becomes 1 or -1, we can stop. */ if (!(node->bfc + bfc_delta)) break; bfc_delta = get_bfc_delta(node); if (subtree_is_unbalanced(node)) { TtreeNode *tmp = node; rebalance(ttree, &tmp, cursor); /* * If after rotation subtree height is not changed, * proccess should be continued. */ if (tmp->bfc) break; node = tmp; } node = node->parent; } }
void bridge_docrange_rep::notify_insert (path p, tree u) { // cout << "Notify insert " << p << ", " << N(u) // << " [ " << begin << "--" << end << " ]\n"; ASSERT (!is_nil (p), "erroneous nil path"); if (p->item > end) { cerr << "\nNotify insert " << u << " at " << p << "\n"; FAILED ("out of range"); } if (p->item >= begin) status= CORRUPTED; else begin += N(u); end += N(u); if (divide) { int i, n= N(acc); for (i=0; i<n; i++) if (p->item < mid[i+1]) break; if (i==n) i--; for (; i<n; i++) { acc[i]->notify_insert (p, u); mid[i+1] += N(u); } // cout << "mid[ins,0]= " << mid << "\n"; rebalance (); // cout << "mid[ins,1]= " << mid << "\n"; } }
/** * Finds the right-most leaf in an AVL tree * * @param[in] tree The tree * @param[in] walkSRPPtr A pointer to the root of the tree to search * @param[in|out] heightChange The height change * * @return The found leaf or null in the case of an error */ static J9AVLTreeNode * findRightMostLeaf(J9AVLTree *tree, J9WSRP *walkSRPPtr, intptr_t *heightChange) { J9AVLTreeNode *find; J9AVLTreeNode *walk = NULL; Trc_AVL_findRightMostLeaf_Entry(tree, walkSRPPtr, heightChange); walk = AVL_SRP_GETNODE(*walkSRPPtr); if (!walk) { Trc_AVL_findRightMostLeaf_NotFound(); return NULL; } find = findRightMostLeaf(tree, &(walk->rightChild), heightChange); if (!find) { /* this is the last node in the list */ find = walk; /* the predecessor at most can have one left child node -- or it is null */ AVL_SRP_PTR_SETNODE(walkSRPPtr, AVL_SRP_GETNODE(find->leftChild)); AVL_SRP_SET_TO_NULL(find->leftChild); *heightChange = -1; if (tree->genericActionHook) { tree->genericActionHook(tree, walk, J9AVLTREE_ACTION_REPLACE_REMOVED_PARENT); } } else { rebalance(tree, NULL, walkSRPPtr, 1, heightChange); } Trc_AVL_findRightMostLeaf_Exit(find); return find; }
//An easy to use, do-everything-in-one-call sort of function commandList GalconAI::update(std::list<Planet> & planets, const std::list<Fleet> & fleets, const std::vector<ShipStats> & shipstats, std::vector<std::list<Building*> > buildRules) { //Set up the list of commands commandList rb; //See if we have waited long enough and the AI is active int time = SDL_GetTicks(); if ((time - updateTime_ < set_.delay && updateTime_ != -1) || !active_) return rb; updateTime_ = time; std::cout << "Update) Attack: " << attTotal_ << " Defense: " << defTotal_ << std::endl; //Compute the best target computeTarget(planets, fleets, shipstats); //Get the commands from rebalancing, attacking, and building rb = rebalance(fleets, shipstats); commandList at = attack(shipstats); commandList bd = build(buildRules, shipstats); //Append at to rb for (commandList::iterator i = at.begin(); i != at.end(); i++) { rb.push_back(*i); } for (commandList::iterator i = bd.begin(); i != bd.end(); i++) { rb.push_back(*i); } //Return the combined list of commands return rb; }
static void fixup_after_insertion(Ttree *ttree, TtreeNode *n, TtreeCursor *cursor) { int bfc_delta = get_bfc_delta(n); TtreeNode *node = n; __add_successor(n); /* check tree for balance after new node was added. */ while ((node = node->parent)) { node->bfc += bfc_delta; /* * if node becomes balanced, tree balance is ok, * so process may be stopped here */ if (!node->bfc) { return; } if (subtree_is_unbalanced(node)) { /* * Because of nature of T-tree rebalancing, just inserted item * may change its position in its node and even the node itself. * Thus if T-tree cursor was specified we have to take care of it. */ rebalance(ttree, &node, cursor); /* * single or double rotation tree becomes balanced * and we can stop here. */ return; } bfc_delta = get_bfc_delta(node); } }
/*! This method returns 'true' if the element has successfully been inserted into the tree. And 'false' otherwise */ bool insert(const _Tp& element) { std::cout << "\n**********************" << std::endl; std::cout << "Inserting : " << element << std::endl; if( not get_root() ) { assert(mSentinel->add_child(element, Direction::RIGHT)); mSize += 1; return true; } // Get the pointer to the root node, and call it // 'currentNode'. Get a pointer to the root node's parent // and call it 'currentParent' node_ptr_t currentNode = avl_node<_Tp>::get_link(get_root()); node_ptr_t currentParent = currentNode; Direction dir = Direction::NULL_DIRECTION; // Walk down the tree while( currentNode != nullptr) { std::cout << "Current node : " << currentNode->mNodeValue << std::endl; std::cout << "Current node at : " << currentNode << std::endl; std::cout << "Current root at : " << get_root() << std::endl; // Check to see if 'element' lies to the left or to // the right of 'currentNode' if( Cmp()(element, currentNode->mNodeValue) ) { // Go left currentParent = currentNode; currentNode = avl_node<_Tp>::get_link(currentNode->left); dir = Direction::LEFT; std::cout << "Going left" << std::endl; } else if( Cmp()(currentNode->mNodeValue, element) ) { // Go right currentParent = currentNode; currentNode = avl_node<_Tp>::get_link(currentNode->right); dir = Direction::RIGHT; std::cout << "Going right" << std::endl; } else { // (a < b = false) and (b < a = false) => (a == b) // 'element' already exists in the tree. return 'false' return false; } } mSize += 1; // 'currentParent' points to a leaf node. and 'dir' records // if we went left or right from that leaf node assert(currentParent->add_child(element, dir)); std::cout << "Number of elements in tree = " << mSize << std::endl; std::cout << "Element at root before re-balance = " << avl_node<_Tp>::get_link(get_root())->mNodeValue << std::endl; std::cout << "Height of tree before re-balance = " << height() << std::endl; std::cout << "location of root before rebalance = " << get_root() << std::endl; rebalance(currentParent); std::cout << "Element at root after re-balance = " << avl_node<_Tp>::get_link(get_root())->mNodeValue << std::endl; std::cout << "Height of tree after re-balance = " << height() << std::endl; std::cout << "location of root after rebalance = " << get_root() << std::endl; std::cout << "**********************" << std::endl; return true; }
/** * Inserts a node into an AVL tree. * This function should always be called externally with walkSRPPtr being NULL. * All recursive calls will have walkPtr==NULL and a value for walkSRPPtr. * * IMPORTANT: It is the responsibility of the caller to ensure that the nodes are aligned * * @param[in] tree The tree * @param[in] walkPtr A pointer to the root of the tree * @param[in] walkSRPPtr Should always be NULL (only used when the function calls itself) * @param[in] node The node to insert (can be a node or an SRP node) * @param[in|out] heightChange The height change * * @return The node inserted */ static J9AVLTreeNode * insertNode(J9AVLTree *tree, J9AVLTreeNode **walkPtr, J9WSRP *walkSRPPtr, J9AVLTreeNode *node, intptr_t *heightChange) { J9AVLTreeNode *find = NULL; J9AVLTreeNode *walk; intptr_t dir; Trc_AVL_insertNode_Entry(tree, walkPtr, walkSRPPtr, node, heightChange); if (!node) { goto _done; } /* Assumption is that if walkSRPPtr is NULL, walkPtr is not */ if (!walkSRPPtr) { walk = AVL_GETNODE(*walkPtr); } else { walk = AVL_SRP_GETNODE(*walkSRPPtr); } if (!walk) { if (!walkSRPPtr) { AVL_SETNODE(*walkPtr, node); /* add the node */ } else { AVL_NNSRP_PTR_SETNODE(walkSRPPtr, node); } *heightChange = 1; /* height of this tree increased one */ if (tree->genericActionHook) { tree->genericActionHook(tree, node, J9AVLTREE_ACTION_INSERT); } Trc_AVL_insertNode_Trivial(node); return node; } dir = tree->insertionComparator(tree, node, walk); if (!dir) { /* node is already in the tree */ *heightChange = 0; /* no change in tree structure */ if (tree->genericActionHook) { tree->genericActionHook(tree, walk, J9AVLTREE_ACTION_INSERT_EXISTS); } Trc_AVL_insertNode_Exists(walk); return walk; } if (dir < 0) { find = insertNode(tree, NULL, &(walk->leftChild), node, heightChange); } else { find = insertNode(tree, NULL, &(walk->rightChild), node, heightChange); } /* if we added a node */ if ((find == node) && (*heightChange)) { rebalance(tree, walkPtr, walkSRPPtr, dir, heightChange); } _done : Trc_AVL_insertNode_Recursive(find); return find; }
void BNode::last_rebalance(){ if( size_c <= 0) return; if( !children[size_c-1]->is_leaf() ) children[size_c-1]->last_rebalance(); rebalance(children[size_c-1], size_c-1); }
void AvlTree<T>::rebalance(const NodePtr& node) { if(!node) { return; } // TODO: rotations node->set_depth(1 + max(node->get_lson_depth(), node->get_rson_depth())); rebalance(node->get_parent()); return; }
static gpr_avl_node *add(const gpr_avl_vtable *vtable, gpr_avl_node *node, void *key, void *value) { long cmp; if (node == NULL) { return new_node(key, value, NULL, NULL); } cmp = vtable->compare_keys(node->key, key); if (cmp == 0) { return new_node(key, value, ref_node(node->left), ref_node(node->right)); } else if (cmp > 0) { return rebalance( vtable, vtable->copy_key(node->key), vtable->copy_value(node->value), add(vtable, node->left, key, value), ref_node(node->right)); } else { return rebalance(vtable, vtable->copy_key(node->key), vtable->copy_value(node->value), ref_node(node->left), add(vtable, node->right, key, value)); } }
/* * Attempts to remove an element from the array. * Returns 0 if unsuccessful (element did not exist), * or the number of moves made if successful (will * likely be greater than 0). * * NOTE: The element is not actually removed. Rather, * it's location is simply set to unused. */ int PMA_remove(PMA* pma, int elem){ int index = find(pma, elem, 1); if (index == -1){ return 0; } CLRBIT(pma->bitmap, index); pma->count--; return rebalance(pma, index, 0, 0); }
Node* Node::extractMaximum ( Node** extracted ) { if (!right_) { // We are the max; pull ourselves out *extracted = this; return left_; } right_ = right_->extractMaximum(extracted); return rebalance(); }
/* * Inserts an element into the packed memory array. * Returns the number of moves (0 if not rebalanced). */ int PMA_insert(PMA* pma, int elem){ int index = find(pma, elem, 0); pma->count++; if (GETBIT(pma->bitmap, index)){ return rebalance(pma, index, 1, elem); } pma->array[index] = elem; SETBIT(pma->bitmap, index); return 0; }
void dict_delete(struct dictionary *d, unsigned int key) { if (d->root == NULL) return; if (d->root->key == key) { d->root = delete_node(d->root); } else { delete_rec(d->root, key); } set_height(d->root); d->root = rebalance(d->root); }
/* * here once per clock tick to see if we should resched */ void hzsched(void) { /* once a second, rebalance will reprioritize ready procs */ if(m->machno == 0) rebalance(); /* unless preempted, get to run for at least 100ms */ if(anyhigher() || (!up->fixedpri && m->ticks > m->schedticks && anyready())){ m->readied = nil; /* avoid cooperative scheduling */ up->delaysched++; } }
struct node *insert_rec(struct node *root, struct node *new_node) { struct node *aux; if (root == NULL) return new_node; if (root-> key > new_node->key) { root->left = insert_rec(root->left, new_node); } else { root->right = insert_rec(root->right, new_node); } aux = rebalance(root); set_height(aux); return aux; }
struct node *swap_with_left(struct node *dad, struct node *child) { struct node *aux; if (child->right->right != NULL) { child->right = swap_with_left(dad, child->right); } else { dad->key = child->right->key; dad->value = child->right->value; aux = child->right; child->right = child->right->left; free(aux); } set_height(dad); rebalance(dad); return child; }
bool AvlTree::add(double x, int w) { if(_root == NIL) { _root = ++_n; _values[_root] = x; _count[_root] = w; _left[_root] = NIL; _right[_root] = NIL; _parent[_root] = NIL; // Update depth and aggregates updateAggregates(_root); } else { int node = _root; int parent = NIL; int cmp; do { cmp = compare(node, x); if(cmp < 0) { parent = node; node = leftNode(node); } else if (cmp > 0) { parent = node; node = rightNode(node); } else { // we merge the node merge(node, x, w); return false; } } while(node != NIL); node = ++_n; _values[node] = x; _count[node] = w; _left[node] = NIL; _right[node] = NIL; _parent[node] = parent; if(cmp < 0) { _left[parent] = node; } else { assert(cmp > 0); _right[parent] = node; } rebalance(node); return true; } }
void hzsched(void) { Proc *up = externup(); /* once a second, rebalance will reprioritize ready procs */ if(machp()->machno == 0) rebalance(); /* with <= 4 cores, we use SMP and core 0 does not set qexpired for us */ //if(sys->nmach <= AMPmincores) if(machp()->ticks - machp()->qstart >= HZ/100) machp()->qexpired = 1; /* unless preempted, get to run */ if(machp()->qexpired && anyready()) up->delaysched++; }
static void *avl_insert(cp_avltree *tree, /* the tree */ cp_avlnode **node, /* current node */ cp_avlnode *parent, /* parent node */ void *key, void *value) /* new entry */ { void *res = NULL; int before = (*node)->balance; int cmp = tree->cmp((*node)->key, key); if (cmp == 0) return update_avlnode(tree, *node, key, value); if (cmp > 0) { /* go left */ if ((*node)->left) res = avl_insert(tree, &(*node)->left, *node, key, value); else { (*node)->left = create_avlnode(tree, key, value); if ((*node)->left == NULL) return NULL; res = (*node)->left->value; (*node)->balance--; tree->items++; } } else /* go right */ { if ((*node)->right) res = avl_insert(tree, &(*node)->right, *node, key, value); else { (*node)->right = create_avlnode(tree, key, value); if ((*node)->right == NULL) return NULL; res = (*node)->right->value; (*node)->balance++; tree->items++; } } if (parent && (*node)->balance && before == 0) parent->balance += (parent->left == *node ? (-1) : (+1)); rebalance(node); return res; }