Skip to content

Commit 8705adb

Browse files
authored
docs : 다른 레포에 저장해 놓은 자료 가져오기 (#2)
1 parent 5d698a7 commit 8705adb

34 files changed

+4052
-0
lines changed

AVL Tree/AVL 트리 실전 예제.md

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# AVL 트리 실전 예제
2+
AVL 트리 실전적인 풀이에서 신경 써야 할 부분은 딱 개이다. <br>
3+
1. RL, LR 등의 판단은 처음 BF가 깨진 곳에서 판단.
4+
2. **BF가 깨진 곳이 부모-자식간에 이어서 여러 곳에서 발생했다면, 아래쪽을 선택해서 재분배한다**
5+
3. LR, RL 회전시 그냥 세 노드에 대해 **중간 값 노드를 루트로 해서, 그 노드를 기준으로 다시 서브트리를 만든다.**
6+
7+
## 예제 1
8+
{MAR, MAY, NOV, AUG, APR, JAN, DEC, JUL, FEB, JUN, OCT, SEP} <br>
9+
10+
기본적으로 책에서 제공된 순서이다. 여러번 연습했다.
11+
![avl1](https://user-images.githubusercontent.com/71186266/206129643-ca66c1e0-50c5-443c-929d-ced7d696c8d9.png)
12+
13+
14+
15+
1. 첫 LR 회전이다. **BF가 깨진 루트를 기준으로** LR로 판단한 것을 확인할 수 있다.
16+
2. MAY, AUG, MAR에 대해 중간값인 MAR이 루트가 되어 새로 트리가 짜인 것을 확인할 수 있다.
17+
3. 왜 MAY 부터 AUG, MAR로 골랐는가? **깨진 곳이 기준이니까**
18+
19+
20+
21+
#### 여기도 중요하다. BF 깨진 곳이 두 곳!
22+
![avl2](https://user-images.githubusercontent.com/71186266/206129645-9c719999-f566-4bd7-9c94-e55f822de919.png)
23+
24+
25+
1. **위에서 언급한 것 처럼 MAR, AUG 둘 다 깨졌지만, AUG를 기준으로 회전한다.**
26+
2. **둘 다 깨졌으면 자식쪽!!!** 그래서 **RL회전이라고 부른다!!**
27+
3. AUG, JAN, DEC를 기준으로 회전하는데, 중간값인 DEC를 루트로 새로 서브트리들을 작성한다
28+
29+
![avl3](https://user-images.githubusercontent.com/71186266/206129648-9db227a8-c645-4abb-8f02-f94b46bb72ea.png)
30+
31+
완성된 모습..
32+
33+
## 예제 2
34+
{'NOV', 'APR', 'OCT', 'MAY', 'DEC', 'AUG', 'SEP', 'JAN', 'JUL', 'JUN', 'MAR', 'FEB'}의 순서로 입력이 들어온 상황. <br>
35+
36+
![KakaoTalk_20221207_172605855](https://user-images.githubusercontent.com/71186266/206127258-74df178f-0901-4c4e-87e6-7b09e65a4db5.jpg)
37+
![RET3](https://user-images.githubusercontent.com/71186266/206127250-103e32e1-b55d-4036-9b40-91298f5261d8.png)
38+
39+
위의 사진은 직접 그려본 것이고, 아래 사진은 visualizer를 사용한 결과이다. 같은 key값의 집합에 대해, 같은 결과를 만든 것을 확인할 수 있다.
40+
41+
### 결론:
42+
1. 같은 정책 하의 같은 key 구성이여도 입력 순서가 다르면, 트리가 다를 수 있다.
43+
2. 동일 key값이 존재한다면 동일 key값 처리 정책에 따라 트리가 달라질 수 있다.
44+
45+
## Reference
46+
- Fundamentals of Data Structures in C++ \<HOROWITZ, SAHNI, MEHTA 저>
47+
- [AVL Tree Visualizer](https://www.cs.usfca.edu/~galles/visualization/AVLtree.html)

AVL Tree/코드/AVL.cpp

+139
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
//
2+
// Created by Jinho on 12/14/2022.
3+
//
4+
#include "AVL.h"
5+
6+
Node::Node(int value) {
7+
data = value;
8+
leftChild = rightChild = NULL;
9+
height = 1;
10+
balanceFactor = 0;
11+
}
12+
13+
void bstree::visit(Node* ptr) {
14+
cout << ptr->data << "\t";
15+
if (ptr->leftChild) cout << "left : " << ptr->leftChild->data << '\t';
16+
else cout << "left : empty\t";
17+
if (ptr->rightChild) cout << "right : " << ptr->rightChild->data << '\t';
18+
else cout << "right : empty\t";
19+
cout << '\n';
20+
}
21+
22+
void bstree::show(Node* root) {
23+
cout << '\n';
24+
queue<Node*> bfsQueue;
25+
bfsQueue.push(root);
26+
Node *nodeNow;
27+
while (!bfsQueue.empty()) {
28+
nodeNow = bfsQueue.front();
29+
bfsQueue.pop();
30+
visit(nodeNow);
31+
if (nodeNow->leftChild) bfsQueue.push(nodeNow->leftChild);
32+
if (nodeNow->rightChild) bfsQueue.push(nodeNow->rightChild);
33+
}
34+
}
35+
36+
Node *bstree::insert(const int value, Node *&nodeNow) {
37+
if (!nodeNow) return nodeNow = new Node(value);
38+
if (value == nodeNow->data) return nodeNow;
39+
if (value < nodeNow->data) nodeNow->leftChild = insert(value, nodeNow->leftChild);
40+
if (value > nodeNow->data) nodeNow->rightChild = insert(value, nodeNow->rightChild);
41+
42+
setBF(nodeNow);
43+
if (nodeNow->balanceFactor < -1 || nodeNow->balanceFactor > 1) return rotation(nodeNow, value);
44+
return nodeNow;
45+
}
46+
47+
bool bstree::search(const int key, Node* root) {
48+
cout << root->data;
49+
50+
if (root->data == key) {
51+
cout << '\n';
52+
return true;
53+
}
54+
55+
cout << " -> ";
56+
57+
Node *nodeNext = key <= root->data ? root->leftChild : root->rightChild;
58+
if (!nodeNext) {
59+
cout << " X (there is no nodes that has value " << key << ") \n";
60+
return false;
61+
}
62+
63+
return search(key, nodeNext);
64+
}
65+
66+
Node *bstree::del(const int key, Node* nodeNow) {
67+
if (!nodeNow) return nodeNow;
68+
if (key < nodeNow->data) nodeNow->leftChild = del(key, nodeNow->leftChild);
69+
else if (key > nodeNow->data) nodeNow->rightChild = del(key, nodeNow->rightChild);
70+
else {
71+
// find the node
72+
if (!nodeNow->leftChild && !nodeNow->rightChild) nodeNow = NULL;
73+
else if (!nodeNow->leftChild) nodeNow = nodeNow->rightChild;
74+
else if (!nodeNow->rightChild) nodeNow = nodeNow->leftChild;
75+
else {
76+
Node *ptr = nodeNow->leftChild;
77+
while (ptr->rightChild) {
78+
ptr = ptr->rightChild;
79+
}
80+
nodeNow->data = ptr->data;
81+
del(nodeNow->data, nodeNow->leftChild);
82+
}
83+
}
84+
if (!nodeNow) return nodeNow;
85+
86+
setBF(nodeNow);
87+
if (nodeNow->balanceFactor < -1 || nodeNow->balanceFactor > 1) return rotation(nodeNow, key);
88+
return nodeNow;
89+
}
90+
91+
void bstree::setBF(Node* startNode) {
92+
int leftHeight, rightHeight;
93+
leftHeight = startNode->leftChild ? startNode->leftChild->height : 0;
94+
rightHeight = startNode->rightChild ? startNode->rightChild->height : 0;
95+
startNode->height = leftHeight < rightHeight ? rightHeight + 1 : leftHeight + 1;
96+
startNode->balanceFactor = leftHeight - rightHeight;
97+
}
98+
99+
Node *bstree::rotation(Node* startNode, int value) {
100+
// 1. LL rotation
101+
Node *tempNode;
102+
if (startNode->leftChild && value <= startNode->leftChild->data) {
103+
tempNode = startNode->leftChild;
104+
rotation(startNode, startNode->leftChild);
105+
return tempNode;
106+
}
107+
108+
// 2. RR rotation
109+
if (startNode->rightChild && startNode->rightChild->data < value) {
110+
tempNode = startNode->rightChild;
111+
rotation(startNode, startNode->rightChild);
112+
return tempNode;
113+
}
114+
115+
// 3. LR rotation
116+
if (value <= startNode->data) {
117+
tempNode = startNode->leftChild->rightChild;
118+
rotation(startNode->leftChild, startNode->leftChild->rightChild);
119+
rotation(startNode, startNode->leftChild);
120+
}
121+
122+
// 4. RL rotation
123+
if (startNode->data < value) {
124+
tempNode = startNode->rightChild->leftChild;
125+
rotation(startNode->rightChild, startNode->rightChild->leftChild);
126+
rotation(startNode, startNode->rightChild);
127+
}
128+
return tempNode;
129+
}
130+
131+
void bstree::rotation(Node* start, Node* end) {
132+
if (start->leftChild == end) {
133+
start->leftChild = end->rightChild;
134+
end->rightChild = start;
135+
} else {
136+
start->rightChild = end->leftChild;
137+
end->leftChild = start;
138+
}
139+
}

AVL Tree/코드/AVL.h

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//
2+
// Created by Jinho on 12/14/2022.
3+
//
4+
5+
#ifndef AVL_H
6+
#define AVL_H
7+
typedef struct Node* nodeptr;
8+
#include <queue>
9+
#include <iostream>
10+
using namespace std;
11+
struct Node {
12+
int data, height, balanceFactor;
13+
Node *leftChild, *rightChild;
14+
Node(int value);
15+
};
16+
17+
class bstree {
18+
public:
19+
void visit(Node* ptr);
20+
Node *insert(const int value, Node*& root);
21+
void show(Node* root);
22+
bool search(const int key, Node* root);
23+
Node *del(const int key, Node* root);
24+
void setBF(Node* startNode);
25+
void rotation(Node* start, Node* end);
26+
Node *rotation(Node* startNode, int value);
27+
};
28+
29+
#endif //ALGORITHEM_AVL_H

AVL Tree/코드/hw12.cpp

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#include "AVL.h"
2+
int main() {
3+
nodeptr root;
4+
int a, choice, findele, delele;
5+
bstree bst;
6+
bool flag = true;
7+
root = NULL;
8+
bst.insert(19, root);
9+
bst.insert(10, root);
10+
bst.insert(46, root);
11+
bst.insert(4, root);
12+
bst.insert(14, root);
13+
bst.insert(37, root);
14+
bst.insert(55, root);
15+
bst.insert(7, root);
16+
bst.insert(12, root);
17+
bst.insert(18, root);
18+
bst.insert(28, root);
19+
bst.insert(40, root);
20+
bst.insert(51, root);
21+
bst.insert(61, root);
22+
bst.insert(21, root);
23+
bst.insert(32, root);
24+
bst.insert(49, root);
25+
bst.insert(58, root);
26+
27+
while (flag) {
28+
cout << "Enter the choice: (1 : search, 2 : add, 3 : delete, 4 : show, 0 : exit)";
29+
cin >> choice;
30+
31+
switch (choice) {
32+
case 1:
33+
cout << "Enter node to search:";
34+
cin >> findele;
35+
if (root != NULL) bst.search(findele, root);
36+
break;
37+
case 2:
38+
cout << "Enter a new value:";
39+
cin >> a;
40+
bst.insert(a, root);
41+
bst.show(root);
42+
break;
43+
case 3:
44+
cout << "Enter node to delete:";
45+
cin >> delele;
46+
bst.del(delele, root);
47+
bst.show(root);
48+
break;
49+
case 4:
50+
if (root != NULL) bst.show(root);
51+
break;
52+
case 0:
53+
cout << "\n\tThank your for using AVL maxTree program\n" << endl;
54+
flag = false;
55+
break;
56+
default: cout << "Sorry! wrong input\n" << endl; break;
57+
}
58+
}
59+
return 0;
60+
}

Heap/Max Heap Code/hw9.cpp

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include "maxheap.h"
2+
#include <iostream>
3+
int main(int argc, char *argv[])
4+
{
5+
int N, i, data;
6+
Maxheap<int> H(10);
7+
if(argc != 2)
8+
{
9+
cerr << "wrong argument count" << endl; return 1;
10+
}
11+
fstream fin(argv[1]);
12+
if(!fin)
13+
{
14+
cerr << argv[1] << " open failed" << endl;
15+
return 1;
16+
}
17+
fin >> N; for(i=0; i<N; i++)
18+
{
19+
fin >> data;
20+
H.Push(data);
21+
}
22+
23+
cout << H; while(!H.IsEmpty())
24+
{
25+
26+
cout << H.Top() << " " ; H.Pop();
27+
}
28+
cout << endl;
29+
fin.close();
30+
return 0;
31+
}

0 commit comments

Comments
 (0)