• Tidak ada hasil yang ditemukan

CSL201 Data Structures

N/A
N/A
Protected

Academic year: 2025

Membagikan "CSL201 Data Structures"

Copied!
29
0
0

Teks penuh

(1)

Lecture 19

Tries

(2)

Linked Stac List

Queu k e

Hea p

Priority Queue

Hash Table

BST AVL Multiway Red-Black Algorithm Analysis

Tools

(3)

Is finding a pattern same as finding a

word in the

dictionary?

(4)

How fast can

you search for a word in a set of

words?

(5)

How to make

ordered tree for

strings?

(6)

Standard Tries

Except root, each node is labeled with a character

The children of a node are alphabetically ordered

The path from root to external nodes yields the string

(7)

a

e

b

r

l l

s u

l l

y

e t

l l

o c

k

p i

d

S = { bear, bell, bid, bull, buy, sell, stock, stop }

(8)

Insert

• Assumption: no word is prefix of other word!

• What if this is not the

case?

(9)

Time complexity

• n total size of the strings in S

• m size of given string

• d size of the alphabet

• s number of words

(10)

Search?

O(m)

(11)

Insert & delete?

O(md)

(12)

Word Matching with a Trie

s e e b e a r ? s e l l s t o c k ! s e e b u l l ? b u y s t o c k ! b i d s t o c k !

a a

h e t h e b e l l ? s t o p ! b i d s t o c k !

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 2425 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86

a r

87 88

a

e

b

l

s u

l

e t

e 0, 24

o c

i

l r

6

l 78

d 47, 58

l 30

y 36

l

12 k

17, 40, 51, 62

p 84 h

e

r 69

a

(13)

How many

nodes do we need?

Can we reduce the

number of nodes?

(14)

a

e

b

r

l l

s u

l l

y

e t

l l

o c

k

p i

d

e

b

ar ll

s u

ll y

ell to

ck p

id

Compressed

Tries

(15)

Compressed Tries

A compressed trie has internal nodes of degree at least two

It is obtained from standard trie by compressing chains of

“redundant” nodes

A redundant node is the one that has only one child

(16)

How many nodes do I have in a

compressed Trie?

(17)

Compressed Trie Properties

Every internal node of T has at least two children and at most d children

T has s leaf nodes

The number of internal nodes of T is O(s)

(18)

s e e b e a r s e l l s t o c k

b u l l b u y b i d

h e b e l l s t o p

0 1 2 3 4

a r

S[0] = S[1] = S[2] = S[3] =

S[4] = S[5] = S[6] =

S[7] = S[8] = S[9] =

0 1 2 3 0 1 2 3

1,0,0

1,1,1 4,1,1 0,1,1 3,1,2

0,0,0 7,0,3

6,1,2

1,2,3 8,2,3 4,2,3 5,2,2 0,2,2 2,2,3 3,3,4 9,3,3

e

b

ar ll

s u

ll y

ell to

ck p

id

hear

e ll

e

(19)

Compact

Representation of Tries

Stores at the nodes ranges of indices instead of substrings

Uses O(s) space, where s is the number of strings in the array

Serves as an auxiliary index structure

(20)

Insertion/Deletion in Compressed Trie

f g

reat one

u or

n r

insert green

f g

reat one

u or

n r

insert green

search ends here

f g

re one

u or

n r at en

(21)

Suffix Trie

e nimize

nimize ze

ze

i mi

mize nimize ze

m i n i m i z e 0 1 2 3 4 5 6 7

7

3 1 5 0 4

2 6

(22)

Suffix Trie

Not all strings are guaranteed to have corresponding suffix trie.

For example: xabxa

a bxa bxa

xa

bxa

a xa

bxa

bxa $ $ bxa

(23)

Pattern Matching Using Suffix Trie

Compressed Trie

Auxiliary Trie

(24)

How much space do we need to make a suffix

trie of n character

string?

(25)

To Trie or Not to Trie?

What would you use to store

phonebook?

(26)

Constructing a Suffix Trie

S[1..n] is the string

Start with a single edge for S

Enter the edges for the suffix S[i..n]

where i goes from 2 to n

Starting at the root node find the

longest part from the root whose label matches a prefix of S[i..n]. At some

point, no further matches are possible

If the point is at a node, then denote this node by w

If it is in the middle of an edge, then insert a new node called w, at this point

Create a new edge running from w to a new leaf labeled S[i..n]

(27)

Example

minimize

minimize minimize

minimize inimize

minimize

minimize

minimize

inimize nimize

minimize nimize

mize nimize

i

(28)

Example

minimize

minimize nimize

mize nimize

i

minimize

nimize

mize nimize

i

inimize ize

m

(29)

Example

e nimize

nimize ze

ze

i mi

mize nimize ze

m i n i m i z e 0 1 2 3 4 5 6 7

7

3 1 5 0 4

2 6

Referensi

Dokumen terkait

Information Gain is generally used to determine the splitting attribute for the root node and internal nodes in a decision tree.. The attribute with the highest

The two main patterns are (1) short-season maize has low yields compared to current yields under changed cli- mate scenarios except in western Illinois, and (2) the halved

Whenever you could have used the operator to eval- uate part of the infix expression (if you were evaluating instead of translating to postfix), you instead copy it to the

Score: 0 Accepted Answers: Type: String G,0 In the map, starting with node S, list the first 7 nodes inspected or expanded by the Best First Search algorithm.. If the algorithm

It keeps an ordered queue Q of nodes that may still need to be split, and proceeds by repeatedly doing the following: if the first node in the tree contains only “positives”, make it a

17 Anatomy of a linked list A linked list consists of: ◦ A sequence of nodes a b c d Each node contains a value and a link pointer or reference to some other node The last node

 Postcondition: Each node at the tree is visited in the following order: First visit the root at the tree, then visit recursively all nodes in the left subtree, then visit recursively

Activity Outcomes: This lab teaches you the following topics:  A linked list  Implementation of linked list using java  Insert Node at the beginning of the list  Traverse List