Advertisement
Guest User

Fixed Something Something

a guest
Mar 31st, 2020
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.04 KB | None | 0 0
  1. /*
  2. NodeCount.cpp
  3. Ryan Schumacher
  4. Version 1.0
  5. 3-31-20
  6. Program will be utilizing the IntBinaryTree class provided via the professor and in class. Utilizing this class I added in a couple sets of member functions to either count all the nodes in the tree and to count all the leaf nodes in the tree. These functions added were
  7. the countNodes and its recursive counterpart as well as countLeafNodes and its recursive counterpart. These classes will have the address of a class object sent to them that holds all the nodes already inserted into the tree. From there it will check the nodes down the
  8. list to see if they contain the NULL value or not. If they are not NULL, the function will then increment the counter and move on to the next node. Similar cases will occur for the leaf node counter but instead, each root will essentially check its left and right nodes
  9. to see whether they are NULL or not. If both left and right point to NULL, then the counter is incremented thus adding one more to the leaf node counter.
  10. */
  11. #include <iostream>
  12. #ifndef INTBINARYTREE_H
  13. #define INTBINARYTREE_H
  14. using namespace std;
  15. class IntBinaryTree
  16. {
  17. private:
  18. struct TreeNode
  19. {
  20. public:
  21. int value; // The value in the node
  22. TreeNode *left; // Pointer to left child node
  23. TreeNode *right; // Pointer to right child node
  24. };
  25. TreeNode *root; // Pointer to the root node
  26. // Private member functions
  27. void insert(TreeNode *&, TreeNode *&);
  28. void destroySubTree(TreeNode *);
  29. void deleteNode(int, TreeNode *&);
  30. int countNodesrecursive(TreeNode *);
  31. int countLeafNodesrecursive(TreeNode *);
  32. void makeDeletion(TreeNode *&);
  33. void displayInOrder(TreeNode *) const;
  34. void displayPreOrder(TreeNode *) const;
  35. void displayPostOrder(TreeNode *) const;
  36. public:
  37. // Constructor
  38. IntBinaryTree()
  39. {
  40. root = nullptr;
  41. }
  42. // Destructor
  43. ~IntBinaryTree()
  44. {
  45. destroySubTree(root);
  46. }
  47. // Binary tree operations
  48. int maxHeight(TreeNode *);
  49. int height(IntBinaryTree *);
  50. void newNode(int);
  51. int countNodes(IntBinaryTree *);
  52. int countLeafNodes(IntBinaryTree *);
  53. void insertNode(int);
  54. bool searchNode(int);
  55. void remove(int);
  56. void displayInOrder() const
  57. {
  58. displayInOrder(root);
  59. }
  60. void displayPreOrder() const
  61. {
  62. displayPreOrder(root);
  63. }
  64. void displayPostOrder() const
  65. {
  66. displayPostOrder(root);
  67. }
  68.  
  69. };
  70. #endif
  71.  
  72.  
  73.  
  74. //insert Member Function inserts a node
  75. void IntBinaryTree::insert(TreeNode *&nodePtr, TreeNode *&newNode)
  76. {
  77. if (nodePtr == nullptr)
  78. nodePtr = newNode; // Insert the node.
  79. else if (newNode->value < nodePtr->value)
  80. insert(nodePtr->left, newNode); // Search the left branch
  81. else
  82. insert(nodePtr->right, newNode); // Search the right branch
  83. }
  84.  
  85. //insertNode Member Function calls insert function
  86. void IntBinaryTree::insertNode(int num)
  87. {
  88. TreeNode *newNode = nullptr; // Pointer to a new node.
  89. // Create a new node and store num in it.
  90. newNode = new TreeNode;
  91. newNode->value = num;
  92. newNode->left = newNode->right = nullptr;
  93. // Insert the node.
  94. insert(root, newNode);
  95. }
  96.  
  97. //destrySubTree Member Function
  98. void IntBinaryTree::destroySubTree(TreeNode *nodePtr)
  99. {
  100. if (nodePtr)
  101. {
  102. if (nodePtr->left)
  103. destroySubTree(nodePtr->left);
  104. if (nodePtr->right)
  105. destroySubTree(nodePtr->right);
  106. delete nodePtr;
  107. }
  108. }
  109.  
  110. //Search tree member function
  111. bool IntBinaryTree::searchNode(int num)
  112. {
  113. TreeNode *nodePtr = root;
  114. while (nodePtr)
  115. {
  116. if (nodePtr->value == num)
  117. return true;
  118. else if (num < nodePtr->value)
  119. nodePtr = nodePtr->left;
  120. else
  121. nodePtr = nodePtr->right;
  122. }
  123. return false;
  124. }
  125.  
  126. //remove Member Function calls deleteNode Member Function
  127. void IntBinaryTree::remove(int num)
  128. {
  129. deleteNode(num, root);
  130. }
  131.  
  132. //deleteNode Member Function
  133. void IntBinaryTree::deleteNode(int num, TreeNode *&nodePtr)
  134. {
  135. if (num < nodePtr->value)
  136. deleteNode(num, nodePtr->left);
  137. else if (num > nodePtr->value)
  138. deleteNode(num, nodePtr->right);
  139. else
  140. makeDeletion(nodePtr);
  141. }
  142.  
  143.  
  144. void IntBinaryTree::makeDeletion(TreeNode *&nodePtr)
  145. {
  146. // Define a temporary pointer to use in reattaching
  147. // the left subtree.
  148. TreeNode *tempNodePtr = nullptr;
  149. if (nodePtr == nullptr)
  150. cout << "Cannot delete empty node.\n";
  151. else if (nodePtr->right == nullptr)
  152. {
  153. tempNodePtr = nodePtr;
  154. nodePtr = nodePtr->left; // Reattach the left child
  155. delete tempNodePtr;
  156. }
  157. else if (nodePtr->left == nullptr)
  158. {
  159. tempNodePtr = nodePtr;
  160. nodePtr = nodePtr->right; // Reattach the right child
  161. delete tempNodePtr;
  162. }
  163. // If the node has two children.
  164. else
  165. {
  166. // Move one node the right.
  167. tempNodePtr = nodePtr->right;
  168. // Go to the end left node.
  169. while (tempNodePtr->left)
  170. tempNodePtr = tempNodePtr->left;
  171. // Reattach the left subtree.
  172. tempNodePtr->left = nodePtr->left;
  173. tempNodePtr = nodePtr;
  174. // Reattach the right subtree.
  175. nodePtr = nodePtr->right;
  176. delete tempNodePtr;
  177. }
  178. }
  179.  
  180. // Recursive node counting member function
  181. int IntBinaryTree::countNodesrecursive(TreeNode *root)
  182. {
  183. int count = 1;
  184. if (root->left != NULL)
  185. {
  186. count += countNodesrecursive(root->left);
  187. }
  188. if (root->right != NULL)
  189. {
  190. count += countNodesrecursive(root->right);
  191. }
  192. return count;
  193. }
  194. //countNode Member Function to check for root then calls the recursive counting function
  195. int IntBinaryTree::countNodes(IntBinaryTree *tree)
  196. {
  197. int count = 0;
  198. if (tree->root != NULL) {
  199. count = countNodesrecursive(tree->root);
  200. }
  201. return count;
  202. }
  203.  
  204. // Recursive leaf node counting member function
  205. int IntBinaryTree::countLeafNodesrecursive(TreeNode *root)
  206. {
  207. int count = 0;
  208. if (root->left == NULL && root->right == NULL) {
  209. count = 1;
  210. }
  211. else {
  212. if (root->left != NULL) {
  213. count += countLeafNodesrecursive(root->left);
  214. }
  215. if (root->right != NULL) {
  216. count += countLeafNodesrecursive(root->right);
  217. }
  218. }
  219. return count;
  220. }
  221. //countNode Member Function to check for root then calls the recursive counting function
  222. int IntBinaryTree::countLeafNodes(IntBinaryTree *tree)
  223. {
  224. int count = 0;
  225. if (tree->root != NULL) {
  226. count = countLeafNodesrecursive(tree->root);
  227. }
  228. return count;
  229. }
  230.  
  231. //depth/height member function
  232. int IntBinaryTree::height(IntBinaryTree* tree) {
  233. int count = 0;
  234. if (tree->root != NULL) {
  235. count = maxHeight(tree->root);
  236. }
  237. return count;
  238. }
  239.  
  240. int IntBinaryTree::maxHeight(TreeNode *root) {
  241. int lCounter = 0, rCounter = 0;
  242. if (root->left != NULL) {
  243. int lDepth = maxHeight(root->left);
  244. lCounter++;
  245. }
  246. if (root->right != NULL) {
  247. int rDepth = maxHeight(root->right);
  248. rCounter++;
  249. }
  250.  
  251. return (lCounter > rCounter) ? (lCounter + 1) : (rCounter + 1);
  252. }
  253.  
  254. /* Helper function that allocates a new node with the
  255. given data and NULL left and right pointers. */
  256. void IntBinaryTree::newNode(int data)
  257. {
  258. TreeNode* Node = new TreeNode();
  259. Node->value = data;
  260. Node->left = NULL;
  261. Node->right = NULL;
  262. }
  263.  
  264. //Display Traversal InOrder
  265. void IntBinaryTree::displayInOrder(TreeNode *nodePtr) const
  266. {
  267. if (nodePtr)
  268. {
  269. displayInOrder(nodePtr->left);
  270. cout << nodePtr->value << endl;
  271. displayInOrder(nodePtr->right);
  272. }
  273. }
  274.  
  275.  
  276. //Display Traversal PreOrder
  277. void IntBinaryTree::displayPreOrder(TreeNode *nodePtr) const
  278. {
  279. if (nodePtr)
  280. {
  281. cout << nodePtr->value << endl;
  282. displayPreOrder(nodePtr->left);
  283. displayPreOrder(nodePtr->right);
  284. }
  285. }
  286.  
  287. //Display Traversal PostOrder
  288. void IntBinaryTree::displayPostOrder(TreeNode *nodePtr) const
  289. {
  290. if (nodePtr)
  291. {
  292. displayPostOrder(nodePtr->left);
  293. displayPostOrder(nodePtr->right);
  294. cout << nodePtr->value << endl;
  295. }
  296. }
  297.  
  298. int main()
  299. {
  300. IntBinaryTree tree;
  301. tree.insertNode(41);
  302. tree.insertNode(20);
  303. tree.insertNode(65);
  304. tree.insertNode(11);
  305. tree.insertNode(29);
  306. tree.insertNode(32);
  307. tree.insertNode(50);
  308. tree.insertNode(91);
  309. tree.insertNode(73);
  310. tree.insertNode(99);
  311. tree.displayInOrder();
  312. cout << "Number of nodes in the tree: " << tree.countNodes(&tree) << endl;
  313. cout << "Number of leaf nodes in the tree: " << tree.countLeafNodes(&tree) << endl;
  314. cout << "Max height of the tree: " << tree.height(&tree) << endl;
  315. system("pause");
  316. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement