5 - String and Matrix-Note Version
5 - String and Matrix-Note Version
String
◼ ai(1≤i≤n)can be letters、numbers or
other character
a=‘BEI’ b=‘JING’
c=‘BEIJING’ d=‘BEI JING’
basic concepts of string S=‘a1a2…an’(n0)
i=4
s: a c a b a a b a a b c a c a a b c
t: a b a a b c a c
j=1
i=5 i=6
s: a c a b a a b a a b c a c a a b c
t: a b a a b c a c
j=1 j=2
i=6i=7 i=8 i=9 i=10i=11i=12i=13i=14
s: a c a b a a b a a b c a c a a b c
t: a b a a b c a c
j=1j=2j=3 j=4 j=5 j=6 j=7 j=8j=9
BF algorithm:
➢loop condition?
➢when the pointers need to go back?
➢how to calculate i & j?
➢when to be done?
➢how to find the position?
Exam & Experiment project
• two courses
• INFO3L3901 Data structures/3902 Experiment
• the online part will finish on July 9th/ upload the rest class
• paper exam
• when?I don’t know!
• level of these points
➢description
➢program
Huffman tree
– optimal decision tree
– minimum time
letter E D C B A 40
C
Grade 0~59 60~69 70~79 80~89 90~100 30
B
% 0.05 0.15 0.40 0.30 0.10
15
D
Y a<60 Y 70a<80 5 10
A E
E N C N
Y a<70 Y 80a<90
D N B N
Y a<80 Y 60a<70
C N D N
Y a<90 Y a<60
B N E N
A A
Huffman tree
• a weighted path length of the smallest binary tree
• path:from a node to another node
• path length: the number of edges on the path
• weight:describe sth‘s property as value/number(percentage、
priority lever…)
w={5, 29, 7, 8, 14, 23, 3, 11} 29 29 42
14 15 23 19
5 29 7 8 14 23 3 11 7 8 11 8
29 7 8 14 23 11 8
5 3
5 3
42 58
29 14 23 11 8 15
23 19 29 29
5 3 7 8
11 8 14 15
29 14 23 15 19
5 3 7 8
7 8 11 8
100
5 3
29 23 19 29 42 58
11 8 14 15 23 19 29 29
5 3 7 8 11 8 14 15
5 3 7 8
Tree Traversals
• 遍历
• 普遍 经历
• visiting every element
• why don’t we discuss “traversal” in List?
• simple:predecessor/successor
• only one way in List(linear)
• multiple successor
• how to choose your way?
• purpose:nonlinear to linear
• L:visit the left subtree
• D:visit the root
• R:visit the right subtree
• How many possible order?
• 6:LDR、LRD、DLR、DRL、RLD、RDL
• left first,then 3 left:
• DLR:preorder traversal
• LDR:inorder traversal
• LRD:postorder traversal
preorder traversal
inorder traversal
postorder traversal
1-D matrix:array
2-D matrix
3-D matrix????
• How to save high dimension matrix?
• Dimension reduction (降维)
• 2D-> 1D
• row major order
• column major order
• Lists
sparse matrix
• suppose the Matrix A(m × n) has t non zero elements;
• t far less than the elements in the matrix
• t≦m×n
• A is a sparse array
row : 5 5 5 8
col : 6
Multi lists 十字链表 i j e
down right
3 0 0
0 5 4
A=
0 0 0
8 0 0
• Experiment Part
• Session6-Part1
• wright a program to implement the pattern matching
• locate the substring
• count the times