Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

ΔΟΜΕΣ ΔΕΔΟΜΕΝΩΝ ΣΕ C - ΜΑΘΗΜΑ 6 (ΕΚΤΥΠΩΣΗ)

2,476 views

Published on

.

Published in: Education
  • Be the first to comment

  • Be the first to like this

ΔΟΜΕΣ ΔΕΔΟΜΕΝΩΝ ΣΕ C - ΜΑΘΗΜΑ 6 (ΕΚΤΥΠΩΣΗ)

  1. 1. C 6: . 1. 1. 2. 3. – 2. 1. 2. 3. ! " #$ 4. # 5. % # 3. # % # 1. &' C: ' 2. &' C: 3. &' C: (' – ) 4. &' C: " * + 5. &' C: % , - 6. &' C: % " 7. &' C: % $ " 8. &' C: .% , - 9. &' C: .% " 10. &' C: .% $ " 4. 1. &' C: " 2. &' C: 3. &' C: / !. % A. 1. 1. « » , : • ! " # ( $ ) ( " ) % ( ) . # & & : • ' - # ( • + ( ) " ( . • . '' ( # ) " . : • ) % : ! • * + # : ,,-,* • . "" : ,',/ : • # " « » (null tree). • 0 # " . • $ "" . A. 1. 2. 1 1 % « » # . 1 " # ,: ! -, /* ' & % , : " , *: $ " , !: " , ( ,) ,*,': , -: '. ,
  2. 2. A. 1. 3. 23 – * • %: ! " # $ • /% % : " & • * + : % • : + # • ! 0 * + : " & + # = 3 ! -, /* ' * 0 * 1 * 2 * 3 : !,,,*,' : 3 A. 2. 1. * " : • : $ " . • "'% : : • 4" " " # . • 1 " # « » 4 " 4 " " ! ( : + $ : • 1 0 " $ • 1 1 " $ • … • 1 H " $ ($ "" ) 1 & " $ " : A. 2. 2. 5 & ! 1: • 0 3 H " n=2H+1-1 # 2: • 1 " n # , 3 H log(n+1)-1 6 H 6 logn ! ( : ( , " : • " 2H # " • " # : 7 7 A. 2. 3. , ( " + #$ : • (init) • % (insert) • .% (delete) • (' (empty) • " # (data) • (traverse) 8 " : • • % « » • • % « » % " % : 8 ( ( $& , " " ( . ( % & ( ) ! % , -1 , AVL , ,+- .
  3. 3. A. 2. 4. 1 ! # 1 # : • 0 # (data), H (height) ( # : • # # • ( # # • 9 # # . . " : 1. 8 " % 3 2. : 1 " & # 3. 1 " & ( 23+1-1=15 ) 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 $ % & ' ( )data = 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 A. 2. 5. ! *+ 1 " ( % # ) : • # " (data) : • 0 (left) • 0 ( (right) • 0 % ( % % root) . . " : data left A right data left B right data left - right data left right data left * right data left / right " % : (left right) NULL ( # ( ( ). root A. 3. ! 1. 8" C: "& ** ' C " : • # (struct) ( : • data # ( % ). • left right ( # . typedef int elem; /* typos dedomenwn dendrou*/ struct node{ /* Typos komvou dendrou */ elem data; /* dedomena */ struct node *left; /* aristero paidi */ struct node *right; /* deksi paidi */ }; typedef struct node TREE_NODE; /* Sinwnimo tou komvou dendrou */ typedef struct node *TREE_PTR; /* Sinwnimo tou deikti komvou */ # ( "& main). A. 3. ! 2. 8" C: ! * ' NULL /* TR_init(): arxikopoiei to dendro */ void TR_init(TREE_PTR *root) { *root=NULL; } : • ( " $ !
  4. 4. A. 3. ! 3. 8" C: 0" – 9 * ' , #" NULL. /* TR_empty(): epistrefei TRUE/FALSE * analoga me to an to dendro einai adeio */ int TR_empty(TREE_PTR root) { return root == NULL; } A. 3. ! 4. 8" C: + # * ' $ ( ) # . /* TR_data(): epistrefei ta dedomena tou komvou pou deixnei o deiktis p */ elem TR_data(TREE_PTR p) { return p->data; } A. 3. ! 5. 8" C: * ) % * ' « % » # % : 1. # 2. % # % «5» data left 5 right )5: root * ! root A. 3. ! 5. 8" C: * ) % * /* TR_insert_root(): Eisagei to stoixeio x sti riza tou dendrou */ int TR_insert_root(TREE_PTR *root,elem x) { TREE_PTR newnode; if (*root!=NULL) return FALSE; newnode=(TREE_NODE *)malloc(sizeof(TREE_NODE)); if (!newnode) { printf("Adynamia desmeusis mnimis"); return FALSE; } newnode->data=x; newnode->left=NULL; newnode->right=NULL; *root=newnode; return TRUE; }
  5. 5. A. 3. ! 6. 8" C: * ! *! ' « » # : 1. # 2. # # *51!-;-' «7» # 2» data left 5 right data left 2 right data left 4 right data left 1 right node data left 7 right A. 3. ! 6. 8" C: * ! *" /* TR_insert_left(): Eisagei to stoixeio x ws aristero paidi tou node */ int TR_insert_left(TREE_PTR node,elem x) { TREE_PTR newnode; if (node->left!=NULL) return FALSE; newnode=(TREE_NODE *)malloc(sizeof(TREE_NODE)); if (!newnode) { printf("Adynamia desmeusis mnimis"); return FALSE; } newnode->data=x; newnode->left=NULL; newnode->right=NULL; node->left=newnode; return TRUE; } A. 3. ! 7. 8" C: * ( *# ' « ( » # ( : 1. # 2. ( # # *51!-;-' «7» # 4» data left 5 right data left 2 right data left 4 right data left 1 right node data left 7 right A. 3. ! 7. 8" C: * ( + /* TR_insert_right(): Eisagei to stoixeio x ws deksi paidi tou node */ int TR_insert_right(TREE_PTR node,elem x) { TREE_PTR newnode; if (node->right!=NULL) return FALSE; newnode=(TREE_NODE *)malloc(sizeof(TREE_NODE)); if (!newnode) { printf("Adynamia desmeusis mnimis"); return FALSE; } newnode->data=x; newnode->left=NULL; newnode->right=NULL; node->right=newnode; return TRUE; }
  6. 6. A. 3. ! 8. 8" C: $ % * ' % : 1. *" . 2. $ # . 3. % NULL 5!-)!.' % data left 5 right )5: root * ! root A. 3. ! 8. 8" C: $ % /* TR_delete_root(): Diagrafei ti riza enos dentrou efoson den exei paidia */ int TR_delete_root(TREE_PTR *root, elem *x) { if ((*root)->left!=NULL || (*root)->right!=NULL) return FALSE; *x=(*root)->data; free(*root); *root=NULL; return TRUE; } A. 3. ! 9. 8" C: $ # ' # ( parent) $ : 1. *" . 2. $ . 3. left # parent NULL. 5!-)!.' # «2» data left 5 right data left 2 right data left 4 right data left 1 right parent data left 7 right A. 3. ! 9. 8" C: $ # /* TR_delete_left(): Diagrafei to aristero paidi tou komvou parent (efoson den exei paidia) */ int TR_delete_left(TREE_PTR parent, elem *x) { TREE_PTR current; if (parent->left==NULL) return FALSE; current=parent->left; if (current->left!=NULL || current->right!=NULL) return FALSE; *x=current->data; free(current); parent->left=NULL; return TRUE; }
  7. 7. A. 3. ! 10. 8" C: $ ( # ' # ( parent) $ ( : 1. *" ( . 2. $ ( . 3. right # parent NULL. parent data left 5 right data left 2 right data left 4 right data left 1 right data left 6 right data left 7 right 5!-)!.' ( # «4» A. 3. ! 10. 8" C: $ ( # /* TR_delete_right(): Diagrafei to deksi paidi tou komvou node (efoson den exei paidia) */ int TR_delete_right(TREE_PTR parent, elem *x) { TREE_PTR current; if (parent->right==NULL) return FALSE; current=parent->right; if (current->left!=NULL || current->right!=NULL) return FALSE; *x=current->data; free(current); parent->right=NULL; return TRUE; } A. 4. & 1. - ! " $ " $ : *( % : (preorder). " 3 : + $ ! 8 ( 8 (inorder). " 3 : ! 8 + $ ( 8 (postorder). " 3 : ! 8 ( 8 + $ A. 4. & 2. 8" C: " ------------------------------------------------------------- (PRE-ORDER) : : !" ------------------------------------------------------------- procedure PRE-ORDER(n) (n ) n PRE-ORDER( n) PRE-ORDER( n) - end procedure n) 5! * !- *:' 5! *)!1'
  8. 8. A. 4. & 2. 8" C: # * " " : 10 146 1975 82 3 1 2 6 7 8 9 3 4 5 " ( 3 ): 10,6,5,2,3,7,8,14,19 A. 4. & 2. 8" C: + 1 ( " ): • + % # # # . • 0 & $ % $ # 10 146 1975 82 3 1 3 : 10,6,5,2,3,7,8,14,19 A. 4. & 2. 8" C: * /* TR_preorder(): Ektypwsi kata tin prodiatetagmeni diadromi */ void TR_preorder(TREE_PTR v) { if(v!=NULL) { TR_print_node(v); TR_preorder(v->left); TR_preorder(v->right); } } A. 4. & 3. 8" C: * ------------------------------------------------------------- (IN-ORDER) : : !" ------------------------------------------------------------- procedure IN-ORDER(v) (v ) IN-ORDER( v) v IN-ORDER( v) - end procedure n*: 5! * !- *:' 5! *)!1'
  9. 9. A. 4. & 3. 8" C: * * " * " : " ( 3 ): 2,3,5,6,7,8,10,14,19 10 146 1975 82 3 7 4 5 6 8 9 3 1 2 : ' " $ % ( $ ( ! % => " ) A. 4. & 3. 8" C: * 1 ( " ): • + % # # # . • 0 & $ % $ # 1 3 : 2,3,5,6,7,8,10,14,19 10 146 1975 82 3 A. 4. & 3. 8" C: * /* TR_inorder(): Ektypwsi kata tin endodiatetagmeni diadromi */ void TR_inorder(TREE_PTR v) { if(v!=NULL) { TR_inorder(v->left); TR_print_node(v); TR_inorder(v->right); } } A. 4. & 5. 8" C: ------------------------------------------------------------- # (POST-ORDER) : : # !" ------------------------------------------------------------- procedure POST-ORDER(v) (v ) POST-ORDER( v) POST-ORDER( v) v - end procedure n* ! 5! * !- *:' 5! *)!1'
  10. 10. A. 4. & 4. 8" C: ! * " " : " ( 3 ): 3,2,5,8,7,6,19,14,10 10 146 1975 82 3 9 6 5 4 8 7 3 2 1 A. 4. & 4. 8" C: " 1 ( " ): • + % # $ # # . • 0 & $ % $ # 1 3 : 3,2,5,8,7,6,19,14,10 10 146 1975 82 3 A. 4. & 4. 8" C: # /* TR_postorder(): Ektypwsi kata tin metadiatetagmeni diadromi */ void TR_postorder(TREE_PTR v) { if(v!=NULL) { TR_postorder(v->left); TR_postorder(v->right); TR_print_node(v); } } ,. ! *$ 1: " + " project tree.dev " # ( " . 1 « » % .

×