Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Questions: 1) What kind of tree does the code below generate? 2) Modify it to do all 3 traversals (Pre-order traversal, In-order traversal, and Post-order

Questions:

1) What kind of tree does the code below generate?

2) Modify it to do all 3 traversals (Pre-order traversal, In-order traversal, and Post-order traversal).

3) Use rand() to generate data. Now what do you get? - I am unsure what was ment by this question so if anyone knows that would be great, if not I guess it is ok to just reply cannot answer 3.

4) How do you organize the input to produce the tree in the image? Prove this with your code

_____________________________________________________________________________________________________________________________________________________

Code:

#include #include struct myTree{ int val; struct myTree *right, *left; }; typedef struct myTree _node; void insert(_node *(*tree), _node *item){ if(!(*tree)){ *tree = item; return; } if (item->val val) insert(&(*tree)->left , item); else if (item->val > (*tree)->val) insert(&(*tree)->right , item); } void traverse (_node *tree){ if(tree->left != NULL) traverse(tree->left); printf("%d ",tree->val); if (tree->right != NULL) traverse(tree->right); } void main (){ _node *current, *root; int i; root = NULL; for(i = 0; i left = current ->right = NULL ; current->val = i; insert( &root, current); } traverse(root); }

_____________________________________________________________________________________________________________________________________________________

Image of tree: I am unsure if 7 6 and 8 are part of the tree or not

image text in transcribed

5

/ \

3 7

/ \ / \

2 4 6 8

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data Visualization A Practical Introduction

Authors: Kieran Healy

1st Edition

0691181624, 978-0691181622

More Books

Students also viewed these Databases questions