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

Slides:



Advertisements
Similar presentations
Chapter 10: Data Structures II
Advertisements

Introduction to Computer Science 2 Lecture 7: Extended binary trees
The Dictionary ADT Definition A dictionary is an ordered or unordered list of key-element pairs, where keys are used to locate elements in the list. Example:
Chapter 4: Trees Part II - AVL Tree
Data Structures Simple data type –int, char, long Reference data type –Integer, String, Composite data type == Data Structure –Collection of elements.
Advanced Database Discussion B Trees. Motivation for B-Trees So far we have assumed that we can store an entire data structure in main memory What if.
Searching Kruse and Ryba Ch and 9.6. Problem: Search We are given a list of records. Each record has an associated key. Give efficient algorithm.
CS 171: Introduction to Computer Science II
Hashing Techniques.
DictionaryADT and Trees. Overview What is the DictionaryADT? What are trees? Implementing DictionaryADT with binary trees Balanced trees DictionaryADT.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 17: Topological Sort Algorithm.
CSC 2300 Data Structures & Algorithms February 27, 2007 Chapter 5. Hashing.
Binary Search Trees CMSC 132 Chapter 8.1 Nelson Padua-Perez Bill Pugh.
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)
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,
CS 206 Introduction to Computer Science II 02 / 11 / 2009 Instructor: Michael Eckmann.
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.
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 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.
Maps A map is an object that maps keys to values Each key can map to at most one value, and a map cannot contain duplicate keys KeyValue Map Examples Dictionaries:
(c) University of Washingtonhashing-1 CSC 143 Java Hashing Set Implementation via Hashing.
B+ Tree What is a B+ Tree Searching Insertion Deletion.
Recursion and Binary Tree ICS 51 – Introductory Computer Organization.
Comp 249 Programming Methodology Chapter 15 Linked Data Structure - Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
1 Hash Tables  a hash table is an array of size Tsize  has index positions 0.. Tsize-1  two types of hash tables  open hash table  array element type.
HASHING Section 12.7 (P ). HASHING - have already seen binary and linear search and discussed when they might be useful (based on complexity)
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary search trees.
CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University1 Hashing CS 202 – Fundamental Structures of Computer Science II Bilkent.
CS212: DATA STRUCTURES Lecture 10:Hashing 1. Outline 2  Map Abstract Data type  Map Abstract Data type methods  What is hash  Hash tables  Bucket.
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,
Information and Computer Sciences University of Hawaii, Manoa
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
Big Java Chapter 16.
CS261 – Recitation 5 Fall Outline Assignment 3: Memory and Timing Tests Binary Search Algorithm Binary Search Tree Add/Remove examples 1.
TECH Computer Science Dynamic Sets and Searching Analysis Technique  Amortized Analysis // average cost of each operation in the worst case Dynamic Sets.
90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 9: Searching Data Structures.
P p Chapter 10 has several programming projects, including a project that uses heaps. p p This presentation shows you what a heap is, and demonstrates.
HIT2037- HIT6037 Software Development in Java 22 – Data Structures and Introduction.
Compsci 201 Midterm 2 Review Jimmy Wei Make sure you sign in on the paper being passed around!
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.
Computer Science: A Structured Programming Approach Using C Trees Trees are used extensively in computer science to represent algebraic formulas;
Hashing Hashing is another method for sorting and searching data.
© 2006 Pearson Education Chapter 10: Non-linear Data Structures Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition.
Chapter 10 Hashing. The search time of each algorithm depend on the number n of elements of the collection S of the data. A searching technique called.
CHAPTER 8 SEARCHING CSEB324 DATA STRUCTURES & ALGORITHM.
Hashing Fundamental Data Structures and Algorithms Margaret Reid-Miller 18 January 2005.
A Introduction to Computing II Lecture 11: Hashtables Fall Session 2000.
Data Structure II So Pak Yeung Outline Review  Array  Sorted Array  Linked List Binary Search Tree Heap Hash Table.
Hashtables. An Abstract data type that supports the following operations: –Insert –Find –Remove Search trees can be used for the same operations but require.
Week 13 - Friday.  What did we talk about last time?  Sorting  Insertion sort  Merge sort  Started quicksort.
Week 10 - Friday.  What did we talk about last time?  Graph representations  Adjacency matrix  Adjacency lists  Depth first search.
24 January Trees CSE 2011 Winter Trees Linear access time of linked lists is prohibitive  Does there exist any simple data structure for.
HEAPS. Review: what are the requirements of the abstract data type: priority queue? Quick removal of item with highest priority (highest or lowest key.
Week 15 – Wednesday.  What did we talk about last time?  Review up to Exam 1.
Week 9 - Monday.  What did we talk about last time?  Practiced with red-black trees  AVL trees  Balanced add.
Week 13 - Wednesday.  What did we talk about last time?  NP-completeness.
1 the BSTree class  BSTreeNode has same structure as binary tree nodes  elements stored in a BSTree are a key- value pair  must be a class (or a struct)
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.
Recursive Objects (Part 4)
COMP 53 – Week Eleven Hashtables.
Binary Search Tree (BST)
Chapter 10: Non-linear Data Structures
Binary Search Trees.
Presentation transcript:

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

2 Today  Basic data structures  Arrays  Linked Lists  Hashtables  Another data structure: Tree

3 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, ARRAY [G].

4 Using Arrays Which of the following lines are valid? Which can fail, and why?  my_array : 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

5 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

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

7 Hashtables Hashtables provide a way to use regular objects as keys (sort of like how we use INTEGER “keys” in arrays). This is essentially a trade-off:  We have to provide a hash function.   The hash function maps K, the set of possible keys, into an integer interval a.. b.  A perfect hash function gives a different integer value for every element of K.  Whenever two different keys give the same hash value, a collision occurs.  Our hash function should be good (minimize collisions)   Our hashtable will always take up more space than it needs to 

8 Good points about Hashtables Hashtables aren’t all that bad though, they provide us with a great solution: they can store and retrieve objects quickly by key! This is a very common operation. For each of the following, define what the key and the values could be:  A telephone book  The index of a book  Google search Name  Telephone Number Concept  Page Search String  Websites Hands-On Would you use a hashtable or an array for storing the pages of a book?

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

10 Tree

11 Tree

12 Tree: A more abstract way node root leaf  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.

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

14 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

15 Exercise: Solution  See code in IDE.

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

17 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

18 Exercise: Solution  See code in IDE.

19 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

20 Exercise: Solution  See code in IDE.

21 The End End of slides Time left? Here‘s another recursion example...

22 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 sum in every row and column is constant.  The numbers are all different

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

24 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

25 Exercise: Solution  See code in IDE.