Advertisement
Guest User

Untitled

a guest
Jan 17th, 2017
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.97 KB | None | 0 0
  1. #ifndef DICTIONARY_DICTIONARY_H
  2. #define DICTIONARY_DICTIONARY_H
  3.  
  4. #include "CustomExceptions.h"
  5.  
  6. template <typename key, typename info>
  7. class Dictionary {
  8. struct node {
  9. key id;
  10. info data;
  11. node* leftChild;
  12. node* rightChild;
  13. node(const key& key, const info& info) : id(key), data(info), leftChild(nullptr), rightChild(nullptr) {};
  14. } *root;
  15. node* insert(node* newNode, const key& newKey, const info& newInfo) {
  16. if (!newNode) {
  17. node* add = nullptr;
  18. try {
  19. add = new node(newKey, newInfo);
  20. }
  21. catch (std::wrong_alloc&ba) {
  22. std::cout << std::endl << "Allocation memory failure." << std::endl;
  23. throw;
  24. }
  25. return add;
  26. }
  27. if (newKey > newNode->id) {
  28. newNode->rightChild = insert(newNode->rightChild, newKey, newInfo);
  29. }
  30. else if (newKey < newNode->id) {
  31. newNode->leftChild = insert(newNode->leftChild, newKey, newInfo);
  32. }
  33. else if (newKey == newNode->id) {
  34. newNode->data = newInfo;
  35. return newNode;
  36. }
  37. return balance(newNode);
  38. }
  39.  
  40. node* LeftLeftRotation(node* parent)
  41. {
  42. node* temp;
  43. temp = parent->leftChild;
  44. parent->leftChild = temp->rightChild;
  45. temp->rightChild = parent;
  46. return temp;
  47. }
  48.  
  49. node* LeftRightRotation(node* parent)
  50. {
  51. node* temp;
  52. temp = parent->leftChild;
  53. parent->leftChild = RightRightRotation(temp);
  54. return LeftLeftRotation(parent);
  55. }
  56.  
  57. node* RightLeftRotation(node* parent)
  58. {
  59. node* temp;
  60. temp = parent->rightChild;
  61. parent->rightChild = LeftLeftRotation(temp);
  62. return RightRightRotation(parent);
  63. }
  64.  
  65. node* RightRightRotation(node* parent)
  66. {
  67. node* temp;
  68. temp = parent->rightChild;
  69. parent->rightChild = temp->leftChild;
  70. temp->leftChild = parent;
  71. return temp;
  72. }
  73.  
  74. node* balance(node* n) {
  75. int balanceFactor = difference(n);
  76. if (balanceFactor > 1)
  77. {
  78. if (difference(n->leftChild) > 0)
  79. n = LeftLeftRotation(n);
  80. else
  81. n = LeftRightRotation(n);
  82. }
  83. else if (balanceFactor < -1)
  84. {
  85. if (difference(n->rightChild) > 0)
  86. n = RightLeftRotation(n);
  87. else
  88. n = RightRightRotation(n);
  89. }
  90. return n;
  91. }
  92. node* findMinimum(node* n) {
  93. if (!n) {
  94. return nullptr;
  95. }
  96. return n->leftChild ? findMinimum(n->leftChild) : n;
  97. }
  98. node* detachMinimum(node* n) {
  99. if (!n) {
  100. return nullptr;
  101. }
  102. if (!n->leftChild) {
  103. return n->rightChild;
  104. }
  105. n->leftChild = detachMinimum(n->leftChild);
  106. return balance(n);
  107. }
  108. node* remove(node* n, const key& Key) {
  109. if (!n) {
  110. throw notExistingKeyUsed();
  111. }
  112. if (Key < n->id) {
  113. n->leftChild = remove(n->leftChild, Key);
  114. }
  115. else if (Key > n->id) {
  116. n->rightChild = remove(n->rightChild, Key);
  117. }
  118. else {
  119. node* ln = n->leftChild;
  120. node* rn = n->rightChild;
  121. delete n;
  122. if (!rn) {
  123. return ln;
  124. }
  125. node* minimal = findMinimum(rn);
  126. minimal->rightChild = detachMinimum(rn);
  127. minimal->leftChild = ln;
  128. return balance(minimal);
  129. }
  130. return balance(n);
  131. }
  132. node* copy(node* n) {
  133. if (!n) {
  134. return nullptr;
  135. }
  136. node* temp = new node(n->id, n->data);
  137. temp->leftChild = copy(n->leftChild);
  138. temp->rightChild = copy(n->rightChild);
  139. return temp;
  140. }
  141.  
  142. node* exist(node* n, const key& k) const {
  143. if (!n) {
  144. throw notExistingKeyUsed();
  145. }
  146. else {
  147. if (n->id == k) {
  148. return n;
  149. }
  150. else if (k < n->id) {
  151. return exist(n->leftChild, k);
  152. }
  153. else if (k > n->id) {
  154. return exist(n->rightChild, k);
  155. }
  156. else {
  157. throw notExistingKeyUsed();
  158. }
  159. }
  160. }
  161. void graph(node* n, int level) {
  162. if (!n) {
  163. std::cout << std::endl;
  164. return;
  165. }
  166. int i;
  167. graph(n->rightChild, level + 1);
  168. for (i = 0; i < level; ++i) {
  169. std::cout << '\t';
  170. }
  171. std::cout << n->id << '[' << n->data << ']' << std::endl;
  172. graph(n->leftChild, level + 1);
  173. }
  174. void clear(node* n) {
  175. if (!n) {
  176. return;
  177. }
  178. clear(n->leftChild);
  179. clear(n->rightChild);
  180. delete n;
  181. }
  182. void printInOrder(std::ostream& os, node* n) const {
  183. if (!n) {
  184. return;
  185. }
  186. printInOrder(os, n->leftChild);
  187. os << n->id << "/" << n->data << " ";
  188. printInOrder(os, n->rightChild);
  189. }
  190. int difference(node* n) {
  191. int leftHeight = height(n->leftChild);
  192. int rightHeight = height(n->rightChild);
  193. int balanceFactor = leftHeight - rightHeight;
  194. return balanceFactor;
  195. }
  196.  
  197. int height(node* n) {
  198. int h = 0;
  199. if (n != nullptr)
  200. {
  201. int leftHeight = height(n->leftChild);
  202. int rightHeight = height(n->rightChild);
  203. int max_height = (leftHeight < rightHeight) ? rightHeight : leftHeight;
  204. h = max_height + 1;
  205. }
  206. return h;
  207. }
  208. public:
  209. Dictionary() : root(nullptr) {};
  210. ~Dictionary() {
  211. clear(root);
  212. }
  213. Dictionary(const Dictionary<key, info>& src) {
  214. root = copy(src.root);
  215. }
  216. Dictionary<key, info>& operator=(const Dictionary<key, info>& src) {
  217. if (this == &src) {
  218. return *this;
  219. }
  220. clear(root);
  221. if (src.root) {
  222. root = copy(src.root);
  223. }
  224. return *this;
  225. }
  226. const info& operator[](const key& k) const {
  227. node* index = nullptr;
  228. try {
  229. index = exist(root, k);
  230. }
  231. catch (notExistingKeyUsed& n) {
  232. std::cout << std::endl << n.what() << std::endl;
  233. throw;
  234. }
  235. return index->data;
  236.  
  237. }
  238. friend std::ostream& operator<<(std::ostream& output, const Dictionary<key, info>& src) {
  239. src.printInOrder(output, src.root);
  240. return output;
  241. }
  242. void insert(const key& newKey, const info& newInfo) {
  243. if (!root) {
  244. root = new node(newKey, newInfo);
  245. }
  246. else {
  247. root = insert(root, newKey, newInfo);
  248. }
  249. }
  250. void remove(const key& key) {
  251. try
  252. {
  253. root = remove(root, key);
  254. }
  255. catch (notExistingKeyUsed& e)
  256. {
  257. std::cout << e.what() << std::endl;
  258. throw;
  259. }
  260. }
  261. void graph() {
  262. if (root) {
  263. graph(root, 1);
  264. }
  265. else {
  266. std::cerr << std::endl << "The tree is empty" << std::endl;
  267. }
  268. }
  269. void clear() {
  270. clear(root);
  271. }
  272. bool exist(const key& k) const {
  273. try {
  274. exist(root, k);
  275. }
  276. catch (notExistingKeyUsed& n) {
  277. return false;
  278. }
  279. return true;
  280. }
  281. };
  282. #endif //DICTIONARY_DICTIONARY_H
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement