Chair of Software Engineering 1 Introduction to Programming Exercise Session Week 10 M. Piccioni 24/25 November 2008.

Slides:



Advertisements
Similar presentations
Chapter 7. Binary Search Trees
Advertisements

Singly linked lists Doubly linked lists
1 Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 13.
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.
Binary Trees Chapter 6. Linked Lists Suck By now you realize that the title to this slide is true… By now you realize that the title to this slide is.
CS 171: Introduction to Computer Science II
Backtracking What is backtracking?
Binary Search Trees CMSC 132 Chapter 8.1 Nelson Padua-Perez Bill Pugh.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (4) Data Structures 11/18/2008 Yang Song.
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.
Introduction to Data Structure, Fall 2006 Slide- 1 California State University, Fresno Introduction to Data Structure Chapter 10 Ming Li Department of.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 9.
Unit 11a 1 Unit 11: Data Structures & Complexity H We discuss in this unit Graphs and trees Binary search trees Hashing functions Recursive sorting: quicksort,
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 Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 7.
1 Section 9.2 Tree Applications. 2 Binary Search Trees Goal is implementation of an efficient searching algorithm Binary Search Tree: –binary tree in.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 10.
Week 7 - Wednesday.  What did we talk about last time?  Recursive running time  Master Theorem  Introduction to trees.
Binary Trees Chapter 6.
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.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Data Structures Using C++ 2E Chapter 6 Recursion.
Binary Search Trees. BST Properties Have all properties of binary tree Items in left subtree are smaller than items in any node Items in right subtree.
Tree.
Data Structures Using C++ 2E Chapter 6 Recursion.
Scott Marino MSMIS Kean University MSAS5104 Programming with Data Structures and Algorithms Week 10 Scott Marino.
Chapter 19: Binary Trees. Objectives In this chapter, you will: – Learn about binary trees – Explore various binary tree traversal algorithms – Organize.
Lecture 10: Class Review Dr John Levine Algorithms and Complexity March 13th 2006.
Searching: Binary Trees and Hash Tables CHAPTER 12 6/4/15 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education,
1 Lecture 11 POLYNOMIALS and Tree sort 2 INTRODUCTION EVALUATING POLYNOMIAL FUNCTIONS Horner’s method Permutation Tree sort.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Course Review Midterm.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
Chapter 6 Binary Trees. 6.1 Trees, Binary Trees, and Binary Search Trees Linked lists usually are more flexible than arrays, but it is difficult to use.
1 Wright State University, College of Engineering Dr. T. Doom, Computer Science & Engineering CS 241 Computer Programming II CS 241 – Computer Programming.
Chapter 9 (modified) Abstract Data Types and Algorithms Nell Dale John Lewis.
CompSci 100e Program Design and Analysis II March 29, 2011 Prof. Rodger CompSci 100e, Spring20111.
9-1 Abstract Data Types Abstract data type A data type whose properties (data and operations) are specified independently of any particular implementation.
CSC 221: Recursion. Recursion: Definition Function that solves a problem by relying on itself to compute the correct solution for a smaller version of.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 9.
Binary Search Trees Nilanjan Banerjee. 2 Goal of today’s lecture Learn about Binary Search Trees Discuss the first midterm.
Binary Search Tree Traversal Methods. How are they different from Binary Trees?  In computer science, a binary tree is a tree data structure in which.
Midterm CSG 110 Karl Lieberherr. Managing Software Development Managers of software development must first be software developers.
CompSci 100e 7.1 Plan for the week l Review:  Union-Find l Understand linked lists from the bottom up and top-down  As clients of java.util.LinkedList.
1 Binary Trees and Binary Search Trees Based on Dale & Co: Object-Oriented Data Structures using C++ (graphics)
2014-T2 Lecture 27 School of Engineering and Computer Science, Victoria University of Wellington  Lindsay Groves, Marcus Frean, Peter Andreae, and Thomas.
CMSC 202, Version 5/02 1 Trees. CMSC 202, Version 5/02 2 Tree Basics 1.A tree is a set of nodes. 2.A tree may be empty (i.e., contain no nodes). 3.If.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Copyright © 2012 Pearson Education, Inc. Chapter 10 Advanced Topics.
(c) University of Washington20-1 CSC 143 Java Trees.
Planning & System installation
Recursive Objects (Part 4)
Binary Search Tree (BST)
Section 8.1 Trees.
COMP 103 Binary Search Trees.
Einführung in die Programmierung Introduction to Programming Prof. Dr
Binary Tree Applications
Lesson Objectives Aims
Trees CMSC 202, Version 5/02.
CMSC 202 Trees.
Binary Search Trees.
Einführung in die Programmierung Introduction to Programming Prof. Dr
Einführung in die Programmierung Introduction to Programming Prof. Dr
The structure of programming
CSC 143 Java Trees.
Einführung in die Programmierung Introduction to Programming Prof. Dr
CO4301 – Advanced Games Development Week 4 Binary Search Trees
Data Structures Using C++ 2E
Warmup Which of the 4 sorting algorithms uses recursion?
Presentation transcript:

Chair of Software Engineering 1 Introduction to Programming Exercise Session Week 10 M. Piccioni 24/25 November 2008

Chair of Software Engineering 2 Announcement In two weeks (December 9th)... Mock exam 2

Chair of Software Engineering 3 Featuring today  Genericity  Recursion  Recursion and Trees!

Chair of Software Engineering 4 Genericity: Type parametrization LIST_OF_ ANIMALS LIST_OF_ PEOPLE LIST_OF_ CARS Type parameterization

Chair of Software Engineering 5 Genericity - why?  To parametrize a class, abstracting away all the operations in common  A single class text may be reused for many different objects types.  To provide an implementation of type safe containers  Helps avoiding objects-test/assignment attempt

Chair of Software Engineering 6 A Generic List class LINKED_LIST[G] feature item: G -- the element cursor is pointing at do... end append(value: G) -- append a value to the list do... end end G will be replaced by the compiler with the needed type that has to be specified at some point. Generic parameter

Chair of Software Engineering 7 A “natural” example class ANIMAL create make feature -- Initialization make (a_genus: STRING) ‏ do genus := a_genus end feature -- Status genus: STRING... end

Chair of Software Engineering the animals: from ____________________ until__________________ _______________ loop x := _____________ print(x.genus)‏ ________________ end 8 Type safe Containers x: ANIMAL animal_list: LINKED_LIST [ANIMAL] a_rock: MINERAL animal_list.put(a_rock) -- does this rock? Hands-On animal_list.start animal_list.after animal_list.item animal_list.forth

Chair of Software Engineering Constrained generic parameter note: “Storage for resources” class STORAGE [G ->RESOURCE] inherit LIST[G] consume_all do from start until after loop item.consume forth end... end 9 A Constrained Generic List item is checked for conformance with RESOURCE -- client code sf: STORAGE [FISH] sg: STORAGE [GRAIN]

Chair of Software Engineering 10 The Waldgarten Project We need to associate each plant in the same category (categories are trees, bushes, herbs and mosses) to a unique id. Suggest one or more classes for the specified context. Hands-On

Chair of Software Engineering 11 Yes, we can (constrain) Solution class PLANT... species: STRING... end Usage: herbs: HASH_TABLE [HERB, STRING] bushes: HASH_TABLE [BUSH, STRING]... herbs.add(myherb, myherb.species)

Chair of Software Engineering Today  Recursion 12

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

Chair of Software Engineering 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 14

Chair of Software Engineering Recursion warm-up exercise 1 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 15 Hands-On

Chair of Software Engineering Recursion warm-up exercise 2 If we pass n=4, how many numbers will be printed and in which order? print_int (n: INTEGER) do if n>1 then print_int (n-1) end print (n) end 16 Hands-On

Chair of Software Engineering Recursion exercise 3 Print a given string in reverse order using a recursive function 17 Hands-On

Chair of Software Engineering Sequences and Recursion 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, Hands-On

Chair of Software Engineering Magic Squares  The sums in every row and column is constant  Numbers are all different

Chair of Software Engineering Magic Squares and Permutations  Finding a magic square 3x3 is related to finding the permutations of 1 to There exist 72 magic 9x9 squares (8 of which distinct)

Chair of Software Engineering Find the Magic Squares Write a program that finds all the eight 9x9 magic squares. Hint 1: Refactor the previous recursive algorithm applying it to permutations (enforce no repetitions) Hint 2: Use two arrays of 9 elements, one for the current permutation and one to know if a number has already been used or not Hint 3: If you are desperate, have a look at the posted solution with the debugger 21 Hands-On

Chair of Software Engineering Going backwards pays off  When the program finds, at the 9th recursion, that a square is not magical, it goes back on the call stack to the previous instance of the function, and even more back if needed.  This technique is called BackTracking and is used in many recursive programs. 22

Chair of Software Engineering Data structures You have seen several data structures  ARRAY, LINKED_LIST, HASH_TABLE, … We will now look at another data structure  (Binary) tree and its recursive traversal 23

Chair of Software Engineering Tree 24

Chair of Software Engineering Tree 25

Chair of Software Engineering Tree – in a more abstract way Node Tree:  One root  Typically several leaves “ROOT” “LEAF” 26

Chair of Software Engineering Binary Tree Node Binary tree:  One root  Typically several leaves  Each node can have at most 2 children (possibly 0 or 1) “ROOT” “LEAF” 27

Chair of Software Engineering Recursive traversal of a binary tree  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 by class APPLICATION which builds a binary tree and calls the traversal feature 28 Hands-On

Chair of Software Engineering Binary Search Tree Binary search tree:  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 “ROOT” “LEAF” 29

Chair of Software Engineering Creating a binary search tree  Implement command put (n: INTEGER) in class NODE which creates and links a new NODE object at the right place in the binary search tree rooted by Current  Test your code by 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 30 Hands-On

Chair of Software Engineering 31 Questions?

Chair of Software Engineering 32 Backup slides

Chair of Software Engineering Searching in a binary search tree  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 by class APPLICATION which builds a binary search tree and calls has 33 Hands-On