???Data structure ? - PowerPoint PPT Presentation

About This Presentation
Title:

???Data structure ?

Description:

Title: ( ) Author: jack Last modified by: Anny Created Date: 7/14/2004 12:57:30 PM Document presentation format – PowerPoint PPT presentation

Number of Views:45
Avg rating:3.0/5.0
Slides: 54
Provided by: Jack1155
Category:

less

Transcript and Presenter's Notes

Title: ???Data structure ?


1
???????
  • ???

2
???Data structure ?
  • ???????????????,???????

3
???????
  • ????(Table)
  • ??(stack)
  • ??(queue)
  • ??(list)
  • ?(tree)
  • ??(graph)
  • table, stack, queue????????List , tree,
    graph??????????

4
??(Stack)
  • ??????????????,???????????????????????,?????

Push
E
D
A
Stack
5
??(Stack)
  • ????????(LIFO)
  • LIFOlast in first out?
  • ????push ???????? pop ??????????

6
Push ????
  • int top0 //top???0
  • push(n)
  • if (topltMaxSize)
  • stacktopn
  • top
  • return 0
  • else
  • return -1

7
Pop ????
  • pop( )
  • if (topgt0)
  • top--
  • kstacktop
  • return k
  • else return -1

8
??(queue)
  • ????????????
  • FIFOFirst In First Out

queue0
queue2
queueN-1
in
queue1
??(queue)
9
??(queue)
  • ???queue0?queuen-1?n??????,???????????head,???
    ????????tail?
  • ?????head???headhead1?
  • ?????tail?????tailtail1
  • ??????headtail0
  • ?????headtail??
  • ????tail1? n ??

10
??(queue)
  • ?????????????????headtailn??
  • ?????queuen-1???queue0?????????????

11
???? (Linked List)
  • ?????????????

12
Linked List???
  • ???????(sequential list ,???)?????????????(inserti
    ons)?????(deletions)?
  • ??mat???cat?????? sat ?
  • Get a node that is currently unused let its
    address be paddr.
  • Set the data field of this node to mat.
  • Set paddrs link field to point to the address
    found in the link field of the node containing
    cat.
  • Set the link field of the node containing cat to
    point to paddr.

13
???????Link List
  • ???????
  • typedef struct list_node list_pointer
  • typedef struct list_node
  • char data4
  • list_pointer link
  • list_pointer ptr NULL

14
???????( node)
  • ?????? node
  • ptr (list_pointer) malloc(sizeof(list_node))
    //????????
  • ????? bat ?? list ?
  • strcpy(ptr-gtdata, bat)
  • ptr-gtlink NULL

15
Create a two-node list
  • typedef struct list_node list_pointer
  • typedef struct list_node
  • int data
  • list_pointer link
  • list_pointer ptr NULL
  • list_pointer create2()
  • list_pointer first, second
  • first (list_pointer) malloc(sizeof(list_node)
    )
  • second (list_pointer) malloc(sizeof(list_node
    ))
  • second-gtlink NULL
  • second-gtdata 20
  • first-gtdata 10
  • first-gtlink second
  • return first

16
????(Deletion )from a list
  • void delete(list_pointer ptr, list_pointer
    trail, list_pointer node)
  • / ptr may change, pass in the address of ptr
    /
  • / trail is the preceding node, ptr is the
    head of the list /
  • if (trail) trail-gtlink node-gtlink
  • else ptr (ptr)-gtlink
  • free(node)

17
Linked List???
  • ???(Polynomials)??

typedef struct poly_node poly_pointer typedef
struct poly_node int coef
int expon poly_pointer link
poly_pointer a, b, d
18
??linked lists
  • If the link field of the last node points to the
    first node in the list, all the nodes of a
    polynomial can be freed more efficiently.
  • Circular ????

19
?(Tree)
  • Tree????????????????
  • ?????????(??????????)???????????,???????,?????????
    ??

20
?(Tree)
  • ??????(node)???????????(branch)????
  • ????????????????,??????????????
  • ????????????(root)?
  • ??????????????(leaf)

21
?(Tree)
  • ?????????2???(?2?),??????(binary tree)

???(binary tree)
22
?(Tree)
  • Depth(??)???????????????,?????????
  • Height(??)??????????,???????
  • ?
  • ??B?G?E???????
  • ??????
  • ??A(?)?????

23
?????(binary search tree)
  • ?????,?????????????,???????????????(???)??????????
    ?(binary search tree)?

24
????????(traversal)
  • ??????????????,??????(traversal)?????????

????
?????
????
25
????????(traversal)
  • ???????,????????????????
  • ????(preorder traversal)
  • ????(inorder traversal)
  • ????(postorder traversal)

26
????(preorder traversal)
  • ???
  • ????
  • ??????????
  • ??????????
  • ??????
  • 50 35 25 40 36 41 60

27
????(inorder traversal)
  • ???
  • ??????????
  • ????
  • ??????????
  • ??????
  • 25 35 36 40 41 50 60

28
????(postorder traversal)
  • ???
  • ??????????
  • ??????????
  • ????
  • ??????
  • 25 36 41 40 35 60 50

29
????
  • ??????,??traversal??????
  • ??-ab/cde
  • ??ab-cd/e
  • ??abcde/-
  • ??????

30
???????
  • ?????????,??????

31
???????
  • ?????????,??????

32
??(Graph)
  • ??(Graph)????(Edge)???(node, Vertex)????????
  • G(V, E)
  • V vertex set
  • E edge set
  • ?????
  • Adjacency list
  • Adjacency matrix

33
?????
  • Breadth-first search (BFS)
  • Depth-first search (DFS)
  • Topological sort
  • Strongly connected components

34
???(undirected Graph)???
35
???(directed Graph)???
36
Breadth-First Search (BFS)
37
Depth-first search (DFS)
38
????(Topological sort)
  • ???????????????????????????????
  • ???????

39
Euler????
  • 1736?,??????????????,????    ???????????????????
        1. ????????    2. ?????????0?2?

40
Spanning Tree(???)
  • A spanning tree of a graph is just a subgraph
    that contains all the vertices and is a tree.
  • A graph may have many spanning trees
  • for instance the complete graph on four vertices

41
Minimum spanning tree
  • The weight of a tree is just the sum of weights
    of its edges.
  • Lemma Let X be any subset of the vertices of G,
    and let edge e be the smallest edge connecting X
    to G-X. Then e is part of the minimum spanning
    tree.

42
Kruskal's algorithm
  • ????,??????????
  • Kruskal's algorithm
  • sort the edges of G in increasing order by length
  • keep a subgraph S of G, initially empty
  • for each edge e in sorted order
  • if the endpoints of e are disconnected in S
  • add e to S
  • return S
  • ????Greedy method (?????)

43
???(edge)?????????????(minimum-cost spanning
trees)??
Sort 5,6,10,12,15,18,21,24,25,30
(a)AB (b)CD (c)CE (d)EF
Minimum cost 56121824 65
44
Minimum cost spanning tree
???????????(Minimum cost spanning
tree)???? (a)17 (b)20 (c)22 (d)14
45
????(Shortest Path)
46
????(Shortest Path)
47
????(Shortest Path)
48
????(Shortest Path)
49
??(sort)
  • ?????(?????)O(n2)
  • ?????(?????)O(n2)
  • ?????(?????)O(nlog2n)
  • Heap ??(?????)O(nlog2n)
  • ????(Merge sort) O(nlog2n)
  • Shell ??(?????)O(n1.2)

50
Merge Sort
  • Definition A sort algorithm that splits the
    items to be sorted into two groups, recursively
    sorts each group, and merges them into a final,
    sorted sequence.
  • Run time is T(n log n).

51
??(Search)
  • ????? O(n)
  • ????? O(log2n)

52
(No Transcript)
53
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com