CS50筆記5_其他數(shù)據(jù)結(jié)構(gòu)More Data Structure

Hash table

- Hash function (return non-negative integers -- hash code) + array (store data types placed in the array location[hash code] in the structure and make that data type a pointer to the linked list)

- Almost a constant search/insert time: O(1)

- A hash table is essentially an array?of?linked lists, drawing from top to bottom. Each linked list in the array has elements of a certain category.

- For example, store names into an array with 26 positions, one for each letter of the alphabet. It takes only one step to find the alphabetic location according to ASCII (A-[0]; H-[7]). If there are names starting with the same letter, the collision can be solved by stitching them together along the linked list from left to right. No need to grow the size of array or move any value.

- Hash function: take some input and deterministically maps it to the location it should go in (input letter output number). In our example, the hash function just returns an index corresponding to the first letter of the name, such as?0?for “Albus” and?25?for “Zacharias”.

unsigned int hash( char *str )

{

? ? ? ? int sum = 0;

? ? ? ? for (int j = 0; str[j] != '\0'; j++)

? ? ? ? {

? ? ? ? ? ? ? ? sum += str[j];

? ? ? ? }

? ? ? ? return sum % HASH_MAX;? ? ? ? ? ? ? ? ? ????????? // return the non-negative hash code

}

- For the worst case, all the names might start with the same letter, so we might end up with the equivalent of a single linked list again ---------------------------------- COLLISION (when some pieces of data run through the hash function and yield the same hash code)

? ? ? ? Solutions for collisions:

? ? ? ? Linear Probing: place the data in the next consecutive element in the array until we find a vacancy

? ? ? ? ? ? ? ? Clustering: more likely to grow the cluster

? ? ? ? Chaining: each element in the array is a pointer to the head of a linked list, which eliminates clustering. Theoretically it may not benefit searching, but the sorting speed can be accelerated in the real world.?

- Optimization when meet too many collisions: we might look at the first two letters, and allocate enough buckets for 26*26 possible hashed values (AA-ZZ), or even the first three letters?(AAA-ZZZ), requiring 26*26*26 buckets:

? ? While using more space in memory, we’re more likely to only need one step to look for a value, reducing our running time for search.

- Example: sorting cards: first putting them in piles by suit, (spades, diamonds, hearts, and clubs), then sort each pile. So it will takes only 13 steps rather than 13*4 steps.

- The worst case running time for a hash table is O(n)?asymptotically, since, as?n?gets very large, each bucket will have on the order of?n?values, even if we have hundreds or thousands of buckets. In practice, though, our running time will be faster since we’re dividing our values into multiple buckets.



Trie?

- Short for “retrieval”. A trie is a tree with arrays as nodes: (H - the root of the tree, unique key & simple boolean value)

- Each array stores 26 letters. Each node from top to bottom is an array of pointers to other nodes.?

- For each word, the first letter will point to an array, where the next valid letter will point to another array, and so on, until we reach a boolean value indicating the end of a valid word, marked in green above. If our word isn’t in the trie, then one of the arrays won’t have a pointer or terminating character for our word.

- The searching time depends on the length of the word we’re looking for only. This might be a fixed maximum and not depend on the number of other words in the data structure, so we can have?O(1) for searching and insertion.

- The cost for this, though, is that we need lots of memory to store pointers and boolean values as indicators of valid words, even though lots of them won’t be used (even the null pointer takes 8 bits, which indicates very inefficient memory usage, rewinding from the contiguous property of arrays).

// Example

typedef struct _trie

{

? ? ? ? char university[20];? ? ? ? ? ? ? ? ? ? ? ? ? ? ?// spaces for 20 characters

? ? ? ? struct _trie* paths[10];? ? ????????????????? ? // an array of ten pointers to other nodes of the same type

}

trie;



Abstract data structures

There are even higher-level constructs,?abstract data structures, where we use our building blocks of arrays, linked lists, hash tables, and tries to?implement?a solution to some problem (implement with some other data structures).

- queue

For example, one abstract data structure is a?queue, like a line of people waiting, where the first value we put in are the first values that are removed, or first-in-first-out (FIFO).?

To add a value we?enqueue?it, and to remove a value we?dequeue?it. This data structure is abstract because it’s an idea that we can implement in different ways: with an array that we resize as we add and remove items, or with a linked list where we append values to the end.

Problems for queue implementing using array: cannot dynamically change the array (one in one out everyone one step forward).

- stack

An “opposite” data structure would be a?stack, where items most recently added are removed first: last-in-first-out (LIFO). At a clothing store, we might take, or?pop, the top sweater from a stack, and new sweaters would be added, or?pushed, to the top as well.

- dictionary

Another example of an abstract data structure is a?dictionary, where we can map keys to values (associate keys with values), such as words to their definitions. We can implement one with a hash table or an array, taking into account the tradeoff between time and space. (think about initial labels for names at the food pickup section)

For the perverse corner case, we meet the two same keys, or too many keys starting from the same letter (remember arrays with finite sizes so we have to cheat on near spaces if we fill spaces for one key).?



Summary

1. Array

? ? ? ? bad insertion, bad deletion, great lookup, relatively easy to sort (like bubble and merge), relatively small size-wise, stuck with fixed-size, no flexibility

2. Linked list

? ? ? ? great insertion, great deletion, bad lookup (rely on linear search), relatively difficult to sort, relatively small size-wise (larger than arrays)

3. hash table

? ? ? ? easy insertion (hash code then insert by linear probing or chaining), easy deletion (if chaining once we found the element), better lookup than linked lists on average, not ideal for sorting, can run the gamut of size

4. trie

? ? ? ? complex insertion, easy deletion (just free the node), fast lookup (depend on the length of data itself), already sorted, heavily space-exhausted

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子泻轰,更是在濱河造成了極大的恐慌疙渣,老刑警劉巖,帶你破解...
    沈念sama閱讀 219,490評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件俗冻,死亡現(xiàn)場離奇詭異礁叔,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)迄薄,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,581評論 3 395
  • 文/潘曉璐 我一進(jìn)店門琅关,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人讥蔽,你說我怎么就攤上這事涣易。” “怎么了冶伞?”我有些...
    開封第一講書人閱讀 165,830評論 0 356
  • 文/不壞的土叔 我叫張陵新症,是天一觀的道長。 經(jīng)常有香客問我碰缔,道長账劲,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,957評論 1 295
  • 正文 為了忘掉前任金抡,我火速辦了婚禮瀑焦,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘梗肝。我一直安慰自己榛瓮,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,974評論 6 393
  • 文/花漫 我一把揭開白布巫击。 她就那樣靜靜地躺著禀晓,像睡著了一般精续。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上粹懒,一...
    開封第一講書人閱讀 51,754評論 1 307
  • 那天重付,我揣著相機(jī)與錄音,去河邊找鬼凫乖。 笑死确垫,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的帽芽。 我是一名探鬼主播删掀,決...
    沈念sama閱讀 40,464評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼导街!你這毒婦竟也來了披泪?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,357評論 0 276
  • 序言:老撾萬榮一對情侶失蹤搬瑰,失蹤者是張志新(化名)和其女友劉穎款票,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體跌捆,經(jīng)...
    沈念sama閱讀 45,847評論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡徽职,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,995評論 3 338
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了佩厚。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片姆钉。...
    茶點(diǎn)故事閱讀 40,137評論 1 351
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡糕伐,死狀恐怖砚殿,靈堂內(nèi)的尸體忽然破棺而出壁却,到底是詐尸還是另有隱情齐遵,我是刑警寧澤豆茫,帶...
    沈念sama閱讀 35,819評論 5 346
  • 正文 年R本政府宣布送浊,位于F島的核電站发钝,受9級特大地震影響仔蝌,放射性物質(zhì)發(fā)生泄漏煞额。R本人自食惡果不足惜思恐,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,482評論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望膊毁。 院中可真熱鬧胀莹,春花似錦、人聲如沸婚温。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,023評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽栅螟。三九已至荆秦,卻和暖如春篱竭,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背步绸。 一陣腳步聲響...
    開封第一講書人閱讀 33,149評論 1 272
  • 我被黑心中介騙來泰國打工掺逼, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人靡努。 一個(gè)月前我還...
    沈念sama閱讀 48,409評論 3 373
  • 正文 我出身青樓坪圾,卻偏偏與公主長得像,于是被迫代替她去往敵國和親惑朦。 傳聞我的和親對象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,086評論 2 355

推薦閱讀更多精彩內(nèi)容