trie and search trees dr. andrew wallace phd beng(hons) euring andrew.wallace@cs.umu.se

Post on 17-Dec-2015

224 Views

Category:

Documents

2 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Trie and Search TreesDr. 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 B

L N E R

W E 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 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

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

Huffman coding

• Frequency encoding

Frequency Value

5 1

8 2

10 3

15 4

20 5

8:25:1

13:*

Huffman coding

Frequency Value

10 3

15 4

20 5

13 *

13:*10:3

23:*

Huffman coding

Frequency Value

15 4

20 5

20:*15:3

35:*

23 *

Huffman coding

Frequency Value

23 *

23:535:4

48:*35 *

Huffman coding

Frequency Value

48 *

35:*23:*

48:*

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:*

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 18

5 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

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

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

6 18

5 8

9

9

10

10

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

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

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

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

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

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?

top related