CSI33 Data Structures

Similar documents
Outline. The Python Memory Model A Linked Implementation of Lists In-Class Work. 1 Chapter 4: Linked Structures and Iterators

CSI33 Data Structures

CSI33 Data Structures

CSI33 Data Structures

CSI33 Data Structures

Linked Lists. Linked List vs Array vs Python List Singly Linked List Bag ADT Revisited Building Linked Lists

Python review. 1 Python basics. References. CS 234 Naomi Nishimura

Homework notes. Homework 2 grades posted on canvas. Homework 3 due tomorrow. Homework 4 posted on canvas. Due Tuesday, Oct. 3

CSI33 Data Structures

CSI33 Data Structures

CS 234. Module 3. September 29, CS 234 Module 3 Data structures, ADTs with no order of any kind, Python review 1 / 19

Abstract Data Types Chapter 1

CSI33 Data Structures

MUTABLE LISTS AND DICTIONARIES 4

Outline. An Application: A Binary Search Tree. 1 Chapter 7: Trees. favicon. CSI33 Data Structures

CSI33 Data Structures

CS 234. Module 3. September 19, CS 234 Module 3 Data structures, ADTs with no order of any kind, Python review 1 / 33

Sets and Maps. Set Commands Set ADT Set ADT implementation Map ADT Map ADT implementation

Linked Structures Chapter John Wiley & Sons, Data Structures and Algorithms Using Python, by Rance D. Necaise.

CSI33 Data Structures

Lecture 12 ADTs and Stacks

Outline. LList: A Linked Implementation of a List ADT Iterators Links vs. Arrays In-class work. 1 Chapter 4: Linked Structures and Iterators

CSI33 Data Structures

CSI32 Object-Oriented Programming

CSI33 Data Structures

AP Computer Science 4325

Python. Jae-Gil Lee Based on the slides by K. Naik, M. Raju, and S. Bhatkar. December 28, Outline

CSI33 Data Structures

CSI33 Data Structures

Do not turn this page until you have received the signal to start. In the meantime, please read the instructions below carefully.

CIS192: Python Programming

61A Lecture 2. Wednesday, September 4, 2013

CSI33 Data Structures

CSI33 Data Structures

