Data Structure (hash function,binary search tree)

这学期的 data structure 终于结束了。

感觉收获还是不小的。

总结了一下学了2样东西,最有用的就是 hash function

东西本身很简单,但是思想太独特了, 大大加快了搜索速度。

首先把 value to be inserted 转换成integer value, 用这个value % anothervalue, anothervalue is constant for all the values to be inserted, % 是求余数的意思。得到结果是几,就把这个数插在哪里。

比方说:valuetobeinserted=10

anothervalue=3

valuetobeinserted%another=10%3=1 ,把data 插到table的第一个slot

但是这样有个问题20%3也等于1,怎么办呢?我们在找一个数:anothervalue2

value%anothervalue2=step, 如果第一个collision了,就把从发生冲突的位置开始,跳step

比方说:6%10=6;16%10=6;there will be collision, anothervalue2=3;假设6已经在六号位。16%3=1;16将被插入5号位,如果5号位也有data,在检查4号位,以此类推

这种方式叫做 double hashing,因为有两个 hash function

还有两种解决冲突的方法,叫做brent hashing 和binary tree hashing.

第二个就是binary search tree, 这个东西很简单,每个数据有一个 priority

比方说:有一个数据在tree 顶端(只有一个数据)他的priority是10,又有数据要进来,和这个数据比较priority,如果比他大插到the first data 右边(root->right=data),如果比他小,插到他左边(root->left=data),他们subtree也都是 binary search tree

但是这样带来一个问题,就是可能插入的所有的data to be inserted 都比那个数大,那么他的右边就会越来越长,为了保持平衡,有人想出了AVL ,tree和left leaning red black tree, 这两种 binary search tree,左右两边基本平衡。
hash function 的应用很多,比如说可以用它来写一个字典的程序,很快就可以搜索到需要的词了
binary search tree 也有许多应用,一组数据每一个都有一个priority,和value值,可以很快找到priority最高的值,也可以按照需要查找

评论

此博客中的热门博文

Embedded System interview Question

MicroKernel & Exokernel 操作系统未来可能的发展

中国城市房地产分析