Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Write a C++ program to print the elements of binary trees using preorder, inorder, and postorder traversal. The program includes the following Declare and implement
Write a C++ program to print the elements of binary trees using preorder, inorder, and postorder traversal. The program includes the following Declare and implement functions preorder, inorder, and postorder in the file funcs.cpp // funcs.cpp #include using namespace std; template struct BinaryNode T element; BinaryNode left; BinaryNode right; BinaryNode(const T & d T()) : element(d) left nullptr; right nullptr; //print the elements of binary tree in preorder template void preorder (const BinaryNode* root) // add your code //print the elements of binary tree in inorden template void inorder(const BinaryNode root) // add your code //print the elements of binary tree in postorder void postorder(const BinaryNode root) // add your code ename T> . The main function is contained in the file lab06. cpp // lab06.cpp #include "funcs.cpp.. BinaryNode* BinaryNode(char>* BinaryNode* BinaryNode* node-A node-B node_C node-D node-E new BinaryNode(char('A'); new BinaryNodechar('B'); new BinaryNode('C'); new BinaryNodechar('D'); new BinaryNode('E' ); = = = node. A->left = node-B; node A->right = node C; node-B->left = node. D; node_B->right node_E; return node A; int main() BinaryNode* root create-binary-tree(); = // add your code // call traversal functions to print elements . Please read the comments and implement the three traversal functions in funcs.cpp Then complete the following steps 1. In the main(), declare a binary tree root in which elements are char type, and call three traversal functions to print the elements of this binary tree 2. Compile and run the program to make sure that your functions work correctly 3. Add a new function create_binary_tree_int(), in which elements are integers. The binary tree shape is, 5 (11) 4 4. In the main(), declare a binary tree root_int in which elements are char type, and call three traversal functions to print the elements of this binary tree 4. In the main(), declare a binary tree root_int in which elements are char type, and call three traversal functions to print the elements of this binary tree 5. Compile and run the program to make sure that your functions work correctly The expected result: preorder: A->B-DE-C- inorder: D-> B-> E-> A-> C-> postorder: D-E -BC->A-> preorder: 1-7- 2 -6->5-11 -3-9-4- inorder: 2-7-5- 6-> 11-> 1> 3-4 -9 > postorder: 2-5-11-6- 7 ->4->9- 3 -1
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started