Rafael Arce Nazario 5 vuotta sitten
commit
8aec17f94a
2 muutettua tiedostoa jossa 337 lisäystä ja 0 poistoa
  1. 0
    0
      README.md
  2. 337
    0
      main.cpp

+ 0
- 0
README.md Näytä tiedosto


+ 337
- 0
main.cpp Näytä tiedosto

@@ -0,0 +1,337 @@
1
+#define CATCH_CONFIG_MAIN  // This tells Catch to provide a main() - only do this in one cpp file
2
+#include "catch.hpp"
3
+#include <queue>
4
+
5
+#include <iostream>
6
+using namespace std;
7
+
8
+class BTNode {
9
+public:
10
+    int key;
11
+    BTNode *left, *right;
12
+    BTNode(int k = 0, BTNode *l = NULL, BTNode *r = NULL) : key(k), left(l), right(r) {}
13
+};
14
+
15
+class BST {
16
+private:
17
+    BTNode *root;
18
+
19
+public:
20
+    BST(): root(NULL) {}
21
+    void insert(int k);
22
+    void insertRec(int k);
23
+    void insertRec(int k, BTNode *r);
24
+
25
+    void remove(int k);
26
+    void remove(int k, BTNode *r);
27
+    void recDisplay(ostream &out) const;
28
+    void recDisplay(ostream &out, BTNode *cur, int dist) const;
29
+
30
+    BTNode *search(int k) const;
31
+    BTNode *searchRec(int k) const;
32
+    BTNode *searchRec(int k, BTNode *r) const;
33
+
34
+
35
+    int getHeight() const;
36
+    int getHeight(BTNode *r) const;
37
+    int sum() const;
38
+    int sum(BTNode *r) const;
39
+    int leafQty() const;
40
+    int leafQty(BTNode *n) const;
41
+    int parentsOfTwo() const;
42
+    int parentsOfTwo(BTNode *n) const;
43
+
44
+    string InOrder() const;
45
+    void InOrder(BTNode *n, string &st) const;
46
+
47
+    string BFS() const;
48
+};
49
+
50
+
51
+
52
+// function search:
53
+//    Given k, a key to search, returns a pointer to the first
54
+//    node that contains such key. If not found returns a NULL pointer.
55
+
56
+BTNode* BST::search(int k) const {
57
+    BTNode* cur = root;
58
+    while (cur) {
59
+        if (k == cur->key)     {return cur;}
60
+            // Found
61
+        else if (k < cur->key) { cur = cur->left; }
62
+        else                   { cur = cur->right; }
63
+    }
64
+    return NULL;
65
+}
66
+
67
+
68
+// function insert:
69
+//    Given k, a key to insert, inserts into the BST.
70
+//    In this implementation if the key already exists in the tree
71
+//    it will be inserted in the RIGHT subtree of the existing key.
72
+
73
+void BST::insert(int k) {
74
+    BTNode *cur;
75
+    BTNode *n = new BTNode(k);
76
+
77
+    if (!root) {
78
+        // This tree is empty, the new node is the root!
79
+        root = n;
80
+    }
81
+    else {
82
+        // Lets search the tree for a place to insert....
83
+        cur = root;
84
+        while (cur) {
85
+            if (k < cur->key){
86
+                // The value to insert is less than current node
87
+                if (cur->left == NULL) {
88
+                    // If we have reached a node who lacks a left child
89
+                    // then our new node will be the left child
90
+                    cur->left = n;
91
+                    cur = NULL;
92
+                }
93
+                else {
94
+                    // keep looking for a place to insert, on the left subtree
95
+                    cur = cur->left;
96
+                }
97
+            }
98
+            else {
99
+                // The value to insert is greater or equal than current node
100
+                if (cur->right == NULL){
101
+                    // If we have reached a node who lacks a right child
102
+                    // then our new node will be the right child
103
+                    cur->right = n;
104
+                    cur = NULL;
105
+                }
106
+                else {
107
+                    // keep looking for a place to insert, on the right subtree
108
+                    cur = cur->right;
109
+                }
110
+            }
111
+        }
112
+    }
113
+}
114
+
115
+// functions recDisplay:
116
+//    Print the tree to the standard output.
117
+
118
+void BST::recDisplay(ostream &out) const {
119
+    recDisplay(out, root, 0);
120
+}
121
+
122
+void BST::recDisplay(ostream &out, BTNode *cur, int dist) const{
123
+    if (cur) {
124
+        if (cur->right) recDisplay(out, cur->right, dist + 1);
125
+        for (int i = 0; i < dist; i++) cout << "    ";
126
+        out << cur->key << endl;
127
+        if (cur->left) recDisplay(out, cur->left, dist + 1);
128
+    }
129
+}
130
+
131
+// functions remove:
132
+//    Given k, a key to remove, removes the first node that it finds with
133
+//    such key.
134
+
135
+
136
+void BST::remove(int k) {
137
+    remove(k, root);
138
+}
139
+
140
+void BST::remove(int k, BTNode *r) {
141
+    BTNode *parent = NULL;
142
+    BTNode *cur = r;
143
+    while (cur) { // Search for node
144
+        if (cur->key == k) { // Node found
145
+            if (cur->left == NULL && cur->right == NULL) {        // Remove leaf
146
+                cout << "cur is a leaf: " << cur->key << endl;
147
+                if (!parent) { // Node is root
148
+                    root = NULL;
149
+                }
150
+                else if (parent->left == cur)
151
+                    parent->left = NULL;
152
+                else
153
+                    parent->right = NULL;
154
+                delete cur;
155
+            }
156
+            else if (cur->left && cur->right == NULL) {    // Remove node with only left child
157
+                if (!parent) // Node is root
158
+                    root = cur->left;
159
+                else if (parent->left == cur)
160
+                    parent->left = cur->left;
161
+                else
162
+                    parent->right = cur->left;
163
+                delete cur;
164
+            }
165
+            else if (cur->left == NULL && cur->right) {    // Remove node with only right child
166
+                if (!parent) // Node is root
167
+                    root = cur->right;
168
+                else if (parent->left == cur)
169
+                    parent->left = cur->right;
170
+                else
171
+                    parent->right = cur->right;
172
+                delete cur;
173
+            }
174
+            else {                                  // Remove node with two children
175
+                // Find successor (leftmost child of right subtree)
176
+                BTNode *suc = cur->right;
177
+
178
+                while (suc->left )
179
+                    suc = suc->left;
180
+                int successorData = suc->key;
181
+                remove(suc->key, cur);     // Remove successor
182
+                cur->key = successorData;
183
+            }
184
+            return; // Node found and removed
185
+        }
186
+        else if (cur->key < k) { // Search right
187
+            parent = cur;
188
+            cur = cur->right;
189
+        }
190
+        else {                     // Search left
191
+            parent = cur;
192
+            cur = cur->left;
193
+        }
194
+    }
195
+    return; // Node not found
196
+}
197
+
198
+
199
+// function getHeight:
200
+//    Returns the height of the tree.
201
+
202
+int  BST::getHeight() const {
203
+    return getHeight(root);
204
+}
205
+
206
+int  BST::getHeight(BTNode *r) const {
207
+    if (!r) return -1;
208
+
209
+    int leftHeight = getHeight(r->left);
210
+    int rightHeight = getHeight(r->right);
211
+    return 1 + max(leftHeight, rightHeight);
212
+}
213
+
214
+// function sum:
215
+//    Returns the sum of all keys in the tree.
216
+
217
+int BST::sum() const { return sum(root);}
218
+
219
+int BST::sum(BTNode *n) const {
220
+    if (!n) return 0;
221
+    else return n->key + sum(n->left) + sum(n->right);
222
+}
223
+
224
+
225
+// function leafQty:
226
+//    Returns the number of leaves in the tree.
227
+
228
+int BST::leafQty() const { return leafQty(root);}
229
+
230
+int BST::leafQty(BTNode *n) const {
231
+    if (!n) return 0;
232
+    if (n->left == NULL && n->right == NULL) return 1;
233
+    else return leafQty(n->left) + leafQty(n->right);
234
+}
235
+
236
+// function parentsOfTwo:
237
+//    Returns the number of nodes that are parents of two children.
238
+
239
+int BST::parentsOfTwo() const {
240
+    return parentsOfTwo(root);
241
+}
242
+int BST::parentsOfTwo(BTNode *n) const {
243
+    if (!n || (!n->left && !n->right)) return 0;
244
+    int resultLeft = parentsOfTwo(n->left);
245
+    int resultRight = parentsOfTwo(n->right);
246
+    if (n->left && n->right) return 1 + resultLeft + resultRight;
247
+    else return resultLeft + resultRight;
248
+}
249
+
250
+// function InOrder:
251
+//    Will return a string containing the sequence of visited keys
252
+//    during an in-order traversal (BFS) of the tree.
253
+
254
+string BST::InOrder() const {
255
+    string st;
256
+    InOrder(root, st);
257
+    return st;
258
+}
259
+void BST::InOrder(BTNode *n, string &st) const {
260
+    if (n) {
261
+        InOrder(n->left, st);
262
+        st = st + to_string(n->key) + " ";
263
+        InOrder(n->right, st);
264
+    }
265
+}
266
+
267
+// function BFS:
268
+//    Will return a string containing the sequence of visited keys
269
+//    during a breadth-first traversal of the tree.
270
+
271
+string BST::BFS() const {
272
+    string st;
273
+    queue<BTNode*> q;
274
+
275
+    if(root!=NULL) q.push(root);
276
+
277
+    while(!q.empty()) {
278
+        BTNode *f = q.front();
279
+        st.append(to_string(f->key));
280
+        st.append(" ");
281
+        if (f->left  != NULL) q.push(f->left);
282
+        if (f->right != NULL) q.push(f->right);
283
+        q.pop();
284
+    }
285
+
286
+    return st;
287
+}
288
+
289
+
290
+// function searchRec:
291
+//    A recursive version of the search algorithm.
292
+
293
+BTNode* BST::searchRec(int k) const { return searchRec(k,root); }
294
+
295
+BTNode* BST::searchRec(int k, BTNode *r) const {
296
+    if (r) {
297
+        if (k == r->key) return r;
298
+        else if (k < r->key)
299
+            return searchRec(k, r->left);
300
+        else
301
+            return searchRec(k, r->right);
302
+    }
303
+    return NULL;
304
+}
305
+
306
+// function insertRec:
307
+//    A recursive version of the insert algorithm.
308
+
309
+void BST::insertRec(int k) {
310
+    if (!root) root = new BTNode(k);
311
+    else insertRec(k, root);
312
+}
313
+void BST::insertRec(int k, BTNode *r) {
314
+    if (k < r->key) {
315
+        if (r->left == NULL) r->left = new BTNode(k);
316
+        else insertRec(k, r->left);
317
+    }
318
+    else { // k is greater than r->key
319
+        if (r->right == NULL) r->right = new BTNode(k);
320
+        else insertRec(k, r->right);
321
+    }
322
+}
323
+
324
+
325
+TEST_CASE( "BST is tested", "[BST]" ) {
326
+    vector<int> v = {8, 9, 5, 2, 4, 10, 1};
327
+    BST B;
328
+    for (auto e: v) B.insert(e);
329
+    REQUIRE (B.InOrder() == "1 2 4 5 8 9 10 ");
330
+    REQUIRE (B.BFS() == "8 5 9 2 10 1 4 ");
331
+
332
+
333
+//    REQUIRE( infix2Postfix("(3 + 4) * 9") == "3 4 + 9 *" );
334
+//    REQUIRE( infix2Postfix("3 + 4 * 9") == "3 4 9 * +" );
335
+//    REQUIRE( infix2Postfix("3 + 4 * (9 + 5)") == "3 4 9 5 + * +" );
336
+//    REQUIRE( infix2Postfix("(3 + 4) * (9 + 5)") == "3 4 + 9 5 + *" );
337
+}