Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<fstream>
- #include <string>
- using namespace std;
- int poziom = 0;
- string cr, cl, cp;
- struct AVLnode
- {
- AVLnode *left;
- AVLnode *right;
- AVLnode *parent;
- int key;
- int bf;
- };
- void printBT(string sp, string sn, AVLnode * v)
- {
- string s;
- if (v)
- {
- s = sp;
- if (sn == cr) s[s.length() - 2] = ' ';
- printBT(s + cp, cr, v->right);
- s = s.substr(0, sp.length() - 2);
- cout << s << sn << v->key <<v->bf<< endl;
- s = sp;
- if (sn == cl) s[s.length() - 2] = ' ';
- printBT(s + cp, cl, v->left);
- }
- }
- void save(AVLnode *root)
- {
- fstream fout;
- fout.open("Drzewo.txt", ios::out | ios::app);
- if (root->left)
- save(root->left);
- fout << root->key << " "<<root->parent->key;
- if (root->right)
- save(root->right);
- fout << endl;
- fout.close();
- }
- void printf(AVLnode *root)
- {
- if (root->left)
- printf(root->left);
- cout << root->key << " o adresie: " << root << " i o rodzicu " << root->parent<<endl;
- if (root->right)
- printf(root->right);
- }
- void addnode(AVLnode **root, int data)
- {
- int x=1;
- AVLnode *temp = (*root);
- AVLnode *temp2 = NULL;
- if (*root == NULL)
- {
- (*root) = new AVLnode;
- (*root)->key = data;
- (*root)->left = NULL;
- (*root)->right = NULL;
- (*root)->parent = NULL;
- (*root)->bf = 0;
- }
- else
- {
- while(temp)
- {
- if (data > temp->key)
- {
- temp2 = temp;
- temp = temp->right;
- }
- else if (data < temp->key)
- {
- temp2 = temp;
- temp = temp->left;
- }
- else {
- cout << "Taka liczba juz jest";
- x = 0;
- break;
- }
- }
- if (x)
- {
- AVLnode *a = new AVLnode;
- a->key = data;
- a->left = NULL;
- a->right = NULL;
- a->parent = temp2;
- a->bf = 0;
- if (temp2->key > data)
- temp2->left = a;
- else
- temp2->right = a;
- }
- }
- }
- void addtxt(AVLnode **root)
- {
- int a;
- ifstream fin;
- fin.open("temp.txt");
- while (!fin.eof())
- {
- fin >> a;
- addnode(root, a);
- }
- }
- void min(AVLnode **root)
- {
- AVLnode *temp = (*root);
- while (temp->left)
- temp = temp->left;
- cout << "Minimalna to: " << temp->key << endl;
- }
- void max(AVLnode **root)
- {
- AVLnode *temp = (*root);
- while (temp->right)
- temp = temp->right;
- cout << "Maksymalna to: " << temp->key << endl;
- }
- AVLnode *search(AVLnode *root, int data)
- {
- while (root)
- {
- if (root->key>data)
- root = root->left;
- if (root->key<data)
- root = root->right;
- if (root->key == data)
- return root;
- }
- return 0;
- }
- void LL(AVLnode **root, AVLnode *a)
- {
- AVLnode *b=a->left;
- AVLnode *p = a->parent;
- if (a == *root)
- *root = b;
- if (b)
- a->left = b->right;
- else
- a->left = NULL;
- if (a->left)
- {
- a->left->parent = a;
- }
- if (b)
- {
- b->right = a;
- b->parent = p;
- }
- a->parent = b;
- if (p == NULL)
- (*root) = b;
- if (p->left == a)
- p->left = b;
- else
- p->right = b;
- if (b)
- {
- if (b->bf == 1) {
- a->bf = 0;
- b->bf = 0;
- }
- else
- {
- a->bf = 0;
- b->bf = -1;
- }
- if (a->parent->parent)
- a->parent->parent->bf = a->parent->parent->bf - 1;
- }
- }
- void RL(AVLnode **root, AVLnode *a)
- {
- AVLnode *b = a->right;
- AVLnode *p = a->parent;
- AVLnode *c = b->left;
- if (a == *root)
- *root = b;
- if (b)
- b->left = c->right;
- else
- b->right = NULL;
- if (b->left)
- {
- b->left->parent = b;
- }
- if(c)
- a->right = c->left;
- if (a->right)
- {
- a->right->parent = a;
- }
- c->left = a;
- c->right = b;
- a->parent = c;
- b->parent = c;
- c->parent = p;
- if (p == NULL)
- (*root) = c;
- if (p->left == a)
- p->left = c;
- else
- p->right = c;
- if (c)
- {
- if (c->bf == -1) {
- a->bf = 1;
- }
- else
- {
- a->bf = 0;
- }
- if (c->bf == 1) {
- b->bf = -1;
- }
- else
- {
- b->bf = 0;
- }
- if (a->parent->parent)
- a->parent->parent->bf = a->parent->parent->bf + 1;
- }
- }
- void LR(AVLnode **root, AVLnode *a)
- {
- AVLnode *b = a->left;
- AVLnode *p = a->parent;
- AVLnode *c = b->right;
- if (a == *root)
- *root = b;
- if (b)
- b->right = c->left;
- else
- b->right = NULL;
- if (b->right)
- {
- b->right->parent = b;
- }
- if (c)
- a->left = c->right;
- if (a->left)
- {
- a->left->parent = a;
- }
- c->right = a;
- c->left = b;
- a->parent = c;
- b->parent = c;
- c->parent = p;
- if (p == NULL)
- (*root) = c;
- if (p->left == a)
- p->left = c;
- else
- p->right = c;
- if (c)
- {
- if (c->bf == 1) {
- a->bf = -1;
- }
- else
- {
- a->bf = 0;
- }
- if (c->bf == -1) {
- b->bf = -1;
- }
- else
- {
- b->bf = 0;
- }
- if (a->parent->parent)
- a->parent->parent->bf = a->parent->parent->bf - 1;
- }
- }
- void RR(AVLnode **root, AVLnode *a)
- {
- AVLnode *b = a->right;
- AVLnode *p = a->parent;
- if (a == *root)
- *root = b;
- if (b)
- a->right = b->left;
- else
- a->right = NULL;
- if (a->right)
- {
- a->right->parent = a;
- }
- if (b)
- {
- b->left = a;
- b->parent = p;
- }
- a->parent = b;
- if (p == NULL)
- (*root) = b;
- if (p->left == a)
- p->left = b;
- else
- p->right = b;
- if (b)
- {
- if (b->bf == -1) {
- a->bf = 0;
- b->bf = 0;
- }
- else
- {
- a->bf = -1;
- b->bf = 1;
- }
- if (a->parent->parent)
- a->parent->parent->bf = a->parent->parent->bf + 1;
- }
- }
- void bf(AVLnode *root, AVLnode *temp)
- {
- while (temp->parent)
- {
- AVLnode *temp2 = temp->parent;
- if (temp->parent->left == temp)
- {
- if (temp->parent->right == NULL)
- {
- temp->parent->bf = (temp->parent->bf)+1;
- }
- else
- while (temp2->parent)
- {
- temp2->bf = temp2->bf + 1;
- temp2 = temp2->parent;
- }
- }
- else
- {
- if (temp->parent->left == NULL)
- {
- temp->parent->bf = (temp->parent->bf)-1;
- }
- else
- while (temp2->parent)
- {
- temp2->bf = temp2->bf - 1;
- temp2 = temp2->parent;
- }
- }
- temp = temp->parent;
- }
- }
- void addAVL(AVLnode **root, int data)
- {
- addnode(root, data);
- AVLnode *temp = search(*root, data);
- bf(*root, temp);
- if(temp->parent)
- while (temp->parent)
- {
- AVLnode *temp2 = temp;
- if (temp->parent->parent)
- {
- if (temp->bf == 0 && temp->parent->bf == 1 && temp->parent->parent->bf == 2) {
- LL(root, temp2->parent->parent);
- }
- if (temp->bf == 0 && temp->parent->bf == -1 && temp->parent->parent->bf == -2)
- RR(root, temp->parent->parent);
- if (temp->bf == 0 && temp->parent->bf == 1 && temp->parent->parent->bf == -2)
- RL(root, temp->parent->parent);
- if (temp->bf == 0 && temp->parent->bf == -1 && temp->parent->parent->bf == 2)
- LR(root, temp->parent->parent);
- }
- temp = temp->parent;
- }
- }
- int main()
- {
- int pyt = 1, nr;
- AVLnode * root = NULL;
- root = NULL;
- cr = cl = cp = " ";
- cr[0] = 218; cr[1] = 196;
- cl[0] = 192; cl[1] = 196;
- cp[0] = 179;
- while (pyt != 0)
- {
- cout << "1. Dodaj element do drzewa" << endl;
- cout << "2. Wyswietl w pliku" << endl;
- cout << "3. Wyszukaj min" << endl;
- cout << "4. Wyszukaj mix" << endl;
- cout << "5. Wyszukaj czegos" << endl;
- cout << "6. Dodaj wartosci z pliku" << endl;
- cin >> pyt;
- switch (pyt) {
- case 1:
- cout << "Podaj liczbe:" << endl;
- cin >> nr;
- addnode(&root, nr);
- break;
- case 2:
- save(root);
- break;
- case 3:
- min(&root);
- break;
- case 4:
- max(&root);
- break;
- case 5:
- cout << "Co chcesz znalezc: ";
- cin >> nr;
- cout << "Szukane to: " << search(root, nr) << endl;
- break;
- case 6:
- addtxt(&root);
- break;
- case 7:
- cout << "Podaj liczbe:" << endl;
- cin >> nr;
- addAVL(&root, nr);
- break;
- case 0:
- break;
- default:
- cout << "Zly nr" << endl;
- break;
- }
- printBT("", "", root);
- cout << endl;
- }
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement