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

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.
Eiffel: Analysis, Design and Programming Bertrand Meyer (Nadia Polikarpova) Chair of Software Engineering.
© Bertrand Meyer and Yishai Feldman Notice Some of the material is taken from Object-Oriented Software Construction, 2nd edition, by Bertrand Meyer (Prentice.
Recursion Genome 559: Introduction to Statistical and Computational Genomics Elhanan Borenstein.
1 Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 13.
Chair of Software Engineering Concurrent Object-Oriented Programming Prof. Dr. Bertrand Meyer Exercise Session 1: Eiffel Introduction.
Comp 122, Spring 2004 Binary Search Trees. btrees - 2 Comp 122, Spring 2004 Binary Trees  Recursive definition 1.An empty tree is a binary tree 2.A node.
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 1 Introduction to Programming Bertrand Meyer Exercise Session November 2008.
Graphs Graphs are the most general data structures we will study in this course. A graph is a more general version of connected nodes than the tree. Both.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 6.
Inheritance Inheritance Reserved word protected Reserved word super
CS 171: Introduction to Computer Science II
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 17: Topological Sort Algorithm.
CS 206 Introduction to Computer Science II 10 / 14 / 2009 Instructor: Michael Eckmann.
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.
Chair of Software Engineering ATOT - Lecture 8, 28 April Advanced Topics in Object Technology Bertrand Meyer.
Chair of Software Engineering OOSC Summer Semester Object-Oriented Software Construction Bertrand Meyer.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 9.
Chair of Software Engineering ATOT - Lecture 9, 30 April Advanced Topics in Object Technology Bertrand Meyer.
Eiffel: Analysis, Design and Programming Bertrand Meyer Chair of Software Engineering.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 10.
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer Lecture 6: Genericity.
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 ATOT - Lecture 7, 23 April Advanced Topics in Object Technology Bertrand Meyer.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 20: Multiple inheritance.
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.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 3.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 20: Binary Trees.
1 Recursion Dr. Bernard Chen Ph.D. University of Central Arkansas.
© Bertrand Meyer and Yishai Feldman Notice Some of the material is taken from Object-Oriented Software Construction, 2nd edition, by Bertrand Meyer (Prentice.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
Data Structures : Project 5 Data Structures Project 5 – Expression Trees and Code Generation.
B-Trees. CSM B-Trees 2 Motivation for B-Trees So far we have assumed that we can store an entire data structure in main memory What if we have so.
CMSC 341 Introduction to Trees. 8/3/2007 UMBC CMSC 341 TreeIntro 2 Tree ADT Tree definition  A tree is a set of nodes which may be empty  If not empty,
A Binary Search Tree Binary Search Trees.
Tree (new ADT) Terminology:  A tree is a collection of elements (nodes)  Each node may have 0 or more successors (called children)  How many does a.
Arrays An array is a data structure that consists of an ordered collection of similar items (where “similar items” means items of the same type.) An array.
1 Wright State University, College of Engineering Dr. T. Doom, Computer Science & Engineering CS 241 Computer Programming II CS 241 – Computer Programming.
Data Structures Using C++ 2E1 Recursion and Backtracking: DFS Depth first search (a way to traverse a tree or graph) Backtracking can be regarded as a.
CMSC 341 B- Trees D. Frey with apologies to Tom Anastasio.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 9.
Induction Proof. Well-ordering A set S is well ordered if every subset has a least element. [0, 1] is not well ordered since (0,1] has no least element.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
ICS 253: Discrete Structures I Induction and Recursion King Fahd University of Petroleum & Minerals Information & Computer Science Department.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 3.
CMSC 341 Introduction to Trees. 2/21/20062 Tree ADT Tree definition –A tree is a set of nodes which may be empty –If not empty, then there is a distinguished.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
1 Arrays of Arrays An array can represent a collection of any type of object - including other arrays! The world is filled with examples Monthly magazine:
2014-T2 Lecture 27 School of Engineering and Computer Science, Victoria University of Wellington  Lindsay Groves, Marcus Frean, Peter Andreae, and Thomas.
CS Prelim Review – 10/15/09  First prelim is TOMORROW at 7:30 PM  Review session – Tonight 7:30 PM, Phillips 101  Things you should do:  Review every.
Copyright © 2012 Pearson Education, Inc. Chapter 10 Advanced Topics.
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.
8/3/2007CMSC 341 BTrees1 CMSC 341 B- Trees D. Frey with apologies to Tom Anastasio.
Given a node v of a doubly linked list, we can easily insert a new node z immediately after v. Specifically, let w the be node following v. We execute.
Section Recursion 2  Recursion – defining an object (or function, algorithm, etc.) in terms of itself.  Recursion can be used to define sequences.
Topic 2: binary Trees COMP2003J: Data Structures and Algorithms 2
Einführung in die Programmierung Introduction to Programming Prof. Dr
Heap Chapter 9 Objectives Upon completion you will be able to:
Lesson Objectives Aims
Einführung in die Programmierung Introduction to Programming Prof. Dr
Einführung in die Programmierung Introduction to Programming Prof. Dr
Einführung in die Programmierung Introduction to Programming Prof. Dr
Presentation transcript:

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

2 Today  Recursion Recursion  Recursion  Inheritance  Genericity

3 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

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

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

6 Exercise: Printing numbers  If we pass n = 4, how many numbers will be printed and in which order? 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

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

8 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

9 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

10 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

11 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

12 Exercise: Magic Squares  A magic square of size NxN is a NxN square such that:  Every cell contains a number between 1 and N 2.  The sums in every row and column is constant.  The numbers are all different

13 Exercise: Magic Squares  Finding a magic square 3x3 is related to finding the permutations of 1 to 9.  There exist 72 magic 3x3 squares

14 Exercise: Magic Squares  Write a program that finds all the 3x3 magic squares.  Hints  Reuse the previous recursive algorithm by applying it to permutations (enforce no repetitions).  Use two arrays of 9 elements, one for the current permutation and one to know if a number has already been used or not. Hands-On

15 Exercise: Solution  See code in IDE.

16 Going backwards pays off  Sometimes even by looking at a part of a potential solution we can say that it is wrong. In this case we can discard the whole set of potential solutions even before they have been built.  This technique is called Backtracking.  See code in IDE.

17 Data structures  You have seen several data structures  ARRAY, LINKED_LIST, HASH_TABLE, …  We will now look at another data structure and see how recursion can be used for traversal.

18 Tree

19 Tree

20 Tree: A more abstract way node root leave  A non-empty tree has one root. An empty tree does not have a root.  Every non-leaf node has links to its children. A leaf does not have children.  There are no cycles.

21 Binary tree node  A binary tree is a tree.  Each non-leaf node can have at most 2 children (possibly 0 or 1).

22 Exercise: Recursive traversal  Implement class NODE with an INTEGER attribute.  In NODE implement a recursive feature that traverses the tree and prints out the INTEGER value of each NODE object.  Test your code with a class APPLICATION which builds a binary tree and calls the traversal feature. Hands-On

23 Exercise: Solution  See code in IDE.

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

25 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

26 Exercise: Solution  See code in IDE.

27 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

28 Exercise: Solution  See code in IDE.

29 Inheritance Principle: Describe a new class as extension or specialization of an existing class (or several with multiple inheritance) If B inherits from A :  As modules: all the services of A are available in B (possibly with a different implementation)  As types: whenever an instance of A is required, an instance of B will be acceptable (“is-a” relationship)

30 Let's play Lego! BRICK LEGO_BRICK LEGO_BRICK_WITH_HOLE LEGO_BRICK_SLANTED

31 Class BRICK deferred class BRICK feature width: INTEGER depth: INTEGER height: INTEGER color: COLOR volume: INTEGER deferred end

32 Class LEGO_BRICK class LEGO_BRICK inherit BRICK feature number_of_nubs: INTEGER do Result :=... end volume: INTEGER do Result :=... end Inherit all features of class BRICK. New feature, calculate all nubs Implementation of volume.

33 Class LEGO_BRICK_SLANTED The feature volume is going to be redefined (=changed). The feature volume comes from LEGO_BRICK class LEGO_BRICK_SLANTED inherit LEGO_BRICK redefine volume end feature volume: INTEGER do Result :=... end

34 Class LEGO_BRICK_WITH_HOLE class LEGO_BRICK_WITH_HOLE inherit LEGO_BRICK redefine volume end feature volume: INTEGER do Result :=... end The feature volume is going to be redefined (=changed). The feature volume comes from LEGO_BRICK

35 Inheritance Notation volume++ BRICK LEGO_BRICK LEGO_BRICK_WITH_HOLE LEGO_BRICK_SLANTED volume* volume+ * volume++ Notation: Deferred * Effective + Redefinition ++

36 Deferred  Deferred  Deferred classes can have deferred features.  A class with at least one deferred feature must be declared as deferred.  A deferred feature does not have an implementation yet.  Deferred classes cannot be instantiated and hence cannot contain a create clause.

37 Effective  Effective  Effective classes do not have deferred features (the “standard case”) ‏.  Effective routines have an implementation of their feature body.

38 Precursor  If a feature was redefined, but you still wish to call the old one, use the Precursor keyword. volume: INTEGER do Result := Precursor -... end

39 Genericity  Genericity lets you parameterize a class. The parameters are types. A single class text may be reused for many different types.

40 Genericity LIST_OF_ CARS SET_OF_ CARS LINKED_LIST_ OF_CARS LIST_OF_ CITIES LIST_OF_ PERSONS Abstraction Specialization Type parameterization Genericity Inheritance

41 A generic list Formal generic parameter Actual generic parameter class LIST [ G ] feature extend (x : G )... last : G... end To use the class: obtain a generic derivation, e.g. cities : LIST [ CITY ]

42 ]-> RESOURCE A generic list with constraints class STORAGE [G inherit LIST [G] feature consume_all do from start until after loop item.consume forth end constrained generic parameter The feature item is checked for conformance with RESOURCE. We can assume this. The feature item is of type G. We cannot assume consume.

43 Type-safe containers  Using genericity you can provide an implementation of type safe containers. This helps avoiding object-tests. x: ANIMAL animal_list: LINKED_LIST [ANIMAL] a_rock: MINERAL animal_list.put (a_rock) -- Does this rock?