Advertisement
Guest User

treetype.cpp

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