Question: Complete the following Binary Search Tree traversal code. Please see the output shown below 1. /* Binary Tree Traversal - Preorder, Inorder, Postorder */ 2.
Complete the following Binary Search Tree traversal code. Please see the output shown below
1. /* Binary Tree Traversal - Preorder, Inorder, Postorder */
2. #include
3. using namespace std;
4.
5. structaNode {
6.
7. //FILL CODE HERE
8.
9.
10. };
11.
12. void Preorder(structaNode *root) {
13.
14. if(root == NULL) return;
15. cout<< " " << root->data ; // Print data
16. Preorder(root->left);
17. Preorder(root->right);
18. }
19.
20. void Inorder(aNode *root) {
21. if(root == NULL) return;
22. //FILL CODE HERE
23.
24. Inorder(root->right);
25. }
26.
27. void Postorder(aNode *root) {
28. if(root == NULL) return;
29. Postorder(root->left);
30. Postorder(root->right);
31. cout<< " " << root->data ; // Print data
32. }
33.
34. aNode* Insert(aNode *root,char data) {
35. if(root == NULL) {
36. root = new aNode();
37.
38.
39. //FILL CODE HERE
40. }
41. else if(data <= root->data)
42. {
43. root->left = Insert(root->left,data);
44. }
45. else
46. //FILL CODE HERE
47. return root;
48. }
49.
50. int main() {
51.
52. aNode* root = NULL;
53. root = Insert(root,'N');
54. root = Insert(root,'A');
55.
56. //FILL CODE HERE
// INSERT NODE 'C'&'J'
57. root = Insert(root,'Q');
58. root = Insert(root,'R');
59.
60. cout<<"Preorder: ";
61. Preorder(root);
62. cout<<" ";
63. //Print Nodes in Inorder
64. cout<<"Inorder: ";
65. Inorder(root);
66. cout<<" ";
67. //Print Nodes in Postorder
68. cout<<"Postorder: ";
69. Postorder(root);
70. cout<<" ";
71. }
Output:
Tree Structure
0xcb1340
M
/ \
0xbe1358 0xbe0f00
B Q
/ \ \
0xcb1358 0xcb1370 0xbe0f18
A C Z
Tree traversal result
a. Preorder:
b. Inorder:
c. Postorder:
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
