Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 C++ Plus Data Structures Nell Dale Chapter 7 Programming with Recursion Modified from the slides by Sylvia Sorkin, Community College of Baltimore County.

Similar presentations


Presentation on theme: "1 C++ Plus Data Structures Nell Dale Chapter 7 Programming with Recursion Modified from the slides by Sylvia Sorkin, Community College of Baltimore County."— Presentation transcript:

1 1 C++ Plus Data Structures Nell Dale Chapter 7 Programming with Recursion Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus

2 2 Recursive Function Call l A recursive call is a function call in which the called function is the same as the one making the call. l In other words, recursion occurs when a function calls itself! l We must avoid making an infinite sequence of function calls (infinite recursion).

3 3 Finding a Recursive Solution l Each successive recursive call should bring you closer to a situation in which the answer is known. l A case for which the answer is known (and can be expressed without recursion) is called a base case. l Each recursive algorithm must have at least one base case, as well as the general (recursive) case

4 4 Three-Question Method of verifying recursive functions l Base-Case Question: Is there a nonrecursive way out of the function? l Smaller-Caller Question: Does each recursive function call involve a smaller case of the original problem leading to the base case? l General-Case Question: Assuming each recursive call works correctly, does the whole function work correctly?

5 5 Writing Recursive Functions l Get an exact definition of the problem to be solved. l Determine the size of the problem on this call to the function. l Identify and solve the base case(s). l Identify and solve the general case(s) in terms of a smaller case of the same problem – a recursive call.

6 6 “Why use recursion?” Those examples could have been written without recursion, using iteration instead. The iterative solution uses a loop, and the recursive solution uses an if statement. However, for certain problems the recursive solution is the most natural solution. This often occurs when pointer variables are used.

7 7 Recursive Linked List Processing

