Skip to content
Dr Jack HW Helper
Home
Browse
About
Contact
Academic Integrity
T
Academic Integrity:
tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.
Home
Browse
W
Write a program that outputs the nodes of a graph in a depth first traversal. So
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
Related Questions
Write a program that must open a file of exactly 33 ints. a. Write a function pr
Question #3652948
Write a program that must open a file with an unknown number of ints for reading
Question #3654888
Write a program that obtains the execution time for finding all the prime number
Question #3828231
Write a program that obtains the execution time of selection sort, insertion sor
Question #3689705
Write a program that obtains the execution time of selection sort, insertion sor
Question #3691669
Write a program that obtains the execution time of selection sort, radix sort, b
Question #3825920
Write a program that obtains the execution time of selection sort, radix sort, b
Question #3826026
Write a program that opens a Java source file, adds line numbers, and saves the
Question #3924479
Navigate
Browse (All)
Browse W
Subjects
Previous
Write a program that outputs the nodes of a graph in a depth first traversal. So
Next
Write a program that outputs the nodes of a graph in a depth-first traversal. Wi