EE 31331 PROGRAMMING METHODOLOGY AND SOFTWARE ENGINEERING PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING Copyright©1999 Angus Wu 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. PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING Copyright©1999 Angus Wu RECURSION FUNDAMENTAL The are two commonly used statements in data structure analysis: • Proof by Induction • Proof by Contradiction/Counter Example PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING Copyright©1999 Angus Wu 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. PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING Copyright©1999 Angus Wu 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 PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING Copyright©1999 Angus Wu Example: Factorial Factorial (n) = [ 1 n x (n-1) x ….. 2 x 1 Iterative algorithm Factorial (n) = [ 1 n x (Factorial (n-1)) Recursive algorithm PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING if n =0 if n >0 if n =0 if n >0 Copyright©1999 Angus Wu i= 1; factN =1; loop ( i< n) factN = factN *i; i = i +1; return factN; PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING Copyright©1999 Angus Wu recursiveFactorial( val n <integer>) if ( n = 0 ) return 1; else return ( n* recursiveFactorial (n-1)); PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING Copyright©1999 Angus Wu 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 ? PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING Copyright©1999 Angus Wu 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) PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING Copyright©1999 Angus Wu 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)? PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING Copyright©1999 Angus Wu 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). PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING Copyright©1999 Angus Wu Characteristics of Recursion if this is a simple case solve it else redefine the problem using recursion PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING Copyright©1999 Angus Wu 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. PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING Copyright©1999 Angus Wu 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 PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING Copyright©1999 Angus Wu Selection Sort 34 34 23 15 15 45 23 15 23 15 34 23 34 23 34 15 45 45 45 45 PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING Copyright©1999 Angus Wu 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 PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING Copyright©1999 Angus Wu 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); } } PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING Copyright©1999 Angus Wu 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”. PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING Copyright©1999 Angus Wu TREE ADT PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING Copyright©1999 Angus Wu 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. A branch node B E F I C D G H PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING Copyright©1999 Angus Wu root A branch 1 B node E F I C D G H 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 PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING Copyright©1999 Angus Wu root A B E F I C D G H 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) PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING Copyright©1999 Angus Wu parent A B E F parent and child I C D child G H 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)} PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING Copyright©1999 Angus Wu ancestor A B E descendent F I C D G H 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) PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING Copyright©1999 Angus Wu level 0 A B E level 1 F I C D level 2 G H 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. PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING Copyright©1999 Angus Wu BINARY TREE PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING Copyright©1999 Angus Wu 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. A B C E D PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING F Copyright©1999 Angus Wu 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 PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING Copyright©1999 Angus Wu 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 PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING Copyright©1999 Angus Wu 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; }; PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING Node left right Copyright©1999 Angus Wu 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. PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING Copyright©1999 Angus Wu BINARY TREE PREORDER TRAVERSALS A B C E D F ABCDEF PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING Copyright©1999 Angus Wu BINARY TREE TRAVERSALS A B C E D algorithm preorder (val root <nodepointer>) if (root is not NULL) process(root); preorder(root-> LeftSubtree); preorder(root-> RightSubtree); return end preorder PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING Copyright©1999 Angus Wu F BINARY TREE INORDER TRAVERSALS A B C E D F Inorder traversal processes the left subtree first, the the rootm and finally the right subtree. C B DAE F PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING Copyright©1999 Angus Wu BINARY TREE TRAVERSALS A B C E D algorithm inorder (val root <nodepointer>) if (root is not NULL) inorder(root-> LeftSubtree); process(root); inorder(root-> RightSubtree); return end inorder PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING Copyright©1999 Angus Wu F BINARY TREE POSTORDER TRAVERSALS A B C E D F Postorder traversal processes the leftmost leaf then followed by the right subtrees and finally the root CDBFEA PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING Copyright©1999 Angus Wu BINARY TREE TRAVERSALS A B C E D algorithm postorder (val root <nodepointer>) if (root is not NULL) postorder(root-> LeftSubtree); postorder(root-> RightSubtree); process(root); return end postorder PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING Copyright©1999 Angus Wu F 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. PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING Copyright©1999 Angus Wu EXPRESSION BINARY TREE + a*(b+c) + d An infix tree with parenthesis * d a ((a*(b+c)) + d) + b c PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING Copyright©1999 Angus Wu PRINTING AN INFIX EXPRESSION BINARY TREE + * d a + b c ((a*(b+c)) + d) 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; PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING Copyright©1999 Angus Wu PRINTING AN PREFIX EXPRESSION BINARY TREE + * d a + b +*a+bcd c algorithm prefix (val tree <tree pointer>) if (tree not empty) { print (tree->token); prefix(tree->LeftPointer); prefix(tree->RightPointer); } return; end prefix; PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING Copyright©1999 Angus Wu PRINTING AN POSTFIX EXPRESSION BINARY TREE + * d a + b abc+*d+ c algorithm postfix (val tree <tree pointer>) if (tree not empty) { postfix(tree->LeftPointer); postfix(tree->RightPointer); print (tree->token); } return; end postfix; PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING Copyright©1999 Angus Wu CREATING AN EXPRESSION TREE Consider the expression : The corresponding postfix is: (a+b)*(c*(d+e)) ab+cde+** * + a * b c + d PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING e Copyright©1999 Angus Wu CREATING AN EXPRESSION TREE algorithm create_tree ab+cde+** { 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;} }} PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING Copyright©1999 Angus Wu 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. PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING Copyright©1999 Angus Wu BINARY SEARCH TREE 17 6 3 17 19 14 valid bst PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING 6 3 19 22 invalid bst Copyright©1999 Angus Wu BINARY SEARCH TREE 23 18 12 44 20 35 52 preorder : 23 18 12 20 44 35 52 inorder: 12 18 20 23 35 44 52 postorder: 12 20 18 35 52 44 23 PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING Copyright©1999 Angus Wu BINARY SEARCH TREE 23 18 12 inorder: 44 20 35 52 12 18 20 23 35 44 52 Note: The inorder traversal of a binary search tree produces an ordered list. PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING Copyright©1999 Angus Wu 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); } PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING Copyright©1999 Angus Wu 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 the largest among all the nodes of the tree. algorithm fmax (val root <pointer>) { if (root->right ==NULL) return (root); return fmax(root->right); } PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING Copyright©1999 Angus Wu BINARY SEARCH TREE AND BINARY SEARCH Search for the letter L A A C E E E E G H I L N P Q R H I L N P Q R H I L L PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING Copyright©1999 Angus Wu BINARY SEARCH TREE AND BINARY SEARCH 23 18 12 44 20 35 52 PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING Search for 20. Starts from the root 23 20 < 23 goes to left tree 18 is the root 20 > 18 goes to right tree Copyright©1999 Angus Wu 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; PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING Copyright©1999 Angus Wu BINARY SEARCH TREE AND BINARY SEARCH 23 18 12 44 20 35 52 19 PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING insert 19 into the tree. root 23 19 < 23, goes to left root 18 19 > 18, goes to right root 20 19 < 20, goes to left since left is null, then add at that point Copyright©1999 Angus Wu BINARY SEARCH TREE AND BINARY SEARCH 23 18 12 44 20 35 52 38 PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING insert 38 into the tree. root 23 38 > 23 goes to right root 44 38 < 44 goes to left root 35 38 > 35 goes to right since right is null, thus insert at that point Copyright©1999 Angus Wu 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; PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING Copyright©1999 Angus Wu