0% found this document useful (0 votes)
95 views10 pages

Sistem Berkas: Lokasi Penyimpanan h1 (Key) h2 (Key)

The document contains details about different hashing techniques including Sistem Berkas, Brent's Method, Binary Tree, and Computed Chaining. It provides sample data, formulas used, and code implementations in C programming language for each hashing technique. For Sistem Berkas, it shows how hash values are calculated for sample data using the given formula. For Brent's Method, Binary Tree and Computed Chaining, it similarly shows the hash values and storage layout for the sample data using the respective techniques.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
95 views10 pages

Sistem Berkas: Lokasi Penyimpanan h1 (Key) h2 (Key)

The document contains details about different hashing techniques including Sistem Berkas, Brent's Method, Binary Tree, and Computed Chaining. It provides sample data, formulas used, and code implementations in C programming language for each hashing technique. For Sistem Berkas, it shows how hash values are calculated for sample data using the given formula. For Brent's Method, Binary Tree and Computed Chaining, it similarly shows the hash values and storage layout for the sample data using the respective techniques.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Sistem Berkas

Rezky Pratomo 140810130013


Anas Tazcia 140810130021
Alvin Niza A
140810130047

A
1. Data : Indra, Susi, Annis, Andri, Silvi dan Reza
Lokasi penyimpanan
Rumus : h0 + T(h1(key)) + T(h2(key))
*Ket :
- h0 = panjang key
- h1 = karakter pertama key
- h2 = karakter terakhir key

[Link](Indra)

= 5 + T(I) + T(A)
= 5 + 13 + 11
= 29

[Link](Susi)

= 4 +T(S) + T(I)
= 4 + 6 + 13
= 23

[Link] (Annis) = 5 + T(A) + T(S)


= 5 + 11 + 6
= 22

[Link](Andri)

= 5 + T(A) + T(I)
= 5 + 11 + 13
= 29

[Link](Silvi)

= 5 + T(S) + T(I)
= 5 + 6 + 13
= 24

[Link](Reza)

= 4 + T(R) + T(A)
= 4 + 14 + 11
= 29

2. Data : Indra, Susi, Annis, Andri, Silvi, Reza


Frekuensi Karakter Pertama dan Karakter Terakhir
a= 4 i= 4 r= 1 s= 3
Nilai dari perfect hashing setiap key tersebut
Indra 8
Andri 8
Susi 7
Indra 8
Annis 7
secara terurut
Annis 7
Andri 8
Silvi 7
Silvi 7
Susi 7
Reza 5
Reza 5
Perfect hashing menggunakan Algoritma Cichellis tidak bisa dilakukan jika suatu
string mempunyai panjang yang sama (h0), karakter pertama (h1) dan karakter
terakhir (h2) sama karena akan menyebabkan terjadinya tubrukan data (collision). Hal
ini terjadi di Key Andri dan Indra.
Sumber referensi : Computational Linguistics: International Series in Modern
Applied Mathematics and Computer Science

Nick Cercone

3. Program
#include "stdio.h"
#include "string.h"
#include "ctype.h"
#define MAX 27
struct data {
int no;
char record[20];
int frek;
int add;
int first;
int last;
};
main(){
int table1[MAX] = {11,3,15,6,13,15,15,13,6,0,1,13,0,14,0,0,15,10,0,0,0,6,15,15,14,0};
char table2[MAX] = "AGMSYBHNTZCIOUDJPVEKQWFLRX";
int G[MAX] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
int n,key,temp, fs, ls;
printf("Masukan banyak data : ");scanf("%d",&n);
key = n;
int dec[n], decadd[n], address[n];
struct data library[n];
for(int i=0;i<n;i++)
{
address[i]=-1;
library[i].frek=0;
}
for(int i=0;i<key;i++)
{
fs = 0;
ls = 0;
printf("Masukan Data ke %d : ",i+1);scanf("%s",&library[i].record);
for(int j=0;j<MAX;j++){
if(toupper(library[i].record[0]) == table2[j])

fs = j;
if(toupper(library[i].record[(strlen(library[i].record)-1)]) == table2[j])
ls = j;
}
library[i].first = fs;
library[i].last = ls;
library[i].no = i+1;
dec[i] = table1[fs] + table1[ls];
library[i].frek = dec[i];
decadd[i] = i;
}
for(int i=0;i<n;i++){
for(int j=i+1;j<n;j++){
if((dec[i] == dec[j] && library[i].no < library[j].no && i < j) || dec[i] > dec[j] ){
temp = dec[i];
dec[i] = dec[j];
dec[j] = temp;
temp = decadd[i];
decadd[i] = decadd[j];
decadd[j] = temp;
}
}
}
printf("\n\nData\tFrek\tAddress\n----------------------------");
for(int i=n-1;i>=0;i--){
for(int j=0;j<1;j++){
address[i] = (strlen(library[decadd[i]].record) + G[library[decadd[i]].first] +
G[library[decadd[i]].last]) % n;
library[decadd[i]].add = address[i];
for(int k=0;k<n;k++){
if(address[i] == address[k] && i!=k){
G[library[decadd[i]].first]++;
j = -1;
}
}
}
printf("\n%s\t%d\t%d",library[decadd[i]].record,library[decadd[i]].frek,address[i]);
}
printf("\n\nAddress\tData\n---------------");
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(i == library[j].add)
temp = j;
}
printf("\n%d\t%s",i,library[temp].record);
}
}

