Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void Insert(NOD<T>* currentNode,const T &dataToInsert,NOD<T>* previous,int direction)
- {
- if (root == NULL)
- {
- NOD<T>* NodeToInsert = new NOD<T>(dataToInsert);
- root = NodeToInsert;
- return;
- }
- if (currentNode == NULL)
- {
- NOD<T>* NodeToInsert = new NOD<T>(dataToInsert);
- NodeToInsert->par = previous;
- if (direction == 1)
- previous->st = NodeToInsert;
- else
- previous->dr = NodeToInsert;
- Insert_Repair(NodeToInsert);
- return;
- }
- else
- if (dataToInsert < currentNode->data)
- {
- direction = 1;
- Insert(currentNode->st, dataToInsert,currentNode,direction);
- currentNode->setHeight();
- }
- else
- {
- direction = 2;
- Insert(currentNode->dr, dataToInsert,currentNode,direction);
- currentNode->setHeight();
- }
- }
- void Insert_Repair(NOD<T>* currentNode)
- {
- if (currentNode->par == nullptr)
- return;
- NOD<T>* x = currentNode->par;
- x->setBalance();
- if (x->balance == 0)
- return;
- else
- if (x->balance == 1 || x->balance == -1)
- Insert_Repair(x);
- else
- {
- if (x->balance == -2)
- {
- if (x->st->balance == -1)
- {
- NOD<T>* y = x->st;
- right_Rotation(x);
- x->setHeight();
- y->setHeight();
- x->setBalance();
- y->setBalance();
- }
- else
- {
- NOD<T>* y = x->st;
- NOD <T>* yd = y->dr;
- Left_Rotation(y);
- right_Rotation(x);
- x->setHeight();
- y->setHeight();
- yd->setHeight();
- x->setBalance();
- y->setBalance();
- yd->setBalance();
- }
- }
- else
- {
- if (x->dr->balance == 1)
- {
- NOD<T>* y = x->dr;
- Left_Rotation(x);
- x->setHeight();
- y->setHeight();
- x->setBalance();
- y->setBalance();
- }
- else
- {
- NOD<T>* y = x->dr;
- NOD<T>* yd = y->st;
- right_Rotation(y);
- Left_Rotation(x);
- x->setHeight();
- y->setHeight();
- yd->setHeight();
- x->setBalance();
- y->setBalance();
- yd->setBalance();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement