Upload
others
View
8
Download
0
Embed Size (px)
Citation preview
Algorithms and Data StructuresExercise I
Banafsheh Azarihttp://www.uni-weimar.de/de/medien/professuren/grafische-
datenverarbeitung/
Bauhaus-University Weimar - CogVis/MMC
November 8, 2016
c©2016 , Algorithms and Data Structures Exercise I Exercise I 1/22
Exercise
• Tuesday ,13:30 , SR013, B11
• Odd weeks
• First Exercise : 08.11.2016
c©2016 , Algorithms and Data Structures Exercise I Exercise I 2/22
Exercise
Conditions
• C/C++ or Java
• Compilable source code (gcc or javac)
• No IDEs
• Readable source code
• Self-contained submission
c©2016 , Algorithms and Data Structures Exercise I Exercise I 3/22
Exercise
Comments
• Sufficient comments (rule of thumb: another Developers canget program logic alone by reading the comments)
• In each! Data: Name, matriculation number, WS2016-2017
c©2016 , Algorithms and Data Structures Exercise I Exercise I 4/22
Exercise
• Tasks: www.uni-weimar.de/de/medien/professuren/grafische-datenverarbeitung/
• Submission: per E-Mail as NameLastname E . . .
c©2016 , Algorithms and Data Structures Exercise I Exercise I 5/22
Data Structures
• Array
• Linked Lists
• Stacks• LIFO• Push• pop
• Queues• FIFO• enqueue• dequeue
• Trees
• Graphs
c©2016 , Algorithms and Data Structures Exercise I Exercise I 6/22
Data Structures
• Array
• Linked Lists
• Stacks• LIFO• Push• pop
• Queues• FIFO• enqueue• dequeue
• Trees
• Graphs
c©2016 , Algorithms and Data Structures Exercise I Exercise I 6/22
Data Structures
• Array
• Linked Lists
• Stacks• LIFO• Push• pop
• Queues• FIFO• enqueue• dequeue
• Trees
• Graphs
c©2016 , Algorithms and Data Structures Exercise I Exercise I 6/22
Data Structures
• Array
• Linked Lists
• Stacks• LIFO• Push• pop
• Queues• FIFO• enqueue• dequeue
• Trees
• Graphs
c©2016 , Algorithms and Data Structures Exercise I Exercise I 6/22
Data Structures
• Array
• Linked Lists
• Stacks• LIFO• Push• pop
• Queues• FIFO• enqueue• dequeue
• Trees
• Graphs
c©2016 , Algorithms and Data Structures Exercise I Exercise I 6/22
Data Structures
• Array
• Linked Lists
• Stacks• LIFO• Push• pop
• Queues• FIFO• enqueue• dequeue
• Trees
• Graphs
c©2016 , Algorithms and Data Structures Exercise I Exercise I 6/22
Data Structures
• Array
• Linked Lists
• Stacks• LIFO• Push• pop
• Queues• FIFO• enqueue• dequeue
• Trees
• Graphs
c©2016 , Algorithms and Data Structures Exercise I Exercise I 6/22
Polynomial
Start
p 0 o 1 n 2 m 4
g�x� = mx� + nx + ox + p
coef expo next
c©2016 , Algorithms and Data Structures Exercise I Exercise I 7/22
Polynomial Addition
Start
Start
a 5 b 3 c 1
p 0 o 1 n 2 m 4
f�x� = ax� + bx + cx
g�x� = mx� + nx� + ox + p
h�x� = f�x� + g�x� = ax� +mx� + bx + nx� + �c + o�x + p
c©2016 , Algorithms and Data Structures Exercise I Exercise I 8/22
Infix– and Postfix Expression
• Infix expression(A * (B + (C / D) ) )
• Postfix expression(A (B (C D /) +) *)
• Prefix expression(* A (+ B (/ C D) ) )
c©2016 , Algorithms and Data Structures Exercise I Exercise I 9/22
Converting Example
stack
original expression 5/7+2*4*3
postfix expression 5
push /
5 7
pop / push +
5 7 / 5 7 / 2
push *
5 7 / 2 4
pop * push *
5 7 / 2 4 *
pop *
5 7 / 2 4 * 3 *
pop +
5 7 / 2 4 * 3 * +
+ *
+ *
+
/
+
c©2016 , Algorithms and Data Structures Exercise IExercise I10/22
Calculation Example
stack
original expression
2 4 * 9 5 + -
push 2
push 4
pop 4 pop 2 push 2*4
push 9 push 5
pop 5 pop 9 push 9+5
pop 14 pop 8 push 8-14
-6
8
14
8
9
5
8
2
4
2
c©2016 , Algorithms and Data Structures Exercise IExercise I11/22
Trees
A
S M
E P L
Q T
Terminology• Node
• External node: leaf• Internal node
• Edge
• Path
• Root
c©2016 , Algorithms and Data Structures Exercise IExercise I13/22
Trees
A
S M
E P L
Q T
N=number of nodesM=number of edgesM=N-1
c©2016 , Algorithms and Data Structures Exercise IExercise I14/22
Trees
A
S M
E P L
Q T
traversing methods
c©2016 , Algorithms and Data Structures Exercise IExercise I16/22
Tree
A
S M
E P N L
X K Q T
traversing methods
• Preorder
• Inorder
• Postorder
• Level-Order
c©2016 , Algorithms and Data Structures Exercise IExercise I17/22
Huffman coding
ABACCDA
010100010000000111010 21 bit
Character D C B A
Code 111 000 100 010
00010010101100 14 bit
Character D C B A
Code 11 10 01 00
0110010101110 13 bit
Character D C B A
Code 111 10 110 0
c©2016 , Algorithms and Data Structures Exercise IExercise I19/22
Huffman coding
Character S I N T E
Frequency 4 5 9 10 29
4 5 9 10 29
4 5
4+5
1 0
9 9 10 29
9 9
9+9
1 0
9 9
18
1 0
5
0
4
1
18 29 10
10 18
10+18
1 0
c©2016 , Algorithms and Data Structures Exercise IExercise I20/22
Palindrome & Anagram
”madam”m ———– ma ———– ad ———– da ———– a
m ———– m
c©2016 , Algorithms and Data Structures Exercise IExercise I21/22
Assignment I
Deadline: 21.November 2016 24:00Write a program that use the given table to produce Hoffman codeand read a sentence made of 0 and 1 as input, decode it andreturns the result as output
Character A E S L P O F
Frequency 20 11 6 9 18 5 10
c©2016 , Algorithms and Data Structures Exercise IExercise I22/22