Upload
ulf
View
27
Download
0
Embed Size (px)
DESCRIPTION
EE 31331 PROGRAMMING METHODOLOGY AND SOFTWARE ENGINEERING. RECURSION. It is a repetitive process in which an algorithm calls itself. Why recursion? It provides a simple mechanism to perform iterative process. It provides much simpler coding. RECURSION FUNDAMENTAL. - PowerPoint PPT Presentation
Citation preview
Copyright©1999 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING
EE 31331 PROGRAMMING METHODOLOGY AND SOFTWARE ENGINEERING
Copyright©1999 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING
It is a repetitive process in which an algorithm callsitself.
Why recursion? It provides a simple mechanism to perform iterative process. It provides much simpler coding.
RECURSION
Copyright©1999 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING
RECURSION FUNDAMENTAL
The are two commonly used statements in data structureanalysis:
• Proof by Induction• Proof by Contradiction/Counter Example
Copyright©1999 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING
PROOF BY INDUCTION
Prove that the Fibonacci numbers, F0=1, F1=1, F2=2, F3=3,F4 = 5, …. Fi = Fi-1 + Fi-2, and satisfy Fi < (5/3)i
Proof of induction starts with the simple trivial case to establish the base case. Then, assuming that the theorem is true for the k th case,based on the given conditions, to prove that it is also true for the k+1 th case. If it is true for the case k+1, by the principle of induction, the theorem will be true for any number if n is finite.
Copyright©1999 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING
PROOF BY INDUCTION
Prove that the Fibonacci numbers, F0=1, F1=1, F2=2, F3=3,F4 = 5, …. Fi = Fi-1 + Fi-2, and satisfy Fi < (5/3)i
It is quite obvious that F1 = 1 < 5/3, F2 = 2 < 25/9.We need to show that Fk+1 < (5/3)k+1
Since Fk+1 = Fk + Fk-1, then Fk+1 < (5/2)k + (5/2)k-1
(5/2)k + (5/2)k-1 = (3/5)(5/3)k+1 + (3/5)2(5/3)k+1
= (3/5 + 9/25) (5/3)k+1 = (24/25) (5/3)k+1 < (5/3)k+1
Thus, Fk+1 < (5/3)k+1
Copyright©1999 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING
Example: FactorialFactorial (n) = [ 1 n x (n-1) x ….. 2 x 1
if n =0if n >0Iterative algorithm
Factorial (n) = [ 1 n x (Factorial (n-1))
if n =0if n >0Recursive algorithm
Copyright©1999 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING
i= 1;factN =1;loop ( i< n) factN = factN *i; i = i +1;return factN;
Copyright©1999 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING
recursiveFactorial( val n <integer>)
if ( n = 0 ) return 1;else return ( n* recursiveFactorial (n-1));
Copyright©1999 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING
RECURSION
Most mathematical functions are described by a simple formula. However, some are in more complicated forms. Define a function, F, valid on positive integers, that satisfies
F(0) = 0, and F(x) = 2F(x-1) + x2
From definition, we have:F(1) = 1, F(2) = 6, F(3) = 21, and F(4) = 58. Here, we have a function defines on itself. We call it recursivefunction. The idea is to implement the recursive function by computer program. Then, how ?
Copyright©1999 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING
RECURSION
int F(int x){
/* 1 */ if (x = = 0) return 0;/* 2 */ else return 2*F(x-1) + x*x;
}
In line 1, it is similar to induction case that establishes the base case. It is the case that solved without recursion. The value for which the function is directly known without resorting to recursion. Simply declare the function, F(x) = 2F(x-1) +x2 without the base case is ambiguous mathematically. line 2 makes the recursion call. (function calls itself)
Copyright©1999 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING
RECURSION
int F(int x){
/* 1 */ if (x = = 0) return 0;/* 2 */ else return 2*F(x-1) + x*x;
}
What will happen if the function is called to evaluate F(-1)?
Copyright©1999 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING
RECURSION
int Bad(unsigned int N){
/* 1 */ if (N = = 0) return 0;/* 2 */ else return Bad (N/3 +1) + N -1;
}
if Bad(1) is called, then line 2 will be executed as it is defined by line 2. But what is the value Bad(1)? It is not defined in thebase case. Then, the computer will keep on executing line 2 until the system runs out of space. In fact, the program does not work for any number except Bad(0).
Copyright©1999 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING
Characteristics of Recursion
if this is a simple case solve itelse redefine the problem using recursion
Copyright©1999 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING
RECURSION
For any valid recursion, the fundamental rules are:
1. Base case. It must include some base cases, which can be solved without recursion.
2. Making progress. For the cases that are to be solved recursively, the recursive call must always be to a case that makes progress toward a base case.
In general, every recursive call must either solve a part of the problem or reduce the size of the problem.
Copyright©1999 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING
recursiveFactorial( val n <integer>)
if ( n = 0 ) return 1;else return ( n* recursiveFactorial (n-1));
Base Case:if ( n = 0) return 1;
Making ProgressrecursiveFactorial(n-1)
*for each call, the argument is towards the base case, n= 0
Copyright©1999 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING
34
15
23
45
34
15
23
45
34
15
23
45
34
15
23
45
34
15
23
45
Selection Sort
Copyright©1999 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING
Selection Sort
Find largest element in the array, switch it with thebottom element. Repeat the same action until the whole array is sorted.
Algorithm if n is 1 the array is sortedelse place the largest array element in the last position Sort the subarray which excludes the last array element
Copyright©1999 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING
Algorithm if n is 1 the array is sortedelse place the largest array element in the last position Sort the subarray which excludes the last array element
void select_sort(int array[], int n){
if (n ==1) return;else { place_largest(array, n); select_sort(array, n-1); }
}
Copyright©1999 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING
Recursion Development
1. Base case. It must include some base cases, which can be solved without recursion.* Termination of the recursion.
2. Making progress. For the cases that are to be solved recursively, the recursive call must always be to a case that makes progress toward a base case.* Dividing the problem into sub-problem with “smaller scale”.
Copyright©1999 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING
TREE ADT
Copyright©1999 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING
Basic Tree ConceptsA tree consists of a finite set of elements called node, anda finite set of directed lines, called branches, that connectthe nodes.
A
DC
FEB
G
H
I
branch node
Copyright©1999 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING
A
DC
FEB
G
H
I
branch 1 node
for node B, branch 1 is an indegree branch.indegree branch is a branch directed towards a nodefor node A, branch 1 is an outdegree branch.outdegree branch is a branch directed away from a node
root
Copyright©1999 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING
A
DC
FEB
G
H
I
A leaf is any node with an outdegree of zero. (C, D, E, G, H, I)Nodes are not the root or leaves, called internal nodes. (B, F)
root
Copyright©1999 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING
A
DC
FEB
G
H
I
A node is a parent if its has child/or successor.Any node with a predecessor is a child. Two or more nodes with the same parent are siblings. {(C,D), (G,H, I)}
parent
parent and child
child
Copyright©1999 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING
A
DC
FEB
G
H
I
Ancestor- is any node in the path from the root to the node(A, B, F)Descendent - is any node in the path below the parent node(B, E, F, C, D, G, H, I)
ancestor
descendent
Copyright©1999 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING
A
DC
FEB
G
H
I
The level of a node is its distance from the root. The height of the tree is the level of the leaf in the longest path from theroot plus 1. By definition, the height of an empty tree is -1.
level 0
level 1
level 2
Copyright©1999 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING
BINARY TREE
Copyright©1999 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING
BINARY TREEIt is a tree in which no node can have more than two subtrees.These subtrees are designated as the left subtree, and right subtree.
A
B
C D
E
F
Copyright©1999 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING
PROPERTIES OF BINARY TREE
Height of Binary Tree
Given that there are N nodes in a tree. The H max. is NThe H min is [log2 N] + 1.Given a height of the binary tree, H, the min. and max. no. ofnodes in the tree are:
N min = H, and N max = 2H -1
Copyright©1999 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING
PROPERTIES OF BINARY TREE
Balance Factor
The balance factor of a binary tree is the difference in height between its left and right subtrees. B = HL-HR
Copyright©1999 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING
STUCTURE OF BINARY TREE NODE
left subtree <pointer to Node>data <dataType>rightSubtree <pointer to Node>
End NODE
typedef struct node *NodePtr; struct node { int info; NodePtr left; NodePtr right; };
Node
le ft righ t
Copyright©1999 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING
BINARY TREE TRAVERSALS
A binary tree traversal requires that each node of the tree be processed There are three way of traversals for a binary tree:preorder, inorder, and postorder
In the preorder traversal, the root node is processed first, followed by the left subtree and the the right subtree. The root goes before the subtree.
Copyright©1999 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING
BINARY TREE PREORDER TRAVERSALS
A
B
C D
E
F
A B C D E F
Copyright©1999 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING
BINARY TREE TRAVERSALSA
B
C D
E
F
algorithm preorder (val root <nodepointer>) if (root is not NULL) process(root); preorder(root-> LeftSubtree); preorder(root-> RightSubtree);returnend preorder
Copyright©1999 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING
BINARY TREE INORDER TRAVERSALS
A
B
C D
E
F
Inorder traversal processes the left subtree first, the therootm and finally the right subtree. C B D A E F
Copyright©1999 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING
BINARY TREE TRAVERSALSA
B
C D
E
F
algorithm inorder (val root <nodepointer>) if (root is not NULL)
inorder(root-> LeftSubtree);process(root);inorder(root-> RightSubtree);
returnend inorder
Copyright©1999 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING
BINARY TREE POSTORDER TRAVERSALS
Postorder traversal processes the leftmost leaf then followed by the right subtrees and finally the root
C D B F E A
A
B
C D
E
F
Copyright©1999 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING
BINARY TREE TRAVERSALS
algorithm postorder (val root <nodepointer>) if (root is not NULL)
postorder(root-> LeftSubtree);postorder(root-> RightSubtree);process(root);
returnend postorder
A
B
C D
E
F
Copyright©1999 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING
EXPRESSION BINARY TREE
An expression tree is a binary tree with the following properties:1. Each leaf is an operand2. The root and the internal nodes are operators ( + - * / )3. Subtrees are sub-expressions with the root being an operator.
Copyright©1999 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING
EXPRESSION BINARY TREE
+
*
+
d
cb
a
a*(b+c) + d
An infix tree with parenthesis
((a*(b+c)) + d)
Copyright©1999 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING
PRINTING AN INFIX EXPRESSION BINARY TREE
+
*
+
d
cb
a
algorithm infix (val tree <tree pointer>)if (tree not empty) if (tree->token is an operand) print (tree->token); else { print (open parenthesis); infix(tree->left); print(tree->token); infix(tree->right); print (close parenthesis);} return;end infix;
((a*(b+c)) + d)
Copyright©1999 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING
PRINTING AN PREFIX EXPRESSION BINARY TREE
+
*
+
d
cb
a
algorithm prefix (val tree <tree pointer>)if (tree not empty)
{ print (tree->token); prefix(tree->LeftPointer); prefix(tree->RightPointer); }return;end prefix;
+*a+bcd
Copyright©1999 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING
PRINTING AN POSTFIX EXPRESSION BINARY TREE
+
*
+
d
cb
a
algorithm postfix (val tree <tree pointer>)if (tree not empty)
{ postfix(tree->LeftPointer); postfix(tree->RightPointer); print (tree->token);
}return;end postfix;
abc+*d+
Copyright©1999 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING
CREATING AN EXPRESSION TREE
Consider the expression : (a+b)*(c*(d+e))The corresponding postfix is: ab+cde+**
*
ed
+c
*
ba
+
Copyright©1999 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING
CREATING AN EXPRESSION TREE
ab+cde+**algorithm create_tree{ do until the end of the expression; {read one value from the expression; if it is an operand { create a one node tree;
push it to the stack; } else if it is an operator { pop two elements from the stack; create a tree with the operator as the root;
create a right leaf with the first element; create a left leaf with the second element; push the root to the stack;} }}
Copyright©1999 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING
BINARY SEARCH TREE
A binary search tree is a binary tree with the following properties:1. All items in the left subtree are less than the root2. All items in the right subtree are greater than or equal to the root3. Each subtree is itself a binary search tree.
Copyright©1999 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING
BINARY SEARCH TREE
17
196
143
17
196
223
valid bst invalid bst
Copyright©1999 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING
BINARY SEARCH TREE
preorder : 23 18 12 20 44 35 52inorder: 12 18 20 23 35 44 52postorder: 12 20 18 35 52 44 23
23
18
2012
44
5235
Copyright©1999 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING
BINARY SEARCH TREE
inorder: 12 18 20 23 35 44 52
Note: The inorder traversal of a binary search tree produces an ordered list.
23
18
2012
44
5235
Copyright©1999 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING
OPERATIONS ON BINARY SEARCH TREE
The common operations on BST are:find min. find max. find the requested data
Find minimum is obvious that the leftmost node is theleast among all the nodes of the tree. algorithm fmin (val root <pointer>){ if (root->left ==NULL) return (root); return fmin(root->left);}
Copyright©1999 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING
OPERATIONS ON BINARY SEARCH TREE
The common operations on BST are:find min. find max. find the requested data
Find maximum is obvious that the rightmost node is thelargest among all the nodes of the tree. algorithm fmax (val root <pointer>){ if (root->right ==NULL) return (root); return fmax(root->right);}
Copyright©1999 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING
BINARY SEARCH TREE AND BINARY SEARCH
Search for the letter L
A EA C E GE E H NI L P Q R
H NI L P Q R
H I L
L
Copyright©1999 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING
BINARY SEARCH TREE AND BINARY SEARCH
23
18
2012
44
5235
Search for 20. Starts from the root 2320 < 23 goes to left tree18 is the root20 > 18 goes to right tree
Copyright©1999 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING
BINARY SEARCH TREE AND BINARY SEARCH
algorithm searchBST (val root <pointer>, val arg <key>){ if (root is NULL) return NULL; if (arg < root->key) return searchBST (root->left, arg); else if (arg > root->key) return searchBST (root->right, arg); else return root;}end searchBST;
Copyright©1999 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING
BINARY SEARCH TREE AND BINARY SEARCH
23
18
2012
44
5235
insert 19 into the tree.root 23 19 < 23, goes to left root 1819 > 18, goes to rightroot 20 19 < 20, goes to leftsince left is null, thenadd at that point
19
Copyright©1999 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING
BINARY SEARCH TREE AND BINARY SEARCH
23
18
2012
44
5235
insert 38 into the tree.root 23 38 > 23 goes to rightroot 4438 < 44 goes to leftroot 35 38 > 35 goes to rightsince right is null, thusinsert at that point
38
Copyright©1999 Angus WuPROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING
BINARY SEARCH TREE AND BINARY SEARCH
algorithm insert (ref root <pointer>, val new <pointer>){ if (root==NULL) { root=new; root->left = NULL; root->right=NULL;} else if (new->key < root->key) insert (root->left, new); else insert(root->right, new); return;}end insert;