forked from dremdeveloper/codingtest_cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtree_array.cpp
More file actions
75 lines (64 loc) · 2 KB
/
Copy pathtree_array.cpp
File metadata and controls
75 lines (64 loc) · 2 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
//############################################################
// | cafe | http://cafe.naver.com/dremdelover |
// | Q&A | https://open.kakao.com/o/gX0WnTCf |
// | business | ultrasuperrok@gmail.com |
//############################################################
/*
Tree 구현 알고리즘 (배열 사용):
- 시간 복잡도: O(1) (노드에 접근, 추가, 삭제 등의 기본 연산)
- 용도: 트리를 메모리에 효율적으로 저장하고 접근하는데 사용됩니다.
- 동작 과정:
1. 부모 노드의 인덱스를 i라고 할 때, 왼쪽 자식은 2*i, 오른쪽 자식은 2*i + 1의 인덱스를 가집니다.
2. 이를 기반으로 트리의 노드를 배열에 삽입하거나 접근합니다.
도식화 예:
1
/ \
2 3
/ \ / \
4 5 6 7
위 트리는 [1, 2, 3, 4, 5, 6, 7]의 배열로 표현할 수 있습니다.
*/
#include<iostream>
using namespace std;
class Tree {
private:
int treeArray[100]; // 크기가 100인 정적 배열을 통해 트리를 구현
int size; // 현재 트리의 크기
public:
Tree() {
size = 0;
// 배열 초기화
for(int i=0; i<100; i++)
treeArray[i] = -1;
}
void insert(int data) {
// 루트 노드가 비어있다면,
if(size == 0) {
treeArray[1] = data;
size = 1;
}
// 루트 노드가 차있다면,
else {
treeArray[size+1] = data;
size++;
}
}
void printTree() {
cout << "Tree: ";
for(int i=1; i<=size; i++)
cout << treeArray[i] << " ";
cout << endl;
}
};
int main() {
Tree t;
t.insert(1);
t.insert(2);
t.insert(3);
t.insert(4);
t.insert(5);
t.insert(6);
t.insert(7);
t.printTree(); // 출력: Tree: 1 2 3 4 5 6 7
return 0;
}