Please read the comments and implement the three traversal functions in treeTraversal.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 3. Add a new function create_binary_tree_int(), in which elements are integers. The binary tree shape is, 2 5 (11) (4) 4. In the main, declare a binary tree root_int which is created by the function create_binary_tree_int(), 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 D - E -> C > inorder: D -> B -> E -> A -> C -> postorder: D -> E > B > C -> A -> preorder: 1 -> 7 -> 2 -> 6 inorder: 2 -> 75 - postorder: 2 -> 5 -> 11 -> 5 <- 6 -> 11 -> 6 -> -> 11 -> 3 -> 1 - -> 3 -> 9 -> 4 -> -> 4 -> 9 -> 7 -> 4 -> 9 -> 3 -> -> 1 -> 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 treeTraversal.cpp. // treeTraversal.cpp #include using namespace std; template struct BinaryNode { T data; BinaryNode✶ left; BinaryNode* right; BinaryNode(const T & d = T()): data(d), left(nullptr), right(nullptr) { } }; //print the elements of binary tree in preorder template void preorder(BinaryNode* t) { // add your code } //print the elements of binary tree in inorder template void inorder(BinaryNode* t) { // add your code } //print the elements of binary tree in postorder template void postorder (BinaryNode* t) { // add your code } The main function is contained in the file Lab08.cpp // lab08.cpp #include "treeTraversal.cpp" BinaryNode* create_binary_tree() { } BinaryNode* node_A = new BinaryNode('A'); BinaryNode* node_B = new BinaryNode('B'); BinaryNode* node_C = new BinaryNode('C'); BinaryNode* node_D = new BinaryNode('D'); BinaryNode* node_E = new BinaryNode('E'); node_A->left = node_B; node_A->right = node_C; node B->left = node_D; node_B->right = node_Ė; return node_A; BinaryNode* create_binary_tree_int() { } BinaryNode* node1 = new BinaryNode(1); BinaryNode* node = new BinaryNode(7); BinaryNode* node3 = new BinaryNode(3); // Add you code to create remain 6 nodes node1->left = node7; node1->right = node3; // Add you code to connect nodes return node1; int main() { } BinaryNode* root = create_binary_tree(); // add your code // call three traversal functions to print elements BinaryNode* root_int = create_binary_tree_int(); // add your code // call three traversal functions to print elements

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

Write the full C++ code for the expected output 

Please read the comments and implement the three traversal functions in treeTraversal.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
3. Add a new function create_binary_tree_int(), in which elements are integers. The binary tree shape is,
2
5
(11) (4)
4. In the main, declare a binary tree root_int which is created by the function create_binary_tree_int(), 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
D - E -> C >
inorder: D -> B -> E -> A -> C ->
postorder: D -> E > B > C -> A ->
preorder: 1 -> 7 -> 2 -> 6
inorder: 2 -> 75 -
postorder: 2 -> 5 -> 11
-> 5
<-
6 -> 11
-> 6 ->
-> 11 -> 3
-> 1 - -> 3
-> 9 -> 4 ->
-> 4 -> 9 ->
7 -> 4 -> 9 -> 3 ->
-> 1 ->
Transcribed Image Text:Please read the comments and implement the three traversal functions in treeTraversal.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 3. Add a new function create_binary_tree_int(), in which elements are integers. The binary tree shape is, 2 5 (11) (4) 4. In the main, declare a binary tree root_int which is created by the function create_binary_tree_int(), 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 D - E -> C > inorder: D -> B -> E -> A -> C -> postorder: D -> E > B > C -> A -> preorder: 1 -> 7 -> 2 -> 6 inorder: 2 -> 75 - postorder: 2 -> 5 -> 11 -> 5 <- 6 -> 11 -> 6 -> -> 11 -> 3 -> 1 - -> 3 -> 9 -> 4 -> -> 4 -> 9 -> 7 -> 4 -> 9 -> 3 -> -> 1 ->
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 treeTraversal.cpp.
// treeTraversal.cpp
#include<iostream>
using namespace std;
template<typename T>
struct BinaryNode
{
T data;
BinaryNode✶ left;
BinaryNode* right;
BinaryNode(const T & d = T()): data(d), left(nullptr), right(nullptr)
{ }
};
//print the elements of binary tree in preorder
template<typename T>
void preorder(BinaryNode<T>* t)
{
// add your code
}
//print the elements of binary tree in inorder
template<typename T>
void inorder(BinaryNode<T>* t)
{
// add your code
}
//print the elements of binary tree in postorder
template<typename T>
void postorder (BinaryNode<T>* t)
{
// add your code
}
The main function is contained in the file Lab08.cpp
// lab08.cpp
#include "treeTraversal.cpp"
BinaryNode<char>* create_binary_tree()
{
}
BinaryNode<char>* node_A = new BinaryNode<char>('A');
BinaryNode<char>* node_B = new BinaryNode<char>('B');
BinaryNode<char>* node_C = new BinaryNode<char>('C');
BinaryNode<char>* node_D = new BinaryNode<char>('D');
BinaryNode<char>* node_E = new BinaryNode<char>('E');
node_A->left = node_B;
node_A->right = node_C;
node B->left = node_D;
node_B->right = node_Ė;
return node_A;
BinaryNode<int>* create_binary_tree_int()
{
}
BinaryNode<int>* node1 = new BinaryNode<int>(1);
BinaryNode<int>* node = new BinaryNode<int>(7);
BinaryNode<int>* node3 = new BinaryNode<int>(3);
// Add you code to create remain 6 nodes
node1->left = node7;
node1->right = node3;
// Add you code to connect nodes
return node1;
int main()
{
}
BinaryNode<char>* root = create_binary_tree();
// add your code
// call three traversal functions to print elements
BinaryNode<int>* root_int = create_binary_tree_int();
// add your code
// call three traversal functions to print elements
Transcribed Image Text: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 treeTraversal.cpp. // treeTraversal.cpp #include<iostream> using namespace std; template<typename T> struct BinaryNode { T data; BinaryNode✶ left; BinaryNode* right; BinaryNode(const T & d = T()): data(d), left(nullptr), right(nullptr) { } }; //print the elements of binary tree in preorder template<typename T> void preorder(BinaryNode<T>* t) { // add your code } //print the elements of binary tree in inorder template<typename T> void inorder(BinaryNode<T>* t) { // add your code } //print the elements of binary tree in postorder template<typename T> void postorder (BinaryNode<T>* t) { // add your code } The main function is contained in the file Lab08.cpp // lab08.cpp #include "treeTraversal.cpp" BinaryNode<char>* create_binary_tree() { } BinaryNode<char>* node_A = new BinaryNode<char>('A'); BinaryNode<char>* node_B = new BinaryNode<char>('B'); BinaryNode<char>* node_C = new BinaryNode<char>('C'); BinaryNode<char>* node_D = new BinaryNode<char>('D'); BinaryNode<char>* node_E = new BinaryNode<char>('E'); node_A->left = node_B; node_A->right = node_C; node B->left = node_D; node_B->right = node_Ė; return node_A; BinaryNode<int>* create_binary_tree_int() { } BinaryNode<int>* node1 = new BinaryNode<int>(1); BinaryNode<int>* node = new BinaryNode<int>(7); BinaryNode<int>* node3 = new BinaryNode<int>(3); // Add you code to create remain 6 nodes node1->left = node7; node1->right = node3; // Add you code to connect nodes return node1; int main() { } BinaryNode<char>* root = create_binary_tree(); // add your code // call three traversal functions to print elements BinaryNode<int>* root_int = create_binary_tree_int(); // add your code // call three traversal functions to print elements
Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Similar 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