-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathMinHeap.cpp
More file actions
79 lines (65 loc) · 1.47 KB
/
Copy pathMinHeap.cpp
File metadata and controls
79 lines (65 loc) · 1.47 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
#include "MinHeap.h"
MinHeap::MinHeap() {
m_root = new MinHeapNode();
}
void MinHeap::GenerateMinHeap(map<int,string> mp)
{
MinHeapNode* temp = m_root;
for(auto current : mp)
{
int key = current.first;
std::string code = current.second;
temp = m_root;
for(int i = 0; i<code.size(); i++)
{
temp->isLeaf = false;
int val;
if(i==code.size()-1)
val = key;
else
{
val = -1;
}
MinHeapNode* newNode = new MinHeapNode(val);
if(code[i] == '0')
{
if(temp->left == 0)
temp->left = newNode;
temp = temp->left;
}
else
{
if(temp->right == 0)
temp->right = newNode;
temp = temp->right;
}
}
}
}
void MinHeap::Traverse(MinHeapNode* root)
{
if(root == 0)
return;
cout << root->m_data << " " << root->isLeaf<<'\n';
Traverse(root->left);
Traverse(root->right);
}
void MinHeap::Get(string temp)
{
MinHeapNode* n = m_root;
for(int i = 0; i<temp.size(); i++)
{
if(temp[i] == '0')
n = n->left;
else
n = n->right;
}
// if(n!=0)
cout << n->m_data;
}
MinHeapNode* MinHeap::GetRoot()
{
return m_root;
}
MinHeap::~MinHeap() {
}