//Function prints list void Weight_print(ListNode * head) { if(head == NULL) { return; } Tree_print(head -> tree_ptr, 0); Weight_print(head -> next); }
//Function prints entire tree void Tree_print(TreeNode * tn, int level) { if(tn == NULL) { return; } TreeNode * lc = tn -> left ; TreeNode * rc = tn -> right ; Tree_print ( lc , level + 1) ; Tree_print ( rc , level + 1) ; int depth ; for ( depth = 0; depth < level ; depth ++) { printf (" ") ; } printf (" freq = %d" , tn -> weight); if ((lc == NULL) && (rc == NULL)) { printf (" value = %d , ā%cā" , tn -> character , tn -> character ) ; } printf ("\n"); }
void Tree_print(Node *node) { //It will print the list in //pre-order traversal printf("%d\n", node->value); List *list = node->list; if(list==NULL) { //It is a leaf node return; } else { //Loop through the children //consideraing them as tree Node *current = list->first; while(current!=NULL) { Tree_print(current); current = current->next; } } }
int main(int argc, char *argv[]) { Node *node1 = Tree_add_root(1); Node *node11 = Node_create(11); Tree_add_child(node1, node11); Node *node111 = Node_create(111); Tree_add_child(node11, node111); Node *node12 = Node_create(12); Tree_add_child(node1, node12); Node *node13 = Node_create(13); Tree_add_child(node1, node13); Node *node131 = Node_create(131); Tree_add_child(node13, node131); /*printf("Print entire tree\n"); Tree_print(node1); printf("Print a subtree\n"); Tree_print(node13);*/ /*int find = 131; Node *found = Tree_search(node1, find); if(found!=NULL) { printf("%d found\n", find); } else { printf("%d not found\n", find); } find = 1311; found = Tree_search(node1, find); if(found==1) { printf("%d found\n", find); } else { printf("%d not found\n", find); }*/ /*printf("Tree before adding at a position.\n"); Tree_print(node1);*/ Tree_add_somewhere(node1, 12, 121); /*printf("Tree after adding at a position.\n"); Tree_print(node1);*/ Node *node1111 = Node_create(1111); Tree_add_child(node111, node1111); Node *node1112 = Node_create(1112); Tree_add_child(node111, node1112); printf("Print tree\n"); Tree_print(node1); int find = 131; Node *found = Tree_find_parent(node1, find); if(found==NULL) { printf("No parent found\n"); } else { printf("Parent for %d is %d\n", find, found->value); } return 0; }