1
+ #include " ../headers/TBalanceTree.h"
2
+
3
+ bool TBalanceTree::IsFull ()
4
+ {
5
+ try {
6
+ TNode* tmp = new TNode (" " , 0 );
7
+ delete tmp;
8
+ }
9
+ catch (...) {
10
+ return true ;
11
+ }
12
+ return false ;
13
+ }
14
+
15
+ bool TBalanceTree::IsEmpty ()
16
+ {
17
+ return pRoot == nullptr ;
18
+ }
19
+
20
+ TBalanceTree::TNode* TBalanceTree:: Search(std::string _key, bool & isFound) {
21
+ count_op = 1 ;
22
+ if (IsEmpty ()) return nullptr ;
23
+ if (pRoot->data .key == _key) { isFound = true ; return pRoot; }
24
+ else if ((pRoot->pLeft == nullptr ) && (pRoot->data .key > _key) || (pRoot->pRight == nullptr ) && (pRoot->data .key < _key)) { isFound = false ; return pRoot; }
25
+ TNode* tmp = pRoot;
26
+ while (true ) {
27
+ if (tmp->data .key == _key) { isFound = true ; return tmp; }
28
+ else if ((tmp->pLeft == nullptr ) && (tmp->data .key > _key) || (tmp->pRight == nullptr ) && (tmp->data .key < _key)) { isFound = false ; return tmp; }
29
+ else if (tmp->data .key > _key) tmp = tmp->pLeft ;
30
+ else if (tmp->data .key < _key) tmp = tmp->pRight ;
31
+ count_op++;
32
+ }
33
+ }
34
+
35
+ void TBalanceTree::Push (std::string _key, int _data) {
36
+ bool isFound;
37
+ TNode* tmp = Search (_key, isFound);
38
+ TNode* node = new TNode (_key, _data);
39
+ if (tmp == nullptr ) pRoot = node;
40
+ else if (isFound) {
41
+ tmp->data .data += _data;
42
+ }
43
+ else {
44
+ if (tmp->data .key > _key) tmp->pLeft = node;
45
+ else if (tmp->data .key < _key) tmp->pRight = node;
46
+ }
47
+ }
48
+
49
+ void TBalanceTree::Del (std::string _key) {
50
+ bool isFound;
51
+ TNode* tmp = Search (_key, isFound);
52
+ if (!isFound) return ;
53
+ else {
54
+ if ((tmp->pLeft == nullptr ) && (tmp->pRight == nullptr )) {
55
+ TNode* tmpPrev = GetPrevNode (tmp);
56
+ if (tmpPrev == nullptr ) pRoot = nullptr ;
57
+ else if (tmpPrev->data .key > tmp->data .key ) tmpPrev->pLeft = nullptr ;
58
+ else if (tmpPrev->data .key < tmp->data .key ) tmpPrev->pRight = nullptr ;
59
+ delete tmp;
60
+ }
61
+ else if (tmp->pLeft == nullptr ) {
62
+ TNode* tmpPrev = GetPrevNode (tmp);
63
+ if (tmpPrev == nullptr ) pRoot = tmp->pRight ;
64
+ else if (tmpPrev->data .key > tmp->data .key ) tmpPrev->pLeft = tmp->pRight ;
65
+ else if (tmpPrev->data .key < tmp->data .key ) tmpPrev->pRight = tmp->pRight ;
66
+ delete tmp;
67
+ }
68
+ else if (tmp->pRight == nullptr ) {
69
+ TNode* tmpPrev = GetPrevNode (tmp);
70
+ if (tmpPrev == nullptr ) pRoot = tmp->pLeft ;
71
+ else if (tmpPrev->data .key > tmp->data .key ) tmpPrev->pLeft = tmp->pLeft ;
72
+ else if (tmpPrev->data .key < tmp->data .key ) tmpPrev->pRight = tmp->pLeft ;
73
+ delete tmp;
74
+ }
75
+ else {
76
+ TNode* tmpSheet = tmp;
77
+ tmpSheet = tmpSheet->pLeft ;
78
+ while (tmpSheet->pRight != nullptr ) tmpSheet = tmpSheet->pRight ;
79
+ TNode* tmpPrevSheet = GetPrevNode (tmpSheet);
80
+ if (tmpPrevSheet == tmp) {
81
+ TNode* tmpPrev = GetPrevNode (tmp);
82
+ if (tmpPrev == nullptr ) pRoot = tmpSheet;
83
+ else if (tmpPrev->data .key > tmp->data .key ) tmpPrev->pLeft = tmpSheet;
84
+ else if (tmpPrev->data .key < tmp->data .key ) tmpPrev->pRight = tmpSheet;
85
+ tmpSheet->pRight = tmp->pRight ;
86
+ delete tmp;
87
+ return ;
88
+ }
89
+ tmpPrevSheet->pRight = tmpSheet->pLeft ;
90
+ TNode* tmpPrev = GetPrevNode (tmp);
91
+ if (tmpPrev == nullptr ) pRoot = tmpSheet;
92
+ else if (tmpPrev->data .key > tmp->data .key ) tmpPrev->pLeft = tmpSheet;
93
+ else if (tmpPrev->data .key < tmp->data .key ) tmpPrev->pRight = tmpSheet;
94
+ if (tmp->pLeft != tmpSheet) tmpSheet->pLeft = tmp->pLeft ;
95
+ tmpSheet->pRight = tmp->pRight ;
96
+ delete tmp;
97
+ }
98
+ }
99
+ }
100
+
101
+ void TBalanceTree::PrintLTR (const TBalanceTree::TNode* _node) {
102
+ if (_node == nullptr ) return ;
103
+ PrintLTR (_node->pLeft );
104
+ std::cout << _node->data .key << " (" << _node->data .data << " )" << std::endl;
105
+ PrintLTR (_node->pRight );
106
+ }
107
+
108
+ void TBalanceTree::Print () {
109
+ PrintLTR (pRoot);
110
+ }
111
+
112
+ TBalanceTree::TNode* TBalanceTree::GetPrevNode (const TBalanceTree::TNode* _node) {
113
+ if (_node == pRoot) return nullptr ;
114
+ TNode* res = pRoot;
115
+ while (true ) {
116
+ if ((res->pLeft == nullptr ) && (res->data .key > _node->data .key ) || (res->pRight == nullptr ) && (res->data .key < _node->data .key )) return nullptr ;
117
+ if ((res->pLeft == _node) || (res->pRight == _node)) return res;
118
+ else if (res->data .key > _node->data .key ) res = res->pLeft ;
119
+ else if (res->data .key < _node->data .key ) res = res->pRight ;
120
+ }
121
+ }
0 commit comments