-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMinHeapTree.h
152 lines (118 loc) · 4.13 KB
/
MinHeapTree.h
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
//
// Created by abasiy on ۰۷/۱۲/۲۰۲۳.
//
#ifndef DATASTRUCTURE_MINHEAPTREE_H
#define DATASTRUCTURE_MINHEAPTREE_H
#include <iostream>
template<class T>
struct node{
int priority;
T data;
bool operator<(node<T> next) {
return this->priority < next.priority;
}
bool operator<=(node<T> next) {
return this->priority <= next.priority;
}
bool operator>(node<T> next) {
return this->priority > next.priority;
}
bool operator>=(node<T> next) {
return this->priority >= next.priority;
}
bool operator==(node<T> next) {
return this->priority == next.priority;
}
bool operator!=(node<T> next) {
return this->priority < next.priority;
}
};
template<class T>
class MinHeapTree {
private:
node<T> *tree_array;
int size_of_tree;
int top;
public:
MinHeapTree<T>(int max_size){
size_of_tree = max_size + 1; // +1 because the first cell of tree_array will be left empty;
this->tree_array = new node<T>[size_of_tree];
top = 1;
}
~MinHeapTree(){
delete[] tree_array;
}
void insert(T data, int priority){
if (!this->isFull()) {
node<T> new_node{priority, data};
int index_of_new_node = top;
tree_array[index_of_new_node] = new_node;
top++;
int index_of_parent = parent_of(index_of_new_node);
while (new_node < tree_array[index_of_parent] && index_of_new_node != 1) {
this->swap(index_of_new_node, index_of_parent);
index_of_new_node = index_of_parent;
index_of_parent = parent_of(index_of_new_node);
}
} else std::cout << " the Tree has no capacity! ";
}
T pop(){
if (!isEmpty()) {
T root_data = tree_array[1].data;
top--;
tree_array[1] = tree_array[top];
int index_of_replaced_root = 1;
int index_of_left_child = indexOfLeftChildOf(index_of_replaced_root);
int index_of_right_child = indexOfRightChildOf(index_of_replaced_root);
int index_of_min_child = indexOfMinChild(index_of_left_child, index_of_right_child);
while (tree_array[index_of_replaced_root] >= tree_array[index_of_min_child]) {
this->swap(index_of_replaced_root, index_of_min_child);
index_of_replaced_root = index_of_min_child;
index_of_left_child = indexOfLeftChildOf(index_of_replaced_root);
index_of_right_child = indexOfRightChildOf(index_of_replaced_root);
if (index_of_right_child >= top ) break;
index_of_min_child = indexOfMinChild(index_of_left_child, index_of_right_child);
}
return root_data;
} else {
cout << " \n the heap tree is empty! \n";
return NULL;
}
}
int parent_of(int index){
return (int)(index / 2);
}
int indexOfLeftChildOf(int index){
return (int)(index * 2);
}
int indexOfRightChildOf(int index){
return (int)(index * 2 + 1);
}
int indexOfMinChild(int index_of_left, int index_of_right){
return (tree_array[index_of_left] > tree_array[index_of_right])? index_of_right: index_of_left;
}
void swap(int index1, int index2){
node<T> buffer = this->tree_array[index1];
this->tree_array[index1] = tree_array[index2];
this->tree_array[index2] = buffer;
}
bool isFull(){
return top == size_of_tree;
}
bool isEmpty(){
return top == 1;
}
void print(){
int left_child, right_child;
for (int i = 1; i < top; ++i) {
left_child = indexOfLeftChildOf(i);
right_child = indexOfRightChildOf(i);
std::cout << " " << tree_array[i].data << "{ ";
if (left_child < top) std::cout << tree_array[left_child].data << ", ";
else std::cout << "-, ";
if (right_child < top) std::cout << tree_array[right_child].data << "} " << endl;
else std::cout << "-} " << endl;
}
}
};
#endif //DATASTRUCTURE_MINHEAPTREE_H