数据结构中的哈希函数2025年4月28日 | 阅读24分钟 每天产生的数据量几乎达到150 Zettabytes,相当于150万亿Gigabytes。随着数据量的飞速增长,需要一种有效且高效的方式来存储这些数据。所谓有效和高效的方式,是指一种能够以最少的时间检索数据的存储方式,因为操作所需的时间越多,与该操作相关的成本就越高。因此,为了降低操作成本并以高效的方式完成任务,我们需要减少特定任务的数据检索时间。而减少检索时间的解决方案就是哈希函数或哈希表。哈希函数用于将数据映射或绑定到特定的哈希值,然后该哈希值将用作索引或键,将该值存储在哈希表中。将数据存储在哈希表中的主要好处是,哈希表中存储数据的检索时间是单位时间。这意味着存储在哈希表中的数据可以以O(1)的时间复杂度读取。因此,哈希表在大幅缩短从哈希表中读取数据所需的时间方面发挥着重要作用。而哈希表的运行需要哈希函数。现在让我们看看什么是哈希函数以及它是如何工作的。 哈希函数可以定义为一种算法或函数,用于将较大尺寸或长度的数据映射或转换为固定尺寸或小的索引或哈希值。换句话说,哈希函数可以定义为一种算法,用于将较高长度或尺寸的数据转换为固定范围或尺寸内的数据。 传递给哈希函数的输入参数是需要映射到某些哈希数据的数据。哈希函数提供的输出或结果表示与该输入参数值关联的哈希值或哈希。哈希函数与实际在内存中存储数据的哈希表相关联,而哈希函数仅用于将这些值映射到哈希表。由哈希函数为作为输入参数传递的数据项返回的哈希值随后用作索引,将该输入数据映射或存储到哈希表中。或者,我们可以说,由哈希函数为作为输入参数传递的数据项返回的哈希值用作存储该数据的键,这将有助于轻松高效地检索存储的数据。 为了使理想的哈希函数能够工作,它应该满足两个基本属性或条件,以便在指定的计算周期内提供最佳结果。为了在哈希表中高效地存储数据,一个高效的哈希函数应满足的这两个基本属性或条件是:
在生成输出数据或哈希值以保持哈希函数效率的同时,需要满足哈希函数的这两个主要条件。 与哈希表结合使用时,哈希函数用于存储和检索数据项或数据记录。哈希函数将与键关联的每个数据或记录转换为一个哈希数,该哈希数用于索引哈希表。当要将一个项添加到表中时,哈希码可能会索引一个空槽(也称为存储桶),在这种情况下,该项就添加到表中。使用哈希函数将输入数据映射到哈希表的索引的方式会导致创建不同类型的哈希。在本文中,我们将介绍两种主要的哈希类型,它们各有优缺点。我们将理解的两种主要的哈希类型是链式哈希方法和开放寻址哈希方法。 在链式哈希中,哈希表中存在的每个槽都充当具有该索引作为哈希函数哈希值输出的输入元素的头节点。因此,如果该索引的头节点为空,则数据将添加到该头节点;否则,如果该索引的头节点已经存在数据,则将新传入的数据附加或添加到该头节点之后。简而言之,我们可以说哈希表的索引充当链表的头节点。 例如,如果我们有一个从0开始到9结束的十个索引的哈希表。那么我们有十个单独的链表,所有这十个不同链表的头节点都存储在这个哈希表的索引中。然后使用哈希函数将值映射或存储到这些不同的链表中。 链式哈希的主要好处是我们可以在此格式中存储任意数量的数据。要存储大量数据,我们只需将数据添加到与哈希函数为该数据返回的索引值或哈希值相对应的链表中的最后一个现有对象或数据即可。但是,在链式哈希表中存储更多数据会降低哈希表的数据搜索或数据检索效率。因为例如,如果索引1处的链表中有n个元素,那么搜索或检索该链表中最后一个元素所需的时间将是O(n),这远大于在哈希表的开放寻址链中搜索或检索数据所需的时间。 在开放寻址哈希表中,计算哈希值或键值,然后将输入数据映射或放置在哈希函数返回的索引值处。链式哈希方法和开放寻址哈希方法的主要区别在于,我们可以在链式哈希技术中添加任意数量的数据,但在开放寻址哈希技术中,添加的数据量等于该哈希表中存在的索引数量。 例如,如果我们有一个从0开始到9结束的十个索引的哈希表。那么我们只能在这个类型的哈希表中存储十个数据。 但是,开放寻址哈希表的一个主要好处是,检索存储在哈希表中的数据需要恒定的时间。 除了这些,根据计算逻辑,哈希函数用于创建结果哈希值,还有不同类型的哈希函数。一些主要的哈希函数类型是:
除了上面提到的这些哈希函数,用户还可以使用他们想要实现的任何类型的哈希逻辑,并根据他们的需求创建哈希函数。 让我们通过一个例子来理解哈希的概念以及在整个过程中哈希函数的使用。 假设我们有一个哈希表,有十个槽或索引,从索引值或槽值零(0)开始。最后一个槽值是九(9)。我们在本例中使用的哈希函数是模运算哈希函数,这意味着传递给哈希函数的输入数据将作为哈希的一部分进行模运算,然后该模运算的结果将作为哈希函数的输出返回,该输出将用作索引或槽键,以在哈希表中存储或映射该输入数据。 最初,哈希表看起来是这样的。哈希表中的所有槽都将为空。
现在,假设输入数据是25。我们将此输入数据传递给哈希函数。在哈希函数中,执行模运算,25的模运算结果将是5。因此,由哈希函数作为输入数据25的哈希值返回的结果值是5。因此,值为25的输入数据将存储在哈希表槽号5中。添加槽号5中的数据后的哈希表如下:
现在,假设输入数据是1。我们将此输入数据传递给哈希函数。在哈希函数中,执行模运算,1的模运算结果将是1。因此,由哈希函数作为输入数据1的哈希值返回的结果值是1。因此,值为1的输入数据将存储在哈希表槽号1中。添加槽号1中的数据后的哈希表如下:
现在,假设输入数据是493。我们将此输入数据传递给哈希函数。在哈希函数中,执行模运算,493的模运算结果将是3。因此,由哈希函数作为输入数据493的哈希值返回的结果值是3。因此,值为493的输入数据将存储在哈希表槽号3中。添加槽号3中的数据后的哈希表如下:
因此,这是在向表中添加四次数据后最终哈希表的外观。 现在,假设输入数据是975。我们将此输入数据传递给哈希函数。在哈希函数中,执行模运算,975的模运算结果将是5。因此,由哈希函数作为输入数据975的哈希值返回的结果值是5。因此,值为975的输入数据将存储在哈希表槽号5中。但是槽号5已经被值为25的数据占用。因此,这是开放链式哈希技术的限制,即我们只能在哈希表中存储特定数量的数据。 存储完成后,下一步是从哈希表中检索数据。对于搜索操作,使用相同的哈希函数来查找存储在哈希表中的数据。在搜索操作中,搜索键再次传递给哈希函数,计算出槽号或索引,并检索该索引中的数据,然后将其与搜索键进行匹配。如果搜索键和从哈希表中获取的数据匹配,则搜索操作成功;否则,如果两者都不匹配,则搜索操作不成功。 对于哈希和哈希表的实际实际应用,需要我们通过编程来实现哈希表的概念。因此,为了更好地理解如何通过编程编写哈希表,让我们编写一个Java代码示例,以编程方式模拟哈希表的功能。 代码输出 Please Choose one of the Operations:: 1. To Insert Data in the Hash Table. 2. To Insert Data from the Hash Table. 3. To Search Data in the Hash Table. 4. To Remove or Delete Data From the Hash Table. 1 Enter the key and value that you want to add to the Hash Table:: 0 10 Data Added Successfully. Type [N or n] to terminate the program. Type [Y or y] to continue the program. Y Please Choose one of the Operations:: 1. To Insert Data in the Hash Table. 2. To Insert Data from the Hash Table. 3. To Search Data in the Hash Table. 4. To Remove or Delete Data From the Hash Table. 1 Enter the key and value that you want to add to the Hash Table:: 1 85 Data Added Successfully. Type [N or n] to terminate the program. Type [Y or y] to continue the program. Y Please Choose one of the Operations:: 1. To Insert Data in the Hash Table. 2. To Insert Data from the Hash Table. 3. To Search Data in the Hash Table. 4. To Remove or Delete Data From the Hash Table. 1 Enter the key and value that you want to add to the Hash Table:: 3 47 Data Added Successfully. Type [N or n] to terminate the program. Type [Y or y] to continue the program. Y Please Choose one of the Operations:: 1. To Insert Data in the Hash Table. 2. To Insert Data from the Hash Table. 3. To Search Data in the Hash Table. 4. To Remove or Delete Data From the Hash Table. 1 Enter the key and value that you want to add to the Hash Table:: 7 149 Data Added Successfully. Type [N or n] to terminate the program. Type [Y or y] to continue the program. Y Please Choose one of the Operations:: 1. To Insert Data in the Hash Table. 2. To Insert Data from the Hash Table. 3. To Search Data in the Hash Table. 4. To Remove or Delete Data From the Hash Table. 2 Contents of the hash table are:: Key Associated Value ------------------------------- 0 10 1 85 3 47 7 149 Type [N or n] to terminate the program. Type [Y or y] to continue the program. Y Please Choose one of the Operations:: 1. To Insert Data in the Hash Table. 2. To Insert Data from the Hash Table. 3. To Search Data in the Hash Table. 4. To Remove or Delete Data From the Hash Table. 3 Enter key for which the data you want:: 3 The value associated with the entered key is = 47 Type [N or n] to terminate the program. Type [Y or y] to continue the program. Y Please Choose one of the Operations:: 1. To Insert Data in the Hash Table. 2. To Insert Data from the Hash Table. 3. To Search Data in the Hash Table. 4. To Remove or Delete Data From the Hash Table. 4 Enter the key that you want to Delete:: 1 Data deleted Successfully. Type [N or n] to terminate the program. Type [Y or y] to continue the program. Y Please Choose one of the Operations:: 1. To Insert Data in the Hash Table. 2. To Insert Data from the Hash Table. 3. To Search Data in the Hash Table. 4. To Remove or Delete Data From the Hash Table. 2 Contents of the hash table are:: Key Associated Value ------------------------------- 0 10 3 47 7 149 Type [N or n] to terminate the program. Type [Y or y] to continue the program. N 在上面的代码中,我们首先四次向哈希表添加数据,然后通过打印哈希表的内容来确认插入。之后,我们通过搜索键搜索哈希表中存在的值,并打印获得的结果。最后,通过删除哈希表的内容来执行删除操作,然后通过调用print_data()函数打印更新后的哈希表。 哈希表也可以在C++、Python、JavaScript等其他语言中实现。让我们看一个C++代码示例,其中包含对哈希表执行的所有基本操作。 C++ 代码输出 Please Choose one of the Operations:: 1. To Insert Data in the Hash Table. 2. To Insert Data from the Hash Table. 3. To Search Data in the Hash Table. 4. To Remove or Delete Data From the Hash Table. 1 Enter the key and value that you want to add to the Hash Table:: 101 56 Data Added Successfully. Type [N or n] to terminate the program. Type [Y or y] to continue the program. Y Please Choose one of the Operations:: 1. To Insert Data in the Hash Table. 2. To Insert Data from the Hash Table. 3. To Search Data in the Hash Table. 4. To Remove or Delete Data From the Hash Table. 1 Enter the key and value that you want to add to the Hash Table:: 102 87 Data Added Successfully. Type [N or n] to terminate the program. Type [Y or y] to continue the program. Y Please Choose one of the Operations:: 1. To Insert Data in the Hash Table. 2. To Insert Data from the Hash Table. 3. To Search Data in the Hash Table. 4. To Remove or Delete Data From the Hash Table. 1 Enter the key and value that you want to add to the Hash Table:: 104 97 Data Added Successfully. Type [N or n] to terminate the program. Type [Y or y] to continue the program. y Please Choose one of the Operations:: 1. To Insert Data in the Hash Table. 2. To Insert Data from the Hash Table. 3. To Search Data in the Hash Table. 4. To Remove or Delete Data From the Hash Table. 2 Contents of the hash table are:: Key Associated Value ------------------------------- 101 56 102 87 104 97 Type [N or n] to terminate the program. Type [Y or y] to continue the program. y Please Choose one of the Operations:: 1. To Insert Data in the Hash Table. 2. To Insert Data from the Hash Table. 3. To Search Data in the Hash Table. 4. To Remove or Delete Data From the Hash Table. 3 Enter key for which the data you want:: 102 The value associated with the entered key is = 87 Type [N or n] to terminate the program. Type [Y or y] to continue the program. y Please Choose one of the Operations:: 1. To Insert Data in the Hash Table. 2. To Insert Data from the Hash Table. 3. To Search Data in the Hash Table. 4. To Remove or Delete Data From the Hash Table. 4 Enter the key that you want to Delete:: 101 Data deleted Successfully. Type [N or n] to terminate the program. Type [Y or y] to continue the program. y Please Choose one of the Operations:: 1. To Insert Data in the Hash Table. 2. To Insert Data from the Hash Table. 3. To Search Data in the Hash Table. 4. To Remove or Delete Data From the Hash Table. 2 Contents of the hash table are:: Key Associated Value ------------------------------- 102 87 104 97 Type [N or n] to terminate the program. Type [Y or y] to continue the program. n 因此,在此代码中,也遵循了相同的操作顺序,即首先是插入操作,然后是搜索操作,最后是删除或移除操作,并通过在每个步骤之后打印哈希表中存在的内容来验证所有这些操作的结果,以成功完成特定任务。 由于在哈希表中插入和搜索数据需要恒定的时间。哈希表在许多计算机问题领域都有应用。哈希表的一些应用或用例是:
因此,本文简要介绍了什么是哈希函数,如何使用哈希函数向哈希表添加数据,哈希表的主要优点和用例,以及在Java、C++等不同编程语言中实现哈希表。 下一主题数据结构中的哈希表 |
我们请求您订阅我们的新闻通讯以获取最新更新。