/* * Return the most frequent word from the samples whose length is the max word size. */ char* most_frequent_word() { char* word = (char*) malloc((max_word_size+1)*sizeof(char)); Tree_node* max_node = most_frequent_child(prob_root); Tree_node* node = max_node; for (int i = 1; i <= max_word_size; i++) { word[max_word_size-i] = node->symbol; node = node->parent; } return word; }
/* * Returns the child of the given node which occurred the most. * If the depth of the node is max_word_size, then return the node. */ Tree_node* most_frequent_child(Tree_node* node) { if (node_depth(node) == max_word_size) { return node; } int max = 0; Tree_node* max_node; Tree_node* current_node = node->child; while (current_node != NULL) { Tree_node* max_child = most_frequent_child(current_node); if (max_child->prob_data != NULL && max_child->prob_data->occurrences > max) { max = max_child->prob_data->occurrences; max_node = max_child; } current_node = current_node->sibling; } return max_node; }
/* * Test the result for the bic calculator. */ int main(int argc, char** argv) { char** samples = read_lines(argv[1]); int depth = strtod(argv[2], NULL); setup_BIC(samples, depth, prob_root, bic_root); print_tree(prob_root, -(depth+1)); printf("\n-----------------\n"); printf("Most: %p\n", most_frequent_leaf(prob_root)); printf("most: %p\n", most_frequent_child(prob_root)); printf("\n-----------------\n"); for(Tree_node t = next_node_depth(prob_root, max_word_size); t; t = next_node_depth(t, max_word_size)) printf("%p\n",t); }