Recursion Practice requiring pointers

Slides:



Advertisements
Similar presentations
Recursion practice. Problem 0 Using recursion (and no arrays), write the code to read in a series of numbers (until EOF) and then print them backwards.
Advertisements

Proof Techniques and Recursion. Proof Techniques Proof by induction –Step 1: Prove the base case –Step 2: Inductive hypothesis, assume theorem is true.
1 General Trees & Binary Trees CSC Trees Previous data structures (e.g. lists, stacks, queues) have a linear structure. Linear structures represent.
CS21, Tia Newhall Binary Search Trees (BST) 1.Hierarchical data structure with a single pointer to root node 2.Each node has at most two child nodes (a.
1 Search Trees - Motivation Assume you would like to store several (key, value) pairs in a data structure that would support the following operations efficiently.
Sorted Array What is BigO for sorted list implemented as: ArrayList: – Search : – Insert(value) : – Remove(value) : LinkedList: – Search : – Insert(value)
Recursion Bryce Boe 2013/11/18 CS24, Fall Outline Wednesday Recap Lab 7 Iterative Solution Recursion Binary Tree Traversals Lab 7 Recursive Solution.
INTRODUCTION TO BINARY TREES P SORTING  Review of Linear Search: –again, begin with first element and search through list until finding element,
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 13 Recursion Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
CS 1031 Trees A Quick Introduction to Graphs Definition of Trees Rooted Trees Binary Trees Binary Search Trees.
Advance Data Structure 1 College Of Mathematic & Computer Sciences 1 Computer Sciences Department م. م علي عبد الكريم حبيب.
CMSC 341 Introduction to Trees. 2/21/20062 Tree ADT Tree definition –A tree is a set of nodes which may be empty –If not empty, then there is a distinguished.
1/14/20161 BST Operations Data Structures Ananda Gunawardena.
Trees 3 The Binary Search Tree Section 4.3. Binary Search Tree Also known as Totally Ordered Tree Definition: A binary tree B is called a binary search.
1 Binary Trees and Binary Search Trees Based on Dale & Co: Object-Oriented Data Structures using C++ (graphics)
Binary Search Trees (BST)
Trees As you arrive: 1.Snarf the code for today’s class 2.Pick up a handout 3.Go ahead and start on the first page of the handout.
Mergeable Heaps David Kauchak cs302 Spring Admin Homework 7?
1 Recursive algorithms Recursive solution: solve a smaller version of the problem and combine the smaller solutions. Example: to find the largest element.
Let’s try it one more time!. Allan Technique Programming Recursively 1.Decide that the problem needs a recursive solution. 2.Decide specifically what.
TREES From root to leaf. Trees  A tree is a non-linear collection  The elements are in a hierarchical arrangement  The elements are not accessible.
Recursion Topic 5.
Data Structures and Design in Java © Rick Mercer
CSCE 3110 Data Structures & Algorithm Analysis
CSCE 3110 Data Structures & Algorithm Analysis
Binary Trees and Binary Search Trees
Binary Search Trees A binary search tree is a binary tree
Binary Trees Linked lists: efficient insertion/deletion, inefficient search ArrayList: search can be efficient, insertion/deletion not Binary trees: efficient.
UNIT III TREES.
Lecture 5 Trees.
CISC220 Fall 2009 James Atlas Lecture 13: Binary Trees.
16IT201/Data structure/Unit II/Binary Search Tree
Lecture 22 Binary Search Trees Chapter 10 of textbook
CMSC 341 Introduction to Trees.
O(lg n) Search Tree Tree T is a search tree made up of n elements: x0 x1 x2 x3 … xn-1 No function (except transverse) takes more than O(lg n) in the.
i206: Lecture 13: Recursion, continued Trees
Chapter 20: Binary Trees.
Binary Search Trees.
Chapter 21: Binary Trees.
General Trees & Binary Trees
Lec 12 March 9, 11 Mid-term # 1 (March 21?)
AVL Trees: AVL Trees: Balanced binary search tree
Tree A tree is a data structure in which each node is comprised of some data as well as node pointers to child nodes
2017, Fall Pusan National University Ki-Joune Li
Binary Search Trees (BSTs)
Trees Lecture 9 CS2110 – Fall 2009.
C++ Plus Data Structures
CS2606 Trees.
A Robust Data Structure
CMSC 202 Trees.
General Trees & Binary Trees
Binary Search Trees (BSTs)
Binary Trees, Binary Search Trees
Non-Linear Structures
2018, Fall Pusan National University Ki-Joune Li
Announcements Prelim 1 on Tuesday! A4 will be posted today
CSC 143 Binary Search Trees.
Binary Search Trees.
Yan Shi CS/SE 2630 Lecture Notes
An Introduction to Programming though C++
Recursion.
Trees A Quick Introduction to Graphs Definition of Trees Rooted Trees
Tree A tree is a data structure in which each node is comprised of some data as well as node pointers to child nodes
Binary Search Trees CS 580U Fall 17.
Binary Trees, Binary Search Trees
Binary Search Tree.
Binary Search Trees (BSTs)
2019, Fall Pusan National University Ki-Joune Li
Presentation transcript:

Recursion Practice requiring pointers

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.

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

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

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

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;

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

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); }

Write the code to insert into a binary search tree.

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; }

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);

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);

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) 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?

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.

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; }

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.

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);

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

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); }