DATA STRUCTURE RECAP Yiqun Zhang University of Houston
Slide 3
are all about PROGRAMS DATA STORE MANIPULATE In Memory
Slide 4
A CHAMPION IN LOL What and how to store: What and how to store:
Name / Appearance Name / Appearance Max / Current health Max /
Current health Max / Current mana Max / Current mana Spells Spells
Position Position Armor Armor Magic resist Magic resist Health
& mana regen Health & mana regen Kill / Death / Assist Kill
/ Death / Assist And so on And so on How to manipulate: Move (hit
the wall?) Physical damage Cast a spell Stun? Silenced? Slowed? And
so on
Slide 5
How to store = data structure How to manipulate = algorithm
Data Structure and Algorithms COSC 2320 are all about PROGRAMMING.
Struggling, but rewarding.
Slide 6
Physical layout of data There are two main streams, just like:
RoomsBOYSGIRLS?of three
Slide 7
Scattered Easy to take in and throw out things. ( Dirty vs.
Dirtier, not so different! ) Easy to take in and throw out things.
( Dirty vs. Dirtier, not so different! ) Hard to find things Hard
to find things Can use up all the tiny spaces Can use up all the
tiny spaces Mixed Hybrid. Hybrid. Contiguous Not so easy to add or
remove things... ( You cant put it here and there! ) Not so easy to
add or remove things... ( You cant put it here and there! ) Easy to
find things! Easy to find things!
Slide 8
Logical layout of data
Slide 9
linear data structures Contiguous: Arrays Contiguous: Arrays
Scattered: Linked lists Scattered: Linked lists
linear data structures Contiguous: Arrays Contiguous: Arrays
Scattered: Linked lists Scattered: Linked lists This is not
enough.
Slide 15
Tower of Hanoi You cannot move the disk at the bottom without
taking off the disks above it. i.e. The last disk put on the rod
has to be moved first.
Slide 16
Apple: Bigger than bigger Customers came first should be served
first.
Slide 17
LIFO & FIFO Last in first out: Stack Last in first out:
Stack First in last out: Queue First in last out: Queue They are
restrictions that can be applied on both arrays and linked lists.
When applied, those data structures can only add or remove elements
in a certain way. (i.e. Stack and queue can be implemented by both
array and linked list) They are restrictions that can be applied on
both arrays and linked lists. When applied, those data structures
can only add or remove elements in a certain way. (i.e. Stack and
queue can be implemented by both array and linked list)
Recursion A recursion function must have: An entrance (initial
conditions) An entrance (initial conditions) A recurrence relation
A recurrence relation An exit (cannot be a infinite loop) An exit
(cannot be a infinite loop)
Slide 21
Think about a program that can print out first n Fibonacci
numbers: Recursion The entrance: The recurrence relation: The
exit:
Slide 22
Recursion stack
Slide 23
Now lets go beyond linear data structures
Slide 24
A singly linked list Each node has only one succeeding element.
Can they have more? Definitely! What does it look like?
Slide 25
Slide 26
Root node Inner node Branch node If the heights of the two
child sub-trees of any node differ by at most one, the binary tree
is balanced (AVL). If the heights of the two child sub-trees of any
node differ by at most one, the binary tree is balanced (AVL). If
the key in any node is larger than the keys in all nodes in that
nodes left sub-tree and smaller than the keys in all nodes in that
nodes right sub-tree, the binary tree is sorted and is called
binary search tree. If the key in any node is larger than the keys
in all nodes in that nodes left sub-tree and smaller than the keys
in all nodes in that nodes right sub-tree, the binary tree is
sorted and is called binary search tree.
GRAPH A graph is a representation of a set of objects where
some pairs of objects are connected by links
Slide 29
Slide 30
They are all graphs! Lets do a comparison.
Slide 31
Linked ListTreeGraph Can contain cycles Can be disconnected
Multiple succeeding / child elements Modellinear hierarchic al
network Graphs are composed of a set of vertices (nodes) and edges,
just like linked lists and trees, but with graphs there are no
rules for the connections between nodes! Freedom !
Slide 32
What do they look like? abc undirected abc directed a can
directly reach b, vice versa. a can directly reach b, but b cannot
directly reach a.
Slide 33
abc Directed cyclic abc Directed acyclic What do they look
like?
Slide 34
abc connected abc disconnected
Slide 35
d d e e f f abc What do they look like? It can even consist of
two disconnected parts! Sub-graph 1 Sub-graph 2
Slide 36
What do they look like?
Slide 37
More complicated? a a b b c c d d e e f f g g h h i i j j
Slide 38
Edges can have weights! a a b b c c d d e e f f g g h h i i j j
2 1 4 2 3 1 4 3 5 2 2 4 2 1 6 2 1 2
Slide 39
Much more complicated? b b a a c c d d e e f f g g h h i i j j
k k l l n n o o m m
Slide 40
Something you use almost everyday?
Slide 41
Lets be more serious
Slide 42
abc
Slide 43
In-degree and Out-degree The in-degree of a vertex v is the
number of edges with v as their terminal vertex. The out-degree of
a vertex v is the number of edges with v as their initial
vertex.
Slide 44
In-degree b b a a c c d d e e f f g g h h i i j j k k l l n n o
o m m
Slide 45
Out-degree b b a a c c d d e e 3 3 g g h h i i j j k k l l n n
o o m m Sink
Slide 46
Now I know what is graph, then how to store it?
Slide 47
Adjacency Matrices
Slide 48
abc a b c abcabc
Slide 49
Adjacency Lists
Slide 50
Slide 51
Which to choose?
Slide 52
Dense graph vs. Sparse graph
Slide 53
If a graph is sparse, its adjacency matrix will have a lot of
0s which are useless and wasting a lot of memory space. If a graph
is dense, each node in its adjacency list will be linked to a very
long linked list. Traversing will be very inefficient So its better
to use adjacency list when the graph is sparse, use adjacency
matrix if the graph is dense. a b c abcabc
Slide 54
Traversing a graph Depth-First Traversal: It is like pre-order
traversal of trees. Go as deep as possible. Edge based: using
stack. Breadth-First Traversal: Its like traversing a binary tree
level-by-level. (The nodes at each level are visited from left to
right.) Vertex based: using queue.