B
1. Data : 35, 61, 49, 22, 44, 26, 11, 23, 46
Bentuk Penyimpanan Brents Method
Rumus:
Hash(key) = key mod 11
i(key) = Q(Key/11) mod 11
Hash(35)
Hash(61)
Hash(49)
Hash(22)
Hash(44)
Hash(26)
Hash(11)
Hash(23)
Hash(46)

=
=
=
=
=
=
=
=
=

2
6
5
0
0
4
0
1
2

i(35)
i(61)
i(49)
i(22)
i(44)
i(26)
i(11)
i(23)
i(46)

=
=
=
=
=
=
=
=
=

3
5
4
2
4
2
1
2
4

0
1
2
3
4
5

2. Program Brents Method


#include
#include
#include
#include

<stdio.h>
<math.h>
<limits.h>
<float.h>

2
2
1
1
3
5
2
3
2
6
4
9
6
1

double BrentsMethod(double lowerLimit, double upperLimit, double (*function)


(double), double errorTol, double maxiter)
{
double a = lowerLimit;
double b = upperLimit;
double c = 0;
double d = DBL_MAX;
double fa = (*function)(a);
double fb = (*function)(b);
double fc = 0;
double s = 0;
double fs = 0;
// if f(a) f(b) >= 0 then error-exit
if (fa * fb >= 0)
{
if (fa < fb)
return a;
else
return b;
}
// if |f(a)| < |f(b)| then swap (a,b) end if
if (fabs(fa) < fabs(fb))
{ double tmp = a; a = b; b = tmp; tmp = fa; fa = fb; fb = tmp;
}
c = a;
fc = fa;
int mflag = 1;
int i = 0;
while (!(fb==0) && (fabs(a-b) > errorTol))
{
if ((fa != fc) && (fb != fc))
// Inverse quadratic interpolation
s = a * fb * fc / (fa - fb) / (fa - fc) + b * fa * fc
/ (fb - fa) /(fb - fc) + c * fa * fb / (fc - fa) /

(fc - fb);
else
// Secant Rule
s = b - fb * (b - a) / (fb - fa);
double tmp2 = (3 * a + b) / 4;
if ((!(((s > tmp2) && (s < b)) || ((s < tmp2) && (s > b)))) ||
(mflag && (fabs(s - b) >= (fabs(b - c) / 2))) ||
(!mflag && (fabs(s - b) >= (fabs(c - d) / 2))))
{
s = (a + b) / 2;
mflag = 1;
}
else
{
if ((mflag && (fabs(b - c) < errorTol)) ||
(!mflag && (fabs(c - d) < errorTol)))
{
s = (a + b) / 2;
mflag = 1;
}
else
mflag = 0;
}
fs = (*function)(s);
d = c;
c = b;
fc = fb;
if (fa * fs < 0) { b = s; fb = fs; }
else { a = s; fa = fs; }
// if |f(a)| < |f(b)| then swap (a,b) end if
if (fabs(fa) < fabs(fb))
{ double tmp = a; a = b; b = tmp; tmp = fa; fa = fb; fb = tmp; }
i++;
if (i > maxiter)
{
printf("Error is %f \n", fb);
break;
}
printf("Number of iterations : %d\n",i);
return b;
}

3. Data : 35, 61, 49, 22, 44, 26, 11, 23, 46


Bentuk Penyimpanan Data Binary Tree
Rumus:
Hash(key) = key mod 11
i(key) = Q(Key/11) mod 11
0
Hash(35) = 2
i(35) = 3
Hash(61) = 6
i(61) = 5
1
Hash(49) = 5
i(49) = 4
Hash(22) = 0
i(22) = 2
2
Hash(44) = 0
i(44) = 4
Hash(26) = 4
i(26) = 2
3
Hash(11) = 0
i(11) = 1
Hash(23) = 1
i(23) = 2
4
Hash(46) = 2
i(46) = 4
5
4. Program
#include
#include
#include
#include

Binary Tree
<iostream.h>
<math.h>
<limits.h>
<float.h>

2
2
1
1
3
5
2
3
2
6
4
9
6
1

struct tnode
{
int data;
struct tnode *left;
struct tnode *right;
};
typedef struct tnode TreeNode;
TreeNode *newItem(int data)
{
TreeNode *pn = (TreeNode *) malloc(sizeof(TreeNode));
pn->data = data;
pn->left = 0;
pn->right = 0;
return pn;
}
TreeNode* insertTree(TreeNode *pn, int data)
{
if (!pn)
return newItem(data);
else
{

if (data <= pn->data)


pn->left = insertTree(pn->left, data);
else

pn->right = insertTree(pn->right, data);


}
return pn;

}
TreeNode *createTree(){
TreeNode * pr = insertTree(0, 4);
insertTree(pr, 1);
insertTree(pr, 2);
insertTree(pr, 3);
insertTree(pr, 5);
insertTree(pr, 6);
return pr;
}

void printPreorder(TreeNode *pn){


if (pn){
printf("preorder = %d\n", pn->data);
printPreorder(pn->left);
printPreorder(pn->right);
}
}
void printPostorder(TreeNode *pn)
{
if (pn)
{
printPostorder(pn->left);
printPostorder(pn->right);
printf("postorder = %d\n", pn->data);
}
}
void printInorder(TreeNode *pn)
{
if (pn)
{
printInorder(pn->left);
printf("inorder = %d\n", pn->data);
printInorder(pn->right);
}
}
int size(TreeNode *pn)
{
if (pn)
return 1 + size(pn->left) + size(pn->right);
else
return 0;

int depth(TreeNode *pn){


if (pn)
{
int ld = depth(pn->left);
int rd = depth(pn->right);
if (ld > rd)
return ld + 1;
else
return rd + 1;

}
else

return 0;
}
int minVal(TreeNode *pn)
{
if (pn)
{
while(pn->left)
pn = pn->left;
return pn->data;
}
else
}

return 0;

int maxVal(TreeNode *pn)


{
if(pn)

while(pn->right)
pn = pn->right;
return pn->data;

}
else
}

return 0;

5. Data : 35, 61, 49, 22, 44, 26, 11, 23, 46


Bentuk Penyimpanan Data Computed Chaining
Rumus:
Hash(key) = key mod 11
i(key) = Q(Key/11) mod 11
Hash(35)
Hash(61)
Hash(49)
Hash(22)
Hash(44)
Hash(26)
Hash(11)
Hash(23)
Hash(46)

=
=
=
=
=
=
=
=
=

2.
6.
5.
0.
0.
4.
0
1
2

i(35)
i(61)
i(49)
i(22)
i(44)
i(26)
i(11)
i(23)
i(46)

=
=
=
=
=
=
=
=
=

3
5
4
2
4
2
1
2
4

6. Program Computed Chaining

0
1
2
3
4
5
6
7
8
9
1
0

Ke
y
22
23
35
46
26
49
61
44
11

class LinkedHashEntry {
private:
int key;
int value;
LinkedHashEntry *next;
public:
LinkedHashEntry(int key, int value) {
this->key = key;
this->value = value;
this->next = NULL;
}
int getKey() {
return key;
}
int getValue() {
return value;
}
void setValue(int value) {
this->value = value;
}
LinkedHashEntry *getNext() {
return next;
}
void setNext(LinkedHashEntry *next) {
this->next = next;

No
f
2
3
2

}
};
const int TABLE_SIZE = 128;
class HashMap {
private:
LinkedHashEntry **table;
public:
HashMap() {
table = new LinkedHashEntry*[TABLE_SIZE];
for (int i = 0; i < TABLE_SIZE; i++)
table[i] = NULL;
}
int get(int key) {
int hash = (key % TABLE_SIZE);
if (table[hash] == NULL)
return -1;
else {
LinkedHashEntry *entry = table[hash];
while (entry != NULL && entry->getKey() != key)
entry = entry->getNext();
if (entry == NULL)
return -1;
else
return entry->getValue();
}
}
void put(int key, int value) {
int hash = (key % TABLE_SIZE);
if (table[hash] == NULL)
table[hash] = new LinkedHashEntry(key, value);
else {
LinkedHashEntry *entry = table[hash];
while (entry->getNext() != NULL)
entry = entry->getNext();
if (entry->getKey() == key)
entry->setValue(value);
else
entry->setNext(new LinkedHashEntry(key, value));
}
}
void remove(int key) {
int hash = (key % TABLE_SIZE);
if (table[hash] != NULL) {
LinkedHashEntry *prevEntry = NULL;
LinkedHashEntry *entry = table[hash];
while (entry->getNext() != NULL && entry->getKey() != key) {
prevEntry = entry;
entry = entry->getNext();
}
if (entry->getKey() == key) {
if (prevEntry == NULL) {
LinkedHashEntry *nextEntry = entry->getNext();
delete entry;
table[hash] = nextEntry;
} else {

LinkedHashEntry *next = entry->getNext();


delete entry;
prevEntry->setNext(next);
}
}
}
}
~HashMap() {
for (int i = 0; i < TABLE_SIZE; i++)
if (table[i] != NULL) {
LinkedHashEntry *prevEntry = NULL;
LinkedHashEntry *entry = table[i];
while (entry != NULL) {
prevEntry = entry;
entry = entry->getNext();
delete prevEntry;
}
}
delete[] table;
}
};

You might also like