Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Introduction to Programming in C - Fall 2010 – Erez Sharvit, Amir Menczel 1 Introduction to Programming in C תרגול 11 20.12.2010.

Similar presentations


Presentation on theme: "1 Introduction to Programming in C - Fall 2010 – Erez Sharvit, Amir Menczel 1 Introduction to Programming in C תרגול 11 20.12.2010."— Presentation transcript:

1 1 Introduction to Programming in C - Fall 2010 – Erez Sharvit, Amir Menczel 1 Introduction to Programming in C תרגול 11 20.12.2010

2 מטרת התרגול רשימה דו-כיוונית עצים בינאריים 2 Introduction to Programming in C - Fall 2010 – Erez Sharvit, Amir Menczel

3 רשימה דו כיוונית (שאלת מבחן 1) רשימה מקושרת ממוינת ודו-כיוונית של איברים מוגדרת על פי ההגדרה הבאה: struct item { int value; int howManyAfter; Item* next; Item* prev; } Item; 3 Introduction to Programming in C - Fall 2010 – Erez Sharvit, Amir Menczel

4 רשימה דו כיוונית (שאלת מבחן 1– המשך) השדה value מציין את ערך האיבר, השדה howManyAfter מציין כמה איברים קיימים ברשימה אחרי האיבר, השדה next מצביע לאיבר הבא ברשימה והשדה prev מצביע לאיבר הקודם ברשימה. הרשימה ממוינת לפי השדה value בסדר עולה. הפונקציהitem* addItem(item *lst, int newVal) יוצרת ומוסיפה לרשימה הממוינת שראשה lst איבר חדש שערכו newVal למיקום הנכון ברשימה, ומעדכנת את כל השדות הרלוונטיים בכל הרשימה. 4 Introduction to Programming in C - Fall 2010 – Erez Sharvit, Amir Menczel

5 5 Item* addItem(Item *lst, int newVal) { Item *prev,*newHead=lst,*newItem; newItem = ?? 1 ?? ; newItem->value = newVal; for(prev=NULL;?? 2 ?? && newVal>lst->value; ?? 3 ??,lst=lst->next); newItem->next = lst; newItem->prev = prev; if (lst) { lst->prev = newItem; newItem->howManyAfter = lst->howManyAfter + 1; } else ?? 4 ??; if( ?? 5 ?? ) ?? 6 ??; else newHead = newItem; } Introduction to Programming in C - Fall 2010 – Erez Sharvit, Amir Menczel

6 6 while(prev) { ?? 7 ??; ?? 8 ??; } return newHead; ?? 1 ?? = (item*)malloc(sizeof(item)); ?? 2 ?? = lst ?? 3 ?? = prev=lst; ?? 4 ?? = newItem->howManyAfter=0; ?? 5 ?? = prev ?? 6 ?? = prev->next=newItem; ?? 7 ?? = prev->howManyAfter=prev->next->howManyAfter+1; orprev->howManyAfter++; ?? 8 ?? = prev=prev->prev; Introduction to Programming in C - Fall 2010 – Erez Sharvit, Amir Menczel

7 עצים בינאריים - חזרה 7 size = 9 height = 3

8 עץ בינארי - סריקות void inorder(node *r){ if(r!=NULL){ inorder(r->left); printf("\t %d",r->data); inorder(r->right); } } 8 void preorder( node *r) { if(r!=NULL) { printf("\t %d",r->data); preorder(r->left); preorder(r->right); } } void postorder( node *r) { if(r!=NULL) { postorder(r->left); postorder(r->right); printf("\t %d",r->data); } }

9 עץ בינארי - מעברים 9 1 1 9 9 8 8 6 6 3 3 2 2 5 5 בכיתה למדתם שלוש שיטות לסריקת עץ (pre, in & post order). עקוב אחר שלוש הסריקות בעץ הנתון ורשום משמאל לימין מהו סדר האיברים הנסרקים בכל אחת מהן. ב -pre הכוונה שקודם סורקים את האב ורק אח " כ את בניו ( השמאלי ואז הימני ). ב-post הכוונה שקודם סורקים את הבנים. ב-in הכוונה שסורקים בן שמאלי, אח"כ את האב ואז את הבן הימני. Introduction to Programming in C - Fall 2010 – Erez Sharvit, Amir Menczel

