Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright©1999 Angus Wu PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING EE 31331 PROGRAMMING METHODOLOGY AND SOFTWARE ENGINEERING.

Similar presentations


Presentation on theme: "Copyright©1999 Angus Wu PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING EE 31331 PROGRAMMING METHODOLOGY AND SOFTWARE ENGINEERING."— Presentation transcript:

1 Copyright©1999 Angus Wu PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING EE 31331 PROGRAMMING METHODOLOGY AND SOFTWARE ENGINEERING

2 Copyright©1999 Angus Wu PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING 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

3 Copyright©1999 Angus Wu PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING RECURSION FUNDAMENTAL The are two commonly used statements in data structure analysis: Proof by Induction Proof by Contradiction/Counter Example

4 Copyright©1999 Angus Wu PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING PROOF BY INDUCTION Prove that the Fibonacci numbers, F 0 =1, F 1 =1, F 2 =2, F 3 =3, F 4 = 5, …. F i = F i-1 + F i-2, and satisfy F i < (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.

5 Copyright©1999 Angus Wu PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING PROOF BY INDUCTION Prove that the Fibonacci numbers, F 0 =1, F 1 =1, F 2 =2, F 3 =3, F 4 = 5, …. F i = F i-1 + F i-2, and satisfy F i < (5/3) i It is quite obvious that F 1 = 1 < 5/3, F 2 = 2 < 25/9. We need to show that F k+1 < (5/3) k+1 Since F k+1 = F k + F k-1, then F k+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, F k+1 < (5/3) k+1

6 Copyright©1999 Angus Wu PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING Example: Factorial Factorial (n) = [ 1 n x (n-1) x ….. 2 x 1 if n =0 if n >0 Iterative algorithm Factorial (n) = [ 1 n x (Factorial (n-1)) if n =0 if n >0 Recursive algorithm

7 Copyright©1999 Angus Wu PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING i= 1; factN =1; loop ( i< n) factN = factN *i; i = i +1; return factN;

8 Copyright©1999 Angus Wu PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING recursiveFactorial( val n ) if ( n = 0 ) return 1; else return ( n* recursiveFactorial (n-1));

9 Copyright©1999 Angus Wu PROGRAMMING 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) + x 2 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 ?

10 Copyright©1999 Angus Wu PROGRAMMING 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) +x 2 without the base case is ambiguous mathematically. line 2 makes the recursion call. (function calls itself)

11 Copyright©1999 Angus Wu PROGRAMMING 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)?

12 Copyright©1999 Angus Wu PROGRAMMING 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 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).

13 Copyright©1999 Angus Wu PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING Characteristics of Recursion if this is a simple case solve it else redefine the problem using recursion

14 Copyright©1999 Angus Wu PROGRAMMING 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.

15 Copyright©1999 Angus Wu PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING recursiveFactorial( val n ) 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

16 Copyright©1999 Angus Wu PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING Selection Sort

17 Copyright©1999 Angus Wu PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING 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

18 Copyright©1999 Angus Wu PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING 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); } }

19 Copyright©1999 Angus Wu PROGRAMMING 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”.

20 Copyright©1999 Angus Wu PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING TREE ADT

21 Copyright©1999 Angus Wu PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING 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

22 Copyright©1999 Angus Wu PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING 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

23 Copyright©1999 Angus Wu PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING 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

24 Copyright©1999 Angus Wu PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING 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

25 Copyright©1999 Angus Wu PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING 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

26 Copyright©1999 Angus Wu PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING 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. level 0 level 1 level 2

27 Copyright©1999 Angus Wu PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING BINARY TREE

28 Copyright©1999 Angus Wu PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING 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.

