Presentation is loading. Please wait.

Presentation is loading. Please wait.

Recursion Practice requiring pointers

Similar presentations


Presentation on theme: "Recursion Practice requiring pointers"— Presentation transcript:

1 Recursion Practice requiring pointers

2 Allan Technique Programming Recursively
Decide that the problem needs a recursive solution. Decide specifically what the procedure will do. What are its inputs and what are its outputs. In a sentence, what does it accomplish? Restate the problem recursively. Look for instances of the problem you defined in step 2. If the procedure returns a value, recursive calls to the function must use the returned values and every path through the function must return a value. Write the procedure using instances of itself. Only worry about a single call of the routine. This is similar to proof by induction. You only solve for one case, but all other cases take care of themselves. Do not be tempted to follow the recursion. Use the routines having the faith that they will work. If you try to follow the recursion while you are writing the procedure, you will become hopelessly lost. How can you follow the recursion when the recursive routine is not finished yet? Make sure you take care of the base case stopping the recursion. I like to take care of this last as it is easier to pinpoint the ending conditions after the general case has been written. Some personalities may not be able to delay this decision, but you will find coding is faster if you are able to delay it. It is like delaying the condition on a loop. In all but the simplest of loops, it is easier to write the termination condition after the loop body is written. Once the routine is written, go ahead and follow the recursion, if you wish.

3 . Class Node{ char data; Node * left; Node * right; } Node * root; in Tree class points to root of the tree.

4 Given a tree Write the recursive code to count the number of nodes in the tree.

5 Assume two classes Class Tree{ Node * root; } Class Node { public: int val; Node* left; Node* right;

6 Given a tree Write the recursive code to count the number of nodes in the tree. int count(Node* curr) { if (curr==NULL) return 0; int leftCt = count(curr->left); int rightCt= count(curr->right); return 1+leftCt+rightCt;

7 Given a tree Write the recursive code to find the largest node in a tree. This is NOT a binary search tree.

8 Given a tree Write the recursive code to find the largest node in a tree. This is NOT a binary search tree. int findBig(Node* curr) { if (curr==NULL) return –maxInt; int leftBig = findBig(curr->left); int rightBig = findBig(curr->right); return max (leftbig,righBig,curr->val); }

9 Write the code to insert into a binary search tree.

10 Write the code to insert into a binary search tree.
BSTNode* insert(BSTNode* r, int value) { if(r == NULL) return new Node(value); if(value <= r->value) { if(r->left == NULL) r->left = new Node(value); else r->left = insert(node->left, value); } else if(r->right == NULL) r->right = new Node(value); r->right = insert(node->right, value); return r; }

11 Write the code to insert into a binary search tree.
//The NERVOUS approach – not falling off the tree void BST::insert(int key, Node *r) { assert(r!=NULL); // Would have to handle inserting into empty tree elsewhere if(key< r->element) { if(r->left!=NULL) insert(key, r->left); else r->left=new Node(key, NULL, NULL); } { if(r->right!=NULL) insert(key, r->right); else r->right=new Node(key,NULL,NULL);

12 Write the code to insert into a binary search tree.
//Falling off tree void BST::insert(int key, Node * &r) { if (r==NULL){ r = new Node(key,NULL, NULL); return; } if(key< r->element) insert(key, r->left); else insert(key, r->right);

13 Given two trees Write the recursive code to determine if one tree is the mirror image of the other.

14 bool mirror(Node* t1, Node* t2)
Given two trees Write the recursive code to determine if one tree is the mirror image of the other. bool mirror(Node* t1, Node* t2) { if (t1==NULL && t2==NULL) return true; if (t1==NULL || t2==NULL) return false; if (t1->val != t2->val) return false; bool OK1 = mirror(t1->left, t2->right) bool OK2 = mirror(t1->right, t2->left) return OK1&& OK2; } Can we make it better?

15 Given a tree Write the recursive code to determine if the tree is leftist. By leftist we mean that a node never has a right child without a left child.

16 Given a tree Write the recursive code to determine if the tree is leftist. By leftist we mean that a node never has a right child without a left child. Bool isLeftist(Node * t) { if (t==NULL) return true; if (t->left ==NULL && t->right !=NULL) return false; leftOK = isLeftist(t->left); rightOK = isLeftist(t->right) return leftOK && rightOK; }

17 Given a left most child next right sibling tree
Count the maximum number of kids any node has. In the tree below, maxKids is 3. Class Node{ char data; Node * leftChild; Node * rightSib; } Node * root; in Tree class points to root of the tree.

18 Iint getMaxKids(Node. t) { int myKids = 0; int maxKids = 0; for (Node
Iint getMaxKids(Node * t) { int myKids = 0; int maxKids = 0; for (Node * kid = t->leftChild; kid != NULL; kid = kid=>rightSib){ myKids++; maxKids = max(maxKids, getMaxKids(kid->leftChild)); } return max(maxKids,myKids);

19 For a multiway tree (stored as left child next right sibling) See if two trees are identical (same values and same shape)

20 For a multiway tree (stored as left child next right sibling) See if two trees are identical (same values and same shape) bool same(Node * t1, Node * t2) { if (t1==NULL && t2==NULL) return true; if (t1==NULL || t2==NULL) return false; if (t1->value !=t2->value) return false; Node * kid1, *kid2; for(kid1=t1->leftChild, kid2=t2->leftChild; kid1!=NULL && kid2!=NULL; kid1=kid1->rightSib, kid2=kid2->rightSib) if (!same(kid1,kid2)) return false; return(kid1==NULL && kid2==NULL); }


Download ppt "Recursion Practice requiring pointers"

Similar presentations


Ads by Google