Complete the following Binary Search Tree traversal code. Please see the output
ID: 3704118 • Letter: C
Question
Complete the following Binary Search Tree traversal code. Please see the output shown below
1. /* Binary Tree Traversal - Preorder, Inorder, Postorder */
2. #include<iostream>
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:
Explanation / Answer
Hi Dear,
I have implementation required methods.
Please add these methods in your code and it will work.
Please let me know in case of any issue.
/* struct code completion */
struct aNode {
char data;
struct aNode *left;
struct aNode *right;
};
/* inorder function completion */
void Inorder(aNode *root) {
if(root == NULL) return;
Inorder(root->left);
cout<< " " << root->data ; // Print data
Inorder(root->right);
}
/* insert method implementation */
aNode* Insert(aNode *root,char data) {
if(root == NULL) {
root = new aNode();
root->data = data;
return root;
}
else if(data <= root->data) {
root->left = Insert(root->left,data);
}
else{
root->right = Insert(root->right,data);
}
return root;
}
Please DONT forgot to rate my answer. We are working hard for you Guys!!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.