29 Copyright©1999 Angus Wu PROGRAMMING 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 N The H min is [log 2 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 = 2 H -1

30 Copyright©1999 Angus Wu PROGRAMMING 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 = H L -H R

31 Copyright©1999 Angus Wu PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING STUCTURE OF BINARY TREE NODE left subtree data rightSubtree End NODE typedef struct node *NodePtr; struct node { int info; NodePtr left; NodePtr right; };

32 Copyright©1999 Angus Wu PROGRAMMING 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.

33 Copyright©1999 Angus Wu PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING BINARY TREE PREORDER TRAVERSALS A B C D E F

34 Copyright©1999 Angus Wu PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING BINARY TREE TRAVERSALS algorithm preorder (val root ) if (root is not NULL) process(root); preorder(root-> LeftSubtree); preorder(root-> RightSubtree); return end preorder

35 Copyright©1999 Angus Wu PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING 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

36 Copyright©1999 Angus Wu PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING BINARY TREE TRAVERSALS algorithm inorder (val root ) if (root is not NULL) inorder(root-> LeftSubtree); process(root); inorder(root-> RightSubtree); return end inorder

37 Copyright©1999 Angus Wu PROGRAMMING 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

38 Copyright©1999 Angus Wu PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING BINARY TREE TRAVERSALS algorithm postorder (val root ) if (root is not NULL) postorder(root-> LeftSubtree); postorder(root-> RightSubtree); process(root); return end postorder

39 Copyright©1999 Angus Wu PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING 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.

40 Copyright©1999 Angus Wu PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING EXPRESSION BINARY TREE a*(b+c) + d An infix tree with parenthesis ((a*(b+c)) + d)

41 Copyright©1999 Angus Wu PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING PRINTING AN INFIX EXPRESSION BINARY TREE algorithm infix (val tree ) 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)

42 Copyright©1999 Angus Wu PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING PRINTING AN PREFIX EXPRESSION BINARY TREE algorithm prefix (val tree ) if (tree not empty) { print (tree->token); prefix(tree->LeftPointer); prefix(tree->RightPointer); } return; end prefix; +*a+bcd

43 Copyright©1999 Angus Wu PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING PRINTING AN POSTFIX EXPRESSION BINARY TREE algorithm postfix (val tree ) if (tree not empty) { postfix(tree->LeftPointer); postfix(tree->RightPointer); print (tree->token); } return; end postfix; abc+*d+

44 Copyright©1999 Angus Wu PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING CREATING AN EXPRESSION TREE Consider the expression :(a+b)*(c*(d+e)) The corresponding postfix is:ab+cde+**

45 Copyright©1999 Angus Wu PROGRAMMING 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;} }}

46 Copyright©1999 Angus Wu PROGRAMMING 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 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.

47 Copyright©1999 Angus Wu PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING BINARY SEARCH TREE valid bstinvalid bst

48 Copyright©1999 Angus Wu PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING 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

49 Copyright©1999 Angus Wu PROGRAMMING 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.

50 Copyright©1999 Angus Wu PROGRAMMING 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 the least among all the nodes of the tree. algorithm fmin (val root ) { if (root->left ==NULL) return (root); return fmin(root->left); }

51 Copyright©1999 Angus Wu PROGRAMMING 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 the largest among all the nodes of the tree. algorithm fmax (val root ) { if (root->right ==NULL) return (root); return fmax(root->right); }

52 Copyright©1999 Angus Wu PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING BINARY SEARCH TREE AND BINARY SEARCH Search for the letter L

53 Copyright©1999 Angus Wu PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING BINARY SEARCH TREE AND BINARY SEARCH Search for 20. Starts from the root 23 20 < 23 goes to left tree 18 is the root 20 > 18 goes to right tree

54 Copyright©1999 Angus Wu PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING BINARY SEARCH TREE AND BINARY SEARCH algorithm searchBST (val root, val arg ) { if (root is NULL) return NULL; if (arg key) return searchBST (root->left, arg); else if (arg > root->key) return searchBST (root->right, arg); else return root; } end searchBST;

55 Copyright©1999 Angus Wu PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING BINARY SEARCH TREE AND BINARY SEARCH 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

56 Copyright©1999 Angus Wu PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING BINARY SEARCH TREE AND BINARY SEARCH 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

57 Copyright©1999 Angus Wu PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING BINARY SEARCH TREE AND BINARY SEARCH algorithm insert (ref root, val new ) { if (root==NULL) { root=new; root->left = NULL; root->right=NULL;} else if (new->key key) insert (root->left, new); else insert(root->right, new); return; } end insert;


Download ppt "Copyright©1999 Angus Wu PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING EE 31331 PROGRAMMING METHODOLOGY AND SOFTWARE ENGINEERING."

Similar presentations


Ads by Google