Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 9.

Slides:



Advertisements
Similar presentations
Chapter 7. Binary Search Trees
Advertisements

Singly linked lists Doubly linked lists
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 5.
Linear vs Binary Search COP What is recursion? // Pre-conditions: exponent is >= to 0 // Post-conditions: returns base exponent int Power(int base,
1 Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 13.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 7: References and Assignment.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 6.
Recursion.
CS 171: Introduction to Computer Science II
Scott Grissom, copyright 2004 Chapter 5 Slide 1 Analysis of Algorithms (Ch 5) Chapter 5 focuses on: algorithm analysis searching algorithms sorting algorithms.
Fundamentals of Computer Science Lecture 14: Recursion Instructor: Evan Korth New York University.
1 Gentle Introduction to Programming Session 4: Arrays, Sorting, Efficiency.
Monday, 12/9/02, Slide #1 CS 106 Intro to CS 1 Monday, 12/9/02  QUESTIONS??  On HW #5 (Due 5 pm today)  Today:  Recursive functions  Reading: Chapter.
CS 206 Introduction to Computer Science II 10 / 14 / 2009 Instructor: Michael Eckmann.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 9.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 10.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 10.
Chair of Software Engineering 1 Introduction to Programming Exercise Session Week 10 M. Piccioni 24/25 November 2008.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 7.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 9.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 11.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 10.
CHAPTER 10 Recursion. 2 Recursive Thinking Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
1 Gentle Introduction to Programming Session 4: Arrays.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 15 Recursive Algorithms.
Week 7 - Wednesday.  What did we talk about last time?  Recursive running time  Master Theorem  Introduction to trees.
Algorithm Cost Algorithm Complexity. Algorithm Cost.
Properties: -Each node has a value -The left subtree contains only values less than the parent node’s value -The right subtree contains only values greater.
A Review of Recursion Dr. Jicheng Fu Department of Computer Science University of Central Oklahoma.
1 Recursion Dr. Bernard Chen Ph.D. University of Central Arkansas.
Stacks & Recursion. Stack pushpop LIFO list - only top element is visible top.
Scott Marino MSMIS Kean University MSAS5104 Programming with Data Structures and Algorithms Week 10 Scott Marino.
1 Decrease-and-Conquer Approach Lecture 06 ITS033 – Programming & Algorithms Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing.
Reynolds 2006 Complexity1 Complexity Analysis Algorithm: –A sequence of computations that operates on some set of inputs and produces a result in a finite.
Chapter 9: Recursion1 CHAPTER 9 RECURSION. Recursion  Concept of recursion  A recursive: Benefit and Cost  Comparison : Iterative and recursive functions.
Chapter 7 Recursion 1CSCI 3333 Data Structures. 2 Recurrent Sequence A recursively defined sequence – First, certain initial values are specified  c.f.,
Information and Computer Sciences University of Hawaii, Manoa
Chapter 11- Recursion. Overview n What is recursion? n Basics of a recursive function. n Understanding recursive functions. n Other details of recursion.
Chapter 12 Recursion, Complexity, and Searching and Sorting
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.
1 TCSS 143, Autumn 2004 Lecture Notes Recursion Koffman/Wolfgang Ch. 7, pp ,
1 Wright State University, College of Engineering Dr. T. Doom, Computer Science & Engineering CS 241 Computer Programming II CS 241 – Computer Programming.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 5.
CSC 221: Recursion. Recursion: Definition Function that solves a problem by relying on itself to compute the correct solution for a smaller version of.
Binary Search Trees Nilanjan Banerjee. 2 Goal of today’s lecture Learn about Binary Search Trees Discuss the first midterm.
Computer Science: A Structured Programming Approach Using C Trees Trees are used extensively in computer science to represent algebraic formulas;
Data Structure Introduction.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 5.
Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Chair of Software Engineering Complement to lecture 11 : Levenshtein.
Java Methods Big-O Analysis of Algorithms Object-Oriented Programming
CS 367 Introduction to Data Structures Lecture 6.
1 Recursion n what is it? n how to build recursive algorithms n recursion analysis n tracing simple recursive functions n hands on attempts at writing.
Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session October 2008.
24 January Trees CSE 2011 Winter Trees Linear access time of linked lists is prohibitive  Does there exist any simple data structure for.
Chapter 8 Searching and Sorting © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Data Structures Arrays and Lists Part 2 More List Operations.
Sorting & Searching Geletaw S (MSC, MCITP). Objectives At the end of this session the students should be able to: – Design and implement the following.
1 Chapter 8 Recursion. 2 Recursive Function Call a recursion function is a function that either directly or indirectly makes a call to itself. but we.
Maitrayee Mukerji. Factorial For any positive integer n, its factorial is n! is: n! = 1 * 2 * 3 * 4* ….* (n-1) * n 0! = 1 1 ! = 1 2! = 1 * 2 = 2 5! =
Section Recursion 2  Recursion – defining an object (or function, algorithm, etc.) in terms of itself.  Recursion can be used to define sequences.
Recursion what is it? how to build recursive algorithms
5.13 Recursion Recursive functions Functions that call themselves
Recursion A problem solving technique where an algorithm is defined in terms of itself A recursive method is a method that calls itself A recursive algorithm.
Binary Search one reason that we care about sorting is that it is much faster to search a sorted list compared to sorting an unsorted list the classic.
Algorithm design and Analysis
Lesson Objectives Aims
Unit 3 Test: Friday.
Binary Search Trees Chapter 7 Objectives
Presentation transcript:

Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 9

2 Today  Feedback on the mock exam  Recursion Recursion  Recursion  Basic data structures  Arrays  Linked Lists  Hashtables

3 Recursion: an example  Fibonacci numbers: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55,...  How can we calculate the n-th Fibonacci number?  Recursive formula: F(n) = F(n-1) + F(n-2)for n > 1 with F(0) = 0, F(1) = 1

4 Recursion: a second example  Another example of recursion Source: en.wikipedia.org/wiki/Recursion

5 A recursive feature fibonacci(n: INTEGER): INTEGER do if n = 0 then Result := 0 elseif n = 1 then Result := 1 else Result := fibonacci(n-1) + fibonacci(n-2) end fib(4) fib(3)fib(2) fib(1)fib(0)fib(2)fib(1) fib(0)  Calculate fibonacci(4)

6 The general notion of recursion A definition for a concept is recursive if it involves an instance of the concept itself  The definition may use more than one “instance of the concept itself ”  Recursion is the use of a recursive definition

7 Thoughts „To iterate is human, to recurse - divine!“ but … computers are built by humans Better use iterative approach if reasonable ?

8 Iteration vs. recursion  Every recursion could be rewritten as an iteration and vice versa.  BUT, depending on how the problem is formulated, this can be difficult or might not give you a performance improvement.

9 Be careful when using recursion!

10 Exercise: Printing numbers  If we pass n = 4, what will be printed? print_int (n: INTEGER) do print (n) if n > 1 then print_int (n - 1) end print_int (n: INTEGER) do if n > 1 then print_int (n - 1) end print (n) end Hands-On

11 Exercise: Reverse string  Print a given string in reverse order using a recursive function. Hands-On

12 Exercise: Solution class APPLICATION create make feature make local s: STRING do create s.make_from_string ("poldomangia") invert(s) end invert (s: STRING) require s /= Void do if not s.is_empty then invert (s.substring (2, s.count)) print (s[1]) end

13 Exercise: Sequences  Write a recursive and an iterative program to print the following: 111,112,113,121,122,123,131,132,133, 211,212,213,221,222,223,231,232,233, 311,312,313,321,322,323,331,332,333,  Note that the recursive solution can use loops too. Hands-On

14 Exercise: Recursive solution cells: ARRAY [INTEGER] handle_cell (n: INTEGER) local i: INTEGER do from i := 1 until i > 3 loop cells [n] := i if (n < 3) then handle_cell (n+1) else print (cells [1].out+cells [2].out+cells [3].out+",") end i := i + 1 end

15 Exercise: Iterative solution from i := 1 until i > 3 loop from j := 1 until j > 3 loop from k := 1 until k > 3 loop print (i.out+j.out+k.out+“,") k := k + 1 end j := j + 1 end i := i + 1 end

16 Arrays An array is a very fundamental data-structure, which is very close to how your computer organizes its memory. An array is characterized by:  Constant time for random reads  Constant time for random writes  Costly to resize (including inserting elements in the middle of the array)  Must be indexed by an integer  Generally very space efficient In Eiffel the basic array class is generic, V_ARRAY [G].

17 Using Arrays Which of the following lines are valid? Which can fail, and why?  my_array : V_ARRAY [STRING]  my_array [“Fred”] := “Sam”  my_array [10] + “’s Hat”  my_array [5] := “Ed”  my_array.force (“Constantine”, 9) Which is not a constant-time array operation? Hands-On Valid, can’t fail Invalid Valid, can fail Valid, can’t fail

18 Linked Lists  Linked lists are one of the simplest data-structures  They consist of linkable cells class LINKABLE [G] create set_value feature set_value (v : G) do value := v end value : G set_next (n : LINKABLE[G]) do next := n end next : LINKABLE [G] end

19 Using Linked Lists Suppose you keep a reference to only the head of the linked list, what is the running time (using big O notation) to:  Insert at the beginning  Insert in the middle  Insert at the end  Find the length of the list What simple optimization could be made to make end- access faster? Hands-On O (1) O (n)

20 Binary search tree  A binary search tree is a binary tree where each node has a COMPARABLE value.  Left sub-tree of a node contains only values less than the node’s value.  Right sub-tree of a node contains only values greater than or equal to the node’s value.

21 Exercise: Adding nodes  Implement command put (n: INTEGER) in class NODE which creates a new NODE object at the correct place in the binary search tree rooted by Current.  Test your code with a class APPLICATION which builds a binary search tree using put and prints out the values using the traversal feature.  Hint: You might need to adapt the traversal feature such that the values are printed out in order. Hands-On

22 Exercise: Solution  See code in IDE.

23 Exercise: Searching  Implement feature has (n: INTEGER): BOOLEAN in class NODE which returns true if and only if n is in the tree rooted by Current.  Test your code with a class APPLICATION which builds a binary search tree and calls has. Hands-On

24 Exercise: Solution  See code in IDE.