Advertisement
Guest User

Untitled

a guest
Aug 18th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.61 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. typedef string ItemType;
  6.  
  7. struct WordNode {
  8. ItemType m_data;
  9. WordNode *m_left;
  10. WordNode *m_right;
  11. // You may add additional data members and member functions in WordNode
  12. int m_count; // to store the count of the word
  13.  
  14. WordNode(ItemType data)
  15. {
  16. m_data = data;
  17. m_left = m_right = nullptr;
  18. m_count = 1;//when a node is created , it means its appearing atleast once
  19. }
  20.  
  21. WordNode(const WordNode &n)
  22. {
  23. m_data = n.m_data;
  24. m_count = n.m_count;
  25. if(n.m_left != nullptr)
  26. m_left = new WordNode(*n.m_left);
  27. else
  28. m_left = nullptr;
  29. if(n.m_right != nullptr)
  30. m_right = new WordNode(*n.m_right);
  31. else
  32. m_right = nullptr;
  33. }
  34.  
  35. int distinctWords()
  36. {
  37. int count = 1;
  38. if(m_left != nullptr)
  39. count += m_left->distinctWords();
  40. if(m_right != nullptr)
  41. count += m_right->distinctWords();
  42. return count;
  43. }
  44.  
  45. int totalWords()
  46. {
  47. int count = m_count;
  48. if(m_left != nullptr)
  49. count += m_left->totalWords();
  50. if(m_right != nullptr)
  51. count += m_right->totalWords();
  52. return count;
  53. }
  54.  
  55. friend ostream& operator << (ostream &out, const WordNode &n);
  56.  
  57. };
  58.  
  59. class WordTree {
  60.  
  61. private:
  62. WordNode *root;
  63. void deleteTree(WordNode *w); //recursively delete tree nodes in post order traversal
  64. public:
  65.  
  66. // default constructor
  67. WordTree() : root(nullptr) { };
  68.  
  69. // copy constructor
  70. WordTree(const WordTree& rhs);
  71.  
  72. // assignment operator
  73. const WordTree& operator=(const WordTree& rhs);
  74.  
  75. // Inserts val at the front of the list
  76. void add(ItemType v);
  77.  
  78. // Returns the number of distince words / nodes
  79. int distinctWords() const;
  80.  
  81. // Returns the total number of words inserted, including duplicate
  82. // values
  83. int totalWords() const;
  84.  
  85. // Prints the LinkedList
  86. friend ostream& operator<<(ostream &out, const WordTree& rhs);
  87.  
  88. // Destroys all the dynamically allocated memory
  89. // in the tree.
  90. ~WordTree();
  91. };
  92.  
  93. WordTree::WordTree(const WordTree& rhs)
  94. {
  95. *this = rhs;
  96. }
  97.  
  98. // assignment operator
  99. const WordTree& WordTree::operator=(const WordTree& rhs)
  100. {
  101. if(rhs.root == nullptr)
  102. root = nullptr;
  103. else
  104. root = new WordNode(*rhs.root);
  105. return *this;
  106. }
  107.  
  108. // Inserts val at the front of the list
  109. void WordTree::add(ItemType v)
  110. {
  111.  
  112. if(root == nullptr)
  113. root = new WordNode(v);
  114. else
  115. {
  116. WordNode *curr = root;
  117. while(curr != nullptr)
  118. {
  119. if(curr->m_data == v) //already word in tree, increase count
  120. {
  121. curr->m_count++;
  122. return;
  123. }
  124. else if(v < curr->m_data)
  125. {
  126. if(curr->m_left == nullptr)
  127. {
  128. curr ->m_left = new WordNode(v);
  129. return;
  130. }
  131. else
  132. curr = curr->m_left;
  133. }
  134. else
  135. {
  136.  
  137. if(curr->m_right == nullptr)
  138. {
  139. curr ->m_right = new WordNode(v);
  140. return;
  141. }
  142. else
  143. curr = curr->m_right;
  144. }
  145. }
  146. }
  147. }
  148. // Returns the number of distince words / nodes
  149. int WordTree::distinctWords() const
  150. {
  151. if(root == nullptr)
  152. return 0;
  153. else
  154. return root->distinctWords();
  155. }
  156.  
  157. // Returns the total number of words inserted, including duplicate
  158. // values
  159. int WordTree::totalWords() const
  160. {
  161. if(root == nullptr)
  162. return 0;
  163. else
  164. return root->totalWords();
  165.  
  166. }
  167.  
  168. // Prints the LinkedList
  169. ostream& operator<<(ostream &out, const WordTree& rhs)
  170. {
  171. if(rhs.root != nullptr)
  172. out << *rhs.root;
  173. return out;
  174.  
  175. }
  176.  
  177. // Destroys all the dynamically allocated memory
  178. // in the tree.
  179. WordTree::~WordTree()
  180. {
  181. deleteTree(root);
  182. }
  183.  
  184. void WordTree::deleteTree(WordNode *w)
  185. {
  186. if(w == nullptr) return;
  187. deleteTree(w->m_left);
  188. deleteTree(w->m_right);
  189. delete w;
  190. }
  191.  
  192. ostream& operator << (ostream &out, const WordNode &n)
  193. {
  194. if(n.m_left != nullptr)
  195. out << *n.m_left;
  196. out << n.m_data << "[" << n.m_count << "]" << endl;
  197.  
  198. if(n.m_right != nullptr)
  199. out << *n.m_right;
  200.  
  201. return out;
  202. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement