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

Slides:



Advertisements
Similar presentations
1 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.
Advertisements

1 Nell Dale Chapter 9 Trees Plus Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.
1 A Two-Level Binary Expression ‘-’ ‘8’ ‘5’ treePtr INORDER TRAVERSAL : has value 3 PREORDER TRAVERSAL: POSTORDER TRAVERSAL: 8 5 -
1 Jake’s Pizza Shop Owner Jake Manager Chef Brad Carol Waitress Waiter Cook Helper Joyce Chris Max Len.
Chapter 4 ADT Sorted List.
Fall 2007CS 2251 Trees Chapter 8. Fall 2007CS 2252 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information.
1 C++ Plus Data Structures Nell Dale Chapter 10 Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex.
Recursion CS 308 – Data Structures. What is recursion? smaller version Sometimes, the best way to solve a problem is by solving a smaller version of the.
Priority Queues, Heaps CS3240, L. grewe 1. 2 Goals Describe a priority queue at the logical level and implement a priority queue as a list Describe the.
Heaps CS 308 – Data Structures.
1 Nell Dale Chapter 9 Trees Plus Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.
Programming with Recursion
Chapter 9 Heaps and Priority Queues. 9-2 What is a Heap? A heap is a binary tree that satisfies these special SHAPE and ORDER properties: –Its shape must.
Data Structures Data Structures Topic #8. Today’s Agenda Continue Discussing Table Abstractions But, this time, let’s talk about them in terms of new.
Lec 15 April 9 Topics: l binary Trees l expression trees Binary Search Trees (Chapter 5 of text)
C++ Plus Data Structures
Recursion.
1 Chapter 7 Recursion. 2 What Is Recursion? l Recursive call A method call in which the method being called is the same as the one making the call l Direct.
1 Chapter 6 Lists Plus. ADT Sorted List Operations Transformers n MakeEmpty n InsertItem n DeleteItem Observers n IsFull n LengthIs n RetrieveItem Iterators.
Data Structures Using C++ 2E Chapter 11 Binary Trees and B-Trees.
1 CS308 Data Structures An application of binary trees: Binary Expression Trees.
Recursion CS Goals Discuss recursion as another form of repetition Do the following tasks, given a recursive routine Determine whether the routine.
9 Priority Queues, Heaps, and Graphs. 9-2 What is a Heap? A heap is a binary tree that satisfies these special SHAPE and ORDER properties: –Its shape.
1 Nell Dale Chapter 6 Lists Plus Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.
1 Nell Dale Chapter 9 Trees Plus Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.
Heaps and Priority Queues CS 3358 – Data Structures.
1 C++ Plus Data Structures Nell Dale Chapter 7 Programming with Recursion Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus.
Chapter 2 Recursion: The Mirrors CS Data Structures Mehmet H Gunes Modified from authors’ slides.
Trees.ppt1 Introduction Many data structures are linear –unique first component –unique last component –other components have unique predecessor and successor.
Binary Trees Chapter 10. Introduction Previous chapter considered linked lists –nodes connected by two or more links We seek to organize data in a linked.
1 Programming with Recursion Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus and Robert Moyer, Montgomery County Community.
1 Lecture 14 Chapter 18 - Recursion. 2 Chapter 18 Topics l Meaning of Recursion l Base Case and General Case in Recursive Function Definitions l Writing.
1 Chapter 13 Recursion. 2 Chapter 13 Topics l Meaning of Recursion l Base Case and General Case in Recursive Function Definitions l Writing Recursive.
1 C++ Plus Data Structures Nell Dale Chapter 7 Programming with Recursion Modified from the slides by Sylvia Sorkin, Community College of Baltimore County.
Recursion Textbook chapter Recursive Function Call a recursive call is a function call in which the called function is the same as the one making.
Computer Science and Software Engineering University of Wisconsin - Platteville 9. Recursion Yan Shi CS/SE 2630 Lecture Notes Partially adopted from C++
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),
Chapter 9 Priority Queues, Heaps, Graphs, and Sets.
Recursion CS 302 – Data Structures Chapter 7. What is recursion? smaller A technique that solves problem by solving smaller versions of the same problem!
1 C++ Plus Data Structures Nell Dale Chapter 7 Programming with Recursion Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus.
Chapter 9 Priority Queues, Heaps, and Graphs. 2 Goals Describe a priority queue at the logical level and implement a priority queue as a list Describe.
1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.
Chapter 9 Priority Queues, Heaps, Graphs 1 Fall 2010.
1 Nell Dale Chapter 8 Binary Search Trees Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data.
1 Recursion. 2 Chapter 15 Topics  Meaning of Recursion  Base Case and General Case in Recursive Function Definitions  Writing Recursive Functions with.
TREES K. Birman’s and G. Bebis’s Slides. Tree Overview 2  Tree: recursive data structure (similar to list)  Each cell may have zero or more successors.
1 C++ Plus Data Structures Nell Dale Chapter 5 Linked Structures Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex.
Tail Recursion l The case in which a function contains only a single recursive call and it is the last statement to be executed in the function. l Tail.
Chapter 7 Programming with Recursion. What Is Recursion? Recursive call A method call in which the method being called is the same as the one.
Chapter 6 Lists Plus Lecture 12. What is a Circular Linked List? A circular linked list is a list in which every node has a successor; the “last” element.
Chapter 8 Binary Search Trees. Chapter 8: Binary Search Trees 8.1 – Trees 8.2 – The Logical Level 8.3 – The Application Level 8.4 – The Implementation.
Data Structures Using C++ 2E Chapter 11 Binary Trees.
Heaps CS 302 – Data Structures Sections 8.8, 9.1, and 9.2.
1 Nell Dale Chapter 8 Binary Search Trees Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.
Chapter 9 Heaps and Priority Queues Lecture 18. Full Binary Tree Every non-leaf node has two children Leaves are on the same level Full Binary Tree.
Course: Programming II - Abstract Data Types HeapsSlide Number 1 The ADT Heap So far we have seen the following sorting types : 1) Linked List sort by.
Recursion Powerful Tool
Programming with Recursion
C++ Plus Data Structures
Programming with Recursion
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
Chapter 9 Priority Queues, Heaps, Graphs, and Sets
C++ Plus Data Structures
CS Data Structures Chapter 17 Heaps Mehmet H Gunes
Heaps and Priority Queues
Yan Shi CS/SE 2630 Lecture Notes
CSI 1340 Introduction to Computer Science II
Chapter 18 Recursion.
C++ Plus Data Structures
Presentation transcript:

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 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 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 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 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 “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 Recursive Linked List Processing

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

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

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 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 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 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 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 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 Removing Recursion When the language doesn’t support recursion, or recursive solution is too costly (space or time), or … l Iteration l Stacking

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 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 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 l Introduce some basic tree vocabulary l Develop algorithms l Implement operations needed to use a binary search tree Goals of this chapter

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 Implementing a Binary Tree with Pointers and Dynamic Data Q V T K S A E L

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

// 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 TreeType CharBST; ‘J’ ‘E’ ‘A’ ‘S’ ‘H’ TreeType ~TreeType IsEmpty InsertItem Private data: root RetrieveItem PrintTree.

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

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 Is ‘F’ in the binary search tree? ‘J’ ‘E’ ‘A’ ‘H’ ‘T’ ‘M’ ‘K’ ‘V’ ‘P’ ‘Z’‘D’‘Q’‘L’‘B’‘S’

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

// 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

// 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

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

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 Traverse a list: -- forward -- backward Traverse a tree: -- there are many ways! PrintTree()

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 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 ‘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 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 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 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 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 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 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 A Binary Expression Tree ‘*’ ‘+’ ‘4’ ‘3’ ‘2’ Infix: ( ( ) * 3 ) Prefix: * Postfix: * has operators in order used

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

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

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

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 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 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))

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 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 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 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 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.

tree And use the numbers as array indexes to store the tree [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] tree.nodes

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]

// 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 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 // 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 * ; rightChild = root * ; 60 ReheapDown()

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 Priority Queue A priority queue is an ADT with the property that only the highest-priority element can be accessed at any time.

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

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

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 End