Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that outputs the nodes of a graph in a depth first traversal. So

ID: 3618022 • Letter: W

Question

Write a program that outputs the nodes of a graph in a depth first traversal.

Explanation / Answer

typedef struct node { int value; struct node *right; struct node *left; } mynode; mynode *root; add_node(int value); void levelOrderTraversal(mynode *root); int main(int argc, char* argv[]) { root = NULL; add_node(5); add_node(1); add_node(-20); add_node(100); add_node(23); add_node(67); add_node(13); printf(" LEVEL ORDER TRAVERSAL "); levelOrderTraversal(root); getch(); } // Function to add a new node... add_node(int value) { mynode *prev, *cur, *temp; temp = malloc(sizeof(mynode)); temp->value = value; temp->right = NULL; temp->left = NULL; if(root == NULL) { printf(" Creating the root.. "); root = temp; return; } prev = NULL; cur = root; while(cur != NULL) { prev = cur; //cur = (value value) ? cur->left:cur->right; if(value value) { cur = cur->left; } else { cur = cur->right; } } if(value value) { prev->left = temp; } else { prev->right = temp; } } // Level order traversal.. void levelOrderTraversal(mynode *root) { mynode *queue[100] = {(mynode *)0}; // Important to initialize! int size = 0; int queue_pointer = 0; while(root) { printf("[%d] ", root->value); if(root->left) { queue[size++] = root->left; } if(root->right) { queue[size++] = root->right; } root = queue[queue_pointer++]; } } Read more: http://wiki.answers.com/Q/Write_a_program_in_c_to_implement_the_depth_first_traversal#ixzz1gzn8PiQo
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote