Advertisement
notKakarot

updatedFile

Jan 26th, 2020
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.19 KB | None | 0 0
  1. #include <string>
  2. #include <fstream>
  3. //#ifndef BINARYTREE_H_
  4. #define BINARYTREE_H_
  5.  
  6. #include <iostream>
  7. using namespace std;
  8. template<typename T>
  9. struct Node
  10. {
  11.     T value;
  12.     Node<T>* left;
  13.     Node<T>* right;
  14. };
  15.  
  16. template<typename T>
  17. class BinaryTree
  18. {
  19.     Node<T>* root;
  20. public:
  21.     BinaryTree();
  22.     virtual ~BinaryTree();
  23.     void insert(const T& val);
  24.     void print();
  25.     void inorder(Node<T>*) const;
  26.  
  27. };
  28.  
  29. // Constructor with no initialization
  30. template<typename T>
  31. inline BinaryTree<T>::BinaryTree()
  32. {
  33.     root = NULL;
  34. }
  35.  
  36. // Optional Desctructor
  37. template<typename T>
  38. inline BinaryTree<T>::~BinaryTree()
  39. {
  40. }
  41.  
  42. // Insert function helps in:
  43. // 1. Comparing the ascii value of the new element compared to root
  44. // 2. Handles the recursion as well.
  45. template<typename T>
  46. inline void BinaryTree<T>::insert(const T& val)
  47. {
  48.  
  49.     Node<T>* new_node = new Node<T>;
  50.     //set value to node
  51.     new_node->value = val;
  52.     // set left and right of new_node to null
  53.     new_node->left = NULL;
  54.     new_node->right = NULL;
  55.     // check if root is null
  56.     if (root == NULL)
  57.     {
  58.         //set new_node as root
  59.         root = new_node;
  60.     }
  61.     else
  62.     {
  63.         Node<T>* current = root, *prev = root;
  64.         while (current != NULL)
  65.         {
  66.             prev = current;
  67.             // check if new_node value is lesser than root
  68.             if (new_node->value < current->value)
  69.             {
  70.                 // left sub tree
  71.                 current = current->left;
  72.             }
  73.             else
  74.             {
  75.                 // right subtree
  76.                 current = current->right;
  77.             }
  78.         }
  79.         if (prev->left == NULL)
  80.         {
  81.             // set as left of prev
  82.             prev->left = new_node;
  83.         }
  84.         else
  85.         {
  86.             // set as right of prev
  87.             prev->right = new_node;
  88.         }
  89.         // loop exits when current is null, means new_node will be
  90.     }
  91.  
  92. }
  93.  
  94. // Helper function for inorder traversal
  95. template<typename T>
  96. inline void BinaryTree<T>::inorder(Node<T>* node) const {
  97.     if (node == NULL)
  98.     {
  99.         return;
  100.     }
  101.     inorder(node->left);
  102.     cout << node->value << endl;
  103.     inorder(node->right);
  104. }
  105.  
  106. // Helper functions for printing node information
  107. template<typename T>
  108. inline void BinaryTree<T>::print()
  109. {
  110.     this->inorder(this->root);
  111. }
  112.  
  113. int main()
  114. {
  115.     BinaryTree<string> bt;
  116.     // variable for reading file stream
  117.     ifstream fin;
  118.     // Varaiable to store character read from file
  119.     char ch = ' ';
  120.     // string variable to stroe string to be added to tree node
  121.     string wordStr = "";
  122.     // open file in read mode. Have to provide full path in certain OSs
  123.     FILE *fileDescriptor;
  124.     fileDescriptor = fopen("data.txt", "r");
  125.  
  126.     char str[1024];
  127.    
  128.     while (fgets(str, sizeof str, fileDescriptor) != NULL)
  129.     {
  130.         // In order to deliberately not skip whitespaces.
  131.         fin >> noskipws >> ch;
  132.        
  133.         // Weeding out capital letters
  134.         if (ch >= 65 && ch <= 90)
  135.         {
  136.             // convert to small case
  137.             ch = ch + 32;
  138.             wordStr += ch;
  139.         }
  140.         // Weeding out empty space, hyphen, new line, tab and end of file characters.
  141.         else if (ch == ' ' || ch == '-' || ch == '\n' || ch == '\t' || fin.eof())
  142.         {
  143.             // add string to tree
  144.             if (wordStr.length() > 0)
  145.             {
  146.                 // if last char is apostrophes, then replace it with end of string char
  147.                 if (wordStr.at(wordStr.length() - 1) == '\'')
  148.                 {
  149.                     wordStr[wordStr.length() - 1] = '\0';
  150.                 }
  151.  
  152.                 // insert word to tree
  153.                 bt.insert(wordStr);
  154.             }
  155.             // clear variable
  156.             wordStr = "";
  157.         }
  158.         // if capital alphabet
  159.         else if (ch >= 97 && ch <= 122)
  160.         {
  161.             wordStr += ch;
  162.         }
  163.         else if (ch == '\'' && wordStr.length() > 0)
  164.         {
  165.             wordStr += ch;
  166.         }
  167.     }
  168.     if (feof(stream))
  169.     {
  170.         bt.print();
  171.     }
  172.     else
  173.     {
  174.         cout << "Error: File opening Error";
  175.     }
  176.     fclose(fileDescriptor)
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement