Notice
Recent Posts
Recent Comments
Link
«   2024/05   »
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
Archives
Today
Total
관리 메뉴

codingfarm

Heap 본문

Algorithm & Data structure/이론

Heap

scarecrow1992 2020. 9. 6. 16:57

낮은 key 값 우선

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
#include<iostream>
 
using namespace std;
 
class HeapNode {
public:
    int key, value;
};
 
 
class Heap {
public:
    int cnt;
    HeapNode nodes[1000];
 
    //이미 정렬된 배열에 대해선 값들을 한번에 넣어서 시간을 단축한다.
    void Init(HeapNode *arr, int n) {
        cnt = n;
        for (int i = 0; i < n; i++)
            nodes[i + 1= arr[i];
    }
 
    void Insert(HeapNode* arg) {
        cnt++;
        int i = cnt;
        while (i != 1 && (arg->key < nodes[i / 2].key)) {
            nodes[i] = nodes[i / 2];
            i /= 2;
        }
        nodes[i] = *arg;
    }
 
    void Erase() {
        HeapNode tmpNode = nodes[cnt];
        cnt--;
        int parent = 1;
        int child = 2;
        while (child <= cnt) {
            if (child + 1 <= cnt && nodes[child].key > nodes[child + 1].key)
                child++;
 
            if (tmpNode.key < nodes[child].key)
                break;
 
            nodes[parent] = nodes[child];
            parent = child;
            child *= 2;
        }
        nodes[parent] = tmpNode;
    }
}; int main(void)
{
    HeapNode nodes[6];
    for (int i = 0; i < 6; i++) {
        nodes[i].value = i;
        nodes[i].key = i;
    }
 
 
    Heap heap;
    heap.Init(nodes, 6);
 
    cout << heap.nodes[1].value << " ";
 
    for (int i = 0; i < 5; i++) {
        heap.Erase();
        cout << heap.nodes[1].value << " ";
    }
 
    HeapNode tmpNode;
 
    tmpNode.key = 0;
    tmpNode.value = 0;
    heap.Insert(&tmpNode);
    cout << heap.nodes[1].value << " ";
 
 
    tmpNode.key = 2;
    tmpNode.value = 2;
    heap.Insert(&tmpNode);
    cout << heap.nodes[1].value << " ";
 
 
    tmpNode.key = 1;
    tmpNode.value = 1;
    heap.Insert(&tmpNode);
    cout << heap.nodes[1].value << " ";
 
 
    tmpNode.key = 8;
    tmpNode.value = 8;
    heap.Insert(&tmpNode);
    cout << heap.nodes[1].value << " ";
 
 
    tmpNode.key = 9;
    tmpNode.value = 9;
    heap.Insert(&tmpNode);
    cout << heap.nodes[1].value << " ";
 
    int n = heap.cnt;
    for (int i = 0; i < n; i++) {
        cout << heap.nodes[1].value << " ";
        heap.Erase();
    }
 
    return 0;
 
}
cs

 

 

 

높은 key 값 우선

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
#include<iostream>
 
using namespace std;
 
class HeapNode {
public:
    int key, value;
};
 
 
class Heap {
public:
    int cnt;
    HeapNode nodes[1000];
 
    //이미 정렬된 배열에 대해선 값들을 한번에 넣어서 시간을 단축한다.
    void Init(HeapNode *arr, int n) {
        cnt = n;
        for (int i = 0; i < n; i++)
            nodes[i + 1= arr[i];
    }
 
    void Insert(HeapNode* arg) {
        cnt++;
        int i = cnt;
        while (i != 1 && (arg->key > nodes[i / 2].key)) {
            nodes[i] = nodes[i / 2];
            i /= 2;
        }
        nodes[i] = *arg;
    }
 
    void Erase() {
        HeapNode tmpNode = nodes[cnt];
        cnt--;
        int parent = 1;
        int child = 2;
        while (child <= cnt) {
            if (child + 1 <= cnt && nodes[child].key < nodes[child + 1].key)
                child++;
 
            if (tmpNode.key > nodes[child].key)
                break;
 
            nodes[parent] = nodes[child];
            parent = child;
            child *= 2;
        }
        nodes[parent] = tmpNode;
    }
}; int main(void)
{
    HeapNode nodes[6];
    for (int i = 5; i >= 0; i--) {
        nodes[5 - i].value = i;
        nodes[5 - i].key = i;
    }
 
 
    Heap heap;
    heap.Init(nodes, 6);
 
    cout << heap.nodes[1].value << " ";
 
    for (int i = 0; i < 5; i++) {
        heap.Erase();
        cout << heap.nodes[1].value << " ";
    }
 
    HeapNode tmpNode;
 
    tmpNode.key = 0;
    tmpNode.value = 0;
    heap.Insert(&tmpNode);
    cout << heap.nodes[1].value << " ";
 
 
    tmpNode.key = 2;
    tmpNode.value = 2;
    heap.Insert(&tmpNode);
    cout << heap.nodes[1].value << " ";
 
 
    tmpNode.key = 1;
    tmpNode.value = 1;
    heap.Insert(&tmpNode);
    cout << heap.nodes[1].value << " ";
 
 
    tmpNode.key = 8;
    tmpNode.value = 8;
    heap.Insert(&tmpNode);
    cout << heap.nodes[1].value << " ";
 
 
    tmpNode.key = 9;
    tmpNode.value = 9;
    heap.Insert(&tmpNode);
    cout << heap.nodes[1].value << " ";
 
    int n = heap.cnt;
    for (int i = 0; i < n; i++) {
        cout << heap.nodes[1].value << " ";
        heap.Erase();
    }
 
    return 0;
 
}
cs

 

 

템플릿 사용

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
#include<iostream>
 
using namespace std;
 
class Node {
public:
    int value, key;
 
    Node(int value_, int key_) : value(value_), key(key_) {}
    Node() : Node(-1-1) {}
 
    bool operator<(const Node& rhs) {
        return this->key < rhs.key;
    }
 
    bool operator>(const Node& rhs) {
        return this->key > rhs.key;
    }
 
    Node& operator=(const Node& rhs) {
        if (this == &rhs)
            return *this;
 
        this->value = rhs.value;
        this->key = rhs.value;
 
        return *this;
    }
};
 
template<typename T>
class Heap {
public:
    Heap() {
        _size = 0;
    }
 
    int Size() {
        return _size;
    }
 
    T Top() {
        return arr[1];
    }
 
    void Push(T& newNode) {
        _size++;
 
        int index = _size, parent = _size / 2;
 
        while (parent >= 1) {
        if (newNode < arr[parent])
        break;
            arr[index] = arr[parent];
            index = parent;
            parent /= 2;
        }
        arr[index] = newNode;
    }
 
    void Pop() {
        T tmpNode = arr[_size];
        _size--;
 
        int parent = 1, child = 2;
 
        while (child <= _size) {
            if (child + 1 <= _size && arr[child] < arr[child + 1])
                child++;
 
            if (tmpNode > arr[child])
                break;
 
            arr[parent] = arr[child];
            parent = child;
            child *= 2;
        }
        arr[parent] = tmpNode;
    }
 
 
private:
    int _size;
    T arr[5000];
};
 
 
int main(void) {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
 
    Heap<int> heap;
 
    int value = 0;
    for (int i = 0; i < 10; i++) {
        heap.Push(value);
        value = (value + 7) % 10;
    }
 
    while (heap.Size() != 0) {
        cout << heap.Top() << " ";
        heap.Pop();
    }
 
    cout << "\n\n";
 
    Heap<Node> heap_;
    Node node;
    value = 0;
    for (int i = 0; i < 10; i++) {
        node.value = value, node.key = value;
        heap_.Push(node);
        value = (value + 7) % 10;
    }
 
    while (heap_.Size() != 0) {
        cout << heap_.Top().value << " ";
        heap_.Pop();
    }
}
cs

 

'Algorithm & Data structure > 이론' 카테고리의 다른 글

구간 길이 구하기  (0) 2021.01.06
BST(Binary Search Tree)  (0) 2020.09.06
LCA(Lowest Common Ancestor)  (0) 2020.09.06
MST(Minimum Spanning Tree) & Kruskal  (0) 2020.09.06
Union Find  (0) 2020.09.06
Comments