差不多長這樣
void dfs(int cur){
cout << cur << '\n'; //前序的訪問時間點
dfs(left);
cout << cur << '\n'; //中序的訪問時間點
dfs(right);
cout << cur << '\n'; //後序的訪問時機點
}
struct node{
int key;
int data;
node *left, *right;
};
struct node{
int key;
int data;
node *left, *right;
node(int key):key(key){
left = nullptr;
right = nullptr;
}
};
struct node{
int find_key(int key_to_find){
if (key_to_find == key) return data;
else if (key_to_find < key){
if (left != nullptr) return left->find_key(key_to_find);
else return -1;
}
else{
if (right != nullptr) return right->find_key(key_to_find);
else return -1;
}
}
};
struct min_heap{
vector <int> v;
void push(int x){
int i = v.size();
v.pb(x);
while (i > 1){
//do something
i /= 2;
}
}
void pop(){
swap(v[1], v.back());
v.pop_back();
int i = 1;
while (true){
int next_node = i;
//compare 3 nodes(if exists)
//find the min node
if (i == next_node) break;
swap(v[i], v[next_node]);
i = next_node;
}
}
};
0 | 1 | 2 | 3 | 4 | 5 |
\(w_1 = 2, w_2 = 3, w_3 = 2, w_4 = 1, w_5 = 5\)