This presentation is the property of its rightful owner.
Sponsored Links
1 / 92

第十章 索引与散列 PowerPoint PPT Presentation


  • 55 Views
  • Uploaded on
  • Presentation posted in: General

第十章 索引与散列. 静态索引结构 动态索引结构 散列 (Hashing) 可扩充散列 小结. 静态索引结构. 普通搜索存在的问题: 当数据对象个数 n 很大时,如果用无序表形式的静态搜索结构存储,采用顺序搜索,则搜索效率极低。如果采用有序表存储形式的静态搜索结构,则插入新记录进行排序,时间开销也很可观。这时可采用 索引方法 来实现存储和搜索。. 线性索引 (Linear Index List). 示例:有一个存放职工信息的数据表,每一个职工对象有近 1k 字节的信息 , 正好占据一个页块的存储空间。建立一个索引表便于数据的搜索。. 索引的优势:

Download Presentation

第十章 索引与散列

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


6152016

(Hashing)


6152016

n


Linear index list

(Linear Index List)

1k ,


6152016

  • 1


6152016

  • n b ()()


6152016

  • K()i

  • i


Inverted index list

(Inverted Index List)

(1)

(2)


6152016


6152016

  • ()


6152016

111


6152016

AND


6152016

m

45


6152016

m m () mm

m

m


6152016

m

  • m

  • m , ,

    • m ,

      P0, K1, P1, K2, P2, , Kn, Pn

      Pi 0 in < mKi 1 i n < m Ki < Ki+1, 1 i < n


6152016

  • Pi Ki+1 Ki0 < i < n

  • Pn Kn

  • P0 K1

  • Pi m 0 in

3


6152016

m

template <class Type> classMtree { //

public:

Triple<Type> &Search ( const Type & );

protected:

Mnode<Type>root;

intm;

}

AVL2 m m h,

m-1 h m mh+1-1

  • h=2 7

  • h=3 3 34-1 = 80

m n m


6152016

m

template <class Type>

Triple<Type> & Mtree<Type> :: Search ( const Type &x )

{

Triple result; //

GetNode ( root ); //

Mnode<Type> *p = root, *q = NULL;

while ( p != NULL ) { //

inti = 0;pkey[(pn)+1] = MAXKEY;

while ( pkey[i+1] < x ) i++; //

if ( pkey[i+1] == x ) { //

result.r = p;result.i = i+1;result.tag = 0;

returnresult;

}

q = p; p = pptr[i]; //

GetNode (p); //

}

result.r = q; result.i = i; result.tag = 1;

return result; //,

}


6152016

B

m B m

  • 2

  • () m/2

B B


6152016

BB

template <class Type> classBtreepublicMtree<Type> {

// m

public:

intInsert ( const Type&x );

int Remove ( const Type&x );

};

template <class Type> class Mnode { //

private:

int n; //

Mnode<Type> *parent; //

Type key[m+1]; // 1m-1

Mnode<Type> *ptr[m+1]; // 0m

};


6152016

B

BB

[ m/2-1, m-1]

m-1


6152016

  • p m-1

    ( m, P0, K1, P1, K2, P2, , Km, Pm)

    Ki < Ki+1, 1 i < m

  • p p q

  • p

    ( m/2-1, P0, K1, P1, , Km/2-1, Pm/2-1)

  • q

    (m-m/2, Pm/2, Km/2+1, Pm/2+1, , Km, Pm)

  • Km/2 q ( Km/2, q )


6152016


6152016

3B


6152016

B

  • B Ki1 in Pi x Ki x x

  • 4

    n 2


6152016

55


6152016

  • nm/2

  • n = m/2-1 () n m/2 ()

    • () Ki (1 i n)

    • () () Ki


6152016

  • () () ()

  • () 1


6152016

65


6152016

70


6152016

  • n = m/2-1 () n = m/2-1

    • p p Pi Pi+1 Pi p Ki+1 Pi

    • p Pi+1 Pi Pi+1

    • p Ki+1 Pi+1

    • p


6152016

  • 0

  • m/2-2


6152016

55


6152016

80


6152016

50


6152016


6152016

75


6152016

B+

  • B+BB

  • m B+

    • m

    • () 2 , m/2 n n-1


6152016

  • n m m

    m1 m1

    n n [m1/2, m1]

  • Ki Pi () ( Ki, Pi )Ki

  • P0 K1B


6152016

  • B+B+

  • B+


6152016

  • B+B

  • B+B

  • B+-


6152016

  • n > m1 (m1+1)/2 (m1+1)/2

  • m


6152016

  • B+- m1/2

  • - n m1/2


6152016

18

12


6152016

  • m1/2-


6152016

33


6152016

  • m/2


Hashing

(Hashing)

()


6152016

  • Hash( )

    Address Hash ( Rec.key )


6152016

12361, 07251, 03309, 30976

hash(x) = x % 73 + 13420

%hash(12361) = hash(07250) = hash(03309) = hash(30976) = 13444


6152016

  • m , 0 m-1

  • key 0 m-1


6152016

1.

Hash ( key ) a * key + b{ a, b }

{ 942148, 941269, 940527, 941630, 941805, 941558, 942047, 940001 }

Hash (key) = key - 940000

Hash (942148) = 2148 Hash (941269) = 1269

Hash (940527) = 527 Hash (941630) = 1630

Hash (941805) = 1805 Hash (941558) = 1558

Hash (942047) = 2047 Hash (940001) = 1


6152016

2.

n d r r

k

i k n/r n k ( k )


6152016

9 4 2 1 4 8 1 = 57.60

9 4 1 2 6 9 2 = 57.60

9 4 0 5 2 7 3 = 17.60

9 4 1 6 3 0 4 = 5.60

9 4 1 8 0 5 5 = 5.60

9 4 1 5 5 8 6 = 5.60

9 4 2 0 4 7

9 4 0 0 0 1


6152016

3.

m, m m p, 20

hash ( key ) = key % p p m

, % p 2

key = 962148 m = 25 HT[25] p= 23 hash ( key ) = key % p

hash ( 962148 ) = 962148 % 23 = 12


6152016

4.

A 01 01 001

A1 0134 20420 042

A9 0144 23420 342

B 02 4 004

DMAX 04150130 21526443617100 443

DMAX1 0415013034 5264473522151420 352

AMAX 01150130 135423617100 236

AMAX1 0115013034 3454246522151420 652


6152016

5.


6152016

  • key = 23938587841 3 , 3 . 4

    23938587841

  • , 3

239

239

385

385

878

878

41

41

1543

1714


6152016

HT m , m , i (0 i < m) i

s ,


6152016

, (s = 1)

0 m-1, R2, R2.key, hash ( R2.key ) j R1 R2


1 linear probing

(1) (Linear Probing)

Burke, Ekers, Broad, Blum, Attlee, Alton, Hecht, Ederly

Hash (x) = ord (x) - ord (A)

ord ( )

Hash (Burke) = 1 Hash (Ekers) = 4

Hash (Broad) = 1 Hash (Blum) = 1

Hash (Attlee) = 0 Hash (Hecht) = 7

Hash (Alton) = 0 Hash (Ederly) = 4

HT[26]m = 26

0 1 2 3 4 5 6 7 25

Attlee Burke Broad Blum Ekers Alton Ederly Hecht

(1) (1) (2) (3) (1) (6) (3) (1)


6152016

Hi

Hi= ( Hi-1 +1 ) % m i =1, 2, , m-1

H0 +1, H0 +2, , m-1, 0, 1, 2, , H0-1

Hi = ( H0+ i ) % m i =1, 2, , m-1


6152016

  • ASLsuccASLunsucc

  • ASLsucc

  • ASLunsucc 09918


6152016

class HashTable{

//

public:

enum KindOfEntry { Active, Empty, Deleted };

HashTable ( ) : buckets ( DefaultSize )

{AllocateHt ( );CurrentSize = 0; }

~HashTable ( ) { delete [ ] ht; }

const HashTable & operator =

( const HashTable & ht2 );

int Find ( const Type & x );

int Insert ( const Type & x );

int Remove ( const Type & x );

int IsIn ( const Type & x )

{ return ( i = Find (x) ) >= 0 ? 1 : 0; }

void MakeEmpty ( );

private:

struct HashEntry {

Type Element;

KindOfEntry info;

int operator== ( HashEntry &, HashEntry & );

int operator!= ( HashEntry &, HashEntry & );

HashEntry ( ) : info (Empty ) { }

HashEntry (const Type & E, KindOfEntry

i = Empty ) : Element (E), info (i) { }

};

enum { DefualtSize = 11 }

HashEntry *ht;

int CurrentSize, TableSize;

void AllocateHt ( )

{ ht = new HashEntry[TableSize ]; }

int FindPos ( const Type & x ) const;

}

ht x


6152016

template <class Type> int HashTable<Type>::

Find ( const Type & x ) {

//

//-TableSize

inti = FindPos ( x ),j = i;

while ( ht[j].info != Empty&&ht[j].Element != x ){

j = ( j + 1 ) % TableSize;

if ( j == i ) return-TableSize;

}

if ( ht[j].info== Active ) returnj;

else -j;

}


6152016

1infoEmpty

2

3BroadinfoEmptyBlumAltonBlumAltondeleted

4


6152016

template <class Type>

voidHashTabe<Type> :: MakeEmpty ( )

{ //

for ( inti = 0;i < TableSize;i++)

ht[i].info = Empty;

CurrentSize = 0;

}


6152016

template <class Type> const HashTable <Type>

&HashTable<Type> ::

operator = ( constHashTable<Type>&ht2 )

{ //ht2

if ( this != &ht2 ) {

delete [ ] ht;

TableSize = ht2.TableSize; AllocateHt ( );

for ( inti = 0;i < TableSize; i++ )

ht[i] = ht2.ht[i];

CurrentSize = ht2.CurrentSize;

}

return *this;

}


6152016

template <class Type> intHashTable<Type>::

Insert (const Type&x )

{ // x

if ( ( int i = Find (x) ) >= 0 ) return 0; //

else if ( i!= -TableSize&& ht[-i].info != Active )

{ // -ix

ht[-i].Element = x;ht[-i].info = Active;

CurrentSize++;

return 1;

}

else return 0;

}


6152016

template <class Type> intHashTable<Type> ::

Remove ( const Type & x ) {

//x

if ( ( int i = Find (x) ) >= 0 ) { //,

ht[i].info = deleted;//

CurrentSize--;

return 1;

}

else return 0;

}


6152016

  • = n / (s*m) n s m

  • P (2-)/(2-2)


2 quadratic probing

(2) (quadratic probing)

x

H0 = hash(x)

Hi= (H0 + i 2 ) % m,

Hi= (H0-i 2 ) % m, i = 1, 2, , (m-1)/2

m 4k+3 k 3, 7, 11, 19, 23, 31, 43, 59, 127, 251, 503, 607, 1019,


6152016

  • H0, H0+1, H0-1, H0+4, H0-4,

  • (H0 -i2 ) % m H0 -i2 < 0

    j = (H0 -i2 ) % m, while( j < 0 ) j += m

  • { Burke, Ekers, Broad, Blum, Attlee, Alton, Hecht, Ederly }

    Hash (x)ord (x)ord ('A')

    Hash (Burke) = 1 Hash (Ekers) = 4

    Hash (Broad) = 1 Hash (Blum) = 1

    Hash (Attlee) = 0 Hash (Hecht) = 7

    Hash (Alton) = 0 Hash (Ederly) = 4


6152016

0 1 2 3 4 5

Blum Burke Broad Ekers Ederly

TableSize = 31

(3) (1) (2) (1) (2)

6 7 8 9 10 11

Hecht

(1)

25 26 27 28 29 30

Alton Attlee

(5) (3)


6152016

(3)

  • Hash( ) key H0 = Hash(key)

  • ReHash( ) key TableSize TableSize

  • m = TableSize

    j = H0 = Hash(key) p = ReHash(key)

    j = ( j + p ) % m pmm


6152016

Hi= (H0 + i * ReHash(key) ) % m

i =1, 2, , m-1

m-1H0


6152016

{ 22, 41, 53, 46, 30, 13, 01, 67 }

Hash(x)(3x) % 11

HT[0..10]m = 11

ReHash(x) = (7x) % 10 +1

Hi = ( Hi-1 + (7x) % 10 +1 ) % 11, i = 1, 2,

H0(22) = 0 H0(41) = 2 H0(53) = 5

H0(46) = 6 H0(30) = 2 H1 = (2+1) = 3

H0(13) = 6 H1 = (6+2) = 8

H0(01) = 3 H1 = (3+8) = 0

H2 = (0+8) = 8

H3 = (8+8) = 5

H4 = (5+8) = 2

H5 = (2+8) = 10

H0(67) = 3 H1 = (3+10) = 2

H2 = (2+10) = 1


6152016

{ Burke, Ekers, Broad, Blum, Attlee, Alton, Hecht, Ederly }Hash (x)ord (x)ord ('A')

Hash (Burke) = 1 Hash (Ekers) = 4

Hash (Broad) = 1 Hash (Blum) = 1

Hash (Attlee) = 0 Hash (Hecht) = 7

Hash (Alton) = 0 Hash (Ederly) = 4

HT[0..25]m = 26


6152016

0

1

2

3

4

5

6

7

8

9

Attlee

Alton

Burke

Broad

Blum

Ekers

Ederly

Hecht


6152016

:

0.5


6152016

  • () ()

  • { 33575, 24050, 4909, 3072, 2241, 930, 762, 500 }


6152016


6152016

20

n O(n)


6152016


6152016

  • n m


6152016

1HT[0..12],m=13

H0key=key% 13; %=mod

Hi=(hi-1+REV(key+1)%11+1)%13;

=1,2,3,m-1

REV(x)10xREV(37)=73, REV(7)=7283120191853278

0 1 2 3 4 5 6 7 8 9 10 11 12


6152016

..

H0key= keymod13

Hi =Hi-revkey + 1mod 11 +1mod 13

0 1 2 3 4 5 6 7 8 9 10 11 12

27 53 2 31 19 20 8 18


6152016

2.

2001.5

Un1 / (1 -),

n =200

Un1 / (1 -) 1.5

1/3

n/m = 200/m = 1/3

m 600

m 4k+3 m 607(607=4*151+3)

hash( key ) = key % m


6152016

  • BB+; B B+


  • Login