Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <vector>
- #include <string>
- using namespace std;
- vector <string> list, list2, tasks;
- struct Knot {
- string Value;
- string Value2;
- Knot *Left = nullptr;
- Knot *Right = nullptr;
- };
- bool isLarger (string v1, string v2) {
- if (v1.size() > v2.size()) return true;
- else if (v1.size() < v2.size()) return false;
- else if (v1 > v2) return true;
- return false;
- }
- void Insert (string v1, string v2, Knot *&root) {
- if (root == nullptr) {
- root = new Knot;
- root->Value = v1;
- root->Value2 = v2;
- }
- else if (root->Value == v1 && root->Value2 == v2) return;
- else if (isLarger(v1, root->Value) || isLarger(v2, root->Value2)) Insert (v1, v2, root->Right);
- else if (!isLarger(v1, root->Value) || !isLarger(v2, root->Value2)) Insert (v1, v2, root->Left);
- else return;
- }
- void Pre_Order (Knot *root) {
- if (root != nullptr) {
- list.push_back(root->Value);
- list2.push_back(root->Value2);
- Pre_Order(root->Left);
- Pre_Order(root->Right);
- }
- }
- string Minimum (Knot *root) {
- //root = root->Right;
- while (root->Left != nullptr) root = root->Left;
- return root->Value;
- }
- void Delete (string v1, string v2, Knot *&root) {
- if(root != nullptr) {
- if (root->Value == v1 && root->Value2 == v2 && root->Left == nullptr && root->Right == nullptr) root = nullptr;
- else if (root->Value == v1 && root->Value2 == v2 && root->Left == nullptr && root->Right != nullptr) root = root->Right;
- else if (root->Value == v1 && root->Value2 == v2 && root->Left != nullptr && root->Right == nullptr) root = root->Left;
- else if (root->Value == v1 && root->Value2 == v2 && root->Left != nullptr && root->Right != nullptr) {
- root->Value = Minimum(root->Right);
- Delete(root->Value, root->Value2, root->Right);
- }
- else if (isLarger(root->Value, v1) || isLarger(root->Value2, v2)) Delete(v1, v2, root->Left);
- else if (!isLarger(root->Value, v1) || !isLarger(root->Value2, v2)) Delete(v1, v2, root->Right);
- }
- }
- void Search (string v1, string v2, Knot *root, int &flag) {
- if (root != nullptr) {
- if (root->Value == v1 && root->Value2 == v2) { flag = 1; return; }
- else if (isLarger(root->Value, v1) || isLarger(root->Value2, v2)) Search (v1, v2, root->Left, flag);
- else if (!isLarger(root->Value, v1) || !isLarger(root->Value2, v2)) Search (v1, v2, root->Right, flag);
- else { flag = 0; return; }
- }
- else { flag = 0; return; }
- }
- void Count (string v, Knot *root, int &c) {
- if (root != nullptr) {
- if (root->Value == v) c++;
- Count (v, root->Left, c);
- Count (v, root->Right, c);
- }
- }
- int main(int argc, char *argv[]) {
- ifstream in ("In.txt");
- ofstream out ("Out.txt");
- if (!in) { cout << "BLAD ODCZYTU PLIKU!\n"; return 1; }
- string n;
- int x, flag = 0, c = 0;
- in >> x;
- Knot *root = nullptr;
- for (int i = 0; i <= x; i++) {
- in >> n;
- string temp = "", after = "", before = "", task = n;
- getline(in, n);
- for (int i = 0; i < n.size(); i++) {
- if (n[i] == ',') { before = temp; temp = ""; }
- else if(n[i] != ' ') temp+=n[i];
- }
- after = temp;
- if (task == "W") Insert (before, after, root);
- else if (task == "U") Delete (before, after, root);
- else if (task == "S") Search (before, after, root, flag);
- else if (task == "L") Count (after, root, c);
- }
- in.close();
- if (flag == 0) out << "NIE\n";
- else if (flag == 1) out << "TAK\n";
- out << c;
- out.flush();
- out.close();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement