Presentation is loading. Please wait.

Presentation is loading. Please wait.

DATA STRUCTURE RECAP Yiqun Zhang University of Houston.

Similar presentations


Presentation on theme: "DATA STRUCTURE RECAP Yiqun Zhang University of Houston."— Presentation transcript:

1

2 DATA STRUCTURE RECAP Yiqun Zhang University of Houston

3 are all about PROGRAMS DATA STORE MANIPULATE In Memory

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…

5 How to store = data structure How to manipulate = algorithm Data Structure and Algorithms COSC 2320 are all about PROGRAMMING. Struggling, but rewarding.

6 Physical layout of data There are two main streams, just like: RoomsBOYSGIRLS?of three

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 can’t put it here and there! ) Not so easy to add or remove things... ( You can’t put it here and there! ) Easy to find things! Easy to find things!

8 Logical layout of data

9 linear data structures Contiguous: Arrays Contiguous: Arrays Scattered: Linked lists Scattered: Linked lists

10 This is array.

11 This is linked list.

12 ARRAY VS. LINKED LIST

13 0x00E4 0x00E5 0x00E6 0x00E7 +1 +2 +3 +0 0x00F1 0x0051 0x50C1 0xABCD null 0x0051 0x00F1 0x00A9 0x0051 0x50C1 0x00A9 0xABCD 0x00A9 null Memory Utilization Space complexity Random access Programming complexity Insert & Delete

14 linear data structures Contiguous: Arrays Contiguous: Arrays Scattered: Linked lists Scattered: Linked lists This is not enough.

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.

16 Apple: Bigger than bigger Customers came first should be served first.

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)

18 Stack Queue

19 Usage of stack and queue Stack: Stack: Evaluate expressions (homework 3). Evaluate expressions (homework 3). Recursion. Recursion. Queue: Queue: Scheduling Scheduling Buffers, pipes… Buffers, pipes…

20 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)

21 Think about a program that can print out first n Fibonacci numbers: Recursion The entrance: The recurrence relation: The exit:

22 Recursion stack

23 Now let’s go beyond linear data structures

24 A singly linked list Each node has only one succeeding element. Can they have more? Definitely! What does it look like?

25

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 node’s left sub-tree and smaller than the keys in all nodes in that node’s 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 node’s left sub-tree and smaller than the keys in all nodes in that node’s right sub-tree, the binary tree is sorted and is called binary search tree.

27 Tree Traversal Pre-order In-order Post-order 50, 17, 12, 23, 19, 72, 54, 87, 76 12, 17, 19, 23, 50, 54, 87, 72, 76 12, 19, 23, 17, 87, 54, 76, 72, 50

28 GRAPH A graph is a representation of a set of objects where some pairs of objects are connected by links

29

30 They are all graphs! Let’s do a comparison.

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 !

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.

33 abc Directed cyclic abc Directed acyclic What do they look like?

34 abc connected abc disconnected

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

36 What do they look like?

37 More complicated? a a b b c c d d e e f f g g h h i i j j

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

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

40 Something you use almost everyday?

41 Let’s be more serious

42 abc

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.

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

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

46 Now I know what is graph, then how to store it?

47 Adjacency Matrices

48 abc a b c abcabc

49 Adjacency Lists

50

51 Which to choose?

52 Dense graph vs. Sparse graph

53 If a graph is sparse, its adjacency matrix will have a lot of 0’s 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 it’s better to use adjacency list when the graph is sparse, use adjacency matrix if the graph is dense. a b c abcabc

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: It’s like traversing a binary tree level-by-level. (The nodes at each level are visited from left to right.) Vertex based: using queue.

55 Depth-first Traversal Start from 0. stack 0 Not visitedVisited 0 1 5 5 6 6 8 8 10 101 5 2 3 32 4 4 7 9 79 3 3 8

56 Breadth-first Traversal

57 Well then…

58


Download ppt "DATA STRUCTURE RECAP Yiqun Zhang University of Houston."

Similar presentations


Ads by Google