lude #include   #define MAX_STRING 200   // ========================== NODE/TREE DEFINITIONS ========================== // define node structure  typedef struct nd {   int data;

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Going off of the given code provide here:

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;

}

 

How do I complete the section of code for void preorder (node* np), void inorder (node* np), and void postorder (node* np)?

### Binary Tree Traversals

To understand tree traversals, let's consider the binary tree depicted below:

```
         A
       /   \
     B      C
   /  \    /  \
 D    E  F    G
/ \  / \ / \   / \
H I J K L M  N O
/ |  | | | |  | \
P Q  R S T U  V W  X Y Z
```

Your task is to program a preorder, inorder, and postorder traversal of this binary tree. Each node is labeled with a character for identification.

### Implementation Instructions

For this task, you are provided with a `walk.c` file which constructs the tree. Your job is to complete three traversal functions: `preorder`, `inorder`, and `postorder`. Each functions should print the node values as they are processed.

```c
// ============================= 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 ===============================
```

### Example Output

Here is a sample output from running the traversals:

```shell
bth@MacBook-Pro Module3 % ./walkhw
PREORDER:
ABDHQPIRSEJTUKVWCFLMXYGNZO

INORDER:
PHQDRISBTJUEVKWALFXMYCNZGO

POSTORDER:
PQHRISDTUJVVKWEBLXYMFZNOGCA
```

### Explanation of the Traversal Methods

#### Preorder Traversal
In a preorder traversal, each node is processed before its child nodes. The general algorithm is:
1. Visit the root node.
2. Traverse the left subtree in preorder.
3. Traverse the right subtree in preorder.

In this example, the preorder traversal outputs: `ABDHQPIRSEJTUKVWCFLMXYGNZO`.

#### Inorder Traversal
In an inorder traversal, each node is processed between its two child nodes. The general algorithm is:
1. Traverse the left subtree in inorder.
2. Visit the root node.
3. Traverse the right subtree in inorder.

For this tree, the inorder traversal outputs: `PHQDRIS
Transcribed Image Text:### Binary Tree Traversals To understand tree traversals, let's consider the binary tree depicted below: ``` A / \ B C / \ / \ D E F G / \ / \ / \ / \ H I J K L M N O / | | | | | | \ P Q R S T U V W X Y Z ``` Your task is to program a preorder, inorder, and postorder traversal of this binary tree. Each node is labeled with a character for identification. ### Implementation Instructions For this task, you are provided with a `walk.c` file which constructs the tree. Your job is to complete three traversal functions: `preorder`, `inorder`, and `postorder`. Each functions should print the node values as they are processed. ```c // ============================= 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 =============================== ``` ### Example Output Here is a sample output from running the traversals: ```shell bth@MacBook-Pro Module3 % ./walkhw PREORDER: ABDHQPIRSEJTUKVWCFLMXYGNZO INORDER: PHQDRISBTJUEVKWALFXMYCNZGO POSTORDER: PQHRISDTUJVVKWEBLXYMFZNOGCA ``` ### Explanation of the Traversal Methods #### Preorder Traversal In a preorder traversal, each node is processed before its child nodes. The general algorithm is: 1. Visit the root node. 2. Traverse the left subtree in preorder. 3. Traverse the right subtree in preorder. In this example, the preorder traversal outputs: `ABDHQPIRSEJTUKVWCFLMXYGNZO`. #### Inorder Traversal In an inorder traversal, each node is processed between its two child nodes. The general algorithm is: 1. Traverse the left subtree in inorder. 2. Visit the root node. 3. Traverse the right subtree in inorder. For this tree, the inorder traversal outputs: `PHQDRIS
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps

Blurred answer
Knowledge Booster
Introduction to Coding
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education