5 Node typedef struct Node int val struct

  • Slides: 32
Download presentation

5 פתרון : כלומר Node - ראשית נוסיף כאמור מצביע נוסף ל typedef struct

5 פתרון : כלומר Node - ראשית נוסיף כאמור מצביע נוסף ל typedef struct Node{ int val; struct Node *left, *right, *next; //next field is for the list }Node;

Node * tree 2 List(Node * root) { Node * left = NULL, *right

Node * tree 2 List(Node * root) { Node * left = NULL, *right = NULL, *tmp = root; if (root) { left = tree 2 List(root->left); right = tree 2 List(root->right); root->next = right; if(left){ tmp=left; while(left->next)//advance to the list tail left=left->next; left->next=root; } } return tmp; } : דוגמה left=tree 2 List(T left); left 1 3 4 5 right=tree 2 List(T right); right 11 12 T next=right; T 8 11 12 while(left next) left=left next; left next=T; tmp 1 3 4 5 8 11 12

void tree 2 List(Node **lst, Node *root){ if(!root) return; tree 2 List(lst, root->right); root->next

void tree 2 List(Node **lst, Node *root){ if(!root) return; tree 2 List(lst, root->right); root->next = *lst; *lst = root; tree 2 List(lst, root->left); } : פתרון נוסף מצרפת tree 2 List פונקציה רשימה של העץ שהיא יוצרת אל הרשימה שמקבלת . כארגומנט הראשון הרשימה של העץ מתווספת . lst משמאל לרשימה הנתונה void main(){ … Node *tmp=NULL; //why tmp must be initialized to 0? tree 2 List(&tmp, tree. Root); //now tmp holds the head of list } tree 2 List(lst, T right); lst 11 12 tree 2 List(lst, T left); n next = (*lst); (*lst)=n; lst 8 11 : דוגמה 12 lst 1 3 … 12

Tree. Node * create. Search. Tree(List. Node * lst) { List. Node * first

Tree. Node * create. Search. Tree(List. Node * lst) { List. Node * first , * second, *temp; Tree. Node * root = NULL; if (!lst) return NULL; /* lst is pointing to the "root to be"*/ root = allocate. Tree. Node(lst->val); lst 8 3 1 } 4 12 11 5 4 8 temp = lst->next; if (temp && (temp->val < lst->val)) { while (temp->next && (temp->next->val < lst->val)) first temp = temp->next; second = temp->next; /* second points to right subtree list. */ temp->next = NULL; first = lst->next; } else { /* No possible left sub-tree*/ first = NULL; second = lst->next; } root->left = create. Search. Tree(first); root->right = create. Search. Tree(second); return root; 5 3 second 1 12 11

7 תרגיל : נתונה ההגדרה הבאה struct tree. Node { int val; struct tree.

7 תרגיל : נתונה ההגדרה הבאה struct tree. Node { int val; struct tree. Node* left; struct tree. Node* right; } : והפונקציה int what(struct tree. Node* node){ if(node == NULL) return 1; if(node->left == NULL && node->right == NULL) return 1; if(node->left == NULL && node->val == node->right->val && what(node->right)) return 1; if(node->right == NULL && node->val == node->left->val && what(node->left)) return 1; if(node->val == node->left->val * node->right->val && what(node->left ) && what(node->right)) return 1; return 0; } ? what מה יעודה של הפונקציה

1 תרגיל ? הבאה התכנית עושה מה void main() { FILE *fp; char ch,

1 תרגיל ? הבאה התכנית עושה מה void main() { FILE *fp; char ch, st[4]; if (!(fp=fopen("testfile. txt", "r"))) exit(1); while ((ch=fgetc(fp))!=EOF) printf("%c", ch); rewind(fp); while (fgets(st, 4, fp)!=NULL) printf("%s", st); if(fclose(fp)==EOF) exit(1); rename("testfile. txt", "testfile 2. txt"); remove("testfile 2. txt"); }

 קריאה מקובץ , כתיבה לקובץ , – יצירת קובץ 2 תרגיל : רשום

קריאה מקובץ , כתיבה לקובץ , – יצירת קובץ 2 תרגיל : רשום טבלת מעקב עבור התכנית הבאה void main() { FILE *fp; char str[100]; int res, num; fp=fopen("testfile. txt", "w"); if (!fp) exit(1); fprintf(fp, "test"); fclose(fp); fp=fopen("testfile. txt", "a"); fprintf(fp, " file"); fclose(fp); fp=fopen("testfile. txt", "w"); fprintf(fp, "line. A 10 tenn"); fprintf(fp, "line. B 20 twentyn"); fprintf(fp, "line. C 30 thirtyn"); fclose(fp); fp=fopen("testfile. txt", "r"); • res=fscanf(fp, "%s%d", str, &num); // line #1 // str=line. A num=10 res=2 res=fscanf(fp, "%d%s", &num, str); // 2 // res=0 res=fscanf(fp, "%2 s%*1 s", str); // 3 // str=te res=1 res=fscanf(fp, "%*c"); // 4 // res=0 res=fscanf(fp, "%8 s%1 d", str, &num); // 5 // str=line. B num=2 res=fscanf(fp, "%[^n]%*c", str); // 6 // str=0 twenty res=1 res=fscanf(fp, "%8[^$]%*c", str); // str=line. C 30 res=1 res=fscanf(fp, "%*[^n]%*c"); // 8 // res=0 res=fscanf(fp, "%d", &num); // 7 // 9 // res=-1=EOF fclose(fp); }

 – תוכנית ראשית 4 שאלה void main () { int ans; ans=diff("1. txt",

– תוכנית ראשית 4 שאלה void main () { int ans; ans=diff("1. txt", "2. txt"); if (ans==-1) printf("Error openning filesn"); else if (ans==1) printf("Identical filesn"); else printf("Different filesn "); }

6 תשובה לשאלה #include <stdio. h> #include <string. h> void calc. Final. Grades(char* filename.

6 תשובה לשאלה #include <stdio. h> #include <string. h> void calc. Final. Grades(char* filename. SA, char* filename. C); void main() { calc. Final. Grades("shana-aleph. txt", "c. txt"); } void calc. Final. Grades(char* filename. SA, char* filename. C) { FILE *f. SA, *f. C, *f. Out; int id. SA, id. C, test. C, avodot. C[6], ok. SA, ok. C; char name. SA[100]; float avg; f. SA = fopen(filename. SA, "r"); f. C = fopen(filename. C, "r"); f. Out = fopen("out. txt", "w"); if(!f. SA || !f. C || !f. Out) { printf("Error opening file. Exiting. "); return; } // reading first line from both files ok. SA = fscanf(f. SA, "%d %[^n]%*c", &id. SA, name. SA); ok. C = fscanf(f. C, "%d %d", &id. C, &test. C); for (i=0; i<6; i++) ok. C = fscanf(f. C, "%d", &avodot. C[i]); fscanf(f. C, "%*c"); // sa = shana-aleph

6 תשובה לשאלה while(ok. SA!=EOF && ok. C!=EOF) { // while both files did

6 תשובה לשאלה while(ok. SA!=EOF && ok. C!=EOF) { // while both files did not reach the end if (id. SA==id. C) {// if IDs are equal avg=0; // calc & print average for (i=0; i<6; i++) avg += avodot. C[i]; avg=(avg/6)*0. 3 + test. C*0. 7; fprintf(f. Out, "%s %. 2 fn", name. SA, avg); // progress in both files ok. SA = fscanf(f. SA, "%d %[^n]%*c", &id. SA, name. SA); ok. C = fscanf(f. C, "%d %d", &id. C, &test. C); for (i=0; i<6; i++) ok. C = fscanf(f. C, "%d", &avodot. C[i]); fscanf(f. C, "%*c"); continue; } if (id. SA<id. C) { // if first id is smaller than the second ok. SA = fscanf(f. SA, "%d %[^n]%*c", &id. SA, name. SA); // progress in the first file continue; } if (id. SA>id. C) {// if second id is smaller than the first ok. C = fscanf(f. C, "%d %d", &id. C, &test. C); // progress in the second file for (i=0; i<6; i++) ok. C = fscanf(f. C, "%d", &avodot. C[i]); fscanf(f. C, "%*c"); continue; } } // while fclose(f. SA); fclose(f. C); fclose(f. Out); }