Data Structure in C - ???? - PowerPoint PPT Presentation

1 / 55
About This Presentation
Title:

Data Structure in C - ????

Description:

Title: Introduction C Language Author: kurt Last modified by: Piggy Created Date: 2/15/2003 7:36:33 PM Document presentation format: – PowerPoint PPT presentation

Number of Views:66
Avg rating:3.0/5.0
Slides: 56
Provided by: kurt111
Category:
Tags: data | heap | sort | structure

less

Transcript and Presenter's Notes

Title: Data Structure in C - ????


1
Data Structure in C -
????
2
??
  • ???? - ????
  • ???? - ???
  • ???
  • ?????
  • ?????
  • ??

3
???? -????
  • ????
  • ??(node)??(edge)
  • ??(ancestor)?????(descendant)??
  • ???(parent node)????(children node)
  • ????(sibling node)
  • ?????(non-terminal node)
  • ????(terminal node)?????(leaf node)
  • ???(degree)
  • ??(level)
  • ??(path)

4
A?K?????,K?A?????
A?B, C, D????,B, C, D?A????
B, C, D?????
J, K, L, G, M, N, I?????,????????????
A?????3
?B??,???2,???1
5
???? -???? (?)
  • ???(Forest)???
  • ???n gt 0???????(disjoint trees)??????,???????????

