Advertisement
Guest User

Untitled

a guest
Nov 20th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.63 KB | None | 0 0
  1. // FILE: bintree.h (part of the namespace main_savitch_10)
  2. // PROVIDES: A template class for a node in a binary tree and functions for
  3. // manipulating binary trees. The template parameter is the type of data in
  4. // each node.
  5. //
  6. // TYPEDEF for the binary_tree_node<Item> template class:
  7. // Each node of the tree contains a piece of data and pointers to its
  8. // children. The type of the data (binary_tree_node<Item>::value_type) is
  9. // the Item type from the template parameter. The type may be any of the C++
  10. // built-in types (int, char, etc.), or a class with a default constructor,
  11. // and an assignment operator.
  12. //
  13. // CONSTRUCTOR for the binary_tree_node<Item> class:
  14. // binary_tree_node(
  15. // const item& init_data = Item( ),
  16. // binary_tree_node<Item>* init_left = NULL,
  17. // binary_tree_node<Item>* init_right = NULL
  18. // )
  19. // Postcondition: The new node has its data equal to init_data,
  20. // and it's child pointers equal to init_left and init_right.
  21. //
  22. // MEMBER FUNCTIONS for the binary_tree_node<Item> class:
  23. // const item& data( ) const <----- const version
  24. // and
  25. // Item& data( ) <----- non-const version
  26. // Postcondition: The return value is a reference to the data from
  27. // this binary_tree_node.
  28. //
  29. // const binary_tree_node* left( ) const <----- const version
  30. // and
  31. // binary_tree_node* left( ) <----- non-const version
  32. // and
  33. // const binary_tree_node* right( ) const <----- const version
  34. // and
  35. // binary_tree_node* right( ) <----- non-const version
  36. // Postcondition: The return value is a pointer to the left or right child
  37. // (which will be NULL if there is no child).
  38. //
  39. // void set_data(const Item& new_data)
  40. // Postcondition: The binary_tree_node now contains the specified new data.
  41. //
  42. // void set_left(binary_tree_node* new_link)
  43. // and
  44. // void set_right(binary_tree_node* new_link)
  45. // Postcondition: The binary_tree_node now contains the specified new link
  46. // to a child.
  47. //
  48. // bool is_leaf( )
  49. // Postcondition: The return value is true if the node is a leaf;
  50. // otherwise the return value is false.
  51. //
  52. // NON-MEMBER FUNCTIONS to maniplulate binary tree nodes:
  53. // tempate <class Process, class BTNode>
  54. // void inorder(Process f, BTNode* node_ptr)
  55. // Precondition: node_ptr is a pointer to a node in a binary tree (or
  56. // node_ptr may be NULL to indicate the empty tree).
  57. // Postcondition: If node_ptr is non-NULL, then the function f has been
  58. // applied to the contents of *node_ptr and all of its descendants, using
  59. // an in-order traversal.
  60. // Note: BTNode may be a binary_tree_node or a const binary tree node.
  61. // Process is the type of a function f that may be called with a single
  62. // Item argument (using the Item type from the node).
  63. //
  64. // tempate <class Process, class BTNode>
  65. // void postorder(Process f, BTNode* node_ptr)
  66. // Same as the in-order function, except with a post-order traversal.
  67. //
  68. // tempate <class Process, class BTNode>
  69. // void preorder(Process f, BTNode* node_ptr)
  70. // Same as the in-order function, except with a pre-order traversal.
  71. //
  72. // template <class Item, class SizeType>
  73. // void print(const binary_tree_node<Item>* node_ptr, SizeType depth)
  74. // Precondition: node_ptr is a pointer to a node in a binary tree (or
  75. // node_ptr may be NULL to indicate the empty tree). If the pointer is
  76. // not NULL, then depth is the depth of the node pointed to by node_ptr.
  77. // Postcondition: If node_ptr is non-NULL, then the contents of *node_ptr
  78. // and all its descendants have been written to cout with the << operator,
  79. // using a backward in-order traversal. Each node is indented four times
  80. // its depth.
  81. //
  82. // template <class Item>
  83. // void tree_clear(binary_tree_node<Item>*& root_ptr)
  84. // Precondition: root_ptr is the root pointer of a binary tree (which may
  85. // be NULL for the empty tree).
  86. // Postcondition: All nodes at the root or below have been returned to the
  87. // heap, and root_ptr has been set to NULL.
  88. //
  89. // template <class Item>
  90. // binary_tree_node<Item>* tree_copy(const binary_tree_node<Item>* root_ptr)
  91. // Precondition: root_ptr is the root pointer of a binary tree (which may
  92. // be NULL for the empty tree).
  93. // Postcondition: A copy of the binary tree has been made, and the return
  94. // value is a pointer to the root of this copy.
  95. //
  96. // template <class Item>
  97. // size_t tree_size(const binary_tree_node<Item>* node_ptr)
  98. // Precondition: node_ptr is a pointer to a node in a binary tree (or
  99. // node_ptr may be NULL to indicate the empty tree).
  100. // Postcondition: The return value is the number of nodes in the tree.
  101.  
  102. #ifndef BINTREE_H
  103. #define BINTREE_H
  104. #include <cstdlib> // Provides NULL and size_t
  105.  
  106. namespace main_savitch_10
  107. {
  108.  
  109. template <class Item>
  110. class binary_tree_node
  111. {
  112. public:
  113. // TYPEDEF
  114. typedef Item value_type;
  115. // CONSTRUCTOR
  116. binary_tree_node(
  117. const Item& init_data = Item( ),
  118. binary_tree_node* init_left = NULL,
  119. binary_tree_node* init_right = NULL
  120. )
  121. {
  122. data_field = init_data;
  123. left_field = init_left;
  124. right_field = init_right;
  125. }
  126. // MODIFICATION MEMBER FUNCTIONS
  127. Item& data( ) { return data_field; }
  128. binary_tree_node* left( ) { return left_field; }
  129. binary_tree_node* right( ) { return right_field; }
  130. void set_data(const Item& new_data) { data_field = new_data; }
  131. void set_left(binary_tree_node* new_left) { left_field = new_left; }
  132. void set_right(binary_tree_node* new_right) { right_field = new_right; }
  133. // CONST MEMBER FUNCTIONS
  134. const Item& data( ) const { return data_field; }
  135. const binary_tree_node* left( ) const { return left_field; }
  136. const binary_tree_node* right( ) const { return right_field; }
  137. bool is_leaf( ) const
  138. { return (left_field == NULL) && (right_field == NULL); }
  139. private:
  140. Item data_field;
  141. binary_tree_node *left_field;
  142. binary_tree_node *right_field;
  143. };
  144.  
  145. // NON-MEMBER FUNCTIONS for the binary_tree_node<Item>:
  146. template <class Process, class BTNode>
  147. void inorder(Process f, BTNode* node_ptr);
  148.  
  149. template <class Process, class BTNode>
  150. void preorder(Process f, BTNode* node_ptr);
  151.  
  152. template <class Process, class BTNode>
  153. void postorder(Process f, BTNode* node_ptr);
  154.  
  155. template <class Item, class SizeType>
  156. void print(binary_tree_node<Item>* node_ptr, SizeType depth);
  157.  
  158. template <class Item>
  159. void tree_clear(binary_tree_node<Item>*& root_ptr);
  160.  
  161. template <class Item>
  162. binary_tree_node<Item>* tree_copy(const binary_tree_node<Item>* root_ptr);
  163.  
  164. template <class Item>
  165. std::size_t tree_size(const binary_tree_node<Item>* node_ptr);
  166.  
  167. template <class Item>
  168. void insert(const binary_tree_node<Item>* node_ptr, const Item& new_data);
  169.  
  170. template <class Item>
  171. bool remove(const binary_tree_node<Item>* node_ptr, const Item& target);
  172.  
  173. template <class Item>
  174. void remove_all(const binary_tree_node<Item>* node_ptr, const Item& target);
  175.  
  176. template <class Item>
  177. binary_tree_node<Item>* find(const binary_tree_node<Item>* node_ptr, const Item& target);
  178.  
  179. template <class Item>
  180. binary_tree_node<Item>* operator +(const binary_tree_node<Item>* left_node_ptr,
  181. const binary_tree_node<Item>* right_node_ptr);
  182.  
  183. template <class Item>
  184. binary_tree_node<Item>* operator +=(const binary_tree_node<Item>* left_node_ptr,
  185. const binary_tree_node<Item>* right_node_ptr);
  186. }
  187.  
  188.  
  189.  
  190. #include "bintree.template"
  191. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement