Repository navigation
Expand file tree
/
Copy pathpractic.cpp
More file actions
278 lines (233 loc) · 5.72 KB
/
Copy pathpractic.cpp
File metadata and controls
278 lines (233 loc) · 5.72 KB
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
#include <iostream>
using namespace std;
// Define your node structure
struct node {
string name;
double score;
node* left;
node* right;
// Constructor for convenience
node(string n, double s) : name(n), score(s), left(nullptr), right(nullptr) {}
};
// Custom stack-like structure for node pointers
class MyStack {
private:
node** arr;
int capacity;
int top_index;
public:
MyStack(int cap) : capacity(cap), top_index(-1) {
arr = new node*[capacity];
}
~MyStack() {
delete[] arr;
}
bool isEmpty() const {
return top_index == -1;
}
void push(node* item) {
if (top_index < capacity - 1) {
arr[++top_index] = item;
}
}
node* pop() {
if (!isEmpty()) {
return arr[top_index--];
}
return nullptr;
}
};
// Define your tree class
class my_tree {
private:
int node_count;
node* root;
public:
my_tree() : node_count(0), root(nullptr) {}
int insert_root(node t);
int insert_left(string tname, node t);
int insert_right(string tname, node t);
double score_sum();
double score_average();
void print_data_inorder();
void print_data_preorder();
void print_data_postorder();
void nonrecursive_inorder();
void print_data_levelorder();
};
void my_tree::nonrecursive_inorder() {
if (root == nullptr) {
return;
}
MyStack stack(100); // Custom stack with capacity
node* current = root;
while (current != nullptr || !stack.isEmpty()) {
// Reach the leftmost node of the current subtree
while (current != nullptr) {
stack.push(current);
current = current->left;
}
// Current is now null, pop the top element from the stack
current = stack.pop();
// Print the current node's data
cout << current->name << " : " << current->score << endl;
// Move to the right child
current = current->right;
}
}
// Your other member functions (insert, print, etc.) go here
int main() {
// Example usage
my_tree tree;
// Build your tree using insert methods
// Traverse in-order non-recursively
tree.nonrecursive_inorder();
return 0;
}
/*
Equality Test
#include <iostream>
struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};
------
bool isSameTree(TreeNode* p, TreeNode* q) {
// 두 트리가 모두 빈 트리인 경우
if (p == nullptr && q == nullptr)
return true;
// 한 트리만 빈 트리인 경우
if (p == nullptr || q == nullptr)
return false;
// 현재 노드의 값이 다른 경우
if (p->val != q->val)
return false;
// 왼쪽 서브트리와 오른쪽 서브트리 재귀적으로 비교
return isSameTree(p->left, q->left) && isSameTree(p->right, q->right);
}
------
int main() {
TreeNode* p = new TreeNode(1);
p->left = new TreeNode(2);
p->right = new TreeNode(3);
TreeNode* q = new TreeNode(1);
q->left = new TreeNode(2);
q->right = new TreeNode(3);
bool result = isSameTree(p, q);
std::cout << "Two trees are " << (result ? "same." : "different.") << std::endl;
return 0;
}
*/
/*
Node *root = NULL;
void Treecreate()
{
Node *p, *t;
Queue q;
create(&q, 100);
int x;
printf("Enter root value");
scanf("%d", &x);
root = new Node;
root->data = x;
root->lchild = root->rchild = NULL;
enqueue(&q, root);
while(!isEmpty(q))
{
p = dequeue(&q);
printf("Enter left child: %d", p->data);
scanf("%d", &x);
if (x != -1)
{
t = new Node;
t->data = x;
t->lchild = t->rchild = NULL;
p->lchild = t;
enqueue(&q, t);
}
printf("Enter right child: %d", p->data);
scanf("%d", &x);
if (x != -1)
{
t = new Node;
t->data = x;
t->lchild = t->rchild = NULL;
p->rchild = t;
enqueue(&q, t);
}
}
}
void preorder(Node *p)
{
if (p)
{
printf("%d", p->data);
preorder(p->lchild);
preorder(p->rchild);
}
}
void postorder(Node *p)
{
if (p)
{
postorder(p->lchild);
postorder(p->rchild);
printf("%d ", p->data);
}
}
void inorder(Node *p)
{
if (p)
{
inorder(p->lchild);
printf("%d ", p->data);
inorder(p->rchild);
}
}
*/
/*
노드제거
Node * BinarySearchTree::removeSequence(Node *node, int _value){
if(node==nullptr)return node;
else if(node -> value > _value){
node -> left=removeSequence(node->left,_value);
}else if(node -> value <_value){
node -> right = removeSequence(node -> right, _value);
}else{
Node *ptr =node;
if(node -> right==nullptr&&node -> left ==nullptr){
delete node;
node=nullptr;
}else if(node -> right==nullptr){
node=node -> right;
delete ptr;
}else{
ptr=searchMaxNode(node->left);
node -> value = ptr -> value;
node -> left = removeSequence(node -> left, ptr -> value);
}
}
return node;
}
void BinarySearchTree::removeNode(int value){
Node *ptr = root;
removeSequence(ptr,value);
}
값 찾기
bool BinarySearchTree::searchValue(int value){
Node *ptr= root;
Node *tmpRoot =nullptr;
while(ptr!=nullptr){
if(ptr->value==value){
cout<<"FOund"<<endl;
return true;
}else if(ptr -> value > value)
ptr=ptr->left;
else ptr=ptr->right;
}
cout<<"not found"<<endl;
return false;
}
*/