总结
1.视频资源P53-P58:
https://www.bilibili.com/video/BV1gE41157pC?p=8&spm_id_from=pageDriver
2.学习内容:
1)哈希表 P53;
2)哈希冲突处理 P54;
3)哈希表添加、删除、包含 P55;
4)基于哈希表实现集合 P56;
5)基于哈希表实现字典 P57;
6)数据结构总结 P58。
2021/11/05回溯
1)根据伪代码编写哈希表类;
具体内容
P53 哈希表
1、哈希表思想;
2、哈希表中将不同类型键对应索引的方法;
3、哈希函数的设计,三个条件的满足;
4、C#中的哈希函数。
P54 哈希冲突处理
1、基于拉链法的哈希表;
2、创建基于拉链法的哈希表类。
P55 哈希表添加、删除、包含
1、实现哈希表添加、删除、包含方法。
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApp1
{
class HashST1<Key>
{
//链表数组
private Linkedlist1<Key>[] hashtable;
//数组容量
private int M;
//数组长度
private int N;
public HashST1(int M)
{
this.M = M;
N = 0;
hashtable = new Linkedlist1<Key>[M];
//对每一个数组索引开辟一个链表
for (int i = 0; i < M; i++)
{
hashtable[i] = new Linkedlist1<Key>();
}
}
public HashST1()
{
M = 97;
N = 0;
hashtable = new Linkedlist1<Key>[97];
//对每一个数组索引开辟一个链表
for (int i = 0; i < M; i++)
{
hashtable[i] = new Linkedlist1<Key>();
}
}
public int Count
{
get { return N; }
}
public bool IsEmpty
{
get { return N == 0; }
}
//对整数取余并保证为正数
private int Hash(Key key)
{
return (key.GetHashCode() & 0x7fffffff) % M;
}
public void Add(Key key)
{
Linkedlist1<Key> list = hashtable[Hash(key)];
if (list.Contains(key))
{
return;
}
else
{
list.InsertFirst(key);
N++;
}
}
public void Remove(Key key)
{
Linkedlist1<Key> list = hashtable[Hash(key)];
if (list.Contains(key))
{
list.Remove(key);
N--;
}
else
{
return;
}
}
public bool Contains(Key key)
{
Linkedlist1<Key> list = hashtable[Hash(key)];
return list.Contains(key);
}
}
}
P56 基于哈希表实现集合
1、创建哈希表集合类;
2、比较基于红黑树与哈希表实现集合的运行效率;
3、C#中的哈希表集合类;
4、解析哈希表无法取代红黑树的原因。
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApp1
{
class HashST1Set<Key>:ISet<Key>
{
private HashST1<Key> hashST1Set;
public HashST1Set(int M)
{
hashST1Set = new HashST1<Key>(M);
}
public HashST1Set()
{
hashST1Set = new HashST1<Key>();
}
public int Count { get { return hashST1Set.Count; } }
public bool IsEmpty { get { return hashST1Set.IsEmpty; } }
public void Add(Key key)
{
hashST1Set.Add(key);
}
public bool Contains(Key key)
{
return hashST1Set.Contains(key);
}
public void Remove(Key key)
{
hashST1Set.Remove(key);
}
}
}
P57 基于哈希表实现字典
1、创建适用于字典的哈希表类;
2、创建哈希表字典类;
3、比较基于红黑树与哈希表实现字典的运行效率;
4、C#中的哈希表字典类。
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApp1
{
class HashST2<Key,Value> where Key:IComparable<Key>
{
//链表数组
private Linkedlist3<Key, Value>[] hashtable;
//数组容量
private int M;
//数组长度
private int N;
public HashST2(int M)
{
this.M = M;
N = 0;
hashtable = new Linkedlist3<Key, Value>[M];
//对每一个数组索引开辟一个链表
for (int i = 0; i < M; i++)
{
hashtable[i] = new Linkedlist3<Key, Value>();
}
}
public HashST2()
{
M = 97;
N = 0;
hashtable = new Linkedlist3<Key, Value>[97];
//对每一个数组索引开辟一个链表
for (int i = 0; i < M; i++)
{
hashtable[i] = new Linkedlist3<Key, Value>();
}
}
public int Count
{
get { return N; }
}
public bool IsEmpty
{
get { return N == 0; }
}
//对整数取余并保证为正数
private int Hash(Key key)
{
return (key.GetHashCode() & 0x7fffffff) % M;
}
public void Add(Key key, Value value)
{
Linkedlist3<Key, Value> list = hashtable[Hash(key)];
if (list.ContainsKey(key))
{
list.Set(key, value);
}
else
{
list.Insert(key,value);
N++;
}
}
public void Remove(Key key)
{
Linkedlist3<Key, Value> list = hashtable[Hash(key)];
if (list.ContainsKey(key))
{
list.Remove(key);
N--;
}
else
{
return;
}
}
public bool ContainsKey(Key key)
{
Linkedlist3<Key,Value> list = hashtable[Hash(key)];
return list.ContainsKey(key);
}
public Value Get(Key key)
{
Linkedlist3<Key, Value> list = hashtable[Hash(key)];
return list.Get(key);
}
public void Set(Key key,Value newValue)
{
Linkedlist3<Key, Value> list = hashtable[Hash(key)];
list.Set(key,newValue);
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApp1
{
class HashST2Dictionary<Key,Value>:IDictionary<Key,Value> where Key:IComparable<Key>
{
private HashST2<Key, Value> hashST2Dictionary;
public int Count { get { return hashST2Dictionary.Count; } }
public bool IsEmpty { get { return hashST2Dictionary.IsEmpty; } }
public HashST2Dictionary(int M)
{
hashST2Dictionary = new HashST2<Key, Value>(M);
}
public HashST2Dictionary()
{
hashST2Dictionary = new HashST2<Key, Value>();
}
public void Remove(Key key)
{
hashST2Dictionary.Remove(key);
}
public void Add(Key key, Value value)
{
hashST2Dictionary.Add(key, value);
}
public bool ContainsKey(Key key)
{
return hashST2Dictionary.ContainsKey(key);
}
public Value Get(Key key)
{
return hashST2Dictionary.Get(key);
}
public void Set(Key key, Value newValue)
{
hashST2Dictionary.Set(key, newValue);
}
}
}
P58 数据结构的总结
1、动态数组;
2、链表;
3、有序数组;
4、二分查找树;
5、红黑树;
6、哈希表。