Attached is a C program traveselter.c. This is a solution to the problem of constructing a binary tree from the inorder and preorder listing of the nodes. It has the recursive function   void list_inorder(struct Node * root)   which lists the nodes of the tree in-order. Rewrite this function so that it is iterative. Hint: Implement a stack and you may assume it can hold a maximum of 100 items. You have to decide what will be in an item of the stack, and design a structure for it. I suggest that one member of an item is a pointer to a tree node.     #include #include   struct Node {     int val;     struct Node * left;     struct Node * right; };   struct Node * create_node(char val); void destroy_node(struct Node * root); void destroy_tree(struct Node * root); void list_preorder(struct Node * root); struct Node * create_tree(char pre_order[], int pre_first, int pre_last, char in_order[], int in_first, int in_last);   void list_inorder(struct Node * root) { if (root==NULL) return; list_inorder(root->left); printf(" %c",root->val); list_inorder(root->right); }       void main() { char pre_order[12] = {'Y','F','H','Z','B','G','C','D','Q','A','K','M'}; char in_order[12] = {'Z','H','B','F','G','C','Y','D','K','A','M','Q'};   struct Node * root= create_tree(pre_order,0,11,in_order,0,11);   printf("Pre-order: "); list_preorder(root); printf("\n"); printf("In-order: "); list_inorder(root); printf("\n");   destroy_tree(root); }   struct Node * create_tree(char pre_order[], int pre_first, int pre_last, char in_order[], int in_first, int in_last) { if (pre_first>pre_last) return NULL; if (in_first>in_last) return NULL;   struct Node * root = create_node(pre_order[pre_first]);   int k; for (k=in_first; in_order[k]!=pre_order[pre_first]; k++);   int left_size=k-in_first; int right_size=in_last-k;   root->left=create_tree(pre_order,pre_first+1,pre_first+left_size,in_order,in_first,k-1);   root->right=create_tree(pre_order,pre_first+left_size+1,pre_first+left_size+right_size,in_order,k+1,k+right_size);   return root; }   struct Node * create_node(char val) { struct Node * node = (struct Node *) malloc(sizeof(struct Node)); node->val = val; node->left = NULL; node->right = NULL; return node; }   void destroy_node(struct Node * root) { free(root); }   void destroy_tree(struct Node * root) { if (root!=NULL) {     destroy_tree(root->left);     destroy_tree(root->right);     destroy_node(root); } }   void list_preorder(struct Node * root) { if (root==NULL) return; printf(" %c",root->val); list_preorder(root->left); list_preorder(root->right); }

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
100%

Attached is a C program traveselter.c. This is a solution to the problem of constructing a binary tree from the inorder and preorder listing

of the nodes. It has the recursive function

 

void list_inorder(struct Node * root)

 

which lists the nodes of the tree in-order. Rewrite this function so that it is iterative.

Hint: Implement a stack and you may assume it can hold a maximum of 100 items. You have to decide what will be in an item of the stack,

and design a structure for it. I suggest that one member of an item is a pointer to a tree node.

 

 

#include <stdlib.h>

#include <stdio.h>

 

struct Node {

    int val;

    struct Node * left;

    struct Node * right;

};

 

struct Node * create_node(char val);

void destroy_node(struct Node * root);

void destroy_tree(struct Node * root);

void list_preorder(struct Node * root);

struct Node * create_tree(char pre_order[], int pre_first, int pre_last, char in_order[], int in_first, int in_last);

 

void list_inorder(struct Node * root)

{

if (root==NULL) return;

list_inorder(root->left);

printf(" %c",root->val);

list_inorder(root->right);

}

 

 

 

void main()

{

char pre_order[12] = {'Y','F','H','Z','B','G','C','D','Q','A','K','M'};

char in_order[12] = {'Z','H','B','F','G','C','Y','D','K','A','M','Q'};

 

struct Node * root= create_tree(pre_order,0,11,in_order,0,11);

 

printf("Pre-order: ");

list_preorder(root);

printf("\n");

printf("In-order: ");

list_inorder(root);

printf("\n");

 

destroy_tree(root);

}

 

struct Node * create_tree(char pre_order[], int pre_first, int pre_last, char in_order[], int in_first, int in_last)

{

if (pre_first>pre_last) return NULL;

if (in_first>in_last) return NULL;

 

struct Node * root = create_node(pre_order[pre_first]);

 

int k;

for (k=in_first; in_order[k]!=pre_order[pre_first]; k++);

 

int left_size=k-in_first;

int right_size=in_last-k;

 

root->left=create_tree(pre_order,pre_first+1,pre_first+left_size,in_order,in_first,k-1);

 

root->right=create_tree(pre_order,pre_first+left_size+1,pre_first+left_size+right_size,in_order,k+1,k+right_size);

 

return root;

}

 

struct Node * create_node(char val)

{

struct Node * node = (struct Node *) malloc(sizeof(struct Node));

node->val = val;

node->left = NULL;

node->right = NULL;

return node;

}

 

void destroy_node(struct Node * root)

{

free(root);

}

 

void destroy_tree(struct Node * root)

{

if (root!=NULL) {

    destroy_tree(root->left);

    destroy_tree(root->right);

    destroy_node(root);

}

}

 

void list_preorder(struct Node * root)

{

if (root==NULL) return;

printf(" %c",root->val);

list_preorder(root->left);

list_preorder(root->right);

}

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 1 images

Blurred answer
Knowledge Booster
Stack
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