Advertisement
Guest User

bintree.template

a guest
Mar 19th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.86 KB | None | 0 0
  1. // FILE: bintree.template
  2. // IMPLEMENTS: The binary_tree node class (see bintree.h for documentation).
  3. #include <cassert> // Provides assert
  4. #include <cstdlib> // Provides NULL, std::size_t
  5. #include <iomanip> // Provides std::setw
  6. #include <iostream> // Provides std::cout
  7.  
  8. namespace main_savitch_10
  9. {
  10. template <class Process, class BTNode>
  11. void inorder(Process f, BTNode* node_ptr)
  12. // Library facilities used: cstdlib
  13. {
  14. if (node_ptr != NULL)
  15. {
  16. inorder(f, node_ptr->left( ));
  17. f( node_ptr->data( ) );
  18. inorder(f, node_ptr->right( ));
  19. }
  20. }
  21.  
  22. template <class Process, class BTNode>
  23. void postorder(Process f, BTNode* node_ptr)
  24. // Library facilities used: cstdlib
  25. {
  26. if (node_ptr != NULL)
  27. {
  28. postorder(f, node_ptr->left( ));
  29. postorder(f, node_ptr->right( ));
  30. f(node_ptr->data( ));
  31. }
  32. }
  33.  
  34. template <class Process, class BTNode>
  35. void preorder(Process f, BTNode* node_ptr)
  36. // Library facilities used: cstdlib
  37. {
  38. if (node_ptr != NULL)
  39. {
  40. f( node_ptr->data( ) );
  41. preorder(f, node_ptr->left( ));
  42. preorder(f, node_ptr->right( ));
  43. }
  44. }
  45.  
  46. template <class Item, class SizeType>
  47. void print(binary_tree_node<Item>* node_ptr, SizeType depth)
  48. // Library facilities used: iomanip, iostream, stdlib
  49. {
  50. if (node_ptr != NULL)
  51. {
  52. print(node_ptr->right( ), depth+1);
  53. std::cout << std::setw(4*depth) << ""; // Indent 4*depth spaces.
  54. std::cout << node_ptr->data( ) << std::endl;
  55. print(node_ptr->left( ), depth+1);
  56. }
  57. }
  58.  
  59. template <class Item>
  60. void tree_clear(binary_tree_node<Item>*& root_ptr)
  61. // Library facilities used: cstdlib
  62. {
  63. binary_tree_node<Item>* child;
  64. if (root_ptr != NULL)
  65. {
  66. child = root_ptr->left( );
  67. tree_clear( child );
  68. child = root_ptr->right( );
  69. tree_clear( child );
  70. delete root_ptr;
  71. root_ptr = NULL;
  72. }
  73. }
  74.  
  75. template <class Item>
  76. binary_tree_node<Item>* tree_copy(const binary_tree_node<Item>* root_ptr)
  77. // Library facilities used: cstdlib
  78. {
  79. binary_tree_node<Item> *l_ptr;
  80. binary_tree_node<Item> *r_ptr;
  81.  
  82. if (root_ptr == NULL)
  83. return NULL;
  84. else
  85. {
  86. l_ptr = tree_copy( root_ptr->left( ) );
  87. r_ptr = tree_copy( root_ptr->right( ) );
  88. return
  89. new binary_tree_node<Item>( root_ptr->data( ), l_ptr, r_ptr);
  90. }
  91. }
  92.  
  93. template <class Item>
  94. size_t tree_size(const binary_tree_node<Item>* node_ptr)
  95. // Library facilities used: cstdlib
  96. {
  97. if (node_ptr == NULL)
  98. return 0;
  99. else
  100. return
  101. 1 + tree_size(node_ptr->left( )) + tree_size(node_ptr->right( ));
  102. }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement