void rotate_left(rb_node* pivot, rb_tree* tree) { #ifdef DEBUG is_correct(tree); assert(!is_leaf(pivot->right, tree)); #endif rb_node* root = pivot->right; rb_node* move = root->left; root->parent = pivot->parent; pivot->right = move; if(!is_leaf(move, tree)) move->parent = pivot; root->left = pivot; pivot->parent = root; if(root->parent) { if (pivot == root->parent->left) root->parent->left = root; else root->parent->right = root; } else { tree->root = root; } #ifdef DEBUG is_correct(tree); #endif }
void SetNodeReg::cleave(const IntervalVector& box, Ctc& Ctcin, Ctc& Ctcout, const double eps) { if(is_leaf() && status == OUT) return; IntervalVector box1(box); IntervalVector box2(box); //try{Ctcin.contract(box1);} //catch(EmptyBoxException&){inter(OUT);return;} Ctcin.contract(box1); if(box1.is_empty()) { inter(OUT); return; } //try{Ctcout.contract(box2);} //catch(EmptyBoxException&){_union(IN);return;} Ctcout.contract(box2); if(box2.is_empty()) { _union(IN);return; } if(is_leaf()) { if(box[var].diam()>eps) { cut(box); left->cleave(left_box(box),Ctcin,Ctcout,eps); right->cleave(right_box(box),Ctcin,Ctcout,eps); } else status = inte(status,UNK); } else { left->cleave(left_box(box),Ctcin,Ctcout,eps); right->cleave(right_box(box),Ctcin,Ctcout,eps); } }
void SetNodeReg::cleave(const IntervalVector& box, Sep& sep, const double eps) { if(is_leaf() && status == OUT) return; IntervalVector box1(box); IntervalVector box2(box); sep.separate(box1,box2); if(box1.is_empty()){ inter(OUT); // cout<<"box: "<<box<<" set to OUT"<<endl; } else if(box2.is_empty()){ _union(IN); cout<<"box: "<<box<<" set to IN"<<endl; } else // continu until box1 and box2 are disjoint { if(is_leaf()) { if(box[var].diam()>eps) { cut(box); left->cleave(left_box(box),sep,eps); right->cleave(right_box(box),sep,eps); } else status = inte(status,UNK); } else { left->cleave(left_box(box),sep,eps); right->cleave(right_box(box),sep,eps); } } }
bool set::insert(const T& item) { if (!loose_insert(item)) return false; if (entry_count > MAX) { set *left = new set; set *right = new set; std::memmove(left->data, data, MIN*sizeof(T)); std::memmove(left->subset, subset, (MIN+1)*sizeof(set*)); left->entry_count = MIN; left->child_count = is_leaf() ? 0 : (MIN+1); std::memmove(right->data, &(data[MIN+1]), MIN*sizeof(T)); std::memmove(right->subset, &(subset[MIN+1]),(MIN+1)*sizeof(set*)); right->entry_count = MIN; right->child_count = is_leaf() ? 0 : (MIN+1); data[0] = data[MIN]; entry_count = 1; subset[0] = left; subset[1] = right; child_count = 2; } ++cardinal; return true; }
void set::print() { for (int i = 0; i<entry_count; i++) { if (!is_leaf()) subset[i]->print(); std::cout << data[i] << " "; } if (!is_leaf()) subset[child_count-1]->print(); }
int btree_delete_ex (PBTREE btree, int node_idx, long target_key) { // target is just a package for the key value. the reference does not // provide the address of the Elem instance to be deleted. // first find the node contain the Elem instance with the given key int parent_index_this = BTREE_INVALID_ELEMENT_IDX; PBTREE_ELEMENT found; int last_visted_node_idx; if (node_idx == BTREE_INVALID_NODE_IDX) node_idx = btree->root_node_idx; last_visted_node_idx = node_idx; found = btree_search_ex (btree, &last_visted_node_idx, target_key); if (!found) return 0; if (is_leaf(btree, last_visted_node_idx) && key_count(btree, last_visted_node_idx) > btree_minimum_keys()) return vector_delete (btree, last_visted_node_idx, target_key); else if (is_leaf(btree, last_visted_node_idx)) { vector_delete (btree, last_visted_node_idx, target_key); // loop invariant: if _node_ is not null_ptr, it points to a node // that has lost an element and needs to import one from a sibling // or merge with a sibling and import one from its parent. // after an iteration of the loop, _node_ may become null or // it may point to its parent if an element was imported from the // parent and this caused the parent to fall below the minimum // element count. while (BTREE_IS_VALID_NODE_IDX(last_visted_node_idx)) { int right, left; // NOTE: the "this" pointer may no longer be valid after the first // iteration of this loop!!! if (last_visted_node_idx == find_root(btree) && is_leaf(btree, last_visted_node_idx)) break; if (last_visted_node_idx == find_root(btree) && !is_leaf(btree, last_visted_node_idx)) // sanity check return 0; // is an extra element available from the right sibling (if any) right = right_sibling(btree, last_visted_node_idx, &parent_index_this); if (BTREE_IS_VALID_NODE_IDX(right) && key_count(btree, right) > btree_minimum_keys()) last_visted_node_idx = rotate_from_right(btree, last_visted_node_idx, parent_index_this); else { // is an extra element available from the left sibling (if any) left = left_sibling(btree, last_visted_node_idx, &parent_index_this); if (BTREE_IS_VALID_NODE_IDX(left) && key_count(btree, left) > btree_minimum_keys()) last_visted_node_idx = rotate_from_left(btree, last_visted_node_idx, parent_index_this); else if (BTREE_IS_VALID_NODE_IDX(right)) last_visted_node_idx = merge_right(btree, last_visted_node_idx, parent_index_this); else if (BTREE_IS_VALID_NODE_IDX(left)) last_visted_node_idx = merge_left(btree, last_visted_node_idx, parent_index_this); } } } else { PBTREE_ELEMENT smallest_in_subtree = smallest_key_in_subtree(btree, found->subtree_node_idx); found->key = smallest_in_subtree->key; found->data_entry_idx = smallest_in_subtree->data_entry_idx; btree_delete_ex (btree, found->subtree_node_idx, smallest_in_subtree->key); } return 1; }
void rb_delete(rb_node* n, rb_tree* tree) { #ifdef DEBUG if(!is_leaf(n->left, tree)) assert(is_leaf(n->right, tree)); if(!is_leaf(n->right, tree)) assert(is_leaf(n->left, tree)); #endif delete_one_child(n, tree); tree->n--; }
// only c1 and c2 can be modified static void interact(const struct cell *c1, const struct cell *c2) { if (is_leaf(c1) && is_leaf(c2)) { direct(c1, c2); } else if (is_leaf(c1)) { map(interact, product(c1, get_subcells(c2))); } else if (is_leaf(c2)) { map(interact, product(get_subcells(c1), c2)); } else { map(interact, product(get_subcells(c1), get_subcells(c2))); } }
void infix(Pointer<T> pointer) { if(pointer) { if(! is_leaf(pointer)) std::cout << "("; infix(pointer->left_); std::cout << pointer->val_; infix(pointer->right_); if(! is_leaf(pointer)) std::cout << ")"; } }
int check_tree(btree *tree){ /* We need to treat the root specially since it can violate some properties */ btree_node *root = tree->root; if(root->n_keys == 0){ return 0; } if(root->n_keys > max_keys){ HERE_FMT("Too many keys in the root\n"); return -1; } if(is_leaf(root)){ uint64_t min = 0; int i; for(i=0;i<root->n_keys;i++){ if(root->keys[i] < min){ HERE_FMT("key %d = %lu < key %d = %lu\n", i, root->keys[i], i-1, root->keys[i-1]); return -1; } min = root->keys[i]; } return 0 ; } int i = 0; //we use i<=root->n_keys since btree nodes have one child more //then they do keys uint64_t min = 0; uint64_t max = root->keys[0]; //Check to make sure all nodes on the same level are either //leaves or internal nodes, not a mix of the two int leaf_status = is_leaf(root->children[i]); for(i=0;i<=root->n_keys;i++){ WARN_ONCE(min > max, "key %d > key %d\n",i,i-1); if(leaf_status != is_leaf(root->children[i])){ HERE_FMT("All leaves are not the same depth"); return -1; } int err = check_node(root->children[i], min, max); if(err){ return err; } min = root->keys[i]; max = (i+1 == root->n_keys ? UINT64_MAX : root->keys[i+1]); } return 0; }
struct node* tree_to_list(struct node* root) { if (root == NULL) return root; if (is_leaf(root)) { root->left = root; root->right = root; return root; } struct node* head = NULL; struct node* end = NULL; struct node* temp = NULL; if (root->left != NULL) { helper(root->left, &head, &end); root->left = end; root->left->right = root; temp = head; } else { temp = root; } if (root->right != NULL) { helper(root->right, &head, &end); root->right = head; root->right->left = root; } else { end = root; } temp->left = end; end->right = temp; return temp; }
int leaf_count(Node node){ if(!node) return 0; else if(is_leaf(node)) return 1; else return(leaf_count(node->left) + leaf_count(node->right)); }
p_queue::size_type p_queue::big_child_index(size_type i) const // Pre: is_leaf(i) returns false // Post: The index of "the bigger child of the item at heap[i]" // has been returned. // (The bigger child is the one whose priority is no smaller // than that of the other child, if there is one.) { assert( !is_leaf(i) ); size_type index1 = 2 * i + 1, index2 = 2 * i + 2; if( index1 <= used - 1 && index2 > used - 1 ) return index1; else if( index1 > used - 1 && index2 <= used - 1 ) return index2; else if( heap[index1].priority > heap[index2].priority ) return index1; else return index2; }
/************************************************************************* * Compute the maximum depth of a tree. *************************************************************************/ int compute_depth (TREE_T * const a_tree) { /* Base case: leaf. */ if (is_leaf(a_tree)) { return(1); } /* Recursive case: internal node. */ else { int max_depth = 0; int num_children = get_num_children(a_tree); int i_child; for (i_child = 0; i_child < num_children; i_child++) { int this_depth = compute_depth(get_nth_child(i_child, a_tree)); if (this_depth > max_depth) { max_depth = this_depth; } } return(max_depth + 1); } /* Unreachable. */ abort(); return(0); }
/* This auxiliary function is only necessary because the tree must end with a semi-colon. */ static void write_tree_aux (TREE_T * a_tree, FILE * outfile) { int i_child; /* If it's a leaf, print it. */ if (is_leaf(a_tree)) { fprintf(outfile, "%s", a_tree->label); if (a_tree->has_length) { fprintf(outfile, ":%7.5f", a_tree->length); } } /* Otherwise, parenthesize and print the children with commas between. */ else { fprintf(outfile, "("); for (i_child = 0; i_child < a_tree->num_children - 1; i_child++) { write_tree_aux(a_tree->children[i_child], outfile); fprintf(outfile, ","); } write_tree_aux(a_tree->children[i_child], outfile); fprintf(outfile, ")"); if (a_tree->has_length) { fprintf(outfile, ":%7.5f", a_tree->length); } } }
static void logprint_qtree(QTREE_NODE *node, int depth) { BBOX b; int i; if (node == NULL) return; b = node->bb; if (!is_leaf(node)) { printlog("newline linethickness 0.3 pts %g %g %g %g %g %g %g %g %g %g\n", b.x, b.y, b.x+b.size, b.y, b.x+b.size, b.y+b.size, b.x, b.y+b.size, b.x, b.y); for (i = 0; i < N_NODES(node); i++) logprint_qtree(node->u.node[i], depth+1); } else { printlog("newline pts %g %g %g %g %g %g %g %g %g %g\n", b.x, b.y, b.x+b.size, b.y, b.x+b.size, b.y+b.size, b.x, b.y+b.size, b.x, b.y); /* if (node == NULL) printlog("newcurve marktype circle fill 1 pts %g %g\n", b.x+0.5*b.size, b.y+0.5*b.size); */ if (node->n_node > 0) { printlog("newcurve marktype cross pts"); for (i = 0; i < node->n_node; i++) printlog(" %g %g", node->u.list[i]->x, node->u.list[i]->y); printlog("\n"); } } }
/************************************************************************** * Compute and store the number of descendants of each node in a tree. **************************************************************************/ static void compute_descendants (TREE_T * a_tree, BOOLEAN_T force) { int i_child; int num_descendants; /* Don't do anything if we've already computed the descendants. */ if (!force && a_tree->has_descendants) { return; } /* Base case: A leaf has only itself as a descendant. */ if (is_leaf(a_tree)) { num_descendants = 1; a_tree->has_descendants = FALSE; } else { /* Recursive case: Descendants of each child. */ num_descendants = 0; a_tree->has_descendants = TRUE; for (i_child = 0; i_child < a_tree->num_children; i_child++) { compute_descendants(get_nth_child(i_child, a_tree), force); num_descendants += get_num_descendants(get_nth_child(i_child, a_tree), force); } } a_tree->num_descendants = num_descendants; }
void *hamt_insert(struct hamt_root *root, void *item) { uint128_t *item_hash = root->hash(root->hash_ud, item); assert(((unsigned long)item & ITEM_MASK) == 0); assert(item); if (unlikely(ston(root->slot) == NULL)) { root->slot = item_to_slot(item); return item; } struct hamt_state s; s.level = 0; s.ptr[0] = &root->slot; void *found_item = __hamt_search(root, item_hash, &s); if (unlikely(found_item != NULL)) { return found_item; } if (!is_leaf(s.ptr[s.level])) { __hamt_add_slot(root, s.ptr[s.level], item, item_hash, s.level); } else { void *leaf = to_leaf(s.ptr[s.level]); uint128_t *leaf_hash = root->hash(root->hash_ud, leaf); __hamt_insert_leaf(root, &s, item_hash, item, leaf_hash, leaf); } return item; }
void flatten(TreeNode* root) { if (!root) return; if (root->right) flatten(root->right); if (root->left) { if (is_leaf(root->left)) { root->left->right = root->right; root->right = root->left; root->left = NULL; return; } flatten(root->left); // var TreeNode* it = root->left; while (it->right) it = it->right; it->right = root->right; root->right = root->left; root->left = NULL; } }
void test_down(rb_node* n, rb_tree* tree) { assert(n->parent); assert(n == n->parent->left || n == n->parent->right); assert(n->left); assert(n->right); if(!is_leaf(n->left, tree)) { assert(n->key >= n->left->key); test_down(n->left, tree); assert(n->left != n->right); } if(!is_leaf(n->right, tree)) { assert(n->key <= n->right->key); test_down(n->right, tree); assert(n->left != n->right); } }
// search keywords in array Keys in the text T // Keys: the 2D char array holding keywords // T: the pointer to the text array // num_states: the number of states in the finite state machine, // this parameter need not to be exact, you can input the maximum number of states // num_keys: the number of keys in the keyword set // alpb: the size of the alphabet. void multi_pattern_search(char *T, char **Keys, int num_states, int num_keys, int alpb){ int **G = construct_trie_and_L(Keys, num_keys, num_states, alpb); int *F = construct_fsm_and_L(Keys,G, alpb, num_keys); int j = 0; int i = 1; int n = strlen(T) - 1; // characters in T are indexed from 1 for(; i <= n; i++){ while(j >= 0 && G[j][id(T[i])] == -1){ j = F[j]; } if(j == -1) j = 0; else { j = G[j][id(T[i])]; if(strlen(L[j]) > 0){ printf("the keyword found at state %d is: ", j); print_char_arr(L[j]); printf("\n"); int keylen = strlen(L[j])-1; // ignore the first empty character printf("the last pos of the text containing the keywords is: %d\n", i); } if(is_leaf(j, G, alpb)){ j = F[j]; } } } }
void list(desktop_t *d, node_t *n, char *rsp, unsigned int depth) { if (n == NULL) return; char line[MAXLEN]; for (unsigned int i = 0; i < depth; i++) strncat(rsp, " ", REMLEN(rsp)); if (is_leaf(n)) { client_t *c = n->client; snprintf(line, sizeof(line), "%c %s %X %u %u %ux%u%+i%+i %c%c%c%c%c", (c->born_as == MODE_AUTOMATIC ? 'a' : 'm'), c->class_name, c->window, c->uid, c->border_width, c->floating_rectangle.width, c->floating_rectangle.height, c->floating_rectangle.x, c->floating_rectangle.y, (c->floating ? 'f' : '-'), (c->transient ? 't' : '-'), (c->fullscreen ? 'F' : '-'), (c->urgent ? 'u' : '-'), (c->locked ? 'l' : '-')); } else { snprintf(line, sizeof(line), "%c %.2f", (n->split_type == TYPE_HORIZONTAL ? 'H' : 'V'), n->split_ratio); } strncat(rsp, line, REMLEN(rsp)); if (n == d->focus) strncat(rsp, " *\n", REMLEN(rsp)); else if (n == d->last_focus) strncat(rsp, " ~\n", REMLEN(rsp)); else strncat(rsp, "\n", REMLEN(rsp)); list(d, n->first_child, rsp, depth + 1); list(d, n->second_child, rsp, depth + 1); }
void rotate_tree(node_t *n, rotate_t rot) { if (n == NULL || is_leaf(n)) return; node_t *tmp; if ((rot == ROTATE_CLOCKWISE && n->split_type == TYPE_HORIZONTAL) || (rot == ROTATE_COUNTER_CLOCKWISE && n->split_type == TYPE_VERTICAL) || rot == ROTATE_FULL_CYCLE) { tmp = n->first_child; n->first_child = n->second_child; n->second_child = tmp; n->split_ratio = 1.0 - n->split_ratio; } if (rot != ROTATE_FULL_CYCLE) { if (n->split_type == TYPE_HORIZONTAL) n->split_type = TYPE_VERTICAL; else if (n->split_type == TYPE_VERTICAL) n->split_type = TYPE_HORIZONTAL; } rotate_tree(n->first_child, rot); rotate_tree(n->second_child, rot); }
static branch_vector break_trees_into_branches( tree_vector const& trees, mcolor_t complete_color = cfg::get().complete_color()) { branch_vector result; for (auto& tree: trees) { node_queue nodes_to_process; nodes_to_process.push(tree.get_root()); while (!nodes_to_process.empty()) { auto node = nodes_to_process.front(); auto node_color = node->get_data(); nodes_to_process.pop(); if (node_color != complete_color) { // Otherwise packed compliment is empty result.push_back(node_color.packed_compliment(complete_color)); } if (!node->is_leaf()) { nodes_to_process.push(node->get_left_child()); nodes_to_process.push(node->get_right_child()); } } } std::sort(result.begin(), result.end()); result.erase(std::unique(result.begin(), result.end()), result.end()); return result; }
static int get_permutations(Node *location, DynArray *current_string, PrefixResult **collection) { HashTable *current_hash_table = location->next; DynArray *internal_array = current_hash_table->array; if (location->isword) { PrefixResult *new_result = malloc(sizeof(PrefixResult)); LIBPREFIX_ASSERT(new_result != NULL, MALLOC_FAILED) new_result->next = *collection; new_result->word = dyn_array_to_str(current_string); *collection = new_result; } if (is_leaf(location)) { return 0; } else { int i; for (i = 0; i < internal_array->size; i++) { Node *next_node = lookup_dyn_array_node(internal_array, i); if (next_node != NULL) { append_dyn_array_char(current_string, next_node->key); if (get_permutations(next_node, current_string, collection) == 0) { pop_dyn_array(current_string); } else { return -1; } } } } return 0; }
// Compute the Number of leaves in the tree of color TreeColor static int nb_leaves(GeometricGraph &G, short TreeColor, tbrin RootBrin, svector<short> &ecolor) {svector<int> marked(1,G.ne(),0); marked.SetName("marked"); svector<bool> is_leaf(1,G.nv(),true); is_leaf.SetName("is_leaf"); tbrin b = RootBrin; int root_distance = 0; int nb_leaves = 0; while (1) {do {b = G.cir[b]; if(b == RootBrin) return nb_leaves; }while (ecolor[b.GetEdge()] != TreeColor && !marked[b.GetEdge()]); if (!marked[b.GetEdge()]) // Montee dans l'arbre {root_distance++; marked[b.GetEdge()] = 1; b = -b; } else // Descente {if (is_leaf[G.vin[b]]) nb_leaves ++; root_distance--; is_leaf[G.vin[-b]] = false; b = -b; } } return nb_leaves; }
void cycle_search::find_random_cycle(graph_access & G, std::vector<NodeID> & cycle) { //first perform a bfs starting from a random node and build the parent array std::deque<NodeID>* bfsqueue = new std::deque<NodeID>; NodeID v = random_functions::nextInt(0, G.number_of_nodes()-1); bfsqueue->push_back(v); std::vector<bool> touched(G.number_of_nodes(),false); std::vector<bool> is_leaf(G.number_of_nodes(),false); std::vector<NodeID> parent(G.number_of_nodes(),0); std::vector<NodeID> leafes; touched[v] = true; parent[v] = v; while(!bfsqueue->empty()) { NodeID source = bfsqueue->front(); bfsqueue->pop_front(); bool is_leaf = true; forall_out_edges(G, e, source) { NodeID target = G.getEdgeTarget(e); if(!touched[target]) { is_leaf = false; touched[target] = true; parent[target] = source; bfsqueue->push_back(target); } } endfor if(is_leaf) leafes.push_back(source); }
/*! * \param v A node. */ size_type degree(const node_type& v) const { if (is_leaf(v)) { // handles boundary cases return 0; } // position of the next node - node position - 1 return m_bv_select1(v.nr+2) - v.pos - 1; }
/************************************************************************* This function populates a trans_matix_array with pointers to matrices and the corresponding time values indexed by the edge number of the phylogenetic tree. The edges are numbered in depth-first order. The three parameters are an evolutinary model, a phylogentic tree, and a pointer to substmatrix_array structure. The function returns an integer containing the number of matrices added to the substmatrix_array structure. *************************************************************************/ static int populate_substmatrix_array( EVOMODEL_T* model, // IN TREE_T* tree, // IN int current_position, // IN SUBSTMATRIX_ARRAY_T* array // OUT ) { // Recursively descend the tree, depth first int num_children = get_num_children(tree); if (is_leaf(tree) != TRUE) { int c = 0; for (c = 0; c < num_children; c++) { TREE_T* child = get_nth_child(c, tree); double t = get_length(child); set_substmatrix_time(array, current_position, t); MATRIX_T* prob_matrix = get_model_prob_matrix(t, model); set_substmatrix_matrix(array, current_position, prob_matrix); free_matrix(prob_matrix); current_position = populate_substmatrix_array( model, child, current_position + 1, array ); } } return current_position; }
rb_node* rb_pred(rb_node* n, rb_tree* tree) { if(!is_leaf(n->left, tree)) return n->left; while(n->parent && n->parent->right != n) n = n->parent; return n->parent; }