���-�������

���� �� �������� ����������� �������� ���������� ������� - ���-�������. ������� ����� ������ �������� � ��� ���� O(1), ����� ��� ���������� ������ - O(n). ���������� ��������� ����������� ����� ����� � ������� �������[1990]�����[1998]. ����� ������ ������ �� ��� ����, ��� ����������� ������� ��������������� �������������. ����� ������ �����, ��������� ��� ���������� ��� �������� �����������[3]. ������ �����, ��������� ��� ��������� �����������[3] ��� �������� ���������[1], ����� �� �����������. ��, ���?
 

������

���-������� - ��� ������� ������ � ��������� ����������, ���������� ���-��������. ��������, �� hashTable ���. 3.1 - ��� ������ �� 8 ���������. ������ ������� ������������ ����� ��������� �� �������� ������, �������� �����. ���-������� � ���� ������� ������ ����� ���� �� 8 � ���������� ������� ��� ������ � �������. ��� ���� ��� ����� �� 0 �� 7 ��������� ��� ��������� � hashTable ��� � ����� ����� �� 0 �� 7, �������� ����������� ���������� �������� ��������.


���. 3.1: ���-�������

����� �������� � ������� ����� �������, �� �������� ����, ����� ���������� ������, � ������� ��� ����� ��������, ����� ��������� ������� � ������ ����� ������. ��������, ����� �������� 11, �� ����� 11 �� 8 � �������� ������� 3. ����� �������, 11 ������� ���������� � ������, �� ������ �������� ��������� hashTable[3]. ����� ����� �����, �� ��� �������� � �������� �� ���������������� ������. ����� ������� �����, �� ������� ��� � ������� ������� ������, ��� ����������.

��� ���������� ��������� � ���-������� ���������� ����� ������������ ������, ������� ������������ � ���� ��������� �������, ������ �� ������� ������������� ����� ���-�������. ���� ����� ���������� �����������. ������ �����, � ������� ��� �������� ������������� � ����� ���-�������, �������� ��� ������ ��� �������� ���������; ��� �������� �� ������� � ���������� ����������.

���� ���-������� ������������ ������������ ��������� ������ ���������� �� ��������� ��������, �� ����������� ���������� ��������� ��������� ������. ��������� ������ - ����� ��� ����� ���������� � ���� ������. ��� ���� �� �������� � ����� �������� �������, ������� � ��������� ��������������� ����������� ������ ���, ����� ���-������ ������. ������ �����, ��� ����� ������� ���-�������. ����� �� ���������� ���� ��������� �� ��������� ��������. ��� ����������� ������� ��������������, ��� unsigned char ������������� � 8 �����, unsigned short int - � 16, unsigned long int - � 32.

typedef unsigned short int HashIndexType;
unsigned char Rand8[256];

HashIndexType Hash(char *str) {
    HashIndexType h;
    unsigned char h1, h2;

    if (*str == 0) return 0;
    h1 = *str; h2 = *str + 1;
    str++;
    while (*str) {
        h1 = Rand8[h1 ^ *str];
        h2 = Rand8[h2 ^ *str];
        str++;
    }
  
    /* h is in range 0..65535 */
    h = ((HashIndexType)h1 << 8)|(HashIndexType)h2;
    /* use division method to scale */
    return h % HashTableSize
}
������ ���-������� ������ ���� ���������� �������, ����� � ��� ���������� ������� ������� ����� ������ ����. ��� ����� �� ������� 3.1, ��� ������ �������, ��� ������ ������� ����� ������ ����� � ���. ���-������� ����� ������������� ��� ������������ ��������� �������. �� ���� ����, ��� ������� ������, ������������� ���������� ������� �, ��������������, ������� ����� ����� � ������ ������ �����������. ����� ���������� ��������� ����� n. ���� ������ ������� ����� 1, �� ������� ����������� � ���� ������ ����� n. ���� ������ ������� ����� 2 � ����������� ��������, �� ��� �������� ����� ���� � ����� �������� �� n/100 ��������� � ������. ��� ������ ��������� ����� ������, � ������� ����� ������. ��� �� ����� � ������� 3.1, ������� ������������ ������� � ������ ����� �������.
������ ����� ������ �����
1 869 128 9
2 432 256 6
4 214 512 4
8 106 1024 4
16 54 2048 3
32 28 4096 3
64 15 8192 3

������� 3.1: ����������� �������� ������� ������ (�s) �� hashTableSize. ����������� 4096 ���������.

����������

���������� ��������� �� �� ��������� typedef TcompGT ������� �������� ���, ����� ��� ��������������� ������, �������� � �������. ��� ������ ��������� ������� ����� ���������� hashTableSize � ������� ����� ��� hashTable. � ���-������� hash ����������� ����� �������. ������� insertNode ������� ������ ��� ����� ���� � ��������� ��� � �������. ������� deleteNode ������� ���� � ����������� ������, ��� �� ������������. ������� findNode ���� � ������� �������� ����.