-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathBstHeapTree.cpp
More file actions
139 lines (130 loc) · 3.05 KB
/
BstHeapTree.cpp
File metadata and controls
139 lines (130 loc) · 3.05 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
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
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
/* Build a special tree heap
* A rooted binary tree with keys in its nodes has the binary search property(BST)
* and Heap property. Each tree node contain an integer i and an integer j.
* If you pay attention only to the first key i in each node, it looks like a binary
* search tree. If you pay attention only to the second key j in each node, it looks
* like a heap.
*/
// http://www.1point3acres.com/bbs/thread-13292-1-1.html
// Note: This is NOT a Treap, Treap 并不是二叉堆Heap,二叉堆必须是完全二叉树,而Treap可以并不一定是。
class Node{
public:
int i, j;
Node* left, *right;
Node(int _i_, int _j_){
this->i = _i_;
this->j = _j_;
}
};
bool compareByBST(const Node* a, const Node* b){
return a->i < b->i;
}
class BstHeapTree{
private:
#if 0
bool compare(int a, int b){
return a < b;//min heap
}
void siftdown(vector<int>& nums, int i){
while(i < nums.size()){
int left = i * 2 + 1;
int right = i * 2 + 2;
int smallest = i;
if(left < nums.size() && compare(left, smallest)){
smallest = left;
}
if(right < nums.size() && compare(right, smallest)){
smallest = right;
}
if(smallest == i){
break;
}
int tmp = nums[i];
nums[i] = nums[smallest];
nums[smallest] = tmp;
i = smallest;
}
}
void heapify(vector<int> nums){
if(nums.size() == 0){
return;
}
for(int i = nums.size() / 2; i >= 0; i--){
siftdown(nums, i);
}
}
#endif
Node* _inner_build(vector<Node*>& nodes, int start, int end){
if(start > end){
return NULL;
}
// find min index
int minIndex = start;
for(int i = start; i <= end; i++){
if(nodes[i]->j < nodes[minIndex]->j){
minIndex = i;
}
}
Node* root = nodes[minIndex];
root->left = _inner_build(nodes, start, minIndex-1);
root->right = _inner_build(nodes, minIndex+1, end);
return root;
}
public:
Node* root;
BstHeapTree(vector<Node*>& nodes){
root = build(nodes);
}
Node* build(vector<Node*>& nodes){
if(nodes.size() == 0){
return NULL;
}
sort(nodes.begin(), nodes.end(), compareByBST);// sort by first value i
return _inner_build(nodes, 0, nodes.size()-1);
}
};
/////////////////////////////////////////////
// Below is for test purpose
/////////////////////////////////////////////
void printLevel(Node * root){
if(root == NULL){
return;
}
queue<Node*> q;
q.push(root);
while(!q.empty()){
int size = q.size();
while(size-- > 0){
Node* node = q.front();
q.pop();
std::cout<<"["<<node->i<<","<<node->j<<"] ";
if(node->left != NULL){
q.push(node->left);
}
if(node->right != NULL){
q.push(node->right);
}
}
std::cout<<std::endl;
}
}
int main(void){
vector<Node*> nodes;
nodes.push_back(new Node(6,30));
nodes.push_back(new Node(5,35));
nodes.push_back(new Node(4,10));
nodes.push_back(new Node(1,40));
nodes.push_back(new Node(2,20));
nodes.push_back(new Node(3,25));
BstHeapTree bhtree(nodes);
printLevel(bhtree.root);
for(auto node : nodes){
delete node;
}
return 0;
}