trie and search trees dr. andrew wallace phd beng(hons) euring [email protected]

33
Trie and Search Trees Dr. Andrew Wallace PhD BEng(hons) EurIng [email protected]

Upload: melvyn-walsh

Post on 17-Dec-2015

224 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Trie and Search Trees Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

Trie and Search TreesDr. Andrew Wallace PhD BEng(hons) EurIng

[email protected]

Page 2: Trie and Search Trees Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

Overview

• Trie

• Binary search trees

Page 3: Trie and Search Trees Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

Trie

• Special type of tree• retrieval

• Dynamic sets

• Key is a string• Root node is an empty string

Page 4: Trie and Search Trees Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

Trie

• Anna : data

• Andrew : data

• Alexandra : data

• Alwen : data

• Bertil : data

• Bridget : data

• Beryl : data

A B

L N E R

W E N

A

Data

Page 5: Trie and Search Trees Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

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

Page 6: Trie and Search Trees Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

Trie Implementation

• As a linked list

• Each node contains• A letter• Link to child

• de la Brandais tree

Page 7: Trie and Search Trees Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

Trie operations

• Insert child

• Delete child

• Child• Look up

Page 8: Trie and Search Trees Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

Trie implementation

• Auto correct

• File structures

• DNA sequencing

• Data compression• LZ78• Huffman encoding

Page 9: Trie and Search Trees Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

Trie implementation

• LZ78• Lossless data compression• Dictionary based• Random access

Page 10: Trie and Search Trees Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

Trie implementation

• She sells sea shells

S H

E

_ L

S_

E A

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

L S

E

L

L

H

Page 11: Trie and Search Trees Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

TrieEncode(n)Dictionary empty Prefix empty DictionaryIndex 1while(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

Page 12: Trie and Search Trees Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

Huffman coding

• Frequency encoding

Frequency Value

5 1

8 2

10 3

15 4

20 5

8:25:1

13:*

Page 13: Trie and Search Trees Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

Huffman coding

Frequency Value

10 3

15 4

20 5

13 *

13:*10:3

23:*

Page 14: Trie and Search Trees Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

Huffman coding

Frequency Value

15 4

20 5

20:*15:3

35:*

23 *

Page 15: Trie and Search Trees Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

Huffman coding

Frequency Value

23 *

23:535:4

48:*35 *

Page 16: Trie and Search Trees Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

Huffman coding

Frequency Value

48 *

35:*23:*

48:*

Page 17: Trie and Search Trees Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

Huffman coding

• To encode:

• Right = 1 and left = 0

• Example• 1 = 110• 2 = 111• 3 = 10• 4 = 00• 5 = 01

8:25:1

13:*10:3

23:*

20:515:4

35:*

48:*

Page 18: Trie and Search Trees Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

Huffman coding

• Applications

• Zip file compression

• Jpeg

• PNG

Page 19: Trie and Search Trees Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

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 18

5 9 12 23

Page 20: Trie and Search Trees Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

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

Page 21: Trie and Search Trees Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

Binary Search trees

• Operations

• Search

• Insert

• Delete

Page 22: Trie and Search Trees Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

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

8 18

5 9 12 23

Page 23: Trie and Search Trees Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

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)

Page 24: Trie and Search Trees Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

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

6 18

5 8

9

9

10

10

Page 25: Trie and Search Trees Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

Binary Search treesTreeInsert(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

Page 26: Trie and Search Trees Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

Binary Search trees

• Delete a node

• Case 1• No sub trees

• Case 2• One sub tree

• Case 3 • Two sub trees

10

8 18

5 9 12

Page 27: Trie and Search Trees Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

Binary Search trees

• Case 2• Move sub tree to the delete node’s position• Delete 18• Move 12 upwards

10

8 18

5 9 12

12

Page 28: Trie and Search Trees Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

Binary Search trees

• Case 3• Delete the node • Promote the lowest value in the right sub tree• Delete 10• Promote 12

10

8 18

5 9 12

12

Page 29: Trie and Search Trees Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

Binary Search treesTreeDelete(T, z)

if left[z] = NULL or right[z] = NULLthen y TreeSuccessor(z)

if left[y] = NULLthen x left[y]else x right[y]

if x = NULLthen p[x] p[y]

if p[y] = NULLthen root[T] x

else if y = left[p[y]]then left[p[y]] x

else right [p[y]] xif y = z

then key[z] key[y]return y

Page 30: Trie and Search Trees Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

Binary Search trees

• Applications• Construction of a dictionary

• In order travers gives a sorted sequence

• Router tables in networking

Page 31: Trie and Search Trees Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

Binary Search trees

• Balancing a binary search tree• Rotations• Self-balancing

• Performed at key times

Page 32: Trie and Search Trees Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

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

Page 33: Trie and Search Trees Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

Questions?