10 תרגיל 1 - פתרון 10 preorder:1,6,8,5,2,9,3 inorder:8,6,2,5,9,1,3 postorder:8,2,9,5,6,3,1 Introduction to Programming in C - Fall 2010 – Erez Sharvit, Amir Menczel

11 הגדרות typedef struct TreeNode { int iData;/*The data of this tree node. */ struct TreeNode *left ;/*Pointer to the left child. */ struct TreeNode *right ;/*Pointer to the right child. */ } TreeNode; typedef struct ListNode { int iData; /*The data of this list node. */ struct ListNode * next ; /*Pointer to the next node*/ } ListNode; 11 Introduction to Programming in C - Fall 2010 – Erez Sharvit, Amir Menczel

12 תרגיל 2 כתוב פונקציה הנקראת sum_tree שמקבלת כקלט מצביע לשורש עץ המכיל ערכים שלמים בצמתיו ומחזירה את סכום הערכים של צמתיו קדקודיו. 12 Introduction to Programming in C - Fall 2010 – Erez Sharvit, Amir Menczel

13 תרגיל 2 - פתרון float sum_tree(TreeNode* tree) { if (!tree) return 0; return ( (tree->iData) + (sum_tree(tree->left)) + (sum_tree(tree->right)) ) ; } 13 Introduction to Programming in C - Fall 2010 – Erez Sharvit, Amir Menczel

14 תרגיל 3 כתוב פונקציה ListNode* tree_to_inorder_list(TreeNode* root) המקבלת כקלט מצביע לעץ ומחזירה מצביע לרשימה מקושרת המחזיקה את אברי העץ לפי סידור inorder. הניחו כי הפונקציות הבאות קיימות: –curr = create_list_node(node->iData); –curNode= append_lists(leftNode, rightNode); 14 3 3 1 1 6 6 6 6 1 1 3 3 Introduction to Programming in C - Fall 2010 – Erez Sharvit, Amir Menczel

15 תרגיל 3 - פתרון ListNode* tree_to_inorder_list(TreeNode* root){ ListNode* curr, *left = NULL, *right = NULL; if (root){ left = tree_to_inorder_list(root->left); right = tree_to_inorder_list(root->right); curr = create_list_node(root->iData); curr->next = right; left = append_lists(left, curr); } return left; } 15 Introduction to Programming in C - Fall 2010 – Erez Sharvit, Amir Menczel

16 שאלת מבחן 2 נתונה ההגדרה הבאה של צומת בעץ בינארי: typedef struct tree tree; struct tree { int value; int count; tree* left, *right; }; 16 כת וב פונקציה רקורסיבית int fillNode(tree * node, int number) שמקבלת צומת בעץ ומספר שלם. הפונקציה תחשב כמה צמתים מתחת לצומת הנוכחי ( כולל הצומת הנוכחי ) מכילים את הערך number של node. את הערך שחושב תכניס למשתנה count ב - node. Introduction to Programming in C - Fall 2010 – Erez Sharvit, Amir Menczel

17 שאלת מבחן 2 - המשך כתוב פונקציה רקורסיבית void fillTree(tree * root) שמקבלת עץ בינארי ומחשבת עבור כל צומת בו כמה צמתים מתחתיו כולל הוא עצמו מכילים את הערך number. את תוצאת החישוב נכניס למשתנה count בצומת הנוכחי. 17 value=5 | count=3 2 | 3 2 | 1 3 | 3 3 | 1 5 | 12 | 13 | 15 | 1 tree 3 | 2

18 שאלת מבחן 2 - פתרון int fillNode(tree *node,int value){ int temp; if(!node) return 0; temp=fillNode(node->left, value) + fillNode(node->right, value); if(node->value==value) node->count = ++temp; return temp; } 18 Introduction to Programming in C - Fall 2010 – Erez Sharvit, Amir Menczel

19 שאלת מבחן 2 – פתרון (המשך) void fillTree(node *root){ if(!root) return; fillNode(root, root->value); fillTree(root->left); fillTree(root->right); } 19 Introduction to Programming in C - Fall 2010 – Erez Sharvit, Amir Menczel


Download ppt "1 Introduction to Programming in C - Fall 2010 – Erez Sharvit, Amir Menczel 1 Introduction to Programming in C תרגול 11 20.12.2010."

Similar presentations


Ads by Google