data structure lab

profileSiper
LabProg07-Tree.pdf

Down load Lab#09 Assignment from the Black board and answer the following questions. For this Lab assignment you will be able to use

your own compiler installed on your laptop or use online compiler.

your ID: Last Name: First Name:

Complete the following Binary Search Tree traversal code.

1. /* Binary Tree Traversal - Preorder, Inorder, Postorder */

2. #include<iostream> 3. using namespace std; 4. 5. struct aNode { 6. 7. 8. 9. 10. }; 11. 12. void Preorder(struct aNode *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. 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. 40. } 41. else if(data <= root->data) 42. { 43. root->left = Insert(root->left, data);

44. } 45. else 46. 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. 57. root = Insert(root,'Q'); 58. root = Insert(root,'R'); 59. 60. cout<<"Preorder: "; 61. Preorder(root); 62. cout<<"\n"; 63. //Print Nodes in Inorder 64. cout<<"Inorder: "; 65. Inorder(root); 66. cout<<"\n"; 67. //Print Nodes in Postorder 68. cout<<"Postorder: "; 69. Postorder(root); 70. cout<<"\n"; 71. }

Q1. Read the code segment and fill in the Line #7 ~ #9?

+10 Marks

Q2. Read the code segment and fill in the line #22 ~ #23?

+10 Marks

Q3. Read the code segment and fill in the line #38 ~ #39?

+10 Marks

Q4. Read the code segment and fill in the line #55 ~ #56?

Instert node 'C' and 'J'

+10 Marks

Q5. Read the code segment and fill in the line #46 ?

+10 Marks

Q6. Draw the structure of the binary tree with its address.

+10 Marks

eg. (if tree created with data set { M, B, Q, Z, A, C }

M

/ \

B Q

/ \ \

A C Z

Draw your tree here

+10 Marks

Q7. Tree traversal result

+20 Marks

a. Preorder:

b. Inorder:

c. Postorder:

Q8. Draw your tree with its address (Memory location) -

+20 Marks

eg. (if tree created with data set { M, B, Q, Z, A, C }

0xcb1340

M

/ \

0xbe1358 0xbe0f00

B Q

/ \ \

0xcb1358 0xcb1370 0xbe0f18

A C Z