Guest User

Untitled

a guest
Mar 19th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.58 KB | None | 0 0
  1. #include"binarysearchtree.h"
  2. //#include"quetype.h"
  3. #include <iostream>
  4. using namespace std;
  5.  
  6. template <class ItemType>
  7. TreeType<ItemType>::TreeType()
  8. {
  9. root=NULL;
  10. }
  11. template <class ItemType>
  12. void Destroy(TreeNode<ItemType>*& tree)
  13. {
  14. if(tree!=NULL)
  15. {
  16. Destroy(tree->left);
  17. Destroy(tree->right);
  18. delete tree;
  19. tree=NULL;
  20. }
  21. }
  22. template <class ItemType>
  23. TreeType<ItemType>::~TreeType()
  24. {
  25. Destroy(root);
  26. }
  27. template <class ItemType>
  28. void TreeType<ItemType>::MakeEmpty()
  29. {
  30. Destroy(root);
  31. }
  32. template <class ItemType>
  33. bool TreeType<ItemType>::IsEmpty()
  34. {
  35. return root==NULL;
  36. }
  37. template <class ItemType>
  38. bool TreeType<ItemType>::IsFull()
  39. {
  40. TreeNode<ItemType>*location;
  41. try{
  42. location=new TreeNode<ItemType>;
  43. delete location;
  44. return false;
  45. }
  46. catch(bad_alloc& exception)
  47. {
  48. return true;
  49. }
  50. }
  51. template <class ItemType>
  52. int CountNodes(TreeNode<ItemType>*tree)
  53. {
  54. if(tree==NULL)
  55. return 0;
  56. else
  57. return CountNodes(tree->left)+CountNodes(tree->right)+1;
  58. }
  59. template <class ItemType>
  60. int TreeType<ItemType>::LengthIs()
  61. {
  62. return CountNodes(root);
  63. }
  64. template <class ItemType>
  65. void Retrieve(TreeNode<ItemType>*tree,ItemType& item,bool& found)
  66. {
  67. if(tree==NULL)
  68. found=false;
  69. else if(item<tree->info)
  70. Retrieve(tree->left,item,found);
  71. else{
  72. item=tree->info;
  73. found=true;
  74. }
  75. }
  76. template <class ItemType>
  77. void TreeType<ItemType>::RetrieveItem(ItemType& item,bool& found)
  78. {
  79. Retrieve(root,item,found);
  80. }
  81. template <class ItemType>
  82. void insert(TreeNode<ItemType>*& tree,ItemType item)
  83. {
  84. if (tree==NULL)
  85. {
  86. tree=new TreeNode<ItemType>;
  87. tree->right=NULL;
  88. tree->left=NULL;
  89. tree->info=item;
  90.  
  91. }
  92. else if(item<tree->info)
  93. insert(tree->right,item);
  94. else
  95. insert(tree->right,item);
  96. }
  97. template <class ItemType>
  98. void TreeType<ItemType>::InsertItem(ItemType item)
  99. {
  100. insert(root,item);
  101. }
  102. /*template <class ItemType>
  103. void delete(TreeNode<ItemType>*& tree,ItemType item)
  104. {
  105. if(item<tree->info)
  106. delete(tree->left,item);
  107. else if(item>tree->info)
  108. delete(tree->right,item);
  109. else
  110. deleteNode(tree);
  111. }*/
  112. template <class ItemType>
  113. void deleteNode(TreeNode<ItemType>*&tree)
  114. {
  115. ItemType data;
  116. TreeNode<ItemType>* tempPtr;
  117.  
  118. tempPtr=tree;
  119. if(tree->left == NULL)
  120. {
  121. tree=tree->right;
  122. delete tempPtr;
  123. }
  124. else if(tree->right==NULL)
  125. {
  126. tree=tree->left;
  127. delete tempPtr;
  128. }
  129. else{
  130. //GetPredecessor(tree->left,data);
  131. tree->info=data;
  132. }
  133. }
Add Comment
Please, Sign In to add comment