Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2020
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.07 KB | None | 0 0
  1. struct ListNode
  2. {
  3. bool isString;
  4. std::string string;
  5. int value;
  6.  
  7. ListNode()
  8. {
  9. isString = true;
  10. string = "No name";
  11. value = NULL;
  12. }
  13.  
  14. ListNode(std::string str)
  15. {
  16. isString = true;
  17. string = str;
  18. value = NULL;
  19. }
  20.  
  21. ListNode(int val)
  22. {
  23. isString = false;
  24. value = val;
  25. }
  26. };
  27.  
  28. struct TableName
  29. {
  30. std::string name;
  31. std::string value;
  32. int size;
  33.  
  34. TableName(std::string name, std::string value)
  35. {
  36. this->name = name;
  37. this->value = value;
  38. }
  39.  
  40. TableName(std::string name, std::string value, int s)
  41. {
  42. this->name = name;
  43. this->value = value;
  44. size = 0;
  45. }
  46. };
  47.  
  48.  
  49. struct Pair
  50. {
  51. std::string name;
  52. std::vector<TableName> nameRow;
  53. std::list<std::vector<ListNode>> list;
  54.  
  55. Pair()
  56. {
  57. name = "No name";
  58. TableName firstRow("Id", "Int");
  59. TableName secondRow("Name", "String");
  60. TableName thirdRow("Value", "Int");
  61. std::vector<TableName> newTable;
  62. newTable.push_back(firstRow);
  63. newTable.push_back(secondRow);
  64. newTable.push_back(thirdRow);
  65. nameRow = newTable;
  66. }
  67.  
  68. Pair(std::string strName, std::list<std::vector<ListNode>> array, std::vector<TableName> tableNameRow)
  69. {
  70. name = strName;
  71. list = array;
  72. nameRow = tableNameRow;
  73. }
  74. };
  75.  
  76. struct TreeNode
  77. {
  78. Pair value;
  79. TreeNode* left, * right;
  80.  
  81. bool operator<(const TreeNode& other)
  82. {
  83. return value.name < other.value.name; // leksicographical compair
  84. }
  85. bool operator==(const TreeNode& other)
  86. {
  87. return value.name == other.value.name; // leksicographical compair
  88. }
  89.  
  90. TreeNode()
  91. {
  92. value.name = "NoName";
  93. left = right = nullptr;
  94. }
  95.  
  96. TreeNode(int s)
  97. {
  98. value.name = "NoName";
  99. left = right = nullptr;
  100. }
  101.  
  102. TreeNode(const Pair& value)
  103. {
  104. this->value = value;
  105. left = right = nullptr;
  106. }
  107. };
  108.  
  109. class Tree
  110. {
  111. private:
  112. TreeNode* root;
  113.  
  114. TreeNode* find(TreeNode* r, const std::string& key) const
  115. {
  116. if (!r) return nullptr;
  117.  
  118. if (r->value.name == key) return r;
  119.  
  120. return r->value.name < key ? find(r->left, key) : find(r->right, key);
  121.  
  122. }
  123.  
  124. TreeNode* insert(TreeNode*& r, const Pair& val)
  125. {
  126. if (!r)
  127. {
  128. r = new TreeNode(val);
  129. return r;
  130. }
  131. if (val.name < r->value.name) r->left = insert(r->left, val);
  132. else r->right = insert(r->right, val);
  133. if (val.name == r->value.name) return r;
  134.  
  135. }
  136.  
  137. TreeNode* findParent(TreeNode* r, const std::string& key)
  138. {
  139. if (!r) return nullptr;
  140. if (r->left->value.name == key || r->right->value.name == key)
  141. {
  142. return r;
  143. }
  144. findParent(r->left, key);
  145. findParent(r->right, key);
  146. }
  147.  
  148. void remove(TreeNode*& r, const std::string& name)
  149. {
  150. TreeNode* toDelete = find(name);
  151. TreeNode* parent = findParent(r, name);
  152.  
  153. int children = !toDelete->left + !toDelete->right;
  154. if (children == 0)
  155. {
  156. if (parent->left->value.name == name) parent->left = nullptr;
  157. else parent->right = nullptr;
  158.  
  159. delete toDelete;
  160. return;
  161. }
  162.  
  163. else if (children == 1)
  164. {
  165. TreeNode* pos = parent->left->value.name == name ? parent->left : parent->right;
  166. TreeNode* par = findParent(r, name);
  167.  
  168. toDelete->left ? pos = toDelete->left : pos = toDelete->right;
  169. delete toDelete;
  170. return;
  171. }
  172.  
  173. TreeNode* del;
  174. TreeNode* maxLeft = toDelete->left;
  175. while (maxLeft->right)
  176. {
  177. del = maxLeft;
  178. maxLeft = maxLeft->right;
  179. }
  180.  
  181. maxLeft->right = nullptr;
  182. toDelete->value = maxLeft->value;
  183. delete maxLeft;
  184. }
  185.  
  186. TreeNode* copy(TreeNode* r, TreeNode* other)
  187. {
  188. if (!r) return nullptr;
  189.  
  190. TreeNode* c = new TreeNode();
  191.  
  192. c->left = copy(r->left, other);
  193. c->right = copy(r->right, other);
  194.  
  195. return c;
  196. }
  197.  
  198. void clear(TreeNode* r)
  199. {
  200. if (!r) return;
  201.  
  202. clear(r->left);
  203. clear(r->right);
  204.  
  205. delete r;
  206. }
  207.  
  208. public:
  209. Tree() : root(nullptr) {}
  210. Tree(const Tree& other)
  211. {
  212. copy(root, other.root);
  213. }
  214. ~Tree()
  215. {
  216. clear(root);
  217. }
  218.  
  219. TreeNode* find(const std::string& key) const
  220. {
  221. return find(root, key);
  222. }
  223.  
  224. TreeNode* insert(const Pair& addedNode)
  225. {
  226. insert(root, addedNode);
  227. return root;
  228. }
  229.  
  230. bool remove(const std::string& removedName)
  231. {
  232. remove(root, removedName);
  233. }
  234.  
  235. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement