Download presentation
Presentation is loading. Please wait.
Published byMelvyn Walsh Modified over 9 years ago
1
Trie and Search Trees Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se
2
Overview Trie Binary search trees
3
Trie Special type of tree retrieval Dynamic sets Key is a string Root node is an empty string
4
Trie Anna : data Andrew : data Alexandra : data Alwen : data Bertil : data Bridget : data Beryl : data A B L NER W EN A Data
5
Trie Implementation As a table 2 x 2 array One columns per letter in the alphabet, n One row per node, m log 2 m to represent the data
6
Trie Implementation As a linked list Each node contains A letter Link to child de la Brandais tree
7
Trie operations Insert child Delete child Child Look up
8
Trie implementation Auto correct File structures DNA sequencing Data compression LZ78 Huffman encoding
9
Trie implementation LZ78 Lossless data compression Dictionary based Random access
10
Trie implementation She sells sea shells SH E _L S _ E A Step PhraseOutput 1 S0, S 2 H0, H 3 E0, E 4 _0, _ 5 SE1, E 6 L0, L 7 LL6, L 8 S_1, _ 9 A 0, A 10 _SHELLS4, SHELLS LS E L L H
11
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
12
Huffman coding Frequency encoding FrequencyValue 51 8 2 103 154 20 5 8:2 5:1 13:*
13
Huffman coding FrequencyValue 103 154 20 5 13* 13:* 10:3 23:*
14
Huffman coding FrequencyValue 154 20 5 20:* 15:3 35:* 23 *
15
Huffman coding FrequencyValue 23 * 23:5 35:4 48:* 35 *
16
Huffman coding FrequencyValue 48 * 35:* 23:* 48:*
17
Huffman coding To encode: Right = 1 and left = 0 Example 1 = 110 2 = 111 3 = 10 4 = 00 5 = 01 8:2 5:1 13:*10:3 23:* 20:5 15:4 35:* 48:*
18
Huffman coding Applications Zip file compression Jpeg PNG
19
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 818 591223
20
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
21
Binary Search trees Operations Search Insert Delete
22
Binary Search trees 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) 10 818 591223
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)
24
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 618 58 9 9 10
25
Binary Search trees
26
Delete a node Case 1 No sub trees Case 2 One sub tree Case 3 Two sub trees 10 818 5912
27
Binary Search trees Case 2 Move sub tree to the delete node’s position Delete 18 Move 12 upwards 10 818 5912
28
Binary Search trees Case 3 Delete the node Promote the lowest value in the right sub tree Delete 10 Promote 12 10 818 5912
29
Binary Search trees
30
Applications Construction of a dictionary In order travers gives a sorted sequence Router tables in networking
31
Binary Search trees Balancing a binary search tree Rotations Self-balancing Performed at key times
32
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
33
Questions?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.