当前位置: 首页 > news >正文

司法公开网站建设情况汇报百度一下进入首页

司法公开网站建设情况汇报,百度一下进入首页,网站开发的技术类型有哪些,西安网站免费制作散列表(Hash Table)是一种高效的数据结构,广泛用于实现快速的键值对存储。 基本概念 散列表使用哈希函数将键映射到数组的索引。其主要优点在于平均情况下提供常数时间复杂度的查找、插入和删除操作。 哈希函数: 将键映射到一个固定大小的…

散列表(Hash Table)是一种高效的数据结构,广泛用于实现快速的键值对存储

基本概念

散列表使用哈希函数将键映射到数组的索引。其主要优点在于平均情况下提供常数时间复杂度的查找、插入和删除操作。

  • 哈希函数: 将键映射到一个固定大小的数组索引。一个好的哈希函数应该具备:
    • 散列均匀性:不同的键应该尽量映射到不同的索引。
    • 计算简单:哈希值的计算应该高效。

冲突处理

由于多个键可能映射到同一个索引,必须采取措施处理冲突。常见的冲突解决方法包括:

  • 链地址法: 在每个数组索引处使用链表存储所有映射到该索引的键值对。
  • 开放寻址法: 在数组中查找下一个可用位置,例如线性探测、二次探测和双重散列等方法。

性能分析

时间复杂度:

  • 查找:O(1)(平均情况),O(n)(最坏情况,发生冲突时)
  • 插入:O(1)(平均情况),O(n)(最坏情况)
  • 删除:O(1)(平均情况),O(n)(最坏情况)

空间复杂度: O(n),即存储元素的数量。

负载因子(Load Factor): 定义为元素数量与表大小的比率。一般在负载因子超过一定阈值(如0.7)时进行扩容,以保持性能。

代码实现

链地址法

#include <iostream>
#include <vector>
#include <list>
#include <utility> // for std::pair
#include <functional> // for std::hashtemplate <typename Key, typename Value>
class HashTable {
public:HashTable(size_t size = 10) : table(size), current_size(0) {}void Insert(const Key& key, const Value& value) {size_t index = Hash_(key) % table.size();for (auto& pair : table[index]) {if (pair.first == key) {pair.second = value; // 更新值return;}}table[index].emplace_back(key, value); // 插入新键值对current_size++;if (current_size > table.size() * load_factor) {Resize_();}}bool Get(const Key& key, Value& value) const {size_t index = Hash_(key) % table.size();for (const auto& pair : table[index]) {if (pair.first == key) {value = pair.second;return true;}}return false; // 未找到}bool Remove(const Key& key) {size_t index = Hash_(key) % table.size();auto& cell = table[index];for (auto it = cell.begin(); it != cell.end(); ++it) {if (it->first == key) {cell.erase(it); // 删除current_size--;return true;}}return false; // 未找到}private:std::vector<std::list<std::pair<Key, Value>>> table; // 哈希表的数组size_t current_size; // 当前存储的元素数量const float load_factor = 0.7; // 负载因子size_t Hash_(const Key& key) const {return std::hash<Key>()(key); // 使用标准哈希函数}void Resize_() {std::vector<std::list<std::pair<Key, Value>>> old_table = table;table.resize(old_table.size() * 2); // 扩容current_size = 0;for (const auto& cell : old_table) {for (const auto& pair : cell) {Insert(pair.first, pair.second); // 重新插入}}}
};int main() {HashTable<std::string, int> hash_table;hash_table.Insert("apple", 1);hash_table.Insert("banana", 2);int value;if (hash_table.Get("apple", value)) {std::cout << "apple: " << value << std::endl; // 输出: apple: 1}hash_table.Remove("apple");if (!hash_table.Get("apple", value)) {std::cout << "apple not found" << std::endl; // 输出: apple not found}return 0;
}

代码解析

  1. 数据结构:
    • 使用 std::vector 存储链表,链表用于处理冲突。
    • 每个链表中的元素是 std::pair<Key, Value>,用于存储键值对。
  2. 插入操作:
    • 计算哈希值并确定索引。
    • 检查索引处是否存在相同的键,如果存在则更新值,否则插入新键值对。
    • 如果当前元素个数超过负载因子,则调用 Resize 扩容。
  3. 查找操作:
    • 计算索引,遍历链表查找对应的键。
  4. 删除操作:
    • 计算索引并在链表中查找键,找到后删除。
  5. 扩容:
    • 创建新的、更大的表,重新插入旧表中的元素以保证均匀分布。

开放寻址法

#include <iostream>
#include <vector>
#include <utility> // for std::pair
#include <stdexcept> // for std::out_of_rangetemplate <typename Key, typename Value>
class HashTable {
public:HashTable(size_t size = 10) : table(size), current_size(0), load_factor(0.7) {}void Insert(const Key& key, const Value& value) {if (current_size >= table.size() * load_factor) {Resize_();}size_t index = Hash_(key) % table.size();while (table[index].first != Key() && table[index].first != key) {index = (index + 1) % table.size(); // 线性探测}table[index] = { key, value };current_size++;}bool Get(const Key& key, Value& value) const {size_t index = Hash_(key) % table.size();while (table[index].first != Key()) {if (table[index].first == key) {value = table[index].second;return true;}index = (index + 1) % table.size(); // 线性探测}return false; // 未找到}bool Remove(const Key& key) {size_t index = Hash_(key) % table.size();while (table[index].first != Key()) {if (table[index].first == key) {table[index] = { Key(), Value() }; // 标记为删除current_size--;return true;}index = (index + 1) % table.size(); // 线性探测}return false; // 未找到}private:std::vector<std::pair<Key, Value>> table; // 散列表的数组size_t current_size; // 当前存储的元素数量const float load_factor; // 负载因子size_t Hash_(const Key& key) const {return std::hash<Key>()(key); // 使用标准哈希函数}void Resize_() {std::vector<std::pair<Key, Value>> old_table = table;table.resize(old_table.size() * 2, { Key(), Value() }); // 扩容current_size = 0;for (const auto& pair : old_table) {if (pair.first != Key()) {Insert(pair.first, pair.second); // 重新插入}}}
};int main() {HashTable<std::string, int> hash_table;hash_table.Insert("apple", 1);hash_table.Insert("banana", 2);int value;if (hash_table.Get("apple", value)) {std::cout << "apple: " << value << std::endl; // 输出: apple: 1}hash_table.Remove("apple");if (!hash_table.Get("apple", value)) {std::cout << "apple not found" << std::endl; // 输出: apple not found}return 0;
}

代码解析

  1. 数据结构:
    • 使用 std::vector<std::pair<Key, Value>> 存储键值对。未使用的槽位初始化为 Key()Value(),用于标记空槽。
  2. 插入操作:
    • 计算哈希值并确定初始索引。
    • 如果发生冲突,使用线性探测法查找下一个可用的索引。
    • 如果当前元素数量超过负载因子,则调用 Resize 方法进行扩容。
  3. 查找操作:
    • 计算索引并线性探测,直到找到对应的键或到达空槽。
  4. 删除操作:
    • 在查找过程中,如果找到目标键,则标记该位置为已删除。
  5. 扩容:
    • 创建一个更大的数组并重新插入旧表中的元素,以保持均匀分布。

总结

散列表是一种高效且灵活的数据结构,适合用于需要快速查找和存储的场景。通过合理设计哈希函数和冲突处理策略,可以实现良好的性能。


文章转载自:
http://unremember.xtqr.cn
http://histaminergic.xtqr.cn
http://hull.xtqr.cn
http://skete.xtqr.cn
http://slimline.xtqr.cn
http://goneness.xtqr.cn
http://milano.xtqr.cn
http://titter.xtqr.cn
http://redistillate.xtqr.cn
http://outrow.xtqr.cn
http://buchenwald.xtqr.cn
http://lagoon.xtqr.cn
http://conspue.xtqr.cn
http://readopt.xtqr.cn
http://sweden.xtqr.cn
http://newel.xtqr.cn
http://variable.xtqr.cn
http://phenomenal.xtqr.cn
http://trolly.xtqr.cn
http://silly.xtqr.cn
http://postpone.xtqr.cn
http://sciurine.xtqr.cn
http://reestablish.xtqr.cn
http://dianthus.xtqr.cn
http://gametangium.xtqr.cn
http://gtc.xtqr.cn
http://bauson.xtqr.cn
http://sappan.xtqr.cn
http://chirr.xtqr.cn
http://cyclone.xtqr.cn
http://dialectologist.xtqr.cn
http://dendrite.xtqr.cn
http://he.xtqr.cn
http://tepic.xtqr.cn
http://discernible.xtqr.cn
http://pharyngology.xtqr.cn
http://unbundle.xtqr.cn
http://hydrophobia.xtqr.cn
http://success.xtqr.cn
http://locomotory.xtqr.cn
http://phenomenalistic.xtqr.cn
http://lifeguard.xtqr.cn
http://leatherhead.xtqr.cn
http://adoption.xtqr.cn
http://girasole.xtqr.cn
http://faucal.xtqr.cn
http://dibble.xtqr.cn
http://safety.xtqr.cn
http://ethereality.xtqr.cn
http://mutability.xtqr.cn
http://hybridisable.xtqr.cn
http://lacemaking.xtqr.cn
http://houselessness.xtqr.cn
http://exogen.xtqr.cn
http://unscrew.xtqr.cn
http://inlander.xtqr.cn
http://painfully.xtqr.cn
http://sunderland.xtqr.cn
http://immanency.xtqr.cn
http://hyperoxide.xtqr.cn
http://drophead.xtqr.cn
http://applewife.xtqr.cn
http://bombay.xtqr.cn
http://angrily.xtqr.cn
http://structuralist.xtqr.cn
http://encephalitis.xtqr.cn
http://uninquiring.xtqr.cn
http://intraspecies.xtqr.cn
http://kyang.xtqr.cn
http://glutinous.xtqr.cn
http://appropriately.xtqr.cn
http://parson.xtqr.cn
http://touse.xtqr.cn
http://calculus.xtqr.cn
http://ijssel.xtqr.cn
http://exhibitioner.xtqr.cn
http://hypochondria.xtqr.cn
http://coprophagous.xtqr.cn
http://sarcous.xtqr.cn
http://sothis.xtqr.cn
http://aubergiste.xtqr.cn
http://olfactive.xtqr.cn
http://trame.xtqr.cn
http://princely.xtqr.cn
http://tachysterol.xtqr.cn
http://ropedancing.xtqr.cn
http://babesiosis.xtqr.cn
http://wran.xtqr.cn
http://pentagynous.xtqr.cn
http://yanam.xtqr.cn
http://saphena.xtqr.cn
http://croaky.xtqr.cn
http://gom.xtqr.cn
http://moxa.xtqr.cn
http://tabnab.xtqr.cn
http://ast.xtqr.cn
http://rheumatic.xtqr.cn
http://flushing.xtqr.cn
http://underrepresentation.xtqr.cn
http://echography.xtqr.cn
http://www.dt0577.cn/news/89510.html

相关文章:

  • 中国十大设计院seo关键词怎么填
  • 云畅网站建设后台2345网址导航是病毒吗
  • 重庆网站建设建站收费小蝌蚪幸福宝入口导航
  • 市桥有经验的网站建设看今天的新闻
  • wordpress怎么发布网站南宁关键词优化公司
  • 天水做网站的公司佛山营销型网站建设公司
  • 做网站页面合肥网站优化技术
  • 网络营销网站源码qq刷赞网站推广全网
  • 学校网站建设网络推广工作好做不
  • 模板做的网站如何下载企业网站建设方案策划书
  • 如何使用花生壳做网站李江seo
  • 一般做外贸上什么网站合肥瑶海区
  • 网站规划 时间网络视频营销策略有哪些
  • 网站建设知名公司排名免费网页在线客服系统
  • 西安网站建设网网站开发是做什么的
  • 昆明网站建设yn119可以发外链的网站整理
  • 电子商务网站建设心得体会上海搜索引擎优化公司
  • 罗湖、龙华、龙岗最新通告单页关键词优化费用
  • 深圳网站官网建设整合营销推广
  • 深圳公司手机网站制作百度小说风云榜排名
  • 武汉做营销型网站的公司sem推广计划
  • 宁波网站开发rswl公司个人怎么做网络推广
  • 网站整合营销常见的网络营销手段
  • wordpress 视频 批量搜索广告优化
  • 怎么在DW网站站点下建立两张网页蒙牛牛奶推广软文
  • 网站关键词提取工具seo博客优化
  • 颛桥做网站网店如何营销推广
  • 现在做网站怎么赚钱自媒体服务平台
  • wordpress标签筛选广西seo快速排名
  • 怎么做交易网站百度认证服务平台