void tree_traversal(struct node *root, int *sum) { if (root == NULL) return; *sum = *sum + root->data; tree_traversal(root->left, sum); tree_traversal(root->right, sum); }
/* Makes inorder tree traversal and outputs nodes in stdout. * * @param tree Root of Huffman tree * @param indent Indent */ void tree_traversal(HuffmanTree* tree, uint indent) { if (tree == nullptr) return; tree_traversal(tree->left, indent + 4); cout << setw(indent) << tree->symbol << ":" << tree->freq << endl; tree_traversal(tree->right, indent + 4); }
void tree_traversal(AST_NODE* root, int (*f[])(AST_NODE*)) { AST_NODE* p; // fprintf(stderr,"tree_traversal: %s\n",name[root->nodeType-FUNC_OFFSET]); if (root == NULL) return; f[root->nodeType-FUNC_OFFSET](root); for (p = root->leftChild; p != NULL; p = p->rightSibling) tree_traversal(p,f); }
int main(int argc, char *argv[]) { int total; int n, i, t0, t1, t2, t3; node_t root; if (argc < 2) { printf("Arguments not sufficient, use default values\n"); } else { G_MAX_DEPTH = atoi(argv[1]); if (argc >= 3) G_ITERATION = atoi(argv[2]); if (argc >= 4) G_TRACEITER = atoi(argv[3]); else G_TRACEITER = 0.8 * G_ITERATION; } total = (pow(N_CHILD, G_MAX_DEPTH + 1) - 1) / (N_CHILD - 1); G_NODES_ARR = xmalloc(sizeof(node_t) * total); memset(G_NODES_ARR, 0x0, sizeof(node_t) * total); printf("Depth: %d, Iter: %d (mtrace the %dth), Nodes: %d\n", G_MAX_DEPTH, G_ITERATION, G_TRACEITER, total); root.dep = 0; root.off = 0; root.idx = 0; root.nchild = 0; for (G_ITER = 0; G_ITER < G_ITERATION; G_ITER++) { t0 = curr_time_micro(); if (G_ITER == G_TRACEITER) mtrace(); tree_build(&root); if (G_ITER == G_TRACEITER) muntrace(); t1 = curr_time_micro(); /* for (i = 0; i < total; i++) { node_t *p = G_NODES_ARR + i; printf("%d: %d %d %d %d\n", i, p->dep, p->off, p->idx, p->nchild); } */ n = 0; tree_traversal(&root, &n); if (n != total) printf("Tree check failed! (%d vs. %d)\n", total, n); t2 = curr_time_micro(); tree_free(); t3 = curr_time_micro(); printf("[%02d] Build: %d Traversal: %d Free: %d\n", G_ITER, t1-t0, t2-t1, t3-t2); } return 0; }
int get_missing_value(struct node *root,int n) { if (root == NULL || n == 0) return -1; int actual_sum = 0, sum = 0; tree_traversal(root, &sum); /*constraint is (-10000 <= N <= 10000) both positive and negative values*/ if (n > 0) { for (int i = 0; i <= n; i++) actual_sum = actual_sum + i; } else { for (int i = 0; i >= n; i--) actual_sum = actual_sum + i; } int missing_value = actual_sum - sum; return missing_value; }
/* Builds Huffman code for a string @str. * * @params str Input string. * @return Huffman code. */ bitset encode_to_huffman(std::string str) { bitset code; map<char, uint> freq_table = make_freq_table(str); HuffmanTree* huff_tree = build_tree(freq_table); map<char, bitset> code_table = count_codes(huff_tree); tree_traversal(huff_tree, 0); // encoding each character of @str for (uint i = 0; i < str.length(); i++) { bitset c_code = code_table[str[i]]; code.insert(code.end(), c_code.begin(), c_code.end()); } delete huff_tree; huff_tree = nullptr; return code; }
int main() { stringstream ss; clock_t begin, end, training_time; string is_save; srand(time(NULL)); cout << "Human pose estimation CVPR2016 based on SMMC DB" << endl; cout << "Developed by Gyeongsik Moon" << endl << endl; cout << "SAVE or LOAD: "; cin >> is_save; cout << endl; vector<node*> tree_head; point *point_train; joint **joint_train; point *point_matching; joint **joint_matching; point *point_test; joint **joint_test; if (!is_save.compare("SAVE")) { point_train = new point[TRAIN_FRAME_NUM * Q_HEIGHT * Q_WIDTH]; joint_train = new joint *[TRAIN_FRAME_NUM]; for (int f = 0; f < TRAIN_FRAME_NUM; f++) joint_train[f] = new joint[JOINT_NUMBER]; //Load training data cout << "Loading training data..." << endl; load_data(point_train, joint_train, TRAINING); cout << endl; //Train a tree cout << "Training start!" << endl; begin = clock(); tree_train(point_train, joint_train, tree_head); end = clock(); training_time = end - begin; cout << endl << "Training complete!" << endl; cout << (float)(end - begin) / CLOCKS_PER_SEC << "seconds for training" << endl << endl; cout << "Saving tree..." << endl; save_tree(tree_head); for (long long int pid = 0; pid < TRAIN_FRAME_NUM * Q_HEIGHT * Q_WIDTH; pid++) { if (point_train[pid].depth != BACKGROUND_DEPTH) { for (int nid = 0; nid < NEAREST_NUM; nid++) delete[] point_train[pid].offset[nid]; delete[] point_train[pid].offset; } } delete[] point_train; for (int f = 0; f < TRAIN_FRAME_NUM; f++) delete[] joint_train[f]; delete[] joint_train; } else if (!is_save.compare("LOAD")) { cout << "Loading tree..." << endl; training_time = 0; load_tree(&tree_head); } else { cout << "Not a proper input..." << endl; return 0; } point_matching = new point[POINT_MATCHING_FRAME_NUM * Q_HEIGHT * Q_WIDTH]; joint_matching = new joint *[POINT_MATCHING_FRAME_NUM]; for (int f = 0; f < POINT_MATCHING_FRAME_NUM; f++) joint_matching[f] = new joint[JOINT_NUMBER]; cout << "Loading DB for point matching..." << endl; load_data(point_matching, joint_matching, POINT_MATCHING); float** center_matching; center_matching = new float*[POINT_MATCHING_FRAME_NUM]; for (int matching_f = 0; matching_f < POINT_MATCHING_FRAME_NUM; matching_f++) { center_matching[matching_f] = new float[COORDINATE_DIM]; center_matching[matching_f][0] = 0; center_matching[matching_f][1] = 0; center_matching[matching_f][2] = 0; float pixel_num = 0; for (int matching_i = 0; matching_i < Q_WIDTH * Q_HEIGHT; matching_i++) { if (point_matching[matching_f * Q_HEIGHT * Q_WIDTH + matching_i].depth != BACKGROUND_DEPTH) { center_matching[matching_f][0] += point_matching[matching_f * Q_HEIGHT * Q_WIDTH + matching_i].x_world; center_matching[matching_f][1] += point_matching[matching_f * Q_HEIGHT * Q_WIDTH + matching_i].y_world; center_matching[matching_f][2] += point_matching[matching_f * Q_HEIGHT * Q_WIDTH + matching_i].depth; pixel_num++; } } center_matching[matching_f][0] /= pixel_num; center_matching[matching_f][1] /= pixel_num; center_matching[matching_f][2] /= pixel_num; } delete[] point_matching; point_test = new point[TEST_FRAME_NUM * Q_HEIGHT * Q_WIDTH]; joint_test = new joint *[TEST_FRAME_NUM]; for (int f = 0; f < TEST_FRAME_NUM; f++) joint_test[f] = new joint[JOINT_NUMBER]; //load test dataset cout << "Loading test dataset..." << endl; load_data(point_test, joint_test, TEST); //Test cout << "Testing start!" << endl; begin = clock(); //tree traversal tree_traversal(point_test, joint_test, center_matching, joint_matching, tree_head); end = clock(); cout << "Testing complete!" << endl << endl; cout << (float)training_time / CLOCKS_PER_SEC << "seoncds for training" << endl; cout << (float)(end - begin) / CLOCKS_PER_SEC << "seoncds for testing" << endl; for (int matching_f = 0; matching_f < POINT_MATCHING_FRAME_NUM; matching_f++) delete[] center_matching[matching_f]; delete[] center_matching; for (int f = 0; f < POINT_MATCHING_FRAME_NUM; f++) delete[] joint_matching[f]; delete[] joint_matching; for (int f = 0; f < TEST_FRAME_NUM; f++) delete[] joint_test[f]; delete[] joint_test; delete[] point_test; return 0; }