Advertisement
Guest User

treetype.cpp

a guest
Dec 5th, 2019
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.81 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include "TreeType.h"
  4. #include "QueType.h"
  5.  
  6.  
  7. using namespace std;
  8.  
  9. struct TreeNode
  10. {
  11. ItemType info;
  12. TreeNode* left;
  13. TreeNode* right;
  14. };
  15.  
  16.  
  17. TreeType::TreeType()
  18. {
  19. }
  20. void Destroy(TreeNode*& tree);
  21.  
  22. TreeType::~TreeType()
  23. // Calls recursive function Destroy to destroy the tree.
  24. {
  25. Destroy(root);
  26. }
  27.  
  28. void Retrieve(TreeNode* tree, ItemType& item, bool& found);
  29.  
  30. ItemType TreeType::GetItem(ItemType item, bool& found)
  31. // Calls recursive function Retrieve to search the tree for item.
  32. {
  33. Retrieve(root, item, found);
  34. return item;
  35. }
  36.  
  37. void Retrieve(TreeNode* tree, ItemType& item, bool& found)
  38. // Recursively searches tree for item.
  39. // Post: If there is an element someItem whose key matches item's,
  40. // found is true and item is set to a copy of someItem;
  41. // otherwise found is false and item is unchanged.
  42. {
  43. if (tree == NULL)
  44. found = false; // item is not found.
  45. else if (item < tree->info)
  46. Retrieve(tree->left, item, found); // Search left subtree.
  47. else if (item > tree->info)
  48. Retrieve(tree->right, item, found);// Search right subtree.
  49. else
  50. {
  51. item = tree->info; // item is found.
  52. found = true;
  53. }
  54. }
  55.  
  56. void Insert(TreeNode*& tree, ItemType item);
  57. void TreeType::PutItem(ItemType item)
  58. // Calls recursive function Insert to insert item into tree.
  59. {
  60. Insert(root, item);
  61. }
  62.  
  63. void Insert(TreeNode*& tree, ItemType item)
  64. // Inserts item into tree.
  65. // Post: item is in tree; search property is maintained.
  66. {
  67. if (tree == NULL)
  68. {// Insertion place found.
  69. tree = new TreeNode;
  70. tree->right = NULL;
  71. tree->left = NULL;
  72. tree->info = item;
  73. }
  74. else if (item < tree->info)
  75. Insert(tree->left, item); // Insert in left subtree.
  76. else
  77. Insert(tree->right, item); // Insert in right subtree.
  78. }
  79.  
  80. void Delete(TreeNode*& tree, ItemType item);
  81.  
  82. void TreeType::DeleteItem(ItemType item)
  83. // Calls recursive function Delete to delete item from tree.
  84. {
  85. Delete(root, item);
  86. }
  87.  
  88. void DeleteNode(TreeNode*& tree);
  89. void Delete(TreeNode*& tree, ItemType item)
  90. // Deletes item from tree.
  91. // Post: item is not in tree.
  92. {
  93. if (item < tree->info)
  94. Delete(tree->left, item); // Look in left subtree.
  95. else if (item > tree->info)
  96. Delete(tree->right, item); // Look in right subtree.
  97. else
  98. DeleteNode(tree); // Node found; call DeleteNode.
  99. }
  100.  
  101. void GetSuccessor(TreeNode* tree, ItemType& data)
  102. // Sets data to the info member of the right-most node in tree.
  103. {
  104. while (tree->left != NULL)
  105. tree = tree->left;
  106. data = tree->info;
  107. }
  108.  
  109. void DeleteNode(TreeNode*& tree)
  110. // Deletes the node pointed to by tree.
  111. // Post: The user's data in the node pointed to by tree is no
  112. // longer in the tree. If tree is a leaf node or has only
  113. // non-NULL child pointer the node pointed to by tree is
  114. // deleted; otherwise, the user's data is replaced by its
  115. // logical predecessor and the predecessor's node is deleted.
  116. {
  117. ItemType data;
  118. TreeNode* tempPtr;
  119.  
  120. tempPtr = tree;
  121. if (tree->left == NULL)
  122. {
  123. tree = tree->right;
  124. delete tempPtr;
  125. }
  126. else if (tree->right == NULL)
  127. {
  128. tree = tree->left;
  129. delete tempPtr;
  130. }
  131. else
  132. {
  133. // GetPredecessor(tree->left, data);
  134. // tree->info = data;
  135. //Delete(tree->left, data); // Delete predecessor node.
  136. GetSuccessor(tree->right, data);
  137. tree->info = data;
  138. Delete(tree->right, data);
  139.  
  140.  
  141. }
  142. }
  143.  
  144.  
  145. int CountNodes(TreeNode* tree);
  146.  
  147. int TreeType::GetLength() const
  148. // Calls recursive function CountNodes to count the
  149. // nodes in the tree.
  150. {
  151. return CountNodes(root);
  152. }
  153.  
  154. int CountNodes(TreeNode* tree)
  155. // Post: returns the number of nodes in the tree.
  156. {
  157. if (tree == NULL)
  158. return 0;
  159. else
  160. return CountNodes(tree->left) + CountNodes(tree->right) + 1;
  161. }
  162.  
  163. bool TreeType::IsEmpty() const
  164. // Returns true if the tree is empty; false otherwise.
  165. {
  166. return root == NULL;
  167. }
  168.  
  169. bool TreeType::IsFull() const
  170. // Returns true if there is no room for another item
  171. // on the free store; false otherwise.
  172. {
  173. TreeNode* location;
  174. try
  175. {
  176. location = new TreeNode;
  177. delete location;
  178. return false;
  179. }
  180. catch(std::bad_alloc exception)
  181. {
  182. return true;
  183. }
  184. }
  185.  
  186. void PrintTree(TreeNode* tree, std::ofstream& outFile);
  187.  
  188. void TreeType::Print(std::ofstream& outFile) const
  189. // Calls recursive function Print to print items in the tree.
  190. {
  191. PrintTree(root, outFile);
  192. }
  193.  
  194. void PrintTree(TreeNode* tree, std::ofstream& outFile)
  195. // Prints info member of items in tree in sorted order on outFile.
  196. {
  197. if (tree != NULL)
  198. {
  199. PrintTree(tree->left, outFile); // Print left subtree.
  200. outFile << tree->info;
  201. PrintTree(tree->right, outFile); // Print right subtree.
  202. }
  203. }
  204.  
  205. void PreOrder(TreeNode*, QueType&);
  206. // Enqueues tree items in preorder.
  207.  
  208.  
  209. void InOrder(TreeNode*, QueType&);
  210. // Enqueues tree items in inorder.
  211.  
  212.  
  213. void PostOrder(TreeNode*, QueType&);
  214. // Enqueues tree items in postorder.
  215.  
  216. void TreeType::ResetTree(OrderType order)
  217. // Calls function to create a queue of the tree elements in
  218. // the desired order.
  219. {
  220. switch (order)
  221. {
  222. case PRE_ORDER : PreOrder(root, preQue);
  223. break;
  224. case IN_ORDER : InOrder(root, inQue);
  225. break;
  226. case POST_ORDER: PostOrder(root, postQue);
  227. break;
  228. }
  229. }
  230.  
  231. void PreOrder(TreeNode* tree, QueType& preQue)
  232. // Post: preQue contains the tree items in preorder.
  233. {
  234. if (tree != NULL)
  235. {
  236. preQue.Enqueue(tree->info);
  237. PreOrder(tree->left, preQue);
  238. PreOrder(tree->right, preQue);
  239. }
  240. }
  241.  
  242.  
  243. void InOrder(TreeNode* tree, QueType& inQue)
  244. // Post: inQue contains the tree items in inorder.
  245. {
  246. if (tree != NULL)
  247. {
  248. InOrder(tree->left, inQue);
  249. inQue.Enqueue(tree->info);
  250. InOrder(tree->right, inQue);
  251. }
  252. }
  253.  
  254. void PostOrder(TreeNode* tree, QueType& postQue)
  255. // Post: postQue contains the tree items in postorder.
  256. {
  257. if (tree != NULL)
  258. {
  259. PostOrder(tree->left, postQue);
  260. PostOrder(tree->right, postQue);
  261. postQue.Enqueue(tree->info);
  262. }
  263. }
  264.  
  265. ItemType TreeType::GetNextItem(OrderType order, bool& finished)
  266. // Returns the next item in the desired order.
  267. // Post: For the desired order, item is the next item in the queue.
  268. // If item is the last one in the queue, finished is true;
  269. // otherwise finished is false.
  270. {
  271. finished = false;
  272. ItemType item;
  273. switch (order)
  274. {
  275. case PRE_ORDER : preQue.Dequeue(item);
  276. if (preQue.IsEmpty())
  277. finished = true;
  278. break;
  279. case IN_ORDER : inQue.Dequeue(item);
  280. if (inQue.IsEmpty())
  281. finished = true;
  282. break;
  283. case POST_ORDER: postQue.Dequeue(item);
  284. if (postQue.IsEmpty())
  285. finished = true;
  286. break;
  287. }
  288. return item;
  289. }
  290.  
  291. void Destroy(TreeNode*& tree)
  292. // Post: tree is empty; nodes have been deallocated.
  293. {
  294. if (tree != NULL)
  295. {
  296. Destroy(tree->left);
  297. Destroy(tree->right);
  298. delete tree;
  299. }
  300. }
  301.  
  302. void TreeType::MakeEmpty()
  303. {
  304. Destroy(root);
  305. root = NULL;
  306. }
  307.  
  308. void CopyTree(TreeNode*& copy, const TreeNode* originalTree);
  309.  
  310. TreeType::TreeType(const TreeType& originalTree)
  311. // Calls recursive function CopyTree to copy originalTree
  312. // into root.
  313. {
  314. CopyTree(root, originalTree.root);
  315. }
  316.  
  317. void TreeType::operator= (const TreeType& originalTree)
  318. // Calls recursive function CopyTree to copy originalTree
  319. // into root.
  320. {
  321. {
  322. if (&originalTree == this)
  323. return; // Ignore assigning self to self
  324. Destroy(root); // Deallocate existing tree nodes
  325. CopyTree(root, originalTree.root);
  326. }
  327.  
  328. }
  329. void CopyTree(TreeNode*& copy, const TreeNode* originalTree)
  330. // Post: copy is the root of a tree that is a duplicate
  331. // of originalTree.
  332. {
  333. if (originalTree == NULL)
  334. copy = NULL;
  335. else
  336. {
  337. copy = new TreeNode;
  338. copy->info = originalTree->info;
  339. CopyTree(copy->left, originalTree->left);
  340. CopyTree(copy->right, originalTree->right);
  341. }
  342. }
  343.  
  344. bool TreeType::IsFullTree(TreeNode * root)
  345. {
  346. // If empty tree
  347. if (root == NULL)
  348. return true;
  349.  
  350. // If leaf node
  351. if (root->left == NULL && root->right == NULL)
  352. return true;
  353.  
  354. // If both left and right are not NULL, and left & right subtrees
  355. // are full
  356. if ((root->left) && (root->right))
  357. return (IsFullTree(root->left) && IsFullTree(root->right));
  358.  
  359. // We reach here when none of the above if conditions work
  360. return false;
  361. }
  362.  
  363. bool TreeType::IsBST(TreeNode * root)
  364. {
  365. static TreeNode *prev = NULL;
  366.  
  367. // traverse the tree in inorder fashion
  368. // and keep track of prev node
  369. if (root)
  370. {
  371. if (!IsBST(root->left))
  372. return false;
  373.  
  374. // Allows only distinct valued nodes
  375. if (prev != NULL &&
  376. root->info <= prev->info)
  377. return false;
  378.  
  379. prev = root;
  380.  
  381. return IsBST(root->right);
  382. }
  383.  
  384. return true;
  385. }
  386.  
  387. /* return the number of nodes in the given level, itemArray[0...] stores the items values
  388. precondition: level >= 0
  389. //itemarray must be already allocated
  390. */
  391.  
  392. int GetNodesHelper (TreeNode * root, ItemType * itemArray, int level);
  393.  
  394. int GetNodesHelper (TreeNode * root, ItemType * itemArray, int level)
  395. {
  396. vector<ItemType> v;
  397. if(level == 0 )
  398. {
  399. //itemArray
  400. v.push_back(root->info);
  401. for(auto i=v.begin(); i!=v.end();i++)
  402. cout<<*i;
  403. return 1;
  404. }
  405. else
  406. {
  407. level--;
  408. return GetNodesHelper(root->left, itemArray, level) + GetNodesHelper(root->right, itemArray, level);
  409. }
  410. }
  411.  
  412. int TreeType::GetNodesAtLevel (ItemType * itemArray, int level)
  413. {
  414. GetNodesHelper(root, itemArray, level);
  415. }
  416.  
  417. //Display the items stored in the parent of the node, the grandparent, and so on
  418. //starting from root (ancestor of all nodes), ... all the way to the parent of the node
  419. void TreeType::PrintAncestors (TreeNode * root,ItemType value)
  420. {
  421. if (root == NULL)
  422. {
  423. cout<<"tree is empty";
  424. return;
  425. }
  426.  
  427. if (root->info == value)
  428. {
  429. cout << root->info << " ";
  430. return;
  431. }
  432.  
  433. PrintAncestors(root->left, value);
  434. PrintAncestors(root->right, value);
  435. }
  436.  
  437. //Add this helper function if you want to implement the walking recursively
  438. //this is a static member function (which does not have calling object).
  439. void TreeType::GetSmallest (TreeNode * root, ItemType & smallest) //this is the helper function
  440. {
  441. struct TreeNode* current =root;
  442. /* loop down to find the leftmost leaf */
  443. while (current->left != NULL)
  444. {
  445. current = current->left;
  446. }
  447.  
  448. cout<<current->info<<" ";
  449.  
  450. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement