Presentation is loading. Please wait.

Presentation is loading. Please wait.

Exercise 5 on Binary Tree Due Date : 6 October Bonus of 2 points if submitted on or before the due date.

Similar presentations


Presentation on theme: "Exercise 5 on Binary Tree Due Date : 6 October Bonus of 2 points if submitted on or before the due date."— Presentation transcript:

1 Exercise 5 on Binary Tree Due Date : 6 October Bonus of 2 points if submitted on or before the due date

2 The Family Ancestry Make a program that will create a binary tree of a family starting from the parents. The program should : Make a program that will create a binary tree of a family starting from the parents. The program should : 1. Create first the root of the binary family tree 2. Add the child to either left or right. (max of 2 children only) 3. Before inserting a surname, search first its parent. Use either inorder/preorder/postorder traversal to search. 4. The program should be able to post a comment for the person, i.e. “dead”, “in USA”, “in the moon”, etc..

3 Illustration John –dead, Mary Anne, Roger-dead Mike, Mary root

4 Your program may have the following options 1- Create parent 1- Create parent 2- Search a person 2- Search a person 3- Insert a Child 3- Insert a Child 4- Post a comment 4- Post a comment

5 Declaration struct node { char *name; char *name; char *remarks; char *remarks; char *partner_name; char *partner_name; char *partner_remarks; char *partner_remarks; node * left, *right; node * left, *right;}

6 Now let’s write the algorithms Create parent Create parent Search a person Search a person Insert a child Insert a child Post a comment Post a comment

7 For 1 bonus point..give me a start.. function CreateParent(root) 1. root = new node; 2. root->name = inputname; 3. root->partnername = partner; 4. root->remarks= “”; 5. root->partner_remarks=“”; 6. return root;

8 Search SearchPerson(isfound,node, inputname) If node !=NULL if node->name = inputname if node->name = inputname isfound = true isfound = true return(node) return(node) else else if not isfound { if not isfound { return(SearchPerson(node->left); return(SearchPerson(node->left); if not isfound() if not isfound() return(SearchPerson(node->right); return(SearchPerson(node->right);

9 Insert a Child InsertChild(parentname,childname,root) parent = SearchPerson(isfound,root,parentname) If parent <> NULL childnode =CreateChild(childname); childnode =CreateChild(childname); if parent->left <> NULL if parent->left <> NULL parent->left = childnode; parent->left = childnode; else else parent->right = childnode; parent->right = childnode;

10 Post A Comment PostComment(inputname,comment,root) person= SearchPerson(isfound,root,inputname) If person <> NULL then person-> remarks = comment person-> remarks = comment


Download ppt "Exercise 5 on Binary Tree Due Date : 6 October Bonus of 2 points if submitted on or before the due date."

Similar presentations


Ads by Google