Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- using namespace std;
- struct Node
- {
- double 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(double key)
- {
- struct Node* node = new Node;
- node->key = key;
- node->left = NULL;
- node->right = NULL;
- node->height = 1;
- return(node);
- }
- struct Node *rightRotate(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);
- }
- bool containsRecursive(Node *current, double key)
- {
- if (current == NULL)
- {
- return false;
- }
- if (current->key == key)
- {
- return true;
- }
- if (key < current->key)
- {
- return containsRecursive(current->left, key);
- }
- else
- {
- return containsRecursive(current->right, key);
- }
- }
- bool contains(Node* root, double key)
- {
- if (root == NULL)
- {
- return false;
- }
- return containsRecursive(root, key);
- }
- Node* add(Node* node, double key)
- {
- if (node == NULL)
- return newNode(key);
- if (key < node->key)
- node->left = add(node->left, key);
- else if (key > node->key)
- node->right = add(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;
- }
- Node * minkeyNode(Node* node)
- {
- Node* current = node;
- while (current->left != NULL)
- current = current->left;
- return current;
- }
- Node* remove(Node* root, double key)
- {
- if (root == NULL)
- return root;
- if ( key < root->key )
- root->left = remove(root->left, key);
- else if( key > root->key )
- root->right = remove(root->right, key);
- else
- {
- if( (root->left == NULL) || (root->right == NULL) )
- {
- Node *temp = root->left ? root->left :
- root->right;
- if (temp == NULL)
- {
- temp = root;
- root = NULL;
- }
- else
- *root = *temp;
- free(temp);
- }
- else
- {
- Node* temp = minkeyNode(root->right);
- root->key = temp->key;
- root->right = remove(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 printRecursive(Node *current)
- {
- if (current == NULL)
- {
- return;
- }
- printRecursive(current->left);
- cout << fixed << setprecision(6) << current->key * 1.0 << " ";
- printRecursive(current->right);
- }
- int main()
- {
- Node* root = NULL;
- string operation;
- double number;
- int N;
- cin >> N;
- cout << fixed;
- for (size_t i = 0; i < N; i++)
- {
- cin >> operation;
- if (operation != "print")
- {
- cin >> number;
- }
- if (operation == "add")
- {
- if(contains(root,number)) cout << number << " already added\n";
- else root = add(root, number);
- }
- else if (operation == "remove")
- {
- if(!contains(root,number)) cout << number << " not found to remove\n";
- else root = remove(root, number);
- }
- else if (operation == "contains")
- {
- if (contains(root, number))
- {
- cout << "yes" << endl;
- }
- else
- {
- cout << "no" << endl;
- }
- }
- else if (operation == "print")
- {
- printRecursive(root);
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement