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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*
삽입
삭제
탐색
높이
bfs
dfs
*/
 
#include <iostream>
#include <math.h>
#include <stdlib.h>
#include <cstdio>
#include <string.h>
using namespace std;
 
typedef struct tnode {
    int data;
    struct tnode* left;
    struct tnode* right;
    bool check;
}TNode;
 
typedef struct node {
    struct node* Next;
    struct node* prv;
    TNode* data;
}Node;
 
 
struct DataStructure {
    Node* Head;
    Node* Tail;
    virtual void push(TNode* data) = 0;
    virtual void pop() = 0;
    void print() {
        if (Head == nullptr) return;
        Node* temp = Head;
        while (temp) {
            cout << temp->data << ", ";
            temp = temp->Next;
        }
    }
    virtual TNode* back() = 0;
    void Initialize() {
        Head = nullptr;
        Tail = nullptr;
    }
};
 
struct Queue {
    Node* Head;
    Node* Tail;
    
    void initialize() {
        Head = nullptr;
        Tail = nullptr;
    }
 
    void push(TNode* data) {
        if (data == nullptr) return;
        Node* temp = (Node*)malloc(sizeof(Node));
        temp->Next = nullptr;
        temp->prv = nullptr;
        temp->data = data;
 
        if (Head == nullptr && Tail == nullptr) {
            Head = temp;
            Tail = temp;
            Head->Next = Tail;
            Tail->prv = Head;
            return;
        }
        Tail->Next = temp;
        temp->prv = Tail;
        Tail = temp;
    }
 
    void pop() {
        if (Tail == nullptr && Head == nullptr) return;
        if (Head == Tail) {
            Head = nullptr;
            Tail = nullptr;
        }
        else {
            Node* temp = Head;
            Head = Head->Next;
            free(temp);
        }
    }
    TNode* back() {
        if (Tail == nullptr && Head == nullptr) return nullptr;
        return Head->data;
    }
 
    void print() {
        if (Head == nullptr && Tail == nullptr) return;
        Node* temp = Head;
        while (temp) {
            cout << temp->data->data << ", ";
            temp = temp->Next;
        }
        cout << endl;
    }
};
 
struct Stack : DataStructure{
    
    void push(TNode* data) {
        if (data == nullptr) return;
        Node* temp = (Node*)malloc(sizeof(Node));
        temp->Next = nullptr;
        temp->prv = nullptr;
        temp->data = data;
        if (Head == nullptr) {
            Head = temp;
            return;
        }
        temp->Next = Head;
        Head->prv = temp;
        Head = temp;
    }
    void pop() {
        if (Head == nullptr) return;
        Node* temp = Head;
        Head = Head->Next;
        if(Head)
            Head->prv = nullptr;
        free(temp);
    }
    void print() {
        if (Head == nullptr) return;
        Node* temp = Head;
        while (temp) {
            cout << temp->data->data << ", ";
            temp = temp->Next;
        }
    }
 
    TNode* back() {
        if (Head == nullptr) return nullptr;
        return Head->data;
    }
};
 
TNode* createTNode(int data) {
    TNode* temp = (TNode*)malloc(sizeof(TNode));
    temp->left = nullptr;
    temp->right = nullptr;
    temp->data = data;
    temp->check = false;
    return temp;
}
 
TNode* InsertTNode(TNode* root, int data) {
    if (root == nullptr) {
        root = createTNode(data);
        return root;
    }
    
    if (root->data < data) return InsertTNode(root->right, data);
    else return InsertTNode(root->left, data);
 
}
 
void InsertTNode_DoubleP(TNode** root, int data) {
    if (*root == nullptr) {
        *root = createTNode(data);
        return;
    }
    if ((*root)->data > data)
        return InsertTNode_DoubleP(&(*root)->left, data);
    else
        return InsertTNode_DoubleP(&(*root)->right, data);
}
void print(TNode* root) {
    if (root == nullptr) return;
 
    print(root->left);
    cout << root->data << ", ";
    print(root->right);
}
 
void removeTNode(TNode** root, int data) {
    if (*root == nullptr) return;
    TNode* temp = *root;
    TNode* temp2 = nullptr;
    while (temp->data != data) {
        temp2 = temp;
        if (temp->data > data) temp = temp->left;
        else temp = temp->right;
    }
    if (temp == nullptr) return;
        
    if (!temp->left && !temp->right) {
        if (temp2->data > data) temp2->left = nullptr;
        free(temp);
    }
    else if (temp->left && temp->right) {
        TNode* temp3 = temp;
        temp3 = temp3->right;
        TNode* temp4 = nullptr;
        while (temp3->left != nullptr) {
            temp4 = temp3;
            temp3 = temp3->left;
        }
        if (temp3 == temp->right) {
            temp3 = temp->left;
            temp3->left = temp->right;
            if (temp2->data > data) temp2->left = temp3;
            else temp2->right = temp3;
        }
        else {
            temp4->left = nullptr;
            if (temp2->data > data) temp2->left = temp3;
            else temp2->right = temp3;
            temp3->left = temp->left;
            temp3->right = temp->right;
        }
    }
}
//
//int height(TNode* root) {
//    if (root == nullptr) return -1;
//    int level = 0;
//    if (root != nullptr) {
//        level = 1 + fmax(height(root->left), height(root->right));
//    }
//    return level;
//}
 
int height(TNode* root) {
    if (root == nullptr) return -1;
    return 1+ fmax(height(root->left), height(root->right));
}
 
int get_leaf(TNode* root) {
    if (root == nullptr) return 0;
    if (!root->left && !root->right) return 1;
    return get_leaf(root->left) + get_leaf(root->right);
}
 
int get_Count_Node(TNode* root) {
    if (root == nullptr) return 0;
    else return 1 + get_Count_Node(root->left) + get_Count_Node(root->right);
}
 
void BreathFirstSearch(TNode* root) {
    struct Queue q;
    q.initialize();
    q.push(root);
    while (q.Head != nullptr) {
        cout << q.back()->data <<", ";
        q.push(q.back()->left);
        q.push(q.back()->right);
        q.pop();
    }
}
 
void DepthFirstSearch(TNode* root) {
    TNode* temp = root;
    Stack s;
    s.Initialize();
    s.push(temp);
    cout << s.back()->data << ", ";
    while (s.Head != nullptr) {
        while (s.back()->left && !s.back()->left->check) {
            s.back()->left->check = true;
            s.push(s.back()->left);
            
            cout << s.back()->data << ", ";
        }
        if (s.back()->right && !s.back()->right->check) {
            s.back()->right->check = true;
            s.push(s.back()->right);
            cout << s.back()->data << ", ";
        }
        else
            s.pop();
        
    }
 
}
 
 
int main() {
 
    struct Queue q;
    q.initialize();
 
    TNode* root = nullptr;
    InsertTNode_DoubleP(&root, 4);
    InsertTNode_DoubleP(&root, 3);
    InsertTNode_DoubleP(&root, 7);
    InsertTNode_DoubleP(&root, 1);
    InsertTNode_DoubleP(&root, 2);
    InsertTNode_DoubleP(&root, 5);
    InsertTNode_DoubleP(&root, 8);
 
    print(root);
    cout << endl;
    
    cout << "Level : " << height(root) << endl;
    cout << "Count of Leaf : " << get_leaf(root);
    cout << "Get Count Node : " << get_Count_Node(root);
    cout << "Breath First Search : "; BreathFirstSearch(root);
    cout << endl;
    cout << "Depth First Search : "; DepthFirstSearch(root);
    return 0;
}
 
cs


'자료구조' 카테고리의 다른 글

프로그래밍시 list.. index.. stl error  (0) 2018.12.02
후위변환과 그 계산.  (0) 2018.05.26
Double Linked List  (0) 2018.05.26
List 자료구조의 기본적 코드  (0) 2018.05.26
두개의 리스트를 합치는 코드  (0) 2018.05.26

+ Recent posts