Survey
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
Trie and Search Trees Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se Overview • Trie • Binary search trees Trie • Special type of tree • retrieval • Dynamic sets • Key is a string • Root node is an empty string Trie • Anna : data • Andrew : data • Alexandra : data • Alwen : data • Bertil : data • Bridget : data • Beryl : data A N L W B E E R N A Data Trie Implementation • As a table • 2 x 2 array • One columns per letter in the alphabet, n • One row per node, m • log2 m to represent the data Trie Implementation • As a linked list • Each node contains • A letter • Link to child • de la Brandais tree Trie operations • Insert child • Delete child • Child • Look up Trie implementation • Auto correct • File structures • DNA sequencing • Data compression • LZ78 • Huffman encoding Trie implementation • LZ78 • Lossless data compression • Dictionary based • Random access Trie implementation • She sells sea shells S E H _ E _ S L A L H E L L S Step Phrase Output 1 S 0, S 2 H 0, H 3 E 0, E 4 _ 0, _ 5 SE 1, E 6 L 0, L 7 LL 6, L 8 S_ 1, _ 9 A 0, A 10 _SHELLS 4, SHELLS Trie Encode(n) Dictionary empty Prefix empty DictionaryIndex 1 while(n is not empty) Char next character in n if(Prefix + Char exists in the Dictionary) Prefix Prefix + Char else if(Prefix is empty) CodeWordForPrefix 0 else CodeWordForPrefix DictionaryIndex for Prefix Output: (CodeWordForPrefix, Char) insertInDictionary( ( DictionaryIndex , Prefix + Char) ) DictionaryIndex++ Prefix empty Huffman coding • Frequency encoding 13:* 5:1 Frequency Value 5 1 8 2 10 3 15 4 20 5 8:2 Huffman coding 23:* 10:3 Frequency Value 10 3 13 * 15 4 20 5 13:* Huffman coding Frequency Value 15 4 20 5 23 * 35:* 15:3 20:* Huffman coding Frequency Value 23 * 35 * 48:* 35:4 23:5 Huffman coding 48:* 23:* 35:* Frequency Value 48 * Huffman coding 48:* • To encode: • Right = 1 and left = 0 • Example • • • • • 1 = 110 2 = 111 3 = 10 4 = 00 5 = 01 35:* 15:4 23:* 20:5 10:3 13:* 5:1 8:2 Huffman coding • Applications • Zip file compression • Jpeg • PNG Binary Search trees • Each node has max two child nodes • Relationship between child nodes • Nodes key is larger than all the nodes in the left sub tree • Nodes key is smaller than all the nodes in the right sub tree 10 8 5 18 9 12 23 Binary Search trees • Binary search tree and a binary tree • In a binary search tree all nodes much have a label • Delete and Insert needs a label as does create tree for the root • Delete can break the tree • Fix it downwards • Insert must insert in sorted order Binary Search trees • Operations • Search • Insert • Delete 10 Binary Search trees 8 • Search • Fast if tree is ordered • Does node have value x? Where is 12? • Search left if node value is greater than x • Search right if node value is less than x • How long will it take? • Tree is complete • O(log n) 5 18 9 12 23 Binary Search trees • Searching a tree TreeSearch(x, k) if x = NULL or k = key[x] then return x if k key[x] then return TreeSearch(left[x], k) else return TreeSearch(right[x], k) Binary Search trees • Insert • Keep the tree complete • Check left sub tree. If it is full, insert the value in the higher tree and move old down the tree 10 9 9 6 5 18 8 10 10 Binary Search trees TreeInsert(T,z) y ← NULL x ← NULL while x = NULL do y ← x if key[z] < key[x] then x ← left[x] else x ← right[x] p[z] ← y if y = NULL then root[T] = z else if key[z] < key[y] then left[y] ← z else right[y] ← z Binary Search trees 10 • Delete a node • Case 1 8 18 • No sub trees • Case 2 • One sub tree • Case 3 • Two sub trees 5 9 12 Binary Search trees • Case 2 10 • Move sub tree to the delete node’s position • Delete 18 • Move 12 upwards 8 5 12 18 9 12 Binary Search trees • Case 3 • • • • Delete the node Promote the lowest value in the right sub tree Delete 10 Promote 12 12 10 8 5 18 9 12 Binary Search trees TreeDelete(T, z) if left[z] = NULL or right[z] = NULL then y ← TreeSuccessor(z) if left[y] = NULL then x ← left[y] else x ← right[y] if x = NULL then p[x] ← p[y] if p[y] = NULL then root[T] ← x else if y = left[p[y]] then left[p[y]] ← x else right [p[y]] ← x if y = z then key[z] ← key[y] return y Binary Search trees • Applications • Construction of a dictionary • In order travers gives a sorted sequence • Router tables in networking Binary Search trees • Balancing a binary search tree • Rotations • Self-balancing • Performed at key times Binary Search trees • Binary tree extensions • Quad tree • As a binary tree but based on 4 instead of 2 • Breaks up a 2D region into 4 parts • Handy for collision detection Questions?