Tree Traversal Coding: How do I code the following in C program? // ====== BEGIN INSERT FUNCTION DEFS TO WALK TREE ========= // define 3 functions - preorder, inorder, postorder to walk tree, printing out data (char) // associated with each node visited: void preorder (node* np) {} void inorder (node* np) {} void postorder (node* np) {} walk.c file with the rest of the code given. #include #include #include #include #define MAX_STRING 200 // ========================== NODE/TREE DEFINITIONS ========================== // define node structure typedef struct nd { int data; struct nd* left; struct nd* right; } node; // "new" function to create a node, set data value to d and children to NULL node* newNode(int d) { node* np; np = (node*)malloc(sizeof(node)); if (np != NULL) { np->data = d; np->left = NULL; np->right = NULL; } return(np); } // declare root of our binary tree node* rootp = NULL; // ========================== STRING DEFINITIONS ========================== // dna is a string defining the content and structure of the tree in pre-order with '\n' for NULL pointers char dna[MAX_STRING] = "ABDHP\n\nQ\n\nIR\n\nS\n\nEJT\n\nU\n\nKV\n\nW\n\nCFL\n\nMX\n\nY\n\nGN\nZ\n\nO\n\n"; // remove the first character from string s and return it, \x01 will be an error code if string is empty char getFirstChar(char* s) { char c; if (strlen(s) > 0) { // copy the first character c = s[0]; // remove the first charcter from the string memmove(s, s+1, strlen(s)); } else { // empty string, set error code c = '\x01'; } return c; } // ========================== MAIN RECURSIVE FUNCTION TO BUILD TREE ========================== // using the instruction in string s, insert the defined nodes into tree pointed to by rp void insertNode (node* rp, char* s) { char c; // temporary character // we assume that rp points to an existing node and // we are going to add its two children based on the next two instructions in s if (rp != NULL) { // if instruction string is not empty then add a new node to the left if (strlen(s) > 0) { // get the first character of the string which is our "instruction" c = getFirstChar(s); if (c == '\x01') { printf("ILLEGAL CHARACTER IN INSTRUCTION STRING\n"); } // if instruction does not tell us to insert null, create a new node and add it as left child, recurse if (c != '\n') { rp->left = newNode(c); insertNode(rp->left, s); } } // if instruction string is not empty then add a new node to the right if (strlen(s) > 0) { // get the first character of the string which is our "instruction" c = getFirstChar(s); if (c == '\x01') { printf("ILLEGAL CHARACTER IN INSTRUCTION STRING\n"); } // if instruction does not tell us to insert null, create a new node and add it as right child, recurse if (c != '\n') { rp->right = newNode(c); insertNode(rp->right, s); } } } return; } // ========================== BEGIN INSERT FUNCTION DEFS TO WALK TREE ========================== // define 3 functions - preorder, inorder, postorder to walk tree, printing out data (char) // associated with each node visited: void preorder (node* np) {} void inorder (node* np) {} void postorder (node* np) {} // ========================== END INSERT FUNCTIONS HERE TO WALK TREE ========================== // ========================== MAIN PROGRAM ========================== int main() { char c; // temporary character variable // prime the pump - add the first node to the tree and then recursively call on children rootp = NULL; // if instruction string is not empty then create root node if (strlen(dna) > 0) { // get the first character of the string which is our "instruction" c = getFirstChar(dna); if (c == '\x01') { printf("ILLEGAL CHARACTER IN INSTRUCTION STRING\n"); } // if instruction does not tell us to insert null, create a new node and add it as left child, recurse if (c != '\n') { rootp = newNode(c); insertNode(rootp, dna); } } // ========================== MAIN PROG CODE TO CALL WALK FUNCTONS ========================== printf("PREORDER:\n"); preorder(rootp); printf("\n\n"); printf("INORDER:\n"); inorder(rootp); printf("\n\n"); printf("POSTORDER:\n"); postorder(rootp); printf("\n\n"); return 0; }
Tree Traversal Coding: How do I code the following in C program?
// ====== BEGIN INSERT FUNCTION DEFS TO WALK TREE =========
// define 3 functions - preorder, inorder, postorder to walk tree, printing out data (char)
// associated with each node visited:
void preorder (node* np) {}
void inorder (node* np) {}
void postorder (node* np) {}
walk.c file with the rest of the code given.
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#define MAX_STRING 200
// ========================== NODE/TREE DEFINITIONS ==========================
// define node structure
typedef struct nd {
int data;
struct nd* left;
struct nd* right;
} node;
// "new" function to create a node, set data value to d and children to NULL
node* newNode(int d) {
node* np;
np = (node*)malloc(sizeof(node));
if (np != NULL) {
np->data = d;
np->left = NULL;
np->right = NULL;
}
return(np);
}
// declare root of our binary tree
node* rootp = NULL;
// ========================== STRING DEFINITIONS ==========================
// dna is a string defining the content and structure of the tree in pre-order with '\n' for NULL pointers
char dna[MAX_STRING] = "ABDHP\n\nQ\n\nIR\n\nS\n\nEJT\n\nU\n\nKV\n\nW\n\nCFL\n\nMX\n\nY\n\nGN\nZ\n\nO\n\n";
// remove the first character from string s and return it, \x01 will be an error code if string is empty
char getFirstChar(char* s) {
char c;
if (strlen(s) > 0) {
// copy the first character
c = s[0];
// remove the first charcter from the string
memmove(s, s+1, strlen(s));
} else {
// empty string, set error code
c = '\x01';
}
return c;
}
// ========================== MAIN RECURSIVE FUNCTION TO BUILD TREE ==========================
// using the instruction in string s, insert the defined nodes into tree pointed to by rp
void insertNode (node* rp, char* s) {
char c; // temporary character
// we assume that rp points to an existing node and
// we are going to add its two children based on the next two instructions in s
if (rp != NULL) {
// if instruction string is not empty then add a new node to the left
if (strlen(s) > 0) {
// get the first character of the string which is our "instruction"
c = getFirstChar(s);
if (c == '\x01') {
printf("ILLEGAL CHARACTER IN INSTRUCTION STRING\n");
}
// if instruction does not tell us to insert null, create a new node and add it as left child, recurse
if (c != '\n') {
rp->left = newNode(c);
insertNode(rp->left, s);
}
}
// if instruction string is not empty then add a new node to the right
if (strlen(s) > 0) {
// get the first character of the string which is our "instruction"
c = getFirstChar(s);
if (c == '\x01') {
printf("ILLEGAL CHARACTER IN INSTRUCTION STRING\n");
}
// if instruction does not tell us to insert null, create a new node and add it as right child, recurse
if (c != '\n') {
rp->right = newNode(c);
insertNode(rp->right, s);
}
}
}
return;
}
// ========================== BEGIN INSERT FUNCTION DEFS TO WALK TREE ==========================
// define 3 functions - preorder, inorder, postorder to walk tree, printing out data (char)
// associated with each node visited:
void preorder (node* np) {}
void inorder (node* np) {}
void postorder (node* np) {}
// ========================== END INSERT FUNCTIONS HERE TO WALK TREE ==========================
// ========================== MAIN PROGRAM ==========================
int main() {
char c; // temporary character variable
// prime the pump - add the first node to the tree and then recursively call on children
rootp = NULL;
// if instruction string is not empty then create root node
if (strlen(dna) > 0) {
// get the first character of the string which is our "instruction"
c = getFirstChar(dna);
if (c == '\x01') {
printf("ILLEGAL CHARACTER IN INSTRUCTION STRING\n");
}
// if instruction does not tell us to insert null, create a new node and add it as left child, recurse
if (c != '\n') {
rootp = newNode(c);
insertNode(rootp, dna);
}
}
// ========================== MAIN PROG CODE TO CALL WALK FUNCTONS ==========================
printf("PREORDER:\n");
preorder(rootp);
printf("\n\n");
printf("INORDER:\n");
inorder(rootp);
printf("\n\n");
printf("POSTORDER:\n");
postorder(rootp);
printf("\n\n");
return 0;
}
![### Tree Traversal Assignment
**Objective**: Program a preorder, inorder, and postorder traversal of the tree shown below.
#### Tree Diagram
```
A
/ | \
B E C
/| /|\ |\
H D I J K L M N O
/ | \ | /|\ | | / | \
P Q R S T U V W X Y Z
```
Each node in the tree is labeled with an uppercase character from A to Z.
**Instructions**:
1. Implement the functions for preorder, inorder, and postorder tree traversal.
2. The tree structure is predefined in the `walk.c` file, and you need to modify this file to fill in the three traversal functions.
**Code Snippet**:
```c
// ====================== BEGIN INSERT FUNCTION DEFS TO WALK TREE ======================
// Define 3 functions - preorder, inorder, postorder to walk the tree, printing out data (char)
// associated with each node visited:
void preorder (node* np) {}
void inorder (node* np) {}
void postorder (node* np) {}
// ======================== END INSERT FUNCTIONS HERE TO WALK TREE =======================
```
**Sample Output**:
Below is an example of what the output should look like when the functions are implemented correctly. This example was run on a MacBook Pro in a terminal.
```
bth@MacBook-Pro Module3 % ./walkhw
PREORDER:
ABDHPQIRSEJTUKVWCFLMXYGNZO
INORDER:
PHQDRISETJUKVWCFLMXMYCNZGO
POSTORDER:
PQHRISDTUJVVKEBLXYMFZNOGCA
```
**Traversals Explained**:
- **Preorder Traversal**: Visit the root node first, then recursively visit the left subtree, followed by the right subtree.
- **Inorder Traversal**: Recursively visit the left subtree first, then visit the root node, and finally recursively visit the right subtree.
- **Postorder Traversal**: Recursively visit the left subtree, then the right subtree, and finally visit the root node.
Complete the `preorder`, `inorder`, and `postorder` functions in `walk.c` to replicate the sample output shown above.](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F8cb58778-70ba-4f7e-90ce-7267bd79a013%2Ffc828b57-2b36-4bf2-9f3d-fa4dd0f14b3d%2Fz5mk2v_processed.png&w=3840&q=75)
![](/static/compass_v2/shared-icons/check-mark.png)
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 2 images
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![Computer Networking: A Top-Down Approach (7th Edi…](https://www.bartleby.com/isbn_cover_images/9780133594140/9780133594140_smallCoverImage.gif)
![Computer Organization and Design MIPS Edition, Fi…](https://www.bartleby.com/isbn_cover_images/9780124077263/9780124077263_smallCoverImage.gif)
![Network+ Guide to Networks (MindTap Course List)](https://www.bartleby.com/isbn_cover_images/9781337569330/9781337569330_smallCoverImage.gif)
![Computer Networking: A Top-Down Approach (7th Edi…](https://www.bartleby.com/isbn_cover_images/9780133594140/9780133594140_smallCoverImage.gif)
![Computer Organization and Design MIPS Edition, Fi…](https://www.bartleby.com/isbn_cover_images/9780124077263/9780124077263_smallCoverImage.gif)
![Network+ Guide to Networks (MindTap Course List)](https://www.bartleby.com/isbn_cover_images/9781337569330/9781337569330_smallCoverImage.gif)
![Concepts of Database Management](https://www.bartleby.com/isbn_cover_images/9781337093422/9781337093422_smallCoverImage.gif)
![Prelude to Programming](https://www.bartleby.com/isbn_cover_images/9780133750423/9780133750423_smallCoverImage.jpg)
![Sc Business Data Communications and Networking, T…](https://www.bartleby.com/isbn_cover_images/9781119368830/9781119368830_smallCoverImage.gif)