8 8 struct NodeType { int info ; NodeType* next ; } class SortedType { public :... // member function prototypes private : NodeType* listData ; } ; struct ListType

9 9 RevPrint(listData); A B C D E FIRST, print out this section of list, backwards THEN, print this element listData

10 10 Base Case and General Case A base case may be a solution in terms of a “smaller” list. Certainly for a list with 0 elements, there is no more processing to do. Our general case needs to bring us closer to the base case situation. That is, the number of list elements to be processed decreases by 1 with each recursive call. By printing one element in the general case, and also processing the smaller remaining list, we will eventually reach the situation where 0 list elements are left to be processed. In the general case, we will print the elements of the smaller remaining list in reverse order, and then print the current pointed to element.

11 11 Using recursion with a linked list void RevPrint ( NodeType* listPtr ) // Pre: listPtr points to an element of a list. // Post: all elements of list pointed to by listPtr have been printed // out in reverse order. { if ( listPtr != NULL )// general case { RevPrint ( listPtr-> next ) ; // process the rest cout info << endl ; // then print this element } // Base case : if the list is empty, do nothing } 11

12 12 How Recursion Works l Static storage allocation associates variable names with memory locations at compile time. l Dynamic storage allocation associates variable names with memory locations at execution time.

13 13 When a function is called... l A transfer of control occurs from the calling block to the code of the function. It is necessary that there is a return to the correct place in the calling block after the function code is executed. This correct place is called the return address. l When any function is called, the run-time stack is used. On this stack is placed an activation record (stack frame) for the function call.

14 14 Stack Activation Frames l The activation record stores the return address for this function call, and also the parameters, local variables, and the function’s return value, if non-void. l The activation record for a particular function call is popped off the run-time stack when the final closing brace in the function code is reached, or when a return statement is reached in the function code. l At this time the function’s return value, if non- void, is brought back to the calling block return address for use there.

15 15 Debugging Recursive Routines l Using the Three-Question Method. l Using a branching statement (if/switch). l Put debug output statement during testing. l …

16 16 Removing Recursion When the language doesn’t support recursion, or recursive solution is too costly (space or time), or … l Iteration l Stacking

17 17 Use a recursive solution when: The depth of recursive calls is relatively “shallow” compared to the size of the problem. l The recursive version does about the same amount of work as the nonrecursive version. l The recursive version is shorter and simpler than the nonrecursive solution. SHALLOW DEPTH EFFICIENCY CLARITY

18 18 Nell Dale Chapter 8 Binary Search Trees Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures

19 19 for an element in a sorted list stored sequentially l in an array: O(Log 2 N) l in a linked list: ? (midpoint = ?) Binary search

20 20 l Introduce some basic tree vocabulary l Develop algorithms l Implement operations needed to use a binary search tree Goals of this chapter

21 21 A binary tree is a structure in which: Each node can have at most two children, and in which a unique path exists from the root to every other node. The two children of a node are called the left child and the right child, if they exist. Binary Tree

22 22 Implementing a Binary Tree with Pointers and Dynamic Data Q V T K S A E L

23 23 Each node contains two pointers template struct TreeNode { ItemType info; // Data member TreeNode * left; // Pointer to left child TreeNode * right; // Pointer to right child };. left. info. right NULL ‘A’ 6000

24 // BINARY SEARCH TREE SPECIFICATION template class TreeType { public: TreeType ( ); // constructor ~TreeType ( ); // destructor bool IsEmpty ( ) const; bool IsFull ( ) const; int NumberOfNodes ( ) const; void InsertItem ( ItemType item ); void DeleteItem (ItemType item ); void RetrieveItem ( ItemType& item, bool& found ); void PrintTree (ofstream& outFile) const;... private: TreeNode * root; }; 24

25 25 TreeType CharBST; ‘J’ ‘E’ ‘A’ ‘S’ ‘H’ TreeType ~TreeType IsEmpty InsertItem Private data: root RetrieveItem PrintTree.

26 26 A Binary Tree Q V T K S A E L Search for ‘S’?

27 27 A special kind of binary tree in which: 1. Each node contains a distinct data value, 2. The key values in the tree can be compared using “greater than” and “less than”, and 3. The key value of each node in the tree is less than every key value in its right subtree, and greater than every key value in its left subtree. A Binary Search Tree (BST) is...

28 28 Is ‘F’ in the binary search tree? ‘J’ ‘E’ ‘A’ ‘H’ ‘T’ ‘M’ ‘K’ ‘V’ ‘P’ ‘Z’‘D’‘Q’‘L’‘B’‘S’

29 29 Is ‘F’ in the binary search tree? ‘J’ ‘E’ ‘A’ ‘H’ ‘T’ ‘M’ ‘K’ ‘V’ ‘P’ ‘Z’‘D’‘Q’‘L’‘B’‘S’

30 // BINARY SEARCH TREE SPECIFICATION template class TreeType { public: TreeType ( ) ; // constructor ~TreeType ( ) ; // destructor bool IsEmpty ( ) const ; bool IsFull ( ) const ; int NumberOfNodes ( ) const ; void InsertItem ( ItemType item ) ; void DeleteItem (ItemType item ) ; void RetrieveItem ( ItemType& item, bool& found ) ; void PrintTree (ofstream& outFile) const ;... private: TreeNode * root ; }; 30

31 // SPECIFICATION (continued) // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // RECURSIVE PARTNERS OF MEMBER FUNCTIONS template void PrintHelper ( TreeNode * ptr, ofstream& outFile ) ; template void InsertHelper ( TreeNode * & ptr, ItemType item ) ; template void RetrieveHelper ( TreeNode * ptr, ItemType& item, bool& found ) ; template void DestroyHelper ( TreeNode * ptr ) ; 31

32 template void TreeType :: RetrieveItem ( ItemType& item, bool& found ) { RetrieveHelper ( root, item, found ) ; } template void RetrieveHelper ( TreeNode * ptr, ItemType& item, bool& found) { if ( ptr == NULL ) found = false ; else if ( item info )// GO LEFT RetrieveHelper( ptr->left, item, found ) ; else if ( item > ptr->info ) // GO RIGHT RetrieveHelper( ptr->right, item, found ) ; else { item = ptr->info ; found = true ; } 32

33 template void TreeType :: InsertItem ( ItemType item ) { InsertHelper ( root, item ) ; } template void InsertHelper ( TreeNode * & ptr, ItemType item ) { if ( ptr == NULL ) { // INSERT item HERE AS LEAF ptr = new TreeNode ; ptr->right = NULL ; ptr->left = NULL ; ptr->info = item ; } else if ( item info )// GO LEFT InsertHelper( ptr->left, item ) ; else if ( item > ptr->info ) // GO RIGHT InsertHelper( ptr->right, item ) ; } 33

34 34 Traverse a list: -- forward -- backward Traverse a tree: -- there are many ways! PrintTree()

35 35 Inorder Traversal: A E H J M T Y ‘J’ ‘E’ ‘A’ ‘H’ ‘T’ ‘M’‘Y’ tree Print left subtree firstPrint right subtree last Print second

36 36 Preorder Traversal: J E A H T M Y ‘J’ ‘E’ ‘A’ ‘H’ ‘T’ ‘M’‘Y’ tree Print left subtree secondPrint right subtree last Print first

37 37 ‘J’ ‘E’ ‘A’ ‘H’ ‘T’ ‘M’‘Y’ tree Print left subtree firstPrint right subtree second Print last Postorder Traversal: A H E M Y T J

38 38 l Is the depth of recursion relatively shallow? Yes. l Is the recursive solution shorter or clearer than the nonrecursive version? Yes. l Is the recursive version much less efficient than the nonrecursive version? No. Recursion or Iteration? Assume: the tree is well balanced.

39 39 Use a recursive solution when (Chpt. 7): The depth of recursive calls is relatively “shallow” compared to the size of the problem. l The recursive version does about the same amount of work as the nonrecursive version. l The recursive version is shorter and simpler than the nonrecursive solution. SHALLOW DEPTH EFFICIENCY CLARITY

40 40 BST: l Quick random-access with the flexibility of a linked structure l Can be implemented elegantly and concisely using recursion l Takes up more memory space than a singly linked list l Algorithms are more complicated Binary Search Trees (BSTs) vs. Linear Lists

41 41 Nell Dale Chapter 9 Trees Plus Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures

42 42 A special kind of binary tree in which: 1. Each leaf node contains a single operand, 2. Each nonleaf node contains a single binary operator, and 3. The left and right subtrees of an operator node represent subexpressions that must be evaluated before applying the operator at the root of the subtree. A Binary Expression Tree is...

43 43 Levels Indicate Precedence When a binary expression tree is used to represent an expression, the levels of the nodes in the tree indicate their relative precedence of evaluation. Operations at higher levels of the tree are evaluated later than those below them. The operation at the root is always the last operation performed.

44 44 A Binary Expression Tree ‘*’ ‘+’ ‘4’ ‘3’ ‘2’ Infix: ( ( 4 + 2 ) * 3 ) Prefix: * + 4 2 3 Postfix: 4 2 + 3 * has operators in order used

45 45 Inorder Traversal: (A + H) / (M - Y) ‘/’ ‘+’ ‘A’ ‘H’ ‘-’ ‘M’‘Y’ tree Print left subtree firstPrint right subtree last Print second

46 46 Preorder Traversal: / + A H - M Y ‘/’ ‘+’ ‘A’ ‘H’ ‘-’ ‘M’‘Y’ tree Print left subtree secondPrint right subtree last Print first

47 47 ‘/’ ‘+’ ‘A’ ‘H’ ‘-’ ‘M’‘Y’ tree Print left subtree firstPrint right subtree second Print last Postorder Traversal: A H + M Y - /

48 48 Function Eval() Definition: Evaluates the expression represented by the binary tree. Size: The number of nodes in the tree. Base Case: If the content of the node is an operand, Func_value = the value of the operand. General Case: If the content of the node is an operator BinOperator, Func_value = Eval(left subtree) BinOperator Eval(right subtree)

49 49 Writing Recursive Functions (Chpt 7) l Get an exact definition of the problem to be solved. l Determine the size of the problem on this call to the function. l Identify and solve the base case(s). l Identify and solve the general case(s) in terms of a smaller case of the same problem – a recursive call.

50 50 Eval(TreeNode * tree) Algorithm: IF Info(tree) is an operand Return Info(tree) ELSE SWITCH(Info(tree)) case + :Return Eval(Left(tree)) + Eval(Right(tree)) case - : Return Eval(Left(tree)) - Eval(Right(tree)) case * : Return Eval(Left(tree)) * Eval(Right(tree)) case / : Return Eval(Left(tree)) / Eval(Right(tree))

51 int Eval ( TreeNode* ptr ) // Pre: ptr is a pointer to a binary expression tree. // Post: Function value = the value of the expression represented // by the binary tree pointed to by ptr. { switch ( ptr->info.whichType ) { case OPERAND : return ptr->info.operand ; case OPERATOR : switch ( tree->info.operation ) { case ‘+’ : return ( Eval ( ptr->left ) + Eval ( ptr->right ) ) ; case ‘-’ : return ( Eval ( ptr->left ) - Eval ( ptr->right ) ) ; case ‘*’ : return ( Eval ( ptr->left ) * Eval ( ptr->right ) ) ; case ‘/’ : return ( Eval ( ptr->left ) / Eval ( ptr->right ) ) ; } 51

52 52 A Nonlinked Representation of Binary Trees Store a binary tree in an array in such a way that the parent-child relationships are not lost

53 53 A full binary tree A full binary tree is a binary tree in which all the leaves are on the same level and every non leaf node has two children. SHAPE OF A FULL BINARY TREE

54 54 A complete binary tree A complete binary tree is a binary tree that is either full or full through the next-to-last level, with the leaves on the last level as far to the left as possible. SHAPE OF A COMPLETE BINARY TREE

55 55 What is a Heap? A heap is a binary tree that satisfies these special SHAPE and ORDER properties: n Its shape must be a complete binary tree. n For each node in the heap, the value stored in that node is greater than or equal to the value in each of its children.

56 56 70 0 60 1 40 3 30 4 12 2 8 5 tree And use the numbers as array indexes to store the tree [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] 70 60 12 40 30 8 tree.nodes

57 57 Parent-Child Relationship? tree.nodes[index]: left child: tree.nodes[index*2 + 1] right child: tree.nodes[index*2 + 2] parent: tree.nodes[(index-1) / 2] Leaf nodes: tree.nodes[numElements / 2] … tree.nodes[numElements - 1]

58 // HEAP SPECIFICATION // Assumes ItemType is either a built-in simple data type // or a class with overloaded realtional operators. template struct HeapType { void ReheapDown ( int root, int bottom ) ; void ReheapUp ( int root, int bottom ) ; ItemType* elements ; // ARRAY to be allocated dynamically int numElements ; }; 58

59 59 ReheapDown(root, bottom) IF elements[root] is not a leaf Set maxChild to index of child with larger value IF elements[root] < elements[maxChild]) Swap(elements[root], elements[maxChild]) ReheapDown(maxChild, bottom)

60 60 // IMPLEMENTATION OF RECURSIVE HEAP MEMBER FUNCTIONS template void HeapType ::ReheapDown ( int root, int bottom ) // Pre: root is the index of the node that may violate the heap // order property // Post: Heap order property is restored between root and bottom { int maxChild ; int rightChild ; int leftChild ; leftChild = root * 2 + 1 ; rightChild = root * 2 + 2 ; 60 ReheapDown()

61 if ( leftChild <= bottom )// ReheapDown continued { if ( leftChild == bottom ) maxChild = leftChld ; else { if (elements [ leftChild ] <= elements [ rightChild ] ) maxChild = rightChild ; else maxChild = leftChild ; } if ( elements [ root ] < elements [ maxChild ] ) { Swap ( elements [ root ], elements [ maxChild ] ) ; ReheapDown ( maxChild, bottom ) ; } 61

62 62 Priority Queue A priority queue is an ADT with the property that only the highest-priority element can be accessed at any time.

63 Priority Queue ADT Specification Structure: The Priority Queue is arranged to support access to the highest priority item Operations: n MakeEmpty n Boolean IsEmpty n Boolean IsFull n Enqueue(ItemType newItem) n Dequeue(ItemType& item) 63

64 Implementation Level Algorithm: Dequeue(): O(log 2 N) n Set item to root element from queue n Move last leaf element into root position n Decrement numItems n items.ReheapDown(0, numItems-1) Enqueue(): O(log 2 N) n Increment numItems n Put newItem in next available position n items.ReheapUp(0, numItems-1) 64

65 Comparison of Priority Queue Implementations 65 EnqueueDequeue HeapO(log 2 N) Linked ListO(N)O(1) Binary Search Tree BalancedO(log 2 N) SkewedO(N) Trade-offs: read Text page 548

66 66 End


Download ppt "1 C++ Plus Data Structures Nell Dale Chapter 7 Programming with Recursion Modified from the slides by Sylvia Sorkin, Community College of Baltimore County."

Similar presentations


Ads by Google