Guest User

Untitled

a guest
Nov 16th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.94 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <fstream>
  5.  
  6.  
  7. struct BstNode
  8. { //structuring a node
  9. std::string data;
  10. BstNode* left;
  11. BstNode* right;
  12. int frequ;
  13. };
  14.  
  15. BstNode* NewNodeCreator(std::string data) //creating a new node, pointing left and right to null
  16. {
  17. /*I make a pointer, pointing to a new space in memory. To allocate that memory I am using new operator, taking sizeof(BstNode) struct*/
  18. BstNode* newNode = new BstNode();
  19.  
  20. newNode->data = data;
  21. newNode->left = newNode->right = NULL; // left and right poiners to NULL
  22. newNode->frequ = 1; //for first time in BST
  23. return newNode;
  24. }
  25.  
  26. std::vector<std::string> readFromTxt()
  27. { //extracting words for a text file
  28. std::ifstream book("text.txt"); //open and read txt file
  29. std::string word;
  30. std::vector<std::string> list;
  31.  
  32. if (!book.is_open())
  33. {
  34. std::cout << "Unable to open file";
  35. system("pause");
  36. exit(1);
  37. }
  38. while (book >> word)
  39. {
  40. list.emplace_back(word); //store the words in a vector
  41. }
  42. return list;
  43. }
  44.  
  45. BstNode* InsertNode(BstNode* root, std::string data)
  46. { //inserting node and creating a binary tree
  47. if (root == NULL)
  48. {
  49. return NewNodeCreator(data);
  50. }
  51. if (data == root->data) // If the string already exists in BST, count+1 and return
  52. {
  53. (root->frequ)++;
  54. return root;
  55. }
  56. else if (root->data > data)
  57. {
  58. root->left = InsertNode(root->left, data);
  59. }
  60. else
  61. {
  62. root->right = InsertNode(root->right, data);
  63. }
  64. return root;
  65. }
  66.  
  67. void InPreorder(BstNode* root) //function for ptinting the BST in pre-order
  68. {
  69. if (root == NULL)
  70. return;
  71. // print data of node
  72. std::cout << "<" << root->frequ << ">" << " " << root->data << "n";
  73.  
  74. //going to left node
  75. InPreorder(root->left);
  76.  
  77. //going to right
  78. InPreorder(root->right);
  79. }
  80.  
  81. void Search(BstNode* root, std::string data) //serching through the BST for specific word
  82. {
  83. if (root == NULL)
  84. {
  85. std::cout << "Not foundn";
  86. return;
  87. }
  88.  
  89. else if (root->data == data)
  90. {
  91. std::cout << "Foundn";
  92. }
  93. else if (data < root->data)
  94. {
  95. std::cout << "Goind leftn";
  96. return Search(root->left, data);
  97. }
  98. else {
  99. std::cout << "Goind rightn";
  100. return Search(root->right, data);
  101. }
  102. }
  103.  
  104. BstNode* minValue(BstNode* root) //easy way to find the min value in the leftmost leaf
  105. {
  106. BstNode* minData = root;
  107.  
  108. while (minData->left != NULL)
  109. {
  110. minData = minData->left;
  111. }
  112. return minData;
  113. }
  114.  
  115. BstNode* NodeDestructor(BstNode* root, std::string data) //deleting a node in BST
  116. {
  117. if (root == NULL)
  118. {
  119. return root;
  120. }
  121. if (data < root->data) // Searching in BST for the value
  122. {
  123. root->left = NodeDestructor(root->left, data);
  124. }
  125. else if (data > root->data)
  126. {
  127. root->right = NodeDestructor(root->right, data);
  128. }
  129. else //when the value is found
  130. {
  131. if (root->left == NULL && root->right == NULL) //for node with no child
  132. {
  133. delete root;
  134. root = NULL;
  135. }
  136. else if (root->left == NULL)//only one child
  137. {
  138. BstNode *temp = root->right;
  139. delete root;
  140. return temp;
  141. }
  142. else if (root->right == NULL)
  143. {
  144. BstNode *temp = root->left;
  145. delete root;
  146. return temp;
  147. }
  148. else //for node with two children
  149. {
  150. BstNode* minData = root->right;
  151.  
  152. while (minData->left != NULL)
  153. {
  154. minData = minData->left;
  155. }
  156. return minData;
  157.  
  158. BstNode *temp = minData;
  159. root->data = temp->data;
  160. root->right = NodeDestructor(root->right, temp->data);
  161. }
  162. }
  163. return root;
  164. }
  165.  
  166. bool isBST(BstNode* root, BstNode* left = NULL, BstNode* right = NULL) //funciton for checking if the tree is properly created
  167. { // Base condition
  168. if (root == NULL)
  169. {
  170. return true;
  171. }
  172.  
  173. if (left != NULL and root->data < left->data)
  174. {
  175. return false;
  176. }
  177.  
  178. if (right != NULL and root->data > right->data)
  179. {
  180. return false;
  181. }
  182. // check recursively for every node.
  183. return isBST(root->left, left, root) and isBST(root->right, root, right);
  184. }
  185.  
  186. int main()
  187. {
  188. BstNode* root = NULL;
  189. int i, note, j;
  190. std::vector<std::string> test; //define a vector to store words from txt file
  191. test = readFromTxt();
  192.  
  193. for (j = 0; j < test.size(); j++) //This loop is bulding the three passing english words
  194. {
  195. std::string str1 = test[j];
  196.  
  197. root = InsertNode(root, str1);
  198. }
  199.  
  200. if (isBST(root, NULL, NULL)) //calling BST check function
  201. {
  202. std::cout << "Is BSTn";
  203. }
  204. else
  205. {
  206. std::cout << "Not a BSTn";
  207. }
  208.  
  209.  
  210. InPreorder(root);
  211. Search(root, "in");
  212. NodeDestructor(root, "in");
  213. InPreorder(root);
  214. Search(root, "in");
  215.  
  216. delete root;
  217. return 0;
  218. }
  219.  
  220. BstNode* NewNodeCreator(std::string data) //creating a new node, pointing left and right to null
  221. {
  222. /*I make a pointer, pointing to a new space in memory. To allocate that memory I am using new operator, taking sizeof(BstNode) struct*/
  223. BstNode* newNode = new BstNode();
  224.  
  225. newNode->data = data;
  226. newNode->left = newNode->right = NULL; // left and right poiners to NULL
  227. newNode->frequ = 1; //for first time in BST
  228. return newNode;
  229. }
  230.  
  231. std::vector<std::string> list;
  232. ...
  233. while (book >> word)
  234. {
  235. list.emplace_back(word); //store the words in a vector
  236. }
  237.  
  238. // Iterate over a stream and build a vector.
  239. std::vector<std::string> list(std::istream_iterator<std::string>(book),
  240. std::istream_iterator());
  241.  
  242. if (!book.is_open())
  243. {
  244. std::cout << "Unable to open file";
  245. system("pause");
  246. exit(1);
  247. }
  248.  
  249. if (!book.is_open()) {
  250. throw std::runtime_errir("Unable to open file");
  251. }
  252.  
  253. BstNode* InsertNode(BstNode* root, std::string data);
  254.  
  255. root = InsertNode(root, "Blob");
  256.  
  257. class BSTTree
  258. {
  259. private:
  260. BSTNode* root;
  261.  
  262. void InsertNode(BSTNode* node, std::string data);
  263. public:
  264. BSTTree()
  265. : root(nullptr)
  266. {}
  267. void InsertNode(std::string data) {
  268. root = InsertNode(root, data);
  269. }
  270. }
  271.  
  272. else //for node with two children
  273. {
  274. BstNode* minData = root->right;
  275.  
  276. while (minData->left != NULL)
  277. {
  278. minData = minData->left;
  279. }
  280. return minData; // This return is wrong.
  281.  
  282. BstNode *temp = minData;
  283. root->data = temp->data;
  284. root->right = NodeDestructor(root->right, temp->data);
  285. }
  286.  
  287. else //for node with two children
  288. {
  289. BstNode* minData = minValue(root->right);
  290.  
  291. root->data = minData->data;
  292. root-> frequ= minData-> frequ;
  293. root->right = NodeDestructor(root->right, root->data);
  294. }
Add Comment
Please, Sign In to add comment