Untitled
unknown
plain_text
2 years ago
5.0 kB
0
Indexable
Never
#include<iostream> #include<fstream> #include<queue> #include<stack> #include<string> #include<sstream> using namespace std; struct Card { string id, name, set, type; bool collectible; int mana, attack, health, armor; }; struct Node_T { Card data; Node_T* left, * right; }; void Init(Node_T*& pRoot) { pRoot = NULL; } Node_T* CreatNode_T(Card value) { Node_T* p = new Node_T; if (p == NULL) { cout << "\nnot enough memory !"; return NULL; } p->data = value; p->left = p->right = NULL; return p; } void LL(Node_T*& pRoot) { Node_T* p = pRoot->right; pRoot->right = p->left; p->left = pRoot; pRoot = p; } void RR(Node_T*& pRoot) { Node_T* p = pRoot->left; pRoot->left = p->right; p->right = pRoot; pRoot = p; } int Height(Node_T* pRoot) { if (pRoot == NULL) return 0; return max(Height(pRoot->left), Height(pRoot->right)) + 1; } void Balance(Node_T*& pRoot) { int bal = Height(pRoot->left) - Height(pRoot->right); if (bal > 1) // Cây lệch trái { if (Height(pRoot->left->left) >= Height(pRoot->left->right)) //Cây lệch trái trái RR(pRoot); if (Height(pRoot->left->left) < Height(pRoot->left->right)) { //Cây lệch trái phải LL(pRoot->left); RR(pRoot); } } if (bal < -1) { //Cây lệch phải if (Height(pRoot->right->right) >= Height(pRoot->right->left)) //Cây lệch phải phải LL(pRoot); if (Height(pRoot->right->right) < Height(pRoot->right->left)) { //Cây lệch phải trái RR(pRoot->right); LL(pRoot); } } } void Insert_ID(Node_T*& pRoot, Node_T* p) { if (pRoot == NULL) { pRoot = p; return; } if (strcmp(pRoot->data.id.c_str(), p->data.id.c_str()) < 0) { Insert_ID(pRoot->right, p); } else if (strcmp(pRoot->data.id.c_str(), p->data.id.c_str()) > 0) { Insert_ID(pRoot->left, p); } else if (strcmp(pRoot->data.id.c_str(), p->data.id.c_str()) == 0) return; Balance(pRoot); } void Insert_Mana_Health_ID(Node_T*& pRoot, Node_T* p) { if (pRoot == NULL) { pRoot = p; return; } if (pRoot->data.mana < p->data.mana) { Insert_ID(pRoot->right, p); } else if (pRoot->data.mana > p->data.mana) { Insert_ID(pRoot->left, p); } else { if (pRoot->data.health < p->data.health) { Insert_ID(pRoot->right, p); } else if (pRoot->data.health > p->data.health) { Insert_ID(pRoot->left, p); } else { if (strcmp(pRoot->data.id.c_str(), p->data.id.c_str()) < 0) { Insert_ID(pRoot->right, p); } else if (strcmp(pRoot->data.id.c_str(), p->data.id.c_str()) > 0) { Insert_ID(pRoot->left, p); } else if (strcmp(pRoot->data.id.c_str(), p->data.id.c_str()) == 0) return; } } Balance(pRoot); } void Insert_HealthAttack_Mana_ID(Node_T*& pRoot, Node_T* p) { if (pRoot == NULL) { pRoot = p; return; } if (pRoot->data.health + pRoot->data.attack < p->data.health + p->data.attack) { Insert_ID(pRoot->right, p); } else if (pRoot->data.health + pRoot->data.attack > p->data.health + p->data.attack) { Insert_ID(pRoot->left, p); } else { if (pRoot->data.mana < p->data.mana) { Insert_ID(pRoot->right, p); } else if (pRoot->data.mana > p->data.mana) { Insert_ID(pRoot->left, p); } else { if (strcmp(pRoot->data.id.c_str(), p->data.id.c_str()) < 0) { Insert_ID(pRoot->right, p); } else if (strcmp(pRoot->data.id.c_str(), p->data.id.c_str()) > 0) { Insert_ID(pRoot->left, p); } else if (strcmp(pRoot->data.id.c_str(), p->data.id.c_str()) == 0) return; } } Balance(pRoot); } Node_T* ReadAVLTree(string filename, int luachon) { Node_T* pRoot; Init(pRoot); ifstream ifs(filename); if (!ifs.is_open()) { cout << "\ncannot open file"; return NULL; } string temp = ""; getline(ifs, temp); // bỏ qua dòng đầu tiên while (!ifs.eof()) { Card c; getline(ifs, c.id, ';'); getline(ifs, c.name, ';'); getline(ifs, c.set, ';'); getline(ifs, c.type, ';'); getline(ifs, temp, ';'); if (strcmp(temp.c_str(), "True") == 0) { c.collectible = true; } else if (strcmp(temp.c_str(), "False") == 0) { c.collectible = false; } getline(ifs, temp, ';'); c.mana = atoi(temp.c_str()); getline(ifs, temp, ';'); c.attack = atoi(temp.c_str()); getline(ifs, temp, ';'); c.health = atoi(temp.c_str()); getline(ifs, temp,'\n'); c.armor = atoi(temp.c_str()); Node_T* p = CreatNode_T(c); if (luachon == 1) // lựa chọn số 1: duyệt theo ID { Insert_ID(pRoot, p); } else if (luachon == 2) // lựa chọn số 2: duyệt theo Mana->Health->ID { Insert_Mana_Health_ID(pRoot, p); } else // lựa chọn số 3: duyệt theo attack + health → mana → ID. { Insert_HealthAttack_Mana_ID(pRoot, p); } } ifs.close(); return pRoot; }