• 検索結果がありません。

ヒントスライドPPT プログラミング演習2 #prog2bkc net

N/A
N/A
Protected

Academic year: 2018

シェア "ヒントスライドPPT プログラミング演習2 #prog2bkc net"

Copied!
18
0
0

読み込み中.... (全文を見る)

全文

(1)

第14週 木構造と探索(3)

(2)

ハッシュ法

定義

 ハッシングに基づく探索法

ハッシングとは

 ハッシュ関数に基づき計算されたハッシュ値

によってキーの格納先を決定

(3)

ハッシュ関数: mod5 の例 1/5

0

1

2

3

4

S={18, 9, 22, 4, 21, 12, 15, 31, 7, 25}

18 % 5 = 3

18

mod

y が5の場合( mod5 )の キーの格納先は、

キーを 5 で割った余り番目の 添字リスト

key next

table

ハッシュ関数 ハッシュ値

struct node{ int key;

struct node

*next;

(4)

ハッシュ関数: mod5 の例 2/5

0

1

2

3

4

S={18, 9, 22, 4, 21, 12, 15, 31, 7, 25}

9 % 5 = 4

18

9

(5)

ハッシュ関数: mod5 の例 3/5

0

1

2

3

4

S={18, 9, 22, 4, 21, 12, 15, 31, 7, 25}

22 % 5 = 2

18

9 22

(6)

ハッシュ関数: mod5 の例 4/5

0

1

2

3

4

S={18, 9, 22, 4, 21, 12, 15, 31, 7, 25}

4 % 5 = 4

18

4 22

9

4 9

key next

key next

4

4 9

key next

key next

3:新しい要素の next を設 4: top の指す先を変更

1:動的メモリ確保( malloc ) メモリを確保する

2:リストの要素の値を設定する

8 週目の復習

リストの先頭に挿入

(7)

ハッシュ関数: mod5 の例 5/5

S={18, 9, 22, 4, 21, 12, 15, 31, 7, 25}

0

1

2

3

4

18

4 7

9

25 15

31 21

12 22

順番に挿入していくと下記のような構造になる

(8)

オプション課題14-1: print_hash の出力

0: 25 -> 15

1: 31 -> 21

2: 7 -> 12 -> 22

3: 18

4: 4 -> 9

print_hash

前ページのハッシュの内容を出力すると

(9)

オプション課題1 4 -1:参考プログラム

int main(){

struct node *table[5] = {NULL,NULL,NULL,NULL,NULL}; /* 略:変数宣言 */

// ハッシュテーブルの作成

// ハッシュテーブルの表示 print_hash(table);

// ハッシュテーブルの消去 return 0;

}

ハッシュテーブルの宣言

14-1 で作成する print_hash

ハッシュを作成

0 1 2 3 4

18 4 7

9

25 15

31 21

12 22

図 6.48 教科書 p179 図 6.48 に示される

リスト集合をプログラム上で 構成する

教科書は双方向リストになって いることに注意

(10)

オプション課題 14- 2: member

STEP1:

 検索するリストを決める

STEP2:

 先頭からリストを辿りキーを探す  

 ・リストの末尾まで見てキーがなかったら:

   NULL を返す

 ・キーがあった場合:

  そのノードを返す

 

struct node *member (struct node **table, int key)

ハッシュテーブル

検索したい key 取り出されたノード

ハッシュが key を含むかどうかを検索する関数

(11)

member の処理例1:1/3

0

1

2

3

4

12 を検索する場合

STEP1:キーを探す添字を決める 12%5 = 2

18

4 7

9

25 15

31 21

12 22

(12)

member の処理例1:2/3

0

1

2

3

4

12 を検索する場合

STEP2:リストを辿りキーを比較

キー(12)が違うので、次のリストを辿る

18

4 7

9

25 15

31 21

12 22

(13)

member の処理例1:3/3

0

1

2

3

4

12 を検索する場合

STEP2:リストを辿り key を比較

キー(12)を見つけたのでノードを返す

18

4 7

9

25 15

31 21

12 22

(14)

member の処理例2:1/3

0

1

2

3

4

1 9を検索する場合

STEP1:キーを探す添字を決める 1 9 %5 = 4

18

4 7

9

25 15

31 21

12 22

(15)

member の処理例2:2/3

0

1

2

3

4

18

4 7

9

25 15

31 21

12 22

19 を検索する場合

STEP2:リストを辿りキーを比較

キー(19)が違うので、次のリストを辿る

(16)

member の処理例2:3/3

0

1

2

3

4

18

4 7

9

25 15

31 21

12 22

19 を検索する場合

STEP2:リストを辿り key を比較 キー(19)が違い、リスト末尾なので NULLを返す(見つからなかった)

(17)

オプション課題1 4 - 2 :参考プログラム

int main(){

struct node *table[5] = {NULL,NULL,NULL,NULL,NULL}; struct node *tmp;

/* 略:変数宣言 */ // ハッシュの作成

  // 標準入力から探索したい値を入力

   printf(" 探索する値を入力してください : ");    scanf("%d",&key);

  // 探索を行う

   tmp = member(table,key);   // 検索結果の出力

   if(tmp != NULL){

printf("%dは見つかりました \n",tmp->key);    }else{

printf("%dは見つかりませんでした \n",key);    }

ハッシュテーブルの宣言

ハッシュを作成

関数 member を利用して、

keyがハッシュに含まれるかを検索する

(18)

著者リスト

1. 安積 卓也(情報システム学科)

2. 泉 朋子 (情報コミュニケーション学科)

参照

関連したドキュメント

We argue inductively for a tree node that the graph constructed by processing each of the child nodes of that node contains two root vertices and that these roots are available for

These allow us to con- struct, in this paper, a Randers, Kropina and Matsumoto space of second order and also to give the L-dual of these special Finsler spaces of order two,

As in 4 , four performance metrics are considered: i the stationary workload of the queue, ii the queueing delay, that is, the delay of a “packet” a fluid particle that arrives at

In Section 3, we employ the method of upper and lower solutions and obtain the uniqueness of solutions of a two-point Dirichlet fractional boundary value problem for a

Before discussing p-adic L-functions we will develop Fourier theory for the multiplicative group; this will be useful because the p-adic L-functions we con- struct arise as

First, a similar technique allows one to con- struct linear algebras for different types of extended extrafunctions (pointwise, compact- wise, and extended distributions) with

社会調査論 調査企画演習 調査統計演習 フィールドワーク演習 統計解析演習A~C 社会統計学Ⅰ 社会統計学Ⅱ 社会統計学Ⅲ.

卒論の 使用言語 選考要件. 志望者への