Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <string>
- #include <fstream>
- //#ifndef BINARYTREE_H_
- #define BINARYTREE_H_
- #include <iostream>
- using namespace std;
- template<typename T>
- struct Node
- {
- T value;
- Node<T>* left;
- Node<T>* right;
- };
- template<typename T>
- class BinaryTree
- {
- Node<T>* root;
- public:
- BinaryTree();
- virtual ~BinaryTree();
- void insert(const T& val);
- void print();
- void inorder(Node<T>*) const;
- };
- // Constructor with no initialization
- template<typename T>
- inline BinaryTree<T>::BinaryTree()
- {
- root = NULL;
- }
- // Optional Desctructor
- template<typename T>
- inline BinaryTree<T>::~BinaryTree()
- {
- }
- // Insert function helps in:
- // 1. Comparing the ascii value of the new element compared to root
- // 2. Handles the recursion as well.
- template<typename T>
- inline void BinaryTree<T>::insert(const T& val)
- {
- Node<T>* new_node = new Node<T>;
- //set value to node
- new_node->value = val;
- // set left and right of new_node to null
- new_node->left = NULL;
- new_node->right = NULL;
- // check if root is null
- if (root == NULL)
- {
- //set new_node as root
- root = new_node;
- }
- else
- {
- Node<T>* current = root, *prev = root;
- while (current != NULL)
- {
- prev = current;
- // check if new_node value is lesser than root
- if (new_node->value < current->value)
- {
- // left sub tree
- current = current->left;
- }
- else
- {
- // right subtree
- current = current->right;
- }
- }
- if (prev->left == NULL)
- {
- // set as left of prev
- prev->left = new_node;
- }
- else
- {
- // set as right of prev
- prev->right = new_node;
- }
- // loop exits when current is null, means new_node will be
- }
- }
- // Helper function for inorder traversal
- template<typename T>
- inline void BinaryTree<T>::inorder(Node<T>* node) const {
- if (node == NULL)
- {
- return;
- }
- inorder(node->left);
- cout << node->value << endl;
- inorder(node->right);
- }
- // Helper functions for printing node information
- template<typename T>
- inline void BinaryTree<T>::print()
- {
- this->inorder(this->root);
- }
- int main()
- {
- BinaryTree<string> bt;
- // variable for reading file stream
- ifstream fin;
- // Varaiable to store character read from file
- char ch = ' ';
- // string variable to stroe string to be added to tree node
- string wordStr = "";
- // open file in read mode. Have to provide full path in certain OSs
- fin.open("data.txt");
- if (fin)
- {
- while (!fin.eof())
- {
- // In order to deliberately not skip whitespaces.
- fin >> noskipws >> ch;
- // Weeding out capital letters
- if (ch >= 65 && ch <= 90)
- {
- // convert to small case
- ch = ch + 32;
- wordStr += ch;
- }
- // Weeding out empty space, hyphen, new line, tab and end of file characters.
- else if (ch == ' ' || ch == '-' || ch == '\n' || ch == '\t' || fin.eof())
- {
- // add string to tree
- if (wordStr.length() > 0)
- {
- // if last char is apostrophes, then replace it with end of string char
- if (wordStr.at(wordStr.length() - 1) == '\'')
- {
- wordStr[wordStr.length() - 1] = '\0';
- }
- // insert word to tree
- bt.insert(wordStr);
- }
- // clear variable
- wordStr = "";
- }
- // if capital alphabet
- else if (ch >= 97 && ch <= 122)
- {
- wordStr += ch;
- }
- else if (ch == '\'' && wordStr.length() > 0)
- {
- wordStr += ch;
- }
- }
- bt.print();
- }
- else
- {
- cout << "Error: File opening Error";
- }
- // close file stream
- fin.close();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement