-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBEISCH.cs
More file actions
206 lines (196 loc) · 5.67 KB
/
BEISCH.cs
File metadata and controls
206 lines (196 loc) · 5.67 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
//table Row for BEISCH Table
class BEISCHTableRow
{
public int? Record { get; set; }
public int? Link { get; set; }
public BEISCHTableRow()
{
Record = null;
Link = null;
}
}
class BEISCH
{
private bool bottom = true;
private BEISCHTableRow[] HashTable { get; set; }
private int ModValue;
//fills all table rows as null
public BEISCH(int modValue)
{
ModValue = modValue;
HashTable = new BEISCHTableRow[modValue];
for (int i = 0; i < modValue; i++)
{
HashTable[i] = new BEISCHTableRow();
}
}
//hash function
private int Hash(int key)
{
return key % ModValue;
}
//inserts key to table
public void Insert(int key)
{
int hashedIndex = Hash(key);
int iterator = 0;
//if table row is null, insert key
if (HashTable[hashedIndex].Record == null)
{
HashTable[hashedIndex].Record = key;
return;
}
//if table row is not null, insert key to bottom or top
else if (HashTable[hashedIndex].Record != null)
{
//if bottom is true, insert key to bottom
if (bottom)
{
bottom = false;
iterator = HashTable.Length - 1;
while (iterator >= 0)
{
if (HashTable[iterator].Record == null)
{
HashTable[iterator].Record = key;
break;
}
else
{
iterator--;
}
}
}
//if bottom is false, insert key to top
else
{
bottom = true;
iterator = 0;
while (iterator <= HashTable.Length - 1)
{
if (HashTable[iterator].Record == null)
{
HashTable[iterator].Record = key;
break;
}
else
{
iterator++;
}
}
}
//set link for previous key
int tempHashedIndex = hashedIndex;
//if link is null, set link
if (HashTable[tempHashedIndex].Link == null)
{
HashTable[tempHashedIndex].Link = iterator;
}
else
{
//if link is not null and link is equal to hashedIndex, set link
if (Hash(HashTable[tempHashedIndex].Record.Value) == hashedIndex)
{
int temp1 = HashTable[tempHashedIndex].Link.Value;
HashTable[iterator].Link = temp1;
HashTable[tempHashedIndex].Link = iterator;
}
//if link is not null and it is not chain of inserted key ,find last link and set link
else
{
while (HashTable[tempHashedIndex].Link != null)
{
tempHashedIndex = HashTable[tempHashedIndex].Link.Value;
}
HashTable[tempHashedIndex].Link = iterator;
}
}
}
}
//returns index of key if it is exist in table
private int GetIndex(int key)
{
int hashedIndex = Hash(key);
if (HashTable[hashedIndex].Record == null)
{
return -1;
}
else if (HashTable[hashedIndex].Record == key)
{
return hashedIndex;
}
else
{
while (HashTable[hashedIndex].Link != null)
{
hashedIndex = HashTable[hashedIndex].Link.Value;
if (HashTable[hashedIndex].Record == key)
{
return hashedIndex;
}
}
}
return -1;
}
//searches key in table
public void Search(int key)
{
int index = GetIndex(key);
if (index != -1)
{
Console.WriteLine("Key {0} is found on index {1}", key, index);
return;
}
Console.WriteLine("Key {0} is not found", key);
}
//returns number of probes for key
public int GetProb(int key)
{
int hashedIndex = Hash(key);
if (HashTable[hashedIndex].Record == null)
{
return 0;
}
else if (HashTable[hashedIndex].Record == key)
{
return 1;
}
else
{
int count = 1;
while (HashTable[hashedIndex].Link != null)
{
hashedIndex = HashTable[hashedIndex].Link.Value;
count++;
if (HashTable[hashedIndex].Record == key)
{
return count;
}
}
}
return 0;
}
//returns average number of probes for all keys inserted
public double AverageProbes(List<int> insertNumbers)
{
double totalProbes = 0;
foreach (var item in insertNumbers)
{
totalProbes += GetProb(item);
}
return totalProbes / insertNumbers.Count;
}
//prints table
public void Print()
{
Console.WriteLine("{0,16}", "--BEISCH--");
string row = string.Format("{0,0}{1,10}{2,7}", "Index", "Record", "Link");
Console.WriteLine(row);
for (int i = 0; i < HashTable.Length; i++)
{
row = string.Format("{0,3}{1,10}{2,7}", i, HashTable[i].Record, HashTable[i].Link);
Console.WriteLine(row);
}
}
}