CIS192: Python Programming Data Types & Comprehensions Harry Smith University of Pennsylvania September 6, 2017 Harry Smith (University of Pennsylvani

What Would Python Print? Tuples, Lists, Dictionaries

Fundamentals of Programming (Python) Object-Oriented Programming. Ali Taheri Sharif University of Technology Spring 2018

TREES AND MUTATION 5

CSI33 Data Structures

Chapter 17: Linked Lists

Sharing, mutability, and immutability. Ruth Anderson UW CSE 160 Spring 2018

CS 331/401 Summer 2018 Midterm Exam

CSC 148 Lecture 3. Dynamic Typing, Scoping, and Namespaces. Recursion

Functions, Scope & Arguments. HORT Lecture 12 Instructor: Kranthi Varala

CS2304: Python for Java Programmers. CS2304: Sequences and Collections

COMP1730/COMP6730 Programming for Scientists. Sequence types, part 2

G Programming Languages - Fall 2012

Abstract Data Types. CS 234, Fall Types, Data Types Abstraction Abstract Data Types Preconditions, Postconditions ADT Examples

Lecture 15: Dictionaries

Python Programming: Lecture 2 Data Types

Linear Structures. Linear Structure. Implementations. Array details. List details. Operations 4/18/2013

Programming Languages: Lecture 11

CS Introduction to Computational and Data Science. Instructor: Renzhi Cao Computer Science Department Pacific Lutheran University Spring 2017

CS Lecture 26: Grab Bag. Announcements

Linear Structures. Linear Structure. Implementations. Array details. List details. Operations 2/10/2013

Data abstractions: ADTs Invariants, Abstraction function. Lecture 4: OOP, autumn 2003

Chapter 11: File-System Interface

COMPSCI 105 S Principles of Computer Science. 17 Linked List(1)

Lecture 18: Lists II. CS1068+ Introductory Programming in Python. Dr Kieran T. Herley 2018/19. Department of Computer Science University College Cork

Algorithms for Bioinformatics

pygtrie Release Jul 03, 2017

Computer Science 9608 (Notes) Chapter: 4.1 Computational thinking and problem-solving

Why study algorithms? CS 561, Lecture 1. Today s Outline. Why study algorithms? (II)

Lists, Mutability, ADTs, and Trees Spring 2019 Guerrilla Section 2: March 2, 2019 Solutions. 1 Sequences. Questions. lst = [1, 2, 3, 4, 5] lst[1:3]

First-Class Synchronization Barriers. Franklyn Turbak Wellesley College

Instructors: Daniel Deutch, Amir Rubinstein, Teaching Assistants: Amir Gilad, Michal Kleinbort

Structure and Interpretation of Computer Programs

Interactive use. $ python. >>> print 'Hello, world!' Hello, world! >>> 3 $ Ctrl-D

Python in the Cling World

61A Lecture 2. Friday, August 28, 2015

Programming to Python

CSI33 Data Structures

Recap: Functions as first-class values

Algorithmic Thinking: Computing with Lists

Queues The Queue ADT. The Queue ADT is defined by the following operations: init Initialize a new empty queue.

CS108 Lecture 16: User Defined Classes. Overview/Questions

EE 355 Unit 17. Python. Mark Redekopp

CSM Mock Final Spring 2018

Announcements. Lecture Agenda. Class Exercise. Hashable. Mutability. COMP10001 Foundations of Computing Iteration

Recursion. Lecture 26 Sections , Robb T. Koether. Hampden-Sydney College. Mon, Apr 6, 2015

Recursion. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 17, 2018

Variable and Data Type I

Course Outline - COMP150. Lectures and Labs

GIS 4653/5653: Spatial Programming and GIS. More Python: Statements, Types, Functions, Modules, Classes

Delayed Expressions Fall 2017 Discussion 9: November 8, Iterables and Iterators. For Loops. Other Iterable Uses

The Object Concept. Object-Oriented Programming Spring 2015

Lecture 24 Tao Wang 1

CSI33 Data Structures

BBM 201 DATA STRUCTURES

Linked Lists. Lecture 16 Sections Robb T. Koether. Hampden-Sydney College. Wed, Feb 22, 2017

CS 11 python track: lecture 3. n Today: Useful coding idioms

DataStruct 9. Hash Tables, Maps, Skip Lists, and Dictionaries

Python Data Structures

61A Lecture 3. Friday, September 5

DECOMPOSITION, ABSTRACTION, FUNCTIONS

INF 212 ANALYSIS OF PROG. LANGS PROCEDURES & FUNCTIONS. Instructors: Kaj Dreef Copyright Instructors.

Introduction to Linked List: Review. Source:

Programming for Engineers in Python

Metadata for Component Optimisation

ECE 364 Software Engineering Tools Lab. Lecture 8 Python: Advanced I

Transcription:

Outline Department of Mathematics and Computer Science Bronx Community College September 18, 2017

Outline Outline 1 Chapter 4: Linked Structures and Iterators

Outline 1 Chapter 4: Linked Structures and Iterators

Variable Names and References Assignment Statements An assignment statement in Python associates an object with the name of a variable. More precisely, the name is associated with a reference to an object of some class, or type. This association remains in effect until a new object reference is associated with the variable through a new assignment statement.

Python Assignment Examples d = Dave j = d j = John d = Smith d Dave

Python Assignment Examples d = Dave j = d j = John d = Smith d j Dave

Python Assignment Examples d = Dave j = d j = John d = Smith d j Dave John

Python Assignment Examples d = Dave j = d j = John d = Smith d j Dave John Smith

Namespaces The Local Dictionary The values of local variables those which are currently active, are kept by Python, along with function names, in a dictionary object, called a namespace. This dictionary is available by calling the built-in function locals(). It can be modified directly. For example, the command del d removes the name d from the local namespace.

Variable Types and References in Python Dynamic Typing Dynamic typing in Python means that a variable does not have a fixed type like int. Rather, its type can change, depending on what object it refers to for its value. This is because the referent (the object referred to) contains the type information for that value. If the variable is assigned to another object, it will then have the type of the other object.

Aliasing lst1 = [1, 2, 3] lst2 = lst1 lst2.append(4) lst1 lst1 1 2 3

Aliasing lst1 = [1, 2, 3] lst2 = lst1 lst2.append(4) lst1 lst1 lst2 1 2 3

Aliasing lst1 = [1, 2, 3] lst2 = lst1 lst2.append(4) lst1 lst1 lst2 1 2 3 4

Aliasing lst1 = [1, 2, 3] lst2 = lst1 lst2.append(4) lst1 [1, 2, 3, 4] lst1 lst2 1 2 3 4

Copying Variable Values in Python Deep and Shallow Copy To avoid the problem of aliasing, we can, by using the copy function, force a new copy of a value to be created, so when it gets changed, the original variable, which refers to the original object, will keep its original value.

Copying Variable Values in Python Deep and Shallow Copy >>>from copy import * >>> b = [1, [2, 3]] >>> c = b b c 1 2 3

Copying Variable Values in Python Shallow Copy >>> d = copy(b) b c 1 2 3 d

Copying Variable Values in Python Deep Copy If a container object refers to other objects, these can be copied as well, using the deepcopy function. >>> e = deepcopy(b) b c 1 2 3 d e

Passing Parameters in Function Calls Formal vs. Actual Parameters Formal parameters are the variable names used in implementing the function. They are listed in parentheses after the function name in the function definition, then used to express the Python commands needed to perform the algorithm of the function. Actual parameters are the variable names used by the program where the function is called with specific values. When it is called, the function cannot change the value of an actual parameter, but it can change an object to which the actual parameter refers.

A Function Call Example def func(e, f, g): e += 2 f.append(4) g = [8, 9] print (e, f, g) def main(): b = 0 c = [1, 2, 3] d = [5, 6, 7] func(b, c, d) print (b, c, d) b c d 0 1 2 3 5 6 7

A Function Call Example def func(e, f, g): e += 2 f.append(4) g = [8, 9] print (e, f, g) def main(): b = 0 c = [1, 2, 3] d = [5, 6, 7] func(b, c, d) print (b, c, d) b 0 c 1 2 3 d 5 6 7 e f g

A Function Call Example def func(e, f, g): e += 2 f.append(4) g = [8, 9] print (e, f, g) def main(): b = 0 c = [1, 2, 3] d = [5, 6, 7] func(b, c, d) print (b, c, d) b 0 c 1 2 3 d 5 6 7 e 2 f g

A Function Call Example def func(e, f, g): e += 2 f.append(4) g = [8, 9] print (e, f, g) def main(): b = 0 c = [1, 2, 3] d = [5, 6, 7] func(b, c, d) print (b, c, d) b 0 c 1 2 3 d 5 6 7 e 2 f 4 g

A Function Call Example def func(e, f, g): e += 2 f.append(4) g = [8, 9] print (e, f, g) def main(): b = 0 c = [1, 2, 3] d = [5, 6, 7] func(b, c, d) print (b, c, d) b 0 c 1 2 3 d 5 6 7 e 2 f 4 g 8 9

A Function Call Example def func(e, f, g): e += 2 f.append(4) g = [8, 9] print (e, f, g) def main(): b = 0 c = [1, 2, 3] d = [5, 6, 7] func(b, c, d) print (b, c, d) 2 [1, 2, 3, 4] [8, 9] b 0 c 1 2 3 d 5 6 7 e 2 f g 4 8 9

A Function Call Example def func(e, f, g): e += 2 f.append(4) g = [8, 9] print (e, f, g) def main(): b = 0 c = [1, 2, 3] d = [5, 6, 7] func(b, c, d) print (b, c, d) 2 [1, 2, 3, 4] [8, 9] 0 [1, 2, 3, 4] [5, 6, 7] b 0 c 1 2 3 d 5 6 7 4

A Weakness of Using Arrays to Implement A List ADT Problem Python uses arrays of references to implement the list ADT, because arrays can easily be traversed by proceeding along a series of contiguous memory locations. Arrays also allow random access, that is, jumping quickly to any index location in the array, according to a formula for the address which is easy to calculate. But using arrays, the insert and delete operations for lists were both Θ(n), since they both require copying much of the list to keep its sequential order. For long lists, this is a problem. Fortunately, another design for sequential lists is possible which has faster insert and delete operations (at the cost of slower random access).

Linked Lists Data Items As Nodes Using the idea of a reference (pointer), ordering items into a list can be accomplished by having each item have its own reference to the next item. The list can be traversed sequentially by hopping from each item to the one it refers to. The value of each item is kept together with the reference/pointer to the next item in an object called a node.

Linked Lists The ListNode Class We can define a class to support this structure, with attributes item (for the data value) and link (for the reference to the next item): class ListNode(object): def init (self, item = None, link = None): """ post: creates a ListNode with the specified data value and link """ self.item = item self.link = link

Building A Linked List n3 = ListNode(3) 3 None n3

Building A Linked List n3 = ListNode(3) n2 = ListNode(2, n3) 2 3 None n2 n3

Building A Linked List n3 = ListNode(3) n2 = ListNode(2, n3) n1 = ListNode(1, n2) 1 2 3 None n1 n2 n3

Inserting a Node Into A Linked List def init (self, item, link): self.item = item self.link = link n25 = ListNode(2.5, n2.link) n2.link = n25 1 2 n1 n2 self item link 2.5 3 None n3

Inserting a Node Into A Linked List def init (self, item, link): self.item = item self.link = link n25 = ListNode(2.5, n2.link) n2.link = n25 1 2 n1 n2 self item 2.5 3 None n3 link 2.5

Inserting a Node Into A Linked List def init (self, item, link): self.item = item self.link = link n25 = ListNode(2.5, n2.link) n2.link = n25 1 2 n1 n2 self item 2.5 3 None n3 link 2.5

Inserting a Node Into A Linked List def init (self, item, link): self.item = item self.link = link n25 = ListNode(2.5, n2.link) n2.link = n25 1 2 n1 n2 2.5 3 None n3 n25

Inserting a Node Into A Linked List def init (self, item, link): self.item = item self.link = link n25 = ListNode(2.5, n2.link) n2.link = n25 1 2 n1 n2 2.5 3 None n3 n25