Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<cstdlib>
- #include<string>
- using namespace std;
- struct Node
- {
- int key;
- struct Node *left;
- struct Node *right;
- int height;
- };
- int max(int a, int b);
- int height(struct Node *N)
- {
- if (N == NULL)
- return 0;
- return N->height;
- }
- int max(int a, int b)
- {
- return (a > b)? a : b;
- }
- struct Node* newNode(int key)
- {
- struct Node* node = (struct Node*)
- malloc(sizeof(struct Node));
- node->key = key;
- node->left = NULL;
- node->right = NULL;
- node->height = 1;
- return(node);
- }
- struct Node *rightRotate(struct Node *y)
- {
- struct Node *x = y->left;
- struct Node *T2 = x->right;
- x->right = y;
- y->left = T2;
- y->height = max(height(y->left), height(y->right))+1;
- x->height = max(height(x->left), height(x->right))+1;
- return x;
- }
- struct Node *leftRotate(struct Node *x)
- {
- struct Node *y = x->right;
- struct Node *T2 = y->left;
- y->left = x;
- x->right = T2;
- x->height = max(height(x->left), height(x->right))+1;
- y->height = max(height(y->left), height(y->right))+1;
- return y;
- }
- int getBalance(struct Node *N)
- {
- if (N == NULL)
- return 0;
- return height(N->left) - height(N->right);
- }
- struct Node * minValueNode(struct Node* node)
- {
- struct Node* current = node;
- while (current->left != NULL)
- current = current->left;
- return current;
- }
- int maxLevel(struct Node* node)
- {
- if(node == NULL)
- {
- return 0;
- }
- else return max(maxLevel(node->left), maxLevel(node->right))+1;
- }
- struct Node* insertAVL(struct Node* node, int key)
- {
- if (node == NULL)
- return(newNode(key));
- if (key < node->key)
- node->left = insertAVL(node->left, key);
- else if (key > node->key)
- node->right = insertAVL(node->right, key);
- else
- return node;
- node->height = 1 + max(height(node->left),
- height(node->right));
- int balance = getBalance(node);
- if (balance > 1 && key < node->left->key)
- return rightRotate(node);
- if (balance < -1 && key > node->right->key)
- return leftRotate(node);
- if (balance > 1 && key > node->left->key)
- {
- node->left = leftRotate(node->left);
- return rightRotate(node);
- }
- if (balance < -1 && key < node->right->key)
- {
- node->right = rightRotate(node->right);
- return leftRotate(node);
- }
- return node;
- }
- struct Node* deleteNodeAVL(struct Node* root, int key)
- {
- if (root == NULL)
- return root;
- if ( key < root->key )
- root->left = deleteNodeAVL(root->left, key);
- else if( key > root->key )
- root->right = deleteNodeAVL(root->right, key);
- else
- {
- if( (root->left == NULL) || (root->right == NULL) )
- {
- struct Node *temp = root->left ? root->left :
- root->right;
- if (temp == NULL)
- {
- temp = root;
- root = NULL;
- }
- else
- *root = *temp;
- free(temp);
- }
- else
- {
- struct Node* temp = minValueNode(root->right);
- root->key = temp->key;
- root->right = deleteNodeAVL(root->right, temp->key);
- }
- }
- if (root == NULL)
- return root;
- root->height = 1 + max(height(root->left),
- height(root->right));
- int balance = getBalance(root);
- if (balance > 1 && getBalance(root->left) >= 0)
- return rightRotate(root);
- if (balance > 1 && getBalance(root->left) < 0)
- {
- root->left = leftRotate(root->left);
- return rightRotate(root);
- }
- if (balance < -1 && getBalance(root->right) <= 0)
- return leftRotate(root);
- if (balance < -1 && getBalance(root->right) > 0)
- {
- root->right = rightRotate(root->right);
- return leftRotate(root);
- }
- return root;
- }
- void preOrder(struct Node *root)
- {
- if(root != NULL)
- {
- printf("%d ", root->key);
- preOrder(root->left);
- preOrder(root->right);
- }
- }
- void inOrder(struct Node *root)
- {
- if(root != NULL)
- {
- inOrder(root->left);
- printf("%d ", root->key);
- inOrder(root->right);
- }
- }
- void postOrder(struct Node *root)
- {
- if(root != NULL)
- {
- postOrder(root->left);
- postOrder(root->right);
- printf("%d ", root->key);
- }
- }
- void drawTreeTMP(struct Node *current, char*tab, int width, int x, int y)
- {
- if(current!=NULL)
- {
- if(current->key/10!=0)
- tab[(width+1)*y+x-1] = '0' + current->key/10;
- tab[(width+1)*y+x] = '0' + current->key%10;
- if(current->right!=NULL)
- {
- tab[(width+1)*(y+1)+(x+1)] ='\\';
- drawTreeTMP(current->right, tab, width, x+2, y+2);
- }
- if(current->left!=NULL)
- {
- tab[(width+1)*(y+1)+(x-1)] = '/';
- drawTreeTMP(current->left, tab, width, x-2, y+2);
- }
- }
- }
- void drawTree(struct Node *root)
- {
- int layers = maxLevel(root);
- int height = layers*3+1;
- int width = layers*5+1;
- char *tab = new char[height*(width+1)+1];
- tab[height*(width+1)] = '\0';
- int x, y;
- for(y=0;y<height;y++)
- {
- for(x=0;x<width;x++)
- {
- if(x==0||x==width-1||y==0||y==height-1)
- tab[(width+1)*y+x] = ' ';
- else
- tab[(width+1)*y+x] = ' ';
- }
- tab[(width+1)*y+x] = '\n';
- }
- drawTreeTMP(root, tab, width, width/2, 2);
- printf("%s", tab);
- free(tab);
- }
- struct Node *postorder(struct Node *root)
- {
- if(root->left != NULL) postorder(root->left);
- return root;
- if(root->right!=NULL) postorder(root->right);
- };
- int main()
- {
- struct Node *rootavl = NULL;
- struct Node *root = NULL;
- int tmp;
- while(1)
- {
- drawTree(root);
- cout<<"1.Usun element\t\t";
- cout<<"2.Dodaj element\t\t";
- cout<<"3.Usun cale drzewo\t\t";
- int tmp2;
- scanf("%d", &tmp2);
- if(tmp2 == 1)
- {
- int nr;
- cin >> nr;
- root = deleteNodeAVL(root, nr);
- }
- else if(tmp2 == 2)
- {
- int nr;
- cout<<"Podaj element:";
- cin >> nr;
- root = insertAVL(root, nr);
- }
- else if(tmp2 == 3)
- {
- while(root->height>1) deleteNodeAVL(postorder(root),postorder(root)->key);
- root=NULL;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement