-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhashheap.cpp
126 lines (117 loc) · 2.87 KB
/
hashheap.cpp
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
// hashheap.cpp
// Dylan Vanmali
// CS130A Spring 2017 Prof. Suri
#include "hashheap.h"
#include "heap.h"
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
/**
* Creates an empty HashHeap with size 97
*/
HashHeap::HashHeap() {
used = 0;
vector<Heap> t (97);
t.swap(table);
}
/**
* Creates an empty HashHeap with size s
* @param {int} s - size of HashHeap
*/
HashHeap::HashHeap(int s) {
used = 0;
vector<Heap> t (s);
t.swap(table);
}
/**
* Hash Function that determines location within the HashHeap
* Works with negative, zero, and positive values
* @param {int} s - size of HashHeap
*/
int HashHeap::hash(int value) {
if (value < 0)
value *= -1;
return value % table.size();
}
/**
* Inserts a value into the HashHeap at its hash location
* @param {int} value - value to insert
* @returns {bool} true if already exists, false if didn't before
*/
bool HashHeap::insert(int value) {
bool exists = table[hash(value)].insert(value);
if (exists == true) {
cout << "error: item already exists" << endl;
}
return exists;
}
/**
* Deletes a value from the HashHeap by accessing its hash location
* @param {int} value - value to delete
* @returns {bool} true if value present and deleted, false if not present
*/
bool HashHeap::deleteValue(int value) {
bool present = table[hash(value)].deleteValue(value);
if (present == false) {
cout << "error: item not present" << endl;
}
return present;
}
/**
* Finds value in the HashHeap by accessing its hash location
* @param {int} value - value to find
* @returns {bool} true if found, false if not
*/
bool HashHeap::lookup(int value) {
bool found = table[hash(value)].lookup(0,value);
if (found == false)
cout << value << " not found " << endl;
else
cout << "found " << value << endl;
return found;
}
/**
* Deletes the Maximum value in the entire HashHeap
*/
void HashHeap::deleteMax() {
if (!table.empty()) {
int* max = table[0].getMax();
for (int i=0; i<table.size(); i++) {
if (max == NULL)
max = table[i].getMax();
else if (table[i].getMax() == NULL)
; // Do Nothing
else if (*(table[i].getMax()) > (*max))
max = table[i].getMax();
}
if (max == NULL)
cout << "error" << endl;
else {
cout << (*max) << endl;
table[hash(*max)].deleteValue(*max);
}
}
else {
cout << "error" << endl;
}
}
/**
* Prints the integer values in order from Max to Min
*/
void HashHeap::print() {
vector<int> print_sorted;
if (!table.empty()) {
for (int i=0; i<table.size(); i++) {
if (!table[i].empty()) {
vector<int>* tmp = table[i].getAll();
print_sorted.insert(print_sorted.end(), tmp->begin(), tmp->end());
}
}
}
sort(print_sorted.begin(), print_sorted.end());
if (!print_sorted.empty())
for (int k=print_sorted.size()-1; k>=0; k--)
cout << print_sorted[k] << " ";
cout << endl;
}