void tree_print (struct Node *tree) { if (tree->left) tree_print(tree->left); printf ("%d ", tree->val); if (tree->right) tree_print(tree->right); }
/* * Recursive tree traversal function with 3 traversing modes */ void tree_trace (TreeNode *root) { if (!root) return; switch (t_order) { case PRE_ORDER: tree_print (root->info); tree_trace (root->left); tree_trace (root->right); return; case IN_ORDER: tree_trace (root->left); tree_print (root->info); tree_trace (root->right); return; case POST_ORDER: tree_trace (root->left); tree_trace (root->right); tree_print (root->info); return; default: return; } }
int main(void) { struct Node *restrict root; struct Node *restrict node; const int exit_status = tree_create(&root, 20u); if (exit_status == 0u) { tree_print(root); PRINT_IS_SORTED(); tree_invert(root); tree_print(root); PRINT_IS_SORTED(); printf("tree_length: %u\n", tree_length(root)); PRINT_NTH(0); PRINT_NTH(-1); PRINT_NTH(10); PRINT_NTH(19); PRINT_NTH(20); tree_free(root); } else { puts("tree_create failed"); } return exit_status; }
/* depth first search to visit all nodes once adapted from here: https://gist.github.com/0xLeo/dbd53a1b47e5cf6c2b34 flag to !=0 to print data */ void tree_print(node* nP, node** newP, long int ctr, int* max, const int* printFlag) { if ( nP && *printFlag ) printf("@depth %lu: %d\n" , ctr, nP->data); ctr++; if ( nP->right ) tree_print(nP->right, newP, ctr, max, printFlag); if ( nP->left ) tree_print(nP->left, newP, ctr, max, printFlag); }
void tree_print(Word * root){ if(root){ tree_print(root->left); printf("%3d %s\n", root->count, root->word); tree_print(root->right); } }
void tree_print(struct tnode *p) { if (p != NULL) { tree_print(p->left); printf("%4d %s\n", p->count, p->word); tree_print(p->right); } }
/* Prints the tree, smaller (lexicographically) to larger */ void tree_print(struct tnode *t) { if (t != NULL) { tree_print(t->left); printf("|%s| - index: %d\n", t->word, t->graph_index); tree_print(t->right); } }
void tree_print(Node *t) { if (t == NULL) return; std::cout << "("; tree_print(t->left); std::cout << t->elt; tree_print(t->right); std::cout << ")"; }
void tree_print (struct Node * tree) { if (tree->left != NULL) tree_print(tree->left); printf("%d %d\n", tree->val, a[tree->val]); if (tree->right != NULL) tree_print(tree->right); };
/*in order print*/ void tree_print(t_node *root) { if (root) { tree_print(root->left); printf("%d\n", root->data); tree_print(root->right); } }
void tree_print (struct Node * tree) { if(tree != NULL) { tree_print(tree->left); printf("%d ", (int)(tree->val)); tree_print(tree->right); } }
void tree_print (struct TreeNode* tree) { if (tree->left) { tree_print (tree->left); } printf ("%d ", tree->val); if (tree->right) { tree_print (tree->right); } }
void tree_print(tree xs) { // {{{ if (tree_empty(xs)) { printf("null"); return; } printf("%i {", xs->value); tree_print(xs->left); printf(", "); tree_print(xs->right); printf("}"); } // }}}
void tree_print( SearchTree T ) { // In order transversal if (T == NULL) { return; } tree_print( T -> left ); // left subtree printf ("r --> %i\n", T -> data); // print root tree_print( T -> right ); // right subtree }
static void tree_print(json_printer* printer, json_print_function print_func, const json_val_t * v) { switch (v->type) { case JSON_OBJECT_BEGIN: json_print_raw(printer, JSON_OBJECT_BEGIN, NULL, NULL); for (int i = 0; i < v->length; i++) { json_print_raw(printer, JSON_KEY, v->u.object[i]->key, v->u.object[i]->key_length); tree_print(printer, print_func, v->u.object[i]->val); } json_print_raw(printer, JSON_OBJECT_END, NULL, NULL); break; case JSON_ARRAY_BEGIN: json_print_raw(printer, JSON_ARRAY_BEGIN, NULL, NULL); for (int i = 0; i < v->length; i++) tree_print(printer, print_func, v->u.array[i]); json_print_raw(printer, JSON_ARRAY_END, NULL, NULL); break; case JSON_STRING: json_print_raw(printer, JSON_STRING, v->u.str_val, (uint32_t)strlen(v->u.str_val)); break; case JSON_INT: { char buffer[32]; int length = sprintf(buffer, "%lld", v->u.int_val); json_print_raw(printer, JSON_INT, buffer, (uint32_t)length); } break; case JSON_FLOAT: { char buffer[32]; int length = sprintf(buffer, "%.17g", v->u.float_val); json_print_raw(printer, JSON_FLOAT, buffer, (uint32_t)length); } break; case JSON_TRUE: json_print_raw(printer, JSON_TRUE, "true", 4); break; case JSON_FALSE: json_print_raw(printer, JSON_STRING, "false", 5); break; case JSON_NULL: json_print_raw(printer, JSON_STRING, "null", 4); break; } }
void tree_print(const TREENODE *tnode, int depth) { int i; for(i=0 ; i<depth ; i++) printf(" "); // Child 찍고 printf("%c\n",tnode->d); if(tnode->child != NULL ) tree_print(tnode->child, depth+1); // Sibling 찍고 if(tnode->sibling != NULL ) tree_print(tnode->sibling, depth); }
int main(int argc, char* argv[]) { //要求:对第i的页面的访问概率正比于1/sqrt(i+1) const int count = 30; const int tests = 10; TreeKeyType* sum = new TreeKeyType[count]; sum[0] = 1; //sum[0]=1 //sum[1]=sum[0]+1/sqrt(2) //sum[2]=sum[1]+1/sqrt(3) //... //sum[n-1]=sum[n-2]+1/sqrt(n) for (int i = 1; i < count; i++) { sum[i] = sum[i - 1] + (double)(1 / sqrt(i + 1)); } TreeKeyType MaxRandValue = sum[count - 1] - 0.00001; tree_node* search_node = tree_init(sum, count, 0); printf("Search node size: %d\n", tree_size(search_node) * sizeof(search_node)); printf("========== tree ==========\n"); tree_print(search_node); printf("========== find ==========\n"); srand((unsigned int)time(NULL)); for (int i = 0; i < tests; i++) { TreeKeyType rnd = rand() / double(RAND_MAX) * MaxRandValue; printf("key: %f, val: %d\n", rnd, tree_find(search_node, rnd)); } delete[] (sum); return 0; }
static void print_graph_node(NODE *node) { int i; GRAPH *graph = CAST_TO_GRAPH(node); printf("\n"); for (i = 0; i < tree_num_children(node); i++) { HASH_ENTRY *he; printf("%d ->", i); he = find_in_hash(graph->forward, tree_get_child(node, i), sizeof(void *)); if (he) { HASH *subhash = he->data; HASH_ITERATOR iter; for (hash_iterator(subhash, &iter); hash_iterator_valid(&iter); hash_iterator_next(&iter)) { NODE *n = iter.entry->key; HASH_ENTRY *he2 = find_in_hash(graph->labels, n, sizeof(void *)); if (he2) printf(" %d", (int) he2->data); } } printf("\n"); tree_print(tree_get_child(node, i), 2); } }
int main() { FILE * out; out = fopen("errors.txt","w"); int i = 1; Lex * test = create_Lex("count(1,1);"); Node * past = get_first(test); Branch * tree; while (past != NULL) { printf("%d token is %d with value %s\n", i, get_token(past), get_value(past)); past = get_next(past); i++; } tree = create_tree(test, out); fclose(out); if (tree == NULL) { printf("Unable to create tree.\n"); } destroy_Lex(test); tree_print(tree); translate(tree, "out.c"); tree_free(tree); _CrtDumpMemoryLeaks(); return 0; }
//prints out what the tree looks like void tree_print(Node* sub_root) { //if the subroot doesn't exist then print out a backslash if(!sub_root) { std::cout << '\\'; return; } //print out the key in the subroot std::cout << sub_root->key; //traverse the tree down the left side first, then the right tree_print(sub_root->left); tree_print(sub_root->right); }
int test_point_crossover(void) { int i; char *t1_before; char *t2_before; char *t1_after; char *t2_after; for (i = 0; i < 1; i++) { setup(); mu_print("BEFORE:\n"); t1_before = tree_string(t1); t2_before = tree_string(t2); mu_print("t1 tree: %s\n", t1_before); mu_print("t2 tree: %s\n", t2_before); mu_print("t1 size: %d depth %d\n", t1->size, t1->depth); mu_print("t2 size: %d depth %d\n", t2->size, t2->depth); point_crossover(t1, t2); mu_print("AFTER:\n"); t1_after = tree_string(t1); t2_after = tree_string(t2); mu_print("t1 tree: %s\n", t1_after); mu_print("t2 tree: %s\n", t2_after); mu_print("t1 size: %d depth %d\n", t1->size, t1->depth); mu_print("t2 size: %d depth %d\n", t2->size, t2->depth); mu_print("\n"); teardown(); free(t1_before); free(t2_before); free(t1_after); free(t2_after); } setup(); mu_print("BEFORE:\n"); tree_print(t1); tree_print(t2); point_crossover(t1, t2); mu_print("AFTER:\n"); tree_print(t1); tree_print(t2); mu_print("\n\nBEFORE:\n"); tree_print(t1); tree_print(t2); point_crossover(t1, t2); mu_print("AFTER:\n"); tree_print(t1); tree_print(t2); teardown(); return 0; }
void add_tree(const unsigned long ip, const u_char * data) { struct tree_type *node = NULL, *newnode = NULL; newnode = packet2tree(data); if (newnode->type == UNKNOWN) { /* couldn't figure out if packet was client or server */ dbg(2, "%s (%lu) unknown client/server", libnet_addr2name4(newnode->ip, RESOLVE), newnode->ip); } /* try to find a simular entry in the tree */ node = RB_FIND(data_tree, &treeroot, newnode); #ifdef DEBUG if (debug > 2) tree_printnode("add_tree", node); #endif /* new entry required */ if (node == NULL) { /* increment counters */ if (newnode->type == SERVER) { newnode->server_cnt++; } else if (newnode->type == CLIENT) { newnode->client_cnt++; } /* insert it in */ RB_INSERT(data_tree, &treeroot, newnode); } else { /* we found something, so update it */ dbg(2, " node: %p\nnewnode: %p", node, newnode); #ifdef DEBUG if (debug > 2) tree_printnode("update node", node); #endif /* increment counter */ if (newnode->type == SERVER) { node->server_cnt++; } else if (newnode->type == CLIENT) { /* temp debug code */ node->client_cnt++; } /* didn't insert it, so free it */ free(newnode); } dbg(2, "------- START NEXT -------"); #ifdef DEBUG if (debug > 2) tree_print(&treeroot); #endif }
//TODO: change this whole thing to use iterators instead of accessing elements //encodes a string into binary representation and then prints //both the binary out and the tree structure out to the screen void encode(const std::string& value) { //traverse through the string and find every unique element and put it //in a vector of Nodes, if there is already a Node with the same key //increment the frequency of the of the key std::vector<Node*> table; for(int x = 0; x < static_cast<int>(value.size()); ++x) { //change the value in the iterator to a character char c = value[x]; //get a vector iterator from the has_key function unsigned int table_it = has_key(table, c); //if the key isn't in the table add it into the table if(table_it == table.size()) { table.push_back(new Node(c)); continue; } //increment the frequency of the key if the key is found table[table_it]->frequency += 1; } //put all the nodes into a min heap and pop off the nodes to create //binary trees. Make each parent node have an '*' as the key Heap m_heap(table); int num_nodes = m_heap.heap_size; while(num_nodes > 0) { Node* parent = new Node(m_heap.pop(), m_heap.pop(), '*'); m_heap.push(parent); num_nodes = m_heap.heap_size; } //traverse the tree, every time going to the left append a '0' to the binary //every time going to the right append a '1' to the binary. Once a leaf node is reached //look up the key in the vector table and put the binary in the node with the key Node* tree = m_heap.pop(); make_binary(tree, table); //traverse the initial string, at every character look up the key in the vector and print out the //binary for that key print(value, table); //do a preorder traversal of the binary tree and print out the characters tree_print(tree); std::cout << '\n'; }
// Print all the trees stored in the priority queue. // Use tree_print to print each item. void pq_print(PQueue *p){ int i; for(i = p->size -1; i >= 0; i--){ tree_print(p->data[i]); printf(", "); } printf("\n"); }
static int print_table_entry(HASH_ENTRY *he, void *data) { if (strcmp("$parent", he->key)) { printf(" %s:", (char *) he->key); tree_print(he->data, 1); } return 0; }
static void tree_print(tree_t* tree) { GList* dlist; printf("%*s%-20s "FF"\n", tree->depth*2, "", tree->name, tree->size); for (dlist = tree->entries; dlist; dlist = dlist->next) { tree = dlist->data; tree_print(tree); } }
virtual StringResultBase* Prettify(const ParseResultBase* parseResult) const { const VinenthzParseResult* pr = static_cast<const VinenthzParseResult*>(parseResult); VinenthzStringResult* sr = new VinenthzStringResult(); json_printer printer; json_print_init(&printer, string_buffer_append, &sr->sb); tree_print(&printer, json_print_pretty, pr->root); json_print_free(&printer); sr->AppendEnds(); return sr; }
static void assert_tree_print(struct string *expected, struct tree_node *root) { struct string *str = alloc_str(); tree_print(root, str); assert_str_equals(expected->value, str->value); free_str(str); free_str(expected); }
int main() { struct tnode *root; char word[MAXWORD]; root = NULL; //'\t' is just for indicate the end while (get_word(word, MAXWORD) != -1) if (isalpha(word[0])) root = add_tree(root, word); tree_print(root); return 0; }
void treeMain(node *list) { t_node *root; node *inList; node *postList; root = buildTree(list); tree_print(root); printf("-999\n"); inList = inOrder(root); printList(inList); printf("-999\n"); freeList(inList); postList = postOrder(root); printList(postList); printf("-999\n"); freeList(postList); printf("%d\n", tree_search(root, 0)); printf("-999\n"); printf("%d\n", tree_search(root, 10)); printf("-999\n"); printf("%d\n", tree_search(root, -2)); printf("-999\n"); printf("%d\n", tree_search(root, 2)); printf("-999\n"); printf("%d\n", tree_search(root, 3)); printf("-999\n"); printf("%d\n", tree_search(root, 9)); printf("-999\n"); printf("%d\n", tree_search(root, 1)); printf("-999\n"); printf("%d\n", bin_tree_search(root, 0)); printf("-999\n"); printf("%d\n", bin_tree_search(root, 10)); printf("-999\n"); printf("%d\n", bin_tree_search(root, -2)); printf("-999\n"); printf("%d\n", bin_tree_search(root, 2)); printf("-999\n"); printf("%d\n", bin_tree_search(root, 3)); printf("-999\n"); printf("%d\n", bin_tree_search(root, 9)); printf("-999\n"); printf("%d\n", bin_tree_search(root, 1)); printf("-999\n"); freeTree(root); }