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; }

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

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.
Transcribed Image Text:### 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.
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 2 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY