Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stack>
- using namespace std;
- class map {
- public:
- class node {
- public:
- node* right;
- node* left;
- node* parent;
- int width;
- int key;
- int presure;
- node(int key, node* parent, int width)
- : right(nullptr), left(nullptr), parent(parent), key(key), width(width) {}
- };
- node* root;
- map()
- :root(nullptr) {}
- void push(int key, node* parent, int width)
- {
- if (key > parent->key)
- {
- if (parent->right == nullptr)
- {
- parent->right = new node(key, parent, width);
- }
- else
- {
- push(key, parent->right, width);
- }
- }
- else if (key < parent->key)
- {
- if (parent->left == nullptr)
- {
- parent->left = new node(key, parent, width);
- }
- else
- {
- push(key, parent->left, width);
- }
- }
- }
- void push(int key, int width)
- {
- if (root == nullptr)
- {
- root = new node(key, nullptr, width);
- }
- else
- {
- push(key, root, width);
- }
- }
- static node* findLeft(node* parent)
- {
- node* temp = parent;
- while (temp->left)
- {
- temp = temp->left;
- }
- return temp;
- }
- static node* findRight(node* parent)
- {
- node* temp = parent;
- while (temp->right)
- {
- temp = temp->right;
- }
- return temp;
- }
- class iterator {
- public:
- node* current;
- std::stack<node*> stack;
- std::stack<node*> stack_minus;
- iterator(node* n)
- : current(n) {}
- node operator *()
- {
- if (current->width > 20) {
- current->presure = 50;
- }
- else {
- current->presure = 35;
- }
- return *current;
- }
- bool operator !=(iterator other)
- {
- return current != other.current;
- }
- void findNode()
- {
- if (current->right)
- {
- stack.push(current);
- current = map::findLeft(current->right);
- }
- else
- {
- current = current->parent;
- if (!stack.empty())
- {
- while (current == stack.top())
- {
- current = stack.top()->parent;
- stack.pop();
- if (stack.empty())
- {
- break;
- }
- }
- }
- }
- }
- void findNode_Minus()
- {
- if (current->left)
- {
- stack_minus.push(current);
- current = map::findRight(current->left);
- }
- else
- {
- current = current->parent;
- if (!stack_minus.empty())
- {
- while (current == stack_minus.top())
- {
- current = stack_minus.top()->parent;
- stack_minus.pop();
- if (stack_minus.empty())
- {
- break;
- }
- }
- }
- }
- }
- iterator& operator ++()
- {
- findNode();
- return *this;
- }
- iterator& operator ++(int)
- {
- findNode();
- return *this;
- }
- iterator& operator --()
- {
- findNode_Minus();
- return *this;
- }
- iterator& operator --(int)
- {
- findNode_Minus();
- return *this;
- }
- };
- iterator begin()
- {
- return iterator(findLeft(root));
- }
- iterator end()
- {
- return iterator(findRight(root));
- }
- };
- int main()
- {
- map s1;
- /*
- s1.push(1, 120);
- s1.push(2, 40);
- s1.push(3, 300);
- s1.push(4, 11);
- */
- int w;
- for (int i = 1; i < 5; i++) {
- cout << "Enter width" << endl;
- cin >> w;
- s1.push(i, w);
- }
- for (map::iterator i = s1.begin(); i != NULL; i++)
- {
- map::node n = *i;
- cout << " key: " << n.key << " width: " << n.width << " pressure: " << n.presure << endl;
- }
- for (map::iterator i = s1.end(); i != NULL; i--)
- {
- map::node n = *i;
- cout << " key: " << n.key << " width: " << n.width << " pressure: " << n.presure << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement