Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Objective of this assignment is to reinforce understanding of Binary Trees and recursion. Write program to implement Binary Trees. You use the Node class from

Objective of this assignment is to reinforce understanding of Binary Trees and recursion.

Write program to implement Binary Trees. You use the Node class from the Java Library, or write own Binary Tree Class, and implement the methods listed below. Hint, review section 25.2 (starts on p. 930) in the text for designing and implenting binary trees using linked lists.

Data is provided for this assignment. The data is in the form of sample tree arrays, listed in the additional data file that is attached to the Assignment as a file called A8-BT-testdata.txt.

You are NOT going to read this data from thefile into program !

You will hard code the test data that is providedin A8-BT-testdata.txt, i.e., copy and paste the data. In addition, note that the sample trees use pre order and in order traversals, as indicated by the sample data array names. For example, the array named, double pre[ ] is data used for pre-order traversal, and the array named double in[ ] is for in-order traversal. After the insertion of data for each tree, you must determine whether the tree is a binary search tree or not, and provide that information in test results.

HINT: Create one tree at a time, using the data provided for that tree, execute the methods on that tree, write the results to the screen, and get a screenshot of each output.

METHODS

1. depth

if root is NULL, return -1

otherwise, determine maximum of the depths of the left and right subtrees, add 1 and return

2. max

test that root is not NULL

get the max in the left subtree, the max in the right subtree, and the data value in root

return the largest of these three values

ignore a subtree if it is empty

3. tree_sum

returns the sum of the values in all the nodes in the tree

4. tree_average

returns the average of the values in all the nodes in the tree

5. tree_is_balanced

returns true if and only if the tree is balanced

this means that the absolute value of the difference in depth between the

two subtrees of root is no more than 1.

it also means that both the left and the right subtree are balanced.

An empty tree is considered to be balanced.

if root is empty, the tree is balanced

otherwise, if either the right or the left subtree is not balanced,

then the tree is not balanced

otherwise, if the difference in depths between the left and right subtrees

is greater than one then the tree is not balanced

otherwise, the tree is balanced

TEST: To test the above methods, create 6 trees, one at a time, using the sample data and write results in the listed form shown below

Tree_Name

Binary search tree ? Yes or no // indicate whether the tree is a BST

Depth

Max

Sum

Average

Is Balanced ?

image text in transcribed
binary_tree sample1 pre = 1, 3} // pre-order in = {1, 2, 3} // in-order // 3 parameter means - 3 items in pre-order and 3 items in in-order return build_from_node_lists (pre, in, 3) binary_tree sample2 int pre2 = { 1, 2, 4, 8, 5, 9, 3, 6, 10, 11, 7, 12} int in2 = {8, 4, 2, 9, 5 // pre order // in order return build_from_node_lists (pre2, in2, 12) // 12 is size binary_tree sample3 // seed 73 int size3 = 57 // size 57 in each double pre3 = {0. 185038, 0. 394342, 0. 291092, 0.289448, 0. 101709, 0.0611125, 0. 193208, 0. 13517, 0. 0404296, 0.410769, 0. 0198583, 0. 432688, 0. 0674109, 0. 151873, 0. 14349, 0.286818, 0.071119, 0. 170354, 0.0670367, 0.44319, 0. 0682843, 0. 270964, 0. 369207, 0. 461848, 0. 427017, 0.377756, 0. 0485934, 0. 0667142, 0. 108895, 0. 153658, 0. 00263633, 0. 222634, 0. 094924, 0. 195702, 0. 170448, 0. 364803, 0. 378102, 0. 440015, 0. 147172, 0. 0208332, 0.340395, 0. 131731, 0. 275594, 0. 390628, 0.317047, 0. 108642, 0.393182, 0.307225, 0. 159842, 0. 292226, 0. 126009, 0. 363135, 0. 183877, 0.228193, 0. 449523, 0. 042972, 0.462529) double in3 = {0. 193208, 0. 13517, 0. 0611125, 0. 101709, 0. 289448, 0. 410769, 0. 0198583, 0. 151873, 0. 0674109, 0. 432688, 0. 0404296, 0. 286818, 0. 071119, 0. 170354, 0. 44319, 0. 0670367, 0. 14349, 0. 291092, 0. 0682843, 0. 270964, 0. 394342, 0.377756, 0. 427017, 0. 461848, 0. 0485934, 0. 369207, 0. 108895, 0. 153658, 0. 00263633, 0. 0667142, 0. 195702, 0. 094924, 0. 170448, 0. 222634, 0. 378102, 0. 364803, 0. 440015, 0. 0208332, 0. 147172, 0. 185038, 0. 275594, 0. 307225, 0. 393182, 0. 108642, 0.317047, 0. 390628, 0. 131731, 0.292226, 0. 126009, 0. 159842, 0. 340395, 0. 183877, 0. 228193, 0.363135, 0. 042972, 0. 449523, 0. 462529} return build_from_node_lists (pre3, in3, size3) binary_tree sample4 // seed 232323 int size = 67 double pre = {0. 0548774, 0. 0219107, 0.399846, 0. 11906, 0.276626, 0. 0025399, 0. 0871276, 0. 414861, 0. 245851, 0.326767, 0. 299142, 0. 177023, 0. 374845, 0.308614, 0. 0240217, 0.372271, 0. 455955, 0.0612401, 0. 335461, 0. 435828, . 0312264, 0. 0884601, 0. 159796, 0. 454999, 0.219714, 0. 197132, 0. 0369807, 0. 11951, 0. 241231, 0. 413826, 0. 184522, 0. 0199605, 0.257216, 0.222754, 0. 272094, 0. 425897, 0. 46714, 0. 0668064, 0. 132584, 0.305703, 0. 359778, 0.368871, 0. 203195, 0. 204924, 0. 258112, 0. 488894, 0. 18149, 0. 0354453, 0. 140185, 0. 130067, 0. 411766, 0.376441, 0. 129284, 0. 184723, 0. 478379, 0.371205, 0. 158867, 0. 487457, 0. 234737, 0. 00210605, 0.340738, 0.356701, 0.349852, 0. 450229, 0. 148242, 0. 393779, 0. 24513, } double in = {0. 276626, 0. 0871276, 0. 0025399, 0. 414861, 0.245851, 0. 11906, 0. 326767, 0. 399846, 0. 299142, 0.335461, 0. 0612401, 0. 455955, 0.372271, 0. 0240217, 0. 308614, 0.374845, 0. 177023, 0. 0312264, 0.0884601, 0. 159796, 0. 435828, 0. 0219107, 0. 219714, 0. 0369807, 0. 413826, 0. 241231, 0. 11951, 0. 184522, 0. 197132, 0.222754, 0.257216, 0. 0199605, 0. 454999, 0. 0668064, 0. 132584, 0. 46714, 0. 305703, 0. 425897, 0. 368871, 0.359778, 0. 272094, 0.258112, 0. 204924, 0.203195, 0. 0548774, 0. 0354453, 0. 130067, 0. 129284, 0.376441, 0.411766, 0. 140185, 0. 18149, 0. 184723, 0.371205, 0. 158867, 0. 478379, 0. 487457, 0. 488894, 0. 00210605, 0. 234737, 0. 450229, 0.349852, 0. 356701, 0. 340738, 0. 24513, 0. 393779, 0. 148242, } return build_from_node_lists (pre, in, size) binary_tree_node sample5 // depth = 10 // seed = 73 int size = 232 double pre = {73. 4985, 46.8521, 14. 3886, 12.9232, 85. 4859, 46. 2981, 80.6752, 88. 638, 4.35911, 58.6892, 84. 1117, 40.5937, 68. 0229, 61. 445, 34. 0708, 35.3704, 18.5848, 96.9019, 92. 7016, 19. 209, 23. 4396, 91.8773, 30.3747, 77. 7968, 61.8916, 88. 8224, 53. 4489, 95. 265, 60. 4812, 39. 0774, 55.1742, 26. 1048, 94.9729, 25. 1438, 48.3416, 53.3558, 68. 7762, 8.53748, 18. 4463, 74.2923, 24.9243, 43. 8824, 85.6614, 85. 3082, 44.935, 76.962, 11.221, 49.8271, 2. 35539, 71.8307, 89. 2918, 8. 40491, 61. 8023, 21.7674, 14.0364, 64. 5149, 69.6479, 39.7651, 14. 1298, 56. 2528, 34.85, 14.2238, 9.7598, 86.5377, 25.5708, 27. 0339, 17. 4183, 77. 4411, 92.9552, 67.7474, 24. 8433, 39. 1404, 57.3637, 86. 793, 7 , 79. 4382, 71.3109, , 29. 2685, 3.97165, 49.9699, 0.527265, 56.5742, 76 76. 9849, 97.5207, 71.6777, 70. 8829, 35. 2898, 88. 0029, 5. 02091, 5.96275, 75. 6205, 98. 4684, 96.2908, 63. 8659, 77.8282, 75.5512, 17.7396, 67 6, 67.5396, 28. 6981, 12. 2225, 38. 7088, 16. 4708, 92.5059, 82. 1537, 94.3072, 14.5779, 51. 7565, 43.5969, 72.9606, 1.56508, 9.78005, 18. 9848, 45.6385, 13.969, 92. 3696, 26. 3461, 56.757, 91. 198, 78. 8685, 37. 0075, 73.8415, 58. 2184, 13. 6569, 57.8895, 96. 2489, 97 . 1185, 64. 7039, 41.3069, 27. 7655, 31.9683, 55. 1188, 52.3135, 66.884, 83.9292, 58. 4453, 78. 1257, 1. 43965, 44. 5268, 29.4732, , 85. 4034, 3.31023, 8. 08591, 54. 1928, 41.5081, 40.9289, 43. 1932, , 77.7617, 9.87374, 8.39879, 20. 0876, 47.2552, 27.5136, 26.8304, , 45. 1827, 86. 6642, 13.2579, 72. 1408, 33. 1812, 9. 41366, 89.4363, 76. 2227, 72. 4771, 34.8774, 16. 4201, 30.744, 31. 1552, 77.9852, 24. 4572, 24.8563, 70. 9599, 65.9745, 16.3165, 86. 4168, 2.51523, 42. 6039, 5 9, 55.5835, 32.5931, 11. 7524, 33. 165, 87. 6593, 6.77963, 44.2723, 16. 1713, 36.9947, 43. 9856, 60. 1331, 14.8278, 95. 1717, 8. 84246, 6. 33744, 38.8297, 79. 0969, 33. 7647, 3. 20686, 12. 2841, 11.0536, 28. 8245, 11. 1387, 14.6653, 43. 1258, 9. 30967, 49.7818, 33.2852, 23. 1558, 66.5455, 33.6988, 69. 4424, 1. 39044, 94. 2626, 57. 0549, 40. 6015, 99. 6742, 54.4458, 79.7501, 92.6126, 10. 4309, 50.9208, 67.283, 10.87, 24. 4578, 28. 0051, 21.931, 69.5968, 11.3731, 32. 7579, 11. 7431, 54. 8151, 24.3329, 65. 0133, 50.714, 68. 4945, 38. 0619, 99. 3297, double in = {80.6752, 88.638, 46. 2981, 58. 6892, 4. 35911, 84. 1117, 40.5937, 85. 4859, 18.5848, 35. 3704, 34.0708, 96.9019, 61. 445, 19.209, 92. 7016, 68. 0229, 91.8773, 23. 4396, 77.7968, 30. 3747, 12.9232, 53. 4489, 95.265, 88. 8224, 3 , 39. 0774, 60. 4812, 55. 1742, 26. 1048, 61.8916, 2 , 25. 1438, 94.9729, 48. 3416, 53. 3558, 14. 3886, 24. 9243, 74.2923, 85. 6614, 43. 8824, 18. 4463, 85. 30 , 85. 3082, 44.935, 8.53748, 11.221, 76.962, 49.8271, 2.35539, 68. 7762, 61.8023, 8. 40491, 89. 2918, 21. 7674, 71.8307, 64.5149, 14.0364, 46.8521, 34.85, 14.2238, 56.2528, 86.5377, 9.7598, 25.5708, 27. 0339, 14. 1298, 92. 9552, 77. 4411, 17. 4183, 67.7474, 39.7651, 79. 4382, 71.3109, 86. 793, 29.2685, 57.3637, 49. 9699, 3 , 39. 1404, 2, 76. 9849, 0.527265, 97.5207, 24.8433, 35. 2898, 7 , 70.8829, 71. 6777, 5.96275, 5. 02091, 88. 0029, 75.6205, 69. 6479, 75.5512, 7 , 77.8282, 2, 17. 7396, 67.5396, 63. 8659, 16.4708, 38. 7088, 12.2225, 92.5059, 28. 6981, 94.3072, 82. 1537, 96. 2908, 51. 7565, 43.5969, 14.5779, 9.78005, 1.56508, 72.9606, 18.9848, 98. 4684, 91. 198, 56.757, 37. 0075, 78.8685, 26. 3461, 73.8415, 58.2184, 92. 3696, 57.8895, 13. 6569, 96.2489, 97. 1185, 13.969, 27. 7655, 41.3069, 64. 7039, 55. 1188, 31.9683, 66.884, 52.3135, 45. 6385, 78 , 78. 1257, 58. 4453, 1.43965, 44.5268, 8 , 83.9292, 3.31023, 8. 08591, 85. 4034, 54. 1928, 29. 4732, 40.9289, 9, 41.5081, 73. 4985, 20. 0876, 8 6, 8.39879, 27.5136, 47.2552 9.87374, 45. 1827, 86. 6642, 26.8304, 72. 1408, 13.2579, 9. 41366, 33. 1812, 77.7617, 34.8774, 72. 4771, 16. 4201, 30.744, 76.2227, 24. 4572, 77.9852, 31. 1552, 65.9745, 70.9599, 24.8563, 16.3165, 89. 4363, 42.6039, 2.51523, 55.5835, 32. 5931, 86. 4168, 33. 165, 1 11. 7524, 43. 1932, 36. 9947, 16. 1713, 44.2723, 60. 1331, 43.9856, 95. 1717, 14.8278, 6. 77963, 38. 8297, 6.33744, 33. 7647, 79. 0969, 8.84246, 28. 8245, 11.0536, 12. 2841, 11. 1387, 3.20686, 43. 1258, 14.6653, 87. 6593, 66.5455, 23. 1558, 69. 4424, 33. 6988, 33. 2852, 1. 39044, 94. 2626, 49. 7818, 99. 6742, 40. 6015, 57. 0549, 54. 4458, 9. 30967, 10. 4309, 50.9208, 92. 6126, 10.87, 24. 4578, 67. 283, 28. 0051, 79.7501, 32. 7579, 11.3731, 11.7431, 54. 8151, 69.5968, 24. 3329, 65.0133, 21.931, 68. 4945, 50.714, 38. 0619, 99.3297, }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Introduction to Wireless and Mobile Systems

Authors: Dharma P. Agrawal, Qing An Zeng

4th edition

1305087135, 978-1305087132, 9781305259621, 1305259629, 9781305537910 , 978-130508713

More Books

Students also viewed these Programming questions