570 likes | 698 Views
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.
E N D
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 • The are two commonly used statements in data structure • analysis: • Proof by Induction • Proof by Contradiction/Counter Example
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.
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
Example: Factorial if n =0 if n >0 Factorial (n) = [ 1 n x (n-1) x ….. 2 x 1 Iterative algorithm if n =0 if n >0 Factorial (n) = [ 1 n x (Factorial (n-1)) Recursive algorithm
i= 1; factN =1; loop ( i< n) factN = factN *i; i = i +1; return factN;
recursiveFactorial( val n <integer>) if ( n = 0 ) return 1; else return ( n* recursiveFactorial (n-1));
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 recursive function. The idea is to implement the recursive function by computer program. Then, how ?
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)
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)?
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 the base 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).
Characteristics of Recursion if this is a simple case solve it else redefine the problem using recursion
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.
recursiveFactorial( val n <integer>) if ( n = 0 ) return 1; else return ( n* recursiveFactorial (n-1)); Base Case: if ( n = 0) return 1; Making Progress recursiveFactorial(n-1) *for each call, the argument is towards the base case, n= 0
Selection Sort Find largest element in the array, switch it with the bottom element. Repeat the same action until the whole array is sorted. Algorithm if n is 1 the array is sorted else place the largest array element in the last position Sort the subarray which excludes the last array element
Algorithm if n is 1 the array is sorted else 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); } }
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”.
Basic Tree Concepts A tree consists of a finite set of elements called node, and a finite set of directed lines, called branches, that connect the nodes. branch node
root branch 1 node for node B, branch 1 is an indegree branch. indegree branch is a branch directed towards a node for node A, branch 1 is an outdegree branch. outdegree branch is a branch directed away from a node
root 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)
parent parent and child child 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)}
ancestor descendent 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)
level 0 level 1 level 2 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 the root plus 1. By definition, the height of an empty tree is -1.
BINARY TREE It is a tree in which no node can have more than two subtrees. These subtrees are designated as the left subtree, and right subtree.
PROPERTIES OF BINARY TREE Height of Binary Tree Given that there are N nodes in a tree. The H max. is N The H min is [log2 N] + 1. Given a height of the binary tree, H, the min. and max. no. of nodes in the tree are: N min = H, and N max = 2H -1
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
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; };
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.
BINARY TREE PREORDER TRAVERSALS A B C D E F
BINARY TREE TRAVERSALS algorithm preorder (val root <nodepointer>) if (root is not NULL) process(root); preorder (root-> LeftSubtree); preorder(root-> RightSubtree); return end preorder
BINARY TREE INORDER TRAVERSALS Inorder traversal processes the left subtree first, the the rootm and finally the right subtree. C B D A E F
BINARY TREE TRAVERSALS algorithm inorder (val root <nodepointer>) if (root is not NULL) inorder(root-> LeftSubtree); process(root); inorder(root-> RightSubtree); return end inorder
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
BINARY TREE TRAVERSALS algorithm postorder (val root <nodepointer>) if (root is not NULL) postorder(root-> LeftSubtree); postorder(root-> RightSubtree); process(root); return end postorder
EXPRESSION BINARY TREE An expression tree is a binary tree with the following properties: 1. Each leaf is an operand 2. The root and the internal nodes are operators ( + - * / ) 3. Subtrees are sub-expressions with the root being an operator.
EXPRESSION BINARY TREE a*(b+c) + d An infix tree with parenthesis ((a*(b+c)) + d)
PRINTING AN INFIX EXPRESSION BINARY TREE 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)
PRINTING AN PREFIX EXPRESSION BINARY TREE algorithm prefix (val tree <tree pointer>) if (tree not empty) { print (tree->token); prefix(tree->LeftPointer); prefix(tree->RightPointer); } return; end prefix; +*a+bcd
PRINTING AN POSTFIX EXPRESSION BINARY TREE algorithm postfix (val tree <tree pointer>) if (tree not empty) { postfix(tree->LeftPointer); postfix(tree->RightPointer); print (tree->token); } return; end postfix; abc+*d+
CREATING AN EXPRESSION TREE Consider the expression : (a+b)*(c*(d+e)) The corresponding postfix is: ab+cde+**
CREATING AN EXPRESSION TREE 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;} }} ab+cde+**
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 root 2. All items in the right subtree are greater than or equal to the root 3. Each subtree is itself a binary search tree.
BINARY SEARCH TREE valid bst invalid bst
BINARY SEARCH TREE preorder : 23 18 12 20 44 35 52 inorder: 12 18 20 23 35 44 52 postorder: 12 20 18 35 52 44 23
BINARY SEARCH TREE inorder: 12 18 20 23 35 44 52 Note: The inorder traversal of a binary search tree produces an ordered list.
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 the least among all the nodes of the tree. algorithm fmin (val root <pointer>) { if (root->left ==NULL) return (root); return fmin(root->left); }