Lab on Trees In class you learned that the binary tree data-structure could repr
ID: 3586192 • Letter: L
Question
Lab on Trees In class you learned that the binary tree data-structure could represented in computer memory contiguously or by random access. Using principled object oriented programming, represent a tree using contiguous access. The tree will be inputted into the computer layer-by-layer starting at the root. Input is terminated with-1 For example blue green yellow pink red purple orange black brown -1 Which yield the following tree structure blue green yello pink purple orange black Question 1. Output a pre-order traversal of the input tree. Output should all be on one line. e.g. output: blue green pink black brown red yellow purple orange put a In-order tra should all ttree. Output s e.g. output black pink brown green red blue purple yellow orange Question 3. Output a post-order traversal of the input tree. Output should all be on one line e.g. output: black brown pink red green purple orange yellow blueExplanation / Answer
Answer 1. Function(in c++) for pre-order traversal of the input tree
preorder(struct node *root) //here node is a structure in c++
{
if (node == NULL)
return;
printf("%d ", node->color);
preorder(node->left);
preorder(node->right);
}
Answer 2. Function(in c++) for In-order traversal of the input tree
inorder(struct node *root)
{
if (node == NULL)
return;
inorder(node->left);
printf("%d ", node->color);
inorder(node->right);
}
Answer 3. Function(in c++) for post-order traversal of the input tree
postorder(struct node *root)
{
if (node == NULL)
return;
postorder(node->left);
postorder(node->right);
printf("%d ", node->color);
}
//here node is =
/*
struct node
{
string color; //name of color
struct node* left; //left child of node
struct node* right; //right child of node
};
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.