int main(){ char t[]="{1,2,3,#,4,5,#,6,7,#,8}"; char t2[]="{1,2,3,#,4,5,#,6,7,8,2}"; struct TreeNode *r = make_tree(t); struct TreeNode *r2 = make_tree(t2); if(isSameTree(r,r2)){ puts("same Tree"); } else { puts("diff Tree"); } printf("after t%s\n",t); char st[]="{1,2,2,3,4,4,3}"; struct TreeNode *sr = make_tree(st); simple_inorder(sr); puts("end ino"); simple_preorder(sr); puts("end pre"); if(isSymmetric(sr)){ puts("symmetric Tree"); } else { puts("no sym Tree"); } }
END_TEST START_TEST(test_eval_predicate_and_or) { peos_resource_t* resources; Tree t_and, t_or, t_res0, t_res1; resources = (peos_resource_t *) calloc(2, sizeof(peos_resource_t)); strcpy(resources[0].name, "res0"); strcpy(resources[0].value, "my_file0"); strcpy(resources[1].name, "res1"); strcpy(resources[1].value, "my_file1"); t_res0 = make_tree("res0", 0, NULL, NULL); t_res1 = make_tree("res1", 0, NULL, NULL); t_and = make_tree(NULL, AND, t_res0, t_res1); t_or = make_tree(NULL, OR, t_res0, t_res1); system("touch my_file0"); system("touch my_file1"); fail_unless(eval_predicate(resources, 2, t_and) == 1, "res0 AND res1 (both exist) failed"); fail_unless(eval_predicate(resources, 2, t_or) == 1, "res0 OR res1 (both exist) failed"); system("rm my_file0"); fail_unless(eval_predicate(resources, 2, t_and) == 0, "res0 AND res1 (res0 doesn't exist) failed"); fail_unless(eval_predicate(resources, 2, t_or) == 1, "res0 OR res1 (res0 doesn't exist) failed"); system("rm my_file1"); fail_unless(eval_predicate(resources, 2, t_and) == 0, "res0 AND res1 (neither exists) failed"); fail_unless(eval_predicate(resources, 2, t_or) == 0, "res0 OR res1 (neither exists) failed"); free(resources); }
PRIVATE double mfe_cost(const char *string, char *structure, const char *target) { #if TDIST Tree *T1; char *xstruc; #endif double energy, distance; if (strlen(string)!=strlen(target)) { fprintf(stderr, "%s\n%s\n", string, target); nrerror("unequal length in mfe_cost"); } energy = fold(string, structure); #if TDIST if (T0 == NULL) { xstruc = expand_Full(target); T0=make_tree(xstruc); free(xstruc); } xstruc = expand_Full(structure); T1=make_tree(xstruc); distance = tree_edit_distance(T0,T1); free(xstruc); free_tree(T1); #else distance = (double) bp_distance(target, structure); #endif cost2 = energy_of_struct(string, target) - energy; return (double) distance; }
END_TEST START_TEST(test_eval_predicate_dot) { peos_resource_t* resources; Tree t_dot, t_res, t_exists; resources = (peos_resource_t *) calloc(1, sizeof(peos_resource_t)); strcpy(resources[0].name, "res"); strcpy(resources[0].value, "my_file"); t_res = make_tree("res", 0, NULL, NULL); t_exists = make_tree("exists", 0, NULL, NULL); t_dot = make_tree(NULL, DOT, t_res, t_exists); /* Happy path: requires { res.exists } */ system("touch my_file"); fail_unless(eval_predicate(resources, 1, t_dot) == 1, "eval_predicate_dot 1 failed"); system("rm my_file"); fail_unless(eval_predicate(resources, 1, t_dot) == 0, "eval_predicate_dot 2 failed"); /* requires { doc.title } -> [isTrue [title ${doc}]] */ /* requires { doc.spellchecked } -> [isTrue [spellchecked ${doc}] */ /* requires { doc.spellchecked == "True" } [spellchecked ${doc}] == true */ free(resources); }
void make_tree(int *tree, int *input, int nodeIndex, int nNode){ if (nodeIndex * 2 > nNode){ tree[nodeIndex - 1] = input[num]; num++; return; } make_tree(tree, input, nodeIndex * 2, nNode); tree[nodeIndex - 1] = input[num]; num++; make_tree(tree, input, nodeIndex * 2 + 1, nNode); }
/* ツリーの作成 */ struct tree *make_tree(struct tree *node, int num) { if( node == NULL ){ node = add_node(num); return node; } if ( num > node->no ) node->left = make_tree(node->left,num); if ( num < node->no ) node->right = make_tree(node->right,num); return node; }
static BinTree::NodeSP make_tree(std::vector<int> arr, int s, int e) { if (e - s < 0) return nullptr; int m = (e + s + 1) / 2; auto root = std::make_shared<BinTree::Node>(arr[m]); auto t1 = make_tree(arr, s, m - 1); auto t2 = make_tree(arr, m + 1, e); BinTree::bst_insert(root, t1); BinTree::bst_insert(root, t2); return root; }
struct kd_node_t* make_tree(struct kd_node_t *t, int len, int i, int dim) { struct kd_node_t *n; if (!len) return 0; if ((n = find_median(t, t + len, i))) { i = (i + 1) % dim; n->left = make_tree(t, n - t, i, dim); n->right = make_tree(n + 1, t + len - (n + 1), i, dim); } return n; }
struct seg_tree_2d_t * create_seg_tree_2d( struct rect_list_t *rect_list){ struct seg_tree_2d_t *list = rect_list_to_tree_list(rect_list,1); struct seg_tree_2d_t *seg_tree = make_tree(list); printf("x seg tree build\n"); struct seg_tree_2d_t *tmp_seg_tree; struct seg_tree_2d_t *stack[1000]; struct rect_list_t *tmp_rect_list = rect_list; int j; for( j = rect_total_number-1; j>= 0; j-- ) {// struct intv *tmp_ob; // tmp_ob = (struct intv *) malloc( sizeof( struct intv ) ); // tmp_ob->low = intervals[j].low; // tmp_ob->up = intervals[j].up; insert_interval( seg_tree, rect_intv[j].x_min, rect_intv[j].x_max, rect_intv[j].y_min, rect_intv[j].y_max); } printf("Insert intervals to X tree done\n"); tmp_seg_tree = seg_tree; int st_p = 0; stack[st_p++] = tmp_seg_tree; while (st_p > 0) { struct seg_tree_2d_t *tmp_node = stack[--st_p]; if (tmp_node->rect_interval_list != NULL) { //build y tree list = rect_list_to_tree_list(tmp_node->rect_interval_list, 0); tmp_node->v_tree = make_tree(list); //insert y interval, remember x and y switched, so need to change the result; for( j = rect_total_number_y-1; j>= 0; j-- ) {// struct intv *tmp_ob; // tmp_ob = (struct intv *) malloc( sizeof( struct intv ) ); // tmp_ob->low = intervals[j].low; // tmp_ob->up = intervals[j].up; insert_interval( tmp_node->v_tree, rect_intv_y[j].x_min, rect_intv_y[j].x_max, rect_intv_y[j].y_min, rect_intv_y[j].y_max); } printf("Insert intervals to y done\n"); } if (tmp_node->left != NULL) { stack[st_p++] = tmp_node->left; } if (tmp_node->right != NULL) { stack[st_p++] = tmp_node->right; } } return seg_tree; }
void tree_thread::prepare(std::string sid) { std::string key = "tree_thread_" + sid; if(cache().fetch_page(key)) return; cache().add_trigger("thread_" + sid); int id = atoi(sid.c_str()); data::tree_thread c; if(!thread_shared::prepare(c,id)) return; cppdb::result r; r=sql<< "SELECT reply_to,id,author,content " "FROM messages WHERE thread_id=? " "ORDER BY reply_to,id DESC" << id; msg_ord_t all; while(r.next()) { int msg_id,rpl_id; std::string author,comment; r>>rpl_id>>msg_id; data::msg &message=all[rpl_id][msg_id]; r>>message.author>>message.content; message.msg_id=msg_id; } make_tree(c.messages,all,0); session()["view"]="tree"; render("tree_thread",c); cache().store_page(key); }
int build_index(t_node **tree) { char *s; char *tmp; int gnl; int i; i = 0; s = NULL; tmp = s; gnl = get_next_line(0, &s); while (gnl) { make_tree(&s, tree); free(tmp); tmp = s; gnl = get_next_line(0, &s); i++; } if (s) free(s); if (i == 1) return (0); if (tmp) free(tmp); return (1); }
void compress(char * source, char * destination) { int freq_array[128]= {0}; FILE * fp_source; FILE * fp_compressed; fp_source = fopen(source,"r"); fp_compressed = fopen("compressed.txt", "wb"); fp_source = frequency_finder(fp_source, freq_array); // display_frequency(freq_array); list_t * head =make_tree(freq_array); // inorder(head->link_node); make_compressed_file(head, fp_compressed, fp_source); fclose(fp_source); fclose(fp_compressed); FILE * fp_recreate = fopen(destination, "wb"); FILE * fp_compressed_read = fopen("compressed.txt", "r"); recreate_file(head->link_node, fp_compressed_read, fp_recreate); printf("\nA compressed file named \"compressed.txt\" is created.\n"); fclose(fp_compressed_read); fclose(fp_recreate); }
int main(){ Knowledge knowledge; ID_table id_table; while(true){ string s; //cin>>s; getline(cin, s); if(s.compare("")==0) break; Token_stream ts(s); try{ Tree_node* root = make_tree(ts, id_table); process(root, id_table); //root->print(""); knowledge.add(root); } catch(char const* e){ cout<<"Error: "<<e; return 1; } catch(string e){ cout<<"Error: "<<e; return 1; } } knowledge.print(); while(knowledge.resolution()); if(knowledge.size()==0) cout<<"true"<<endl<<endl; else cout<<"false"<<endl<<endl; //knowledge.print(); return 0; }
unsigned ffs_make_fsys_2(FILE *dst_fp, struct file_entry *list, char *mountpoint, char *destname) { struct tree_entry *trp; ffs_sort_t *sort; if(mountpoint == NULL) mountpoint = ""; // // If target endian is unknown, set to little endian // if (target_endian == -1) target_endian = 0; // // Make sure block size is defined and reasonable (at least 1K) // if (block_size < 1024) mk_flash_exit("Error: block_size not defined or incorrectly defined (>1K), exiting ...\n"); trp = make_tree(list); sort = ffs_entries(trp, mountpoint); write_f3s(sort, block_size); write_image(dst_fp); return 0; }
/* Treeify a body by appending the trees of the lines. */ void make_tree_from_body(NODE *body) { NODE *body_ptr, *end_ptr = NIL, *tree = NIL; if (body == NIL || (is_tree(body) && generation__tree(body) == the_generation)) return; if (is_tree(body)) untreeify_body(body); for (body_ptr = body; body_ptr != NIL; body_ptr = cdr(body_ptr)) { tree = car(body_ptr); if (tree == NIL) continue; /* skip blank line */ this_line = tree; make_tree(tree); if (is_tree(tree)) { tree = tree__tree(tree); make_line(tree, car(body_ptr)); if (end_ptr == NIL) settree__tree(body, tree); else setcdr(end_ptr, tree); if (generation__tree(car(body_ptr)) == UNBOUND) setgeneration__tree(body, UNBOUND); /* untreeify(car(body_ptr)); */ while (cdr(tree) != NIL) tree = cdr(tree); end_ptr = tree; } else { /* error while treeifying */ untreeify(body); return; } } settype(body, TREE); }
int main(int argc,char *argv[]) { int i, j, x, y, count, gene_num, seq_len; //1番目の引数で指定した転写因子の複数の結合部位配列を読み込む gene_num=read_promoter(argv[1]); for(i=0;i<gene_num;i++){ printf(">%s\n%s\n", pro[i].name, pro[i].seq); } printf("------------------\n"); seq_len = strlen(pro[0].seq); count = 0; for(i = 1; i < gene_num; i++){/*make genelogical tree */ /* Compare 0 with others, * it is not good way, but it is easy to implement*/ make_map(seq_len, 0, i); comb[count].x = i; strcpy(comb[count].name, pro[i].name); comb[count].score = dp[seq_len-1][seq_len-1]; count++; } make_tree(count); for(i = 1; i < gene_num; i++){ make_map(seq_len, 1, i); if(i == 1){ strcpy(result[0].name, pro[0].name); } strcpy(result[i].name, pro[i].name); traceback(seq_len-1, seq_len-1, i); } print_result(gene_num); return 0; }
tree_t * make_parameter_list(identifier_list_t * idents, type_t * type, parameter_list_t * next) { parameter_list_t * node = malloc(sizeof(parameter_list_t)); node->idents = idents; node->type = type; node->next = next; return make_tree(node, PARAMETER_LIST_T); }
void step() { int i, epoch = 0; float fx, fy; clock_t timer_start, timer_end; double time_elapsed = 0; float time_per_frame; while (steps > 0 && epoch < steps) { printf("[STEP %d]\n", epoch+1); timer_start = clock(); make_tree(); for (i = 0; i < n_bodies; i++) { compute_force(i, 0, &fx, &fy); update_pos(i, fx, fy); } timer_end = clock(); time_elapsed += timer_end-timer_start; if (is_print_tree) print_tree(0, 0, "root"); if (is_print_bodies) print_bodies("[UPDATED POSITION]"); free_nodes(); epoch += 1; } time_per_frame = (time_elapsed/(double)CLOCKS_PER_SEC)/epoch; printf("\n[TIME]\n"); printf("Time per iteration: %f s\n", time_per_frame); printf("Iteration per second: %f\n", 1/time_per_frame); }
t_tree *npi(t_token *token, char *str, t_env *env) { t_npi npi; npi.tree = NULL; npi.stack = NULL; npi.output = NULL; token = exotic_and_var(token, env); if (!token) return (NULL); if (!token->next) return (createnode(NULL, token->token)); while (token) { my_stack(token->token, &npi.stack, &npi.output); token = token->next; } while (npi.stack) { make_tree(&npi.output, npi.stack->data); npi.stack = npi.stack->next; } npi.tree = npi.output->tree; delete_npi(npi.stack, npi.output); if (check_full_tree(npi.tree, str) == 1) return (npi.tree); return (NULL); }
void main() { struct treenode *root = make_tree(); }
static void Run() { // 정렬된 배열에서 높이가 가장 낮은 이진 탐색 트리를 생성하라. std::vector<int> arr{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; auto root = make_tree(arr, 0, (int)arr.size() - 1); BinTree::print(root, 0); }
/** * constructor : makes the tree if a valid array of nodes are given */ kdtree::kdtree(struct kd_node_t *& t, UINT len, UINT dim) : _dim(dim) { _root = nullptr; if ( len > 0) { _root = make_tree ( t, len, 0); } }
TreeNode* make_tree(const vector<int>& inorder, const vector<int>& postorder, int p, int q, int len) { if (0 == len) return nullptr; if (1 == len) return new TreeNode(inorder[p]); int val = postorder[q + len - 1]; int k = p_[val]; TreeNode* root = new TreeNode(val); int d = k - p; root->left = make_tree(inorder, postorder, p, q, d); root->right = make_tree(inorder, postorder, p + d + 1, q + d, len - d - 1); return root; }
struct btree *make_tree(int sorted[], size_t l, size_t u, int skew, int skew_res) { if (l > u) { return NULL; } size_t mid = skew_mid(l, u, skew, skew_res); struct btree* root = malloc(sizeof(struct btree)); root->key = sorted[mid]; if (mid != 0) { root->left = make_tree(sorted, l, mid-1, skew, skew_res); } root->right = make_tree(sorted, mid+1, u, skew, skew_res); return root; }
tree_t * make_subprogram_head(identifier_t * ident, parameter_list_t * params, type_t * type) { subprogram_head_t * node = malloc(sizeof(subprogram_head_t)); node->ident = ident; node->params = params; node->type = type; find_identifier(node->ident->ident)->node->type = type; return make_tree(node, SUBPROGRAM_HEAD_T); }
struct s_tree *huff_tree(unsigned int treelevel, struct symbolsin *symbolsin) { struct s_tree *tree = NULL; tree = make_tree(256, NULL, NULL); tree = tree_uniform_build(0, treelevel, symbolsin); printf("height = %d\n", height(tree)); printf("size = %d\n", size(tree)); return tree; }
/*}}}*/ void tree_to_array(RGB** img, node tree)/*{{{*/ { switch(tree.stat) { case ALL_NODE: for(int i=0;i<4;++i) tree_to_array(img,tree.u.child[i]); break; case LR_NODE: case UD_NODE: for(int i=0;i<2;++i) tree_to_array(img,tree.u.child[i]); break; case NO_NODE: for(int i=tree.r;i<tree.r+tree.h;++i) for(int j=tree.c;j<tree.c+tree.w;++j) img[i][j] = tree.u.color; break; default: puts("Error caught in tree_to_array."); } }/*}}}*/ node make_tree(RGB** img, int r, int c, int h, int w)/*{{{*/ { node base; int i,j; for(i=r;i<r+h;++i) { for(j=c;j<c+w;++j) if(img[i][j].r!=img[r][c].r || img[i][j].g!=img[r][c].g || img[i][j].b!=img[r][c].b) break; if(j!=c+w) break; } base.r = r; base.c = c; base.h = h; base.w = w; if(i==r+h&&j==c+w) { base.stat = NO_NODE; base.u.color = img[r][c]; /* if(tree_progress(false,0)!=tree_progress(false,w*h)&&tree_progress(false,0)%5==0) printf("%d%% complete\n",tree_progress(false,0)); */ } else { if(w==1) { base.stat = UD_NODE; base.u.child = (node*) malloc(sizeof(node)*2); base.u.child[0] = make_tree(img,r,c,h/2,w); base.u.child[1] = make_tree(img,r+h/2,c,h-h/2,w); } else if(h==1) { base.stat = LR_NODE; base.u.child = (node*) malloc(sizeof(node)*2); base.u.child[0] = make_tree(img,r,c,h,w/2); base.u.child[1] = make_tree(img,r,c+w/2,h,w-w/2); } else { base.stat = ALL_NODE; base.u.child = (node*) malloc(sizeof(node)*4); base.u.child[0] = make_tree(img,r,c,h/2,w/2); base.u.child[1] = make_tree(img,r+h/2,c,h-h/2,w/2); base.u.child[2] = make_tree(img,r,c+w/2,h/2,w-w/2); base.u.child[3] = make_tree(img,r+h/2,c+w/2,h-h/2,w-w/2); } } return base; }/*}}}*/
struct tree *make_tree(int depth) //@ requires true; //@ ensures tree(result, depth); { if (depth == 0) { //@ close tree(0, 0); return 0; } else { struct tree *left = make_tree(depth - 1); struct tree *right = make_tree(depth - 1); int value = rand(); struct tree *t = malloc(sizeof(struct tree)); if (t == 0) abort(); t->left = left; t->right = right; t->value = value % 2000; //@ close tree(t, depth); return t; } }
int main(void) { struct tree *root; int i, num; char c[4]; for( i = 0; i < KEY_SIZE; i++ ){ printf("%d> ",i+1); num = get_num(); if( i == 0 ){ root = make_tree(NULL,num); } else{ make_tree(root,num); } } print_tree(root); /* キー入力により実行が停止されるまで、探索キーの * 入力と探索、結果の表示を繰り返す */ while(1){ printf("key>"); num = get_num(); search(root,num); printf("Do you wanna continue(yes/no)? "); while(1){ fgets(c,100,stdin); for(i=0;c[i] != '\n' && i != 3;i++); c[i] = '\0'; if(strcmp(c,"no") == 0){ return 0; } if(strcmp(c,"yes") == 0){ break; } } } return 0; }
void test(int nodes[], int n) { TreeNode *root = make_tree(nodes, n); Solution s; std::vector<std::vector<int> > result = s.levelOrderBottom(root); for (int i = 0; i < result.size(); ++i) { for (int j = 0; j < result[i].size(); ++j) { printf("%d ", result[i][j]); } printf("\n"); } printf("\n"); }