C Code for n-ary tree
Short Description
C code to create n-ary tree...
Description
/* Functionality: 1)Tree Creation: * User will be asked to enter number of nodes and values of each node. * User has to enter data using keyboard. * N-ary tree will be created based on data given by user. 2)Conditions considering while tree creation: * All child values will be less than parent value. * All sibling values will be greater than previous siblings. * As Root node is parent of all nodes it will have maximum value. 3)N-ary tree structure: * each node will have info regarding first child and next sibling. * Root can't have siblings. * user can enter as many nodes as he want. * Any node can have any number of childs or siblings( Root can have only childs) 4)Output: * User can see created tree information imediately or can save it in fil e. * user can get tree stucture in 3 formats: Inorder,Preorder,Postorder. * User can give ouput file name while saving output in file. */
#include #include #include struct tree_el { // n-ary tree node definition int val; struct tree_el * sibling, * child; }; typedef struct tree_el node; void insert(node ** tree, node * item) { // Function to insert new node if(!(*tree)) { *tree = item; // Root Node insertion return; } if(item->valval) // child node inserion insert(&(*tree)->child, item); else if(item->val>(*tree)->val) // sibling node insertion insert(&(*tree)->sibling, item); } void print_node_info(node * tree) { // Function to print node information: Node value,Node first child,Node next sibling if(tree->child && tree->sibling) { printf("%d, parent of %d and sibling of %d\n",tree->val, (tree->child) ->val,(tree->sibling)->val);
} else if (tree->child) { printf("%d, parent of %d\n",tree->val, (tree->child)->val); } else if (tree->sibling) { printf("%d, sibling of %d\n",tree->val,(tree->sibling)->val); } else printf("%d\n",tree->val); } void in_order_print(node * tree) { // Function to print Node information in Inor der if(tree->child) in_order_print(tree->child); print_node_info(tree); if(tree->sibling) in_order_print(tree->sibling); } void pre_order_print(node * tree) { // Function to print Node information in pre order print_node_info(tree); if(tree->child) pre_order_print(tree->child); if(tree->sibling) pre_order_print(tree->sibling); } void post_order_print(node * tree) { // Function to print Node information in po storder if(tree->child) post_order_print(tree->child); if(tree->sibling) post_order_print(tree->sibling); print_node_info(tree); } void save_node_info(node * tree,char * file_name) {// Function to save node info rmation in file: Node value,Node first child,Node next sibling FILE *ptr_file; ptr_file = fopen(file_name, "a"); if(tree->child && tree->sibling) { fprintf(ptr_file,"%d, parent of %d and sibling of %d\n",tree->val, (tr ee->child)->val,(tree->sibling)->val); } else if (tree->child) { fprintf(ptr_file,"%d, parent of %d\n",tree->val, (tree->child)->val); } else if (tree->sibling) { fprintf(ptr_file,"%d, sibling of %d\n",tree->val,(tree->sibling)->val) ; } else fprintf(ptr_file,"%d\n",tree->val); fclose(ptr_file); }
void in_order_save(node * tree,char * file_name) { // Function to save Node info rmation in file in Intorder if(tree->child) in_order_save(tree->child,file_name); save_node_info(tree,file_name); if(tree->sibling) in_order_save(tree->sibling,file_name); } void pre_order_save(node * tree,char * file_name) { // Function to print Node in formation in file in preorder save_node_info(tree,file_name); if(tree->child) pre_order_save(tree->child,file_name); if(tree->sibling) pre_order_save(tree->sibling,file_name); } void post_order_save(node * tree,char * file_name) { // Function to print Node i nformation in file in postorder if(tree->child) post_order_save(tree->child,file_name); if(tree->sibling) post_order_save(tree->sibling,file_name); save_node_info(tree,file_name); } void delete_tree(node * tree) // Function to delete tree { if (tree) { delete_tree(tree->child); delete_tree(tree->sibling); free(tree); } } void main() { // Main Function node * curr, * root; int i,num_of_nodes,*node_values,temp,choice,exit=1,print_choice,order_choice; char file_name[100]; FILE *ptr_file; root = NULL; while (exit) // While loop to control program termination { strcpy(file_name,""); print_choice= order_choice= choice = num_of_nodes = temp = 0; node_values= NULL; printf("\n\nPlease select option:\n"); // Options to proceed further in the prog ram. printf("1.Create new tree\n"); printf("2.Print existing tree\n"); printf("3.Exit\n"); scanf("%d",&choice); switch(choice) { case 1: // Creating a new tree if(root) delete_tree(root); // Deleting existing tree before new tree creation.
root = NULL; printf("\nPlease give number of nodes to be entered:\n"); scanf("%d",&num_of_nodes); node_values = calloc(num_of_nodes,sizeof(int)); for(i=1;isibling = NULL; curr->val = node_values[i-1]; insert(&root, curr); } break; case 2: // Printing tree information if(root) // Checking whether tree exist or not. { printf("\nPlease select print Location:\n"); // Options to get output printf("1.Print Here\n"); printf("2.Print in a file\n"); printf("3.Do 1 and 2.\n"); scanf("%d",&print_choice); printf("\nPlease select Print order:\n"); // Options to select Order of nodes in output printf("1.In-order\n"); printf("2.pre-order\n"); printf("3.post-order\n"); scanf("%d",&order_choice); if(print_choice == 2 || print_choice == 3) // Checking whether " file name" is required from user { printf("\nPlease enter file name to Print:\n"); scanf("%s",file_name); ptr_file = fopen(file_name, "w"); if (!ptr_file) // Checking file name given user is exis t or not { printf("no file...\n\n"); if (print_choice =3) print_choice = 1; else break;
} else fclose(ptr_file); } switch(order_choice) { case 1: // Inorder case if(print_choice == 1 || print_choice == 3) // Checking whether output should be shown immediately { printf("\nin order print:\n"); in_order_print(root); } if(print_choice == 2 || print_choice == 3) // Checking whether output should be saved in file or not. { in_order_save(root,file_name); printf("\nData successfully printed in file.\n"); } break; case 2: // preorder case if(print_choice == 1 || print_choice == 3) { printf("\npre order print:\n"); pre_order_print(root); } if(print_choice == 2 || print_choice == 3) { pre_order_save(root,file_name); printf("\nData successfully printed in file.\n"); } break; case 3: // postorder case if(print_choice == 1 || print_choice == 3) { printf("\npost order print:\n"); post_order_print(root); } if(print_choice == 2 || print_choice == 3) { post_order_save(root,file_name); printf("\nData successfully printed in file.\n"); } break; } } else printf("\nTree doesn't exist\n"); // Dispalying info if tre e is not created till now. break; case 3: // Termination of the program exit=0; break; } } }
View more...
Comments