Program a preorder, inorder, postorder traversal of this tree: A В C E F G H K L M N 向向 Y P V Print the node values "processed" in each. Modify walk.cassociated with this assignment as it creates the tree for you. All you have to do is fill in the three functions, taking advantage of already supplied code. === 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: roid preorder (node* np) {} zoid inorder (node* np) {} void postorder (node* np) {} 1

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 this in C program? (see image)

Given code (need to code the part in bold)

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

}

Program a preorder, inorder, postorder traversal of this tree:
A
E
F
G
K
M
N
向向
U
V
W
Y
Print the node values "processed" in each. Modify walk.c associated with this assignment as it
creates the tree for you. All you have to do is fill in the three functions, taking advantage of
already supplied code.
// =====:
====== 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) {}
1
//
END INSERT FUNCTIONS HERE TO WALK TREE =========:
View the sample output below:
[bth@MacBook-Pro Module3 % ./walkhw
PREORDER:
ABDHPQIRSEJTUKVWCFLMXYGNZO
INORDER:
PHQDRISBTJUEVKWALFXMYCNZGO
POSTORDER:
PQHRSIDTUJVWKEBLXYMFZNOGCA
Transcribed Image Text:Program a preorder, inorder, postorder traversal of this tree: A E F G K M N 向向 U V W Y Print the node values "processed" in each. Modify walk.c associated with this assignment as it creates the tree for you. All you have to do is fill in the three functions, taking advantage of already supplied code. // =====: ====== 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) {} 1 // END INSERT FUNCTIONS HERE TO WALK TREE =========: View the sample output below: [bth@MacBook-Pro Module3 % ./walkhw PREORDER: ABDHPQIRSEJTUKVWCFLMXYGNZO INORDER: PHQDRISBTJUEVKWALFXMYCNZGO POSTORDER: PQHRSIDTUJVWKEBLXYMFZNOGCA
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Similar questions
  • SEE MORE QUESTIONS
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