Advertisement
Guest User

Faafda

a guest
Jan 26th, 2020
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.24 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.     fin.open("data.txt");
  124.     if (fin)
  125.     {
  126.         while (!fin.eof())
  127.         {
  128.             // In order to deliberately not skip whitespaces.
  129.             fin >> noskipws >> ch;
  130.            
  131.             // Weeding out capital letters
  132.             if (ch >= 65 && ch <= 90)
  133.             {
  134.                 // convert to small case
  135.                 ch = ch + 32;
  136.                 wordStr += ch;
  137.             }
  138.             // Weeding out empty space, hyphen, new line, tab and end of file characters.
  139.             else if (ch == ' ' || ch == '-' || ch == '\n' || ch == '\t' || fin.eof())
  140.             {
  141.                 // add string to tree
  142.                 if (wordStr.length() > 0)
  143.                 {
  144.                     // if last char is apostrophes, then replace it with end of string char
  145.                     if (wordStr.at(wordStr.length() - 1) == '\'')
  146.                     {
  147.                         wordStr[wordStr.length() - 1] = '\0';
  148.                     }
  149.  
  150.                     // insert word to tree
  151.                     bt.insert(wordStr);
  152.                 }
  153.                 // clear variable
  154.                 wordStr = "";
  155.             }
  156.             // if capital alphabet
  157.             else if (ch >= 97 && ch <= 122)
  158.             {
  159.                 wordStr += ch;
  160.             }
  161.             else if (ch == '\'' && wordStr.length() > 0)
  162.             {
  163.                 wordStr += ch;
  164.             }
  165.         }
  166.         bt.print();
  167.     }
  168.     else
  169.     {
  170.         cout << "Error: File opening Error";
  171.     }
  172.     // close file stream
  173.     fin.close();
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement