-
Notifications
You must be signed in to change notification settings - Fork 0
/
heap.h
48 lines (47 loc) · 1.2 KB
/
heap.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
#ifndef __HEAP_H__
#define __HEAP_H__
#pragma once
#include <string>
using namespace std;
class TreeNode
{
public:
int value;
string key;
TreeNode* leftChild;
TreeNode* rightChild;
TreeNode * parent;
TreeNode(string key, int value)
{
this->key = key;
this->value = value;
leftChild = NULL;
rightChild = NULL;
}
bool operator < (const TreeNode& anotherNode);
bool operator > (const TreeNode& anotherNode);
bool operator == (const TreeNode& anotherNode);
bool isLeaf();
void swapNodes(TreeNode * e);
};
class BinaryHeap
{
private:
bool isMaxHeap;
int heapSize;
public:
TreeNode * root;
BinaryHeap(bool isMaxHeap);
void heapify(int size, TreeNode * nodes);
bool insert(TreeNode * node);
TreeNode * extract();
int size();
TreeNode * peek();
void printTree(TreeNode * e);
void upheap(TreeNode * e);
int maxHeight(int n);
void addLevel(TreeNode * e, TreeNode * from);
char * bitString(int num);
void downheap();
};
#endif