6
???? - ???
  • ????? (p. 3)
  • ?????
  • ???????????????
  • Ex. ?p.3??????(A(B(E(J), F(K, L)), C(G), D(H(M,
    N), I))
  • ?? - ?????
  • ???????????(???)??,????????,????????? ?
    ??-??(left child-right sibling)???

7
???? - ??? (?)
  • ???????????(left most)????????(closest right)????
  • ??????????????????

8
???? - ??? (?)
  • ?????2????
  • ???-?????????????????45???

9
???
  • ???(binary tree)
  • ???????????????,??????????????????????(right
    subtree)????(left subtree)
  • ??(?????????)
  • ????????????(???????????)
  • ???????????(??????)
  • ???????????????2 (??????)

10
??? (?)
????? (fully binary tree)
??? (left skewed tree)
????? (complete binary tree)
11
??? (?)
  • ??
  • ???????i?????????2i-1,igt1
  • ????(???)?k????,2k-1,kgt1
  • ?????,?n0?????????,n2????????2???,n0n21
  • ???
  • ???????
  • ??????,???????,???????
  • ???????,???????????,????????????

12
??? (?)
13
  • ?????
  • ???????????????
  • ?????????????(parent)
  • ???????????????PARENT,?????????

typedef struct node tree_pointer typedef struct
node int data tree_pointer
left_child, right_child
14
??? (?)
  • ?????(traversal)
  • ???????????,????????????
  • ????
  • ????(inorder)??????(L????),??????(V??,????),???
    ???(R????)
  • ????(preorder)?????(V) ,???????(L) ,??????(R)
  • ????(postorder)??????(L) ,???????(R) ,?????(V)

15
void inorder(Node type tree) if (tree !
NULL) inorder(tree-gtllink)
printf(d, tree-gtdata)
inorder(tree-gtrlink)
????A/BCDE
void preorder(Node type tree) if (tree !
NULL) printf(d, tree-gtdata)
preorder(tree-gtllink) preorder(tree-gtrlin
k)
void postorder(Node type tree) if (tree !
NULL) postorder(tree-gtllink)
postorder(tree-gtrlink) printf(d,
tree-gtdata)
????/ABCDE
????ABC/DE
16
(No Transcript)
17
  • ??????????????
  • ????????????,???????,?????
  • ???????????????
  • ????????????????????
  • ?????45?

18
  • ????????????
  • ??
  • ??????????????(???45?)
  • ???????????????????
  • ?????45?

19
??? (?)
  • ??????????
  • ????
  • ?????????(priority)????(associativity)???????
  • ??????????,??????,??????,??????????????,??????????
    ???

20
(No Transcript)
21
??? (?)
  • ????????
  • ????inorder?preorder?????????????
  • ????inorder?postorder?????????????
  • ????preorder?postorder??????????????
  • ??pp. 6-706-73

22
?????
  • ?????(binary search tree)
  • ????????????,??????,???????,??????????????(key
    value),????????
  • ????????????????????
  • ????????????????????
  • ??????????????
  • ????????
  • ????????(input sequence),?????????????,?????????,?
    ???????????

23
????? (?)
  • ????????
  • ????????,??????,????????????,????????

??48
??90
24
  • / ???????,?????????????? /
  • void access(char name, int score)
  • struct student node, prev
  • if(search(name) ! NULL) / ?????????? /
  • printf("Student s has existed!\n", name)
  • return
  • ptr (struct student ) malloc(sizeof(struct
    student))
  • strcpy(ptr-gtname, name)
  • ptr-gtscore score
  • ptr-gtllink ptr-gtrlink NULL

25
  • if(root NULL) / ?????NULL??? /
  • root ptr
  • else / ??????NULL??? /
  • node root
  • while(node ! NULL) / ??????? /
  • prev node
  • if(strcmp(ptr-gtname, node-gtname) lt 0)
  • node node-gtllink
  • else
  • node node-gtrlink
  • if(strcmp(ptr-gtname, prev-gtname) lt 0)
  • prev-gtllink ptr
  • else
  • prev-gtrlink ptr

26
  • / ??target???? /
  • struct student search(char target)
  • struct student node
  • node root
  • while(node ! NULL)
  • if(strcmp(target, node-gtname) 0)
  • return node
  • else
  • / target??????,???? /
  • if(strcmp(target, node-gtname) lt 0)
  • node node-gtllink
  • else / target??????,???? /
  • node node-gtrlink
  • return node

27
????? (?)
  • ????????
  • ?????????,??????
  • ??????????,????????????????????????,?????????

28
????? (?)
?????????
?????????
29
  • / ???????????? /
  • void removing(char name)
  • struct student del_node
  • if((del_node search(name)) NULL) /
    ?????????? /
  • printf("Student s not found!\n", name)
  • return
  • / ??????????? /
  • if(del_node-gtllink ! NULL del_node-gtrlink !
    NULL)
  • del_node replace(del_node)
  • else / ?????????? /
  • if(del_node root)
  • root NULL
  • else
  • connect(del_node, 'n')
  • free(del_node) / ????? /
  • printf("Data of student s deleted!\n", name)

30
  • / ?????????????? /
  • struct student replace(struct student node)
  • struct student re_node
  • / ???????????,?????????????? /
  • if((re_node search_re_r(node-gtrlink)) NULL)
  • re_node search_re_l(node-gtllink)
  • if(re_node-gtrlink ! NULL) / ?????????????? /
  • connect(re_node, 'r')
  • else
  • if(re_node-gtllink ! NULL) / ??????????????
    /
  • connect(re_node, 'l')
  • else / ????????????? /
  • connect(re_node, 'n')
  • strcpy(node-gtname, re_node-gtname)
  • node-gtscore re_node-gtscore
  • return re_node

31
  • / ????????? /
  • struct student search_re_r(struct student node)
  • struct student re_node
  • re_node node
  • while(re_node ! NULL re_node-gtllink ! NULL)
  • re_node re_node-gtllink
  • return re_node
  • / ????????? /
  • struct student search_re_l(struct student node)
  • struct student re_node
  • re_node node
  • while(re_node ! NULL re_node-gtrlink ! NULL)
  • re_node re_node-gtrlink
  • return re_node

32
  • / ??????????,link?r???????,?l??????,
  • ?m??????NULL /
  • void connect(struct student node, char link)
  • struct student parent
  • parent search_p(node) / ????? /
  • / ???????????? /
  • if(strcmp(node-gtname, parent-gtname) lt 0)
  • if(link 'r') / link?r /
  • parent-gtllink node-gtrlink
  • else
  • if(link 'l') / link?l /
  • parent-gtllink node-gtllink
  • else / link?m /
  • parent-gtllink NULL

33
  • else / ???????????? /
  • if(link 'r') / link?r /
  • parent-gtrlink node-gtrlink
  • else
  • if(link 'l') / link?l /
  • parent-gtrlink node-gtllink
  • else / link?m /
  • parent-gtrlink NULL

34
  • / ??node???? /
  • struct student search_p(struct student node)
  • struct student parent
  • parent root
  • while(parent ! NULL)
  • if(strcmp(node-gtname, parent-gtname) lt 0)
  • if(strcmp(node-gtname, parent-gtllink-gtname)
    0)
  • return parent
  • else
  • parent parent-gtllink
  • else
  • if(strcmp(node-gtname, parent-gtrlink-gtname)
    0)
  • return parent
  • else
  • parent parent-gtrlink
  • return NULL

35
????? (?)
  • ??????????????
  • ??
  • ??????????queue?
  • ??????????????
  • ??????????,????????????,?????????,??????????
  • ????????????
  • ????????,???????????????,???????stack?,???????

36
?????
  • ?????(threaded binary tree)
  • ????????????link field?null link,????????link?????
    ,???link???????(thread)???
  • ??????????

1. ?LBIT1?,LLINK?????
2. ?LBIT0?,LLINK???
3. ?RBIT1?,RLINK?????
4. ?RBIT0?,RLINK???
37
???? (???????)
38
????? (?)
  • ??
  • ??????????????stack,??????????????
  • ????????????,????????????????????????
  • ?????????????????,????????????

39
????? (?)
  • ????????

40
  • void insert_right( struct tbintree
    node_parent,struct tbintree node)
  • struct tbintree w
  • node-gtrchild node_parent-gtrchild
  • node-gtrbit node_parent-gtrbit
  • node-gtlchild node_parent
  • node-gtlbit 0
  • node_parent-gtrchild node
  • node_parent-gtrbit 1
  • if ( node-gtrbit 1 ) /node????tree/
  • w insucc( node )
  • w-gtlchild node

????????????
41
  • void insert_left( struct tbintree
    node_parent,struct tbintree node)
  • struct tbintree w
  • node-gtlchild node_parent-gtlchild
  • node-gtlbit node_parent-gtlbit
  • node-gtrchild node_parent
  • node-gtrbit 0
  • node_parent-gtlchild node
  • node_parent-gtlbit 1
  • if ( node-gtlbit 1 ) /node ????tree/
  • w inpred( node )
  • w-gtrchild node

????????????
42
  • ????????

43
  • if ( ptr-gtlbit 0 ptr-gtrbit 0 )
  • if ( ptr_parent root ) /???????/
  • ptr_parent-gtlchild root
  • ptr_parent-gtlbit 0
  • /?????/
  • else if ( ptr-gtnumber lt ptr_parent-gtnumber )
  • ptr_parent-gtlchild ptr-gtlchild
  • ptr_parent-gtlbit 0
  • else /?????/
  • ptr_parent-gtrchild ptr-gtrchild
  • ptr_parent-gtrbit 0
  • free(ptr)

?????????????
44
  • else if ( ptr-gtlbit 1 ptr-gtrbit 1 )
  • /?ptr??????,???????????/
  • ptr_pred inpred ( ptr)
  • ptr_pred-gtrchild ptr-gtrchild
  • ptr_pred-gtrbit ptr-gtrbit
  • ptr_parent-gtlchild ptr-gtlchild
  • free(ptr)
  • else /????????/
  • if ( ptr_parent root ) /??????/
  • if ( ptr-gtlbit 1 )
  • ptr_pred inpred(ptr)
  • root-gtlchild ptr-gtlchild
  • ptr_pred-gtrchild root
  • else
  • ptr_succ insucc(ptr)
  • root-gtlchild ptr-gtrchild
  • ptr_succ-gtlchild root

45
  • else
  • if ( ptr-gtnumber lt ptr_parent-gtnumber )
  • ptr_parent-gtlchild ptr-gtlchild
  • else
  • ptr_parent-gtrchild ptr-gtrchild

??????????????
46
??
  • ??(heap)
  • ??????????,?????????????,??????????
  • ??????????????(???????????)
  • Heap??????,??Heap Sort
  • ???????????,??heap sort???????????????
  • ??,??????????????????,??????Heap,???????Stack(????
    )?Queue(????)???

47
  • Heap???
  • ????
  • ?????? ,?????????,????????????,????
  • ??????,???????????

48
?? (?)
  • Heap???

49
  • void insert_f(void)
  • int id_temp
  • if(last_index gt MAX) / ???????,?????? /
  • printf("\n Login members are more than
    d!!\n", MAX)
  • printf(" Please wait for a minute!!\n")
  • else
  • printf("\n Please enter login ID number ")
  • scanf("d", id_temp)
  • create(id_temp) / ???? /
  • printf(" Login successfully!!\n")
  • void create(int id_temp) / ID_TEMP????? /
  • heap_treelast_index id_temp / ????????
    /
  • adjust_u(heap_tree, last_index) / ?????? /

50
  • void adjust_u(int temp, int index) /
    INDEX?????????INDEX /
  • while(index gt 1) / ??????????? /
  • if(tempindex lt tempindex/2) /
    ?????????,?????? /
  • break
  • else
  • exchange(tempindex, tempindex/2)
  • index / 2
  • void exchange(int id1, int id2) /
    ?????ID1?ID2????? /
  • int id_temp
  • id_temp id1
  • id1 id2
  • id2 id_temp

51
?? (?)
  • Heap???

52
  • void delete_f(void)
  • int id_temp, del_index
  • if(last_index lt 1) / ?????,?????? /
  • printf("\n No member to logout!!\n")
  • printf(" Please check again!!\n")
  • else
  • printf("\n Please enter logout ID number ")
  • scanf("d", id_temp)
  • del_index search(id_temp) / ??????? /
  • if(del_index 0) / ?????,?????? /
  • printf(" ID number not found!!\n")
  • else
  • removes(del_index) / ????,?????? /
  • printf(" ID number d logout!!\n", id_temp)

53
  • int search(int id_temp) / ?????ID_TEMP?? /
  • int c_index
  • for(c_index 1 c_index lt MAX c_index)
  • if(id_temp heap_treec_index)
  • return c_index / ????????????INDEX /
  • return 0 / ??????0 /
  • void removes(int index_temp) /
    INDEX_TEMP???????INDEX /
  • / ????????????? /
  • heap_treeindex_temp heap_treelast_index
  • heap_treelast_index-- 0
  • if(last_index gt 1) / ???????1?,???? /
  • / ????????PARENT NODE,????? /
  • if(heap_treeindex_temp gt heap_treeindex_temp
    / 2 index_temp gt 1)
  • adjust_u(heap_tree, index_temp)
  • else / ???????CHILDEN NODE,????? /
  • adjust_d(heap_tree, index_temp, last_index-1)

54
  • void adjust_d(int temp, int index1, int index2)
  • / ID_TEMP??????,INDEX_TEMP???????CHILDEN
    NODE?INDEX /
  • int id_temp, index_temp
  • id_temp tempindex1
  • index_temp index1 2
  • / ??????INDEX??????????INDEX,????? /
  • while(index_temp lt index2)
  • if((index_temp lt index2) (tempindex_temp lt
    tempindex_temp1))
  • index_temp / INDEX_TEMP???????CHILDEN
    NODE???? /
  • if(id_temp gt tempindex_temp) /
    ???????,?????? /
  • break
  • else
  • tempindex_temp/2 tempindex_temp
  • index_temp 2
  • tempindex_temp/2 id_temp

55
?? (?)
  • ??min-heap?
  • max-heap?????????????????
  • min-heap??????????????????
Write a Comment
User Comments (0)
About PowerShow.com