-
Notifications
You must be signed in to change notification settings - Fork 0
/
pennant.cpp
91 lines (80 loc) · 1.64 KB
/
pennant.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <stack>
#include "node.h"
#include "pennant.h"
Pennant::Pennant(Node* root) {
this->root = root;
}
Pennant::~Pennant() {
if (this->root != NULL) {
std::stack<Node *> nodes;
nodes.push(this->root);
while (nodes.size() > 0) {
Node *current = nodes.top();
nodes.pop();
if (current->left != NULL) {
nodes.push(current->left);
}
if (current->right != NULL) {
nodes.push(current->right);
}
delete current;
}
}
}
void Pennant::p_merge(Pennant *&y) {
if (y == NULL) return;
y->root->right = this->root->left;
this->root->left = y->root;
y->root = NULL;
delete y;
y = NULL;
}
Pennant* Pennant::p_split() {
if (this->root->left == NULL) {
return NULL;
}
Pennant *y = new Pennant(this->root->left);
this->root->left = y->root->right;
y->root->right = NULL;
return y;
}
void Pennant::FA(Pennant *&x, Pennant *&y, Pennant *&carry) {
if (x == NULL && y == NULL && carry == NULL) {
return;
}
if (x != NULL && y == NULL && carry == NULL) {
return;
}
if (x == NULL && y != NULL && carry == NULL) {
x = y;
y = NULL;
return;
}
if (x == NULL && y == NULL && carry != NULL) {
x = carry;
carry = NULL;
return;
}
if (x != NULL && y != NULL && carry == NULL) {
x->p_merge(y);
carry = x;
x = NULL;
return;
}
if (x != NULL && y == NULL && carry != NULL) {
x->p_merge(carry);
carry = x;
x = NULL;
return;
}
if (x == NULL && y != NULL && carry != NULL) {
y->p_merge(carry);
carry = y;
y = NULL;
return;
}
if (x != NULL && y != NULL && carry != NULL) {
carry->p_merge(y);
return;
}
}