Скачать презентацию Lecture G — Collections Unit G 1 Скачать презентацию Lecture G — Collections Unit G 1

4f65fb59b604571fb57fb22bf7e03613.ppt

  • Количество слайдов: 45

Lecture G - Collections Unit G 1 – Abstract Data Types Lecture G - Lecture G - Collections Unit G 1 – Abstract Data Types Lecture G - Collections Slide 1 of 45.

Collections Array-based implementations of simple collection abstract data types Lecture G - Collections Slide Collections Array-based implementations of simple collection abstract data types Lecture G - Collections Slide 2 of 45.

Collection data types • Very often one creates objects that hold collections of items. Collection data types • Very often one creates objects that hold collections of items. • Each type of collection has rules for accessing the elements in it § § § Array: the items are indexed by integers, and each item can be accessed according to its index Set: the items are un-ordered and appear at most once, and items are accessed according to their value Map (dictionary, associative array): the items are indexed by keys, and each item can be accessed according to its key Queue: the items are ordered and can be accessed only in FIFO order (first in first out) Stack: the items are ordered and can be accessed only in LIFO order (last in first out) Lecture G - Collections Slide 3 of 45.

Abstract data types • Data types are defined abstractly: what operations they • • Abstract data types • Data types are defined abstractly: what operations they • • • support. Each data type can be implemented in various different ways Each implementation has its own data structure Implementations may differ in their efficiency for various operations or in their storage requirements This is the standard dichotomy of interface vs. implementations In this course we will only look at very simplementations of basic abstract data types Lecture G - Collections Slide 4 of 45.

The Set abstract data type • A Set is an un-ordered collection of items The Set abstract data type • A Set is an un-ordered collection of items with no duplicates allowed. It supports the following operations: Create an empty Set § Add an item to the set § Delete an item form the set § Check whether an item is in the set § • We will exhibit an implementation using an array to hold the items • Our solution will be for a set of integers • Generic solutions are also possible • This implementation is not efficient Lecture G - Collections Slide 5 of 45.

Set public class Set { // The elements of this set, in no particular Set public class Set { // The elements of this set, in no particular order private int[] elements; // The number of elements in this set private int size; private final DEFAULT_LENGTH = 100; public Set(int max. Size) { elements = new int[max. Size]; size=0; } public Set() { this(DEFAULT_LENGTH); } Lecture G - Collections Slide 6 of 45.

Set methods public boolean contains (int x) { for(int j=0 ; j<size ; j++) Set methods public boolean contains (int x) { for(int j=0 ; j

Set – resizing on overflow public void safe. Insert(int x) { if ( !contains(x) Set – resizing on overflow public void safe. Insert(int x) { if ( !contains(x) ) { if (size == elements. length) resize(); elements[size++] = x; } } private void resize() { int[] temp = new int[2*elements. length]; for (int j=0 ; j

Set – variants for union methods public void insert. Set(Set s) { for (int Set – variants for union methods public void insert. Set(Set s) { for (int j= 0 ; j < s. size ; j++) insert(s. elements[j]); } public Set union(Set s} { Set u = new Set(size +s. size ); u. insert. Set(this); u. insert. Set(s); return u; } public static Set union(Set s, Set t) { return s. union(t); } Lecture G - Collections Slide 9 of 45.

Immutable Set • An immutable object type is an object that can not be Immutable Set • An immutable object type is an object that can not be • • changed after its construction An immutable set does not support the insert and delete operations Instead, its constructor will receive an array of the elements in the set It only supports the contains predicate We will show an efficient implementation for immutable sets contains() will use binary search § contains() will require only O(log N) time § Lecture G - Collections Slide 10 of 45.

Immutable. Set public class Immutable. Set { /** The elements of this set, ordered Immutable. Set public class Immutable. Set { /** The elements of this set, ordered from lowest to * highest */ private int[] elements; /** The elements given as the parameter do not have * to be ordered. We do assume that there are no * duplicates. */ public Immutable. Set(int[] elements) { this. elements = new int[elements. length]; for(int j=0 ; j < elements. length; j++) this. elements[j]=elements[j]; Merge. Sort. merge. Sort(this. elements); } Lecture G - Collections Slide 11 of 45.

Immutable. Set – binary search public boolean contains (int x) { return contains(x, 0, Immutable. Set – binary search public boolean contains (int x) { return contains(x, 0, elements. length - 1); } private boolean contains(int x, int low, int high){ if (low > high) return false; int med = (low + high) / 2; if (x == elements[med]) return true; else if (x < elements[med]) return contains(x, low, med-1); else return contains(x, med+1, high); } Lecture G - Collections Slide 12 of 45.

Lecture G - Collections Unit G 2 - Stacks Lecture G - Collections Slide Lecture G - Collections Unit G 2 - Stacks Lecture G - Collections Slide 13 of 45.

Stacks How to use and implement stacks Lecture G - Collections Slide 14 of Stacks How to use and implement stacks Lecture G - Collections Slide 14 of 45.

The Stack data type • A stack holds an ordered collection of items, items The Stack data type • A stack holds an ordered collection of items, items can be accessed only in first-in-last-out order • Its operations are: Create an empty stack § Push an item into the stack § Pop an item from the stack – removes (and returns) the last item that was pushed onto the stack § Check whether the stack it empty or not § • Used in many aspects of implementation of high level programming languages • Most CPUs support stacks in hardware Lecture G - Collections Slide 15 of 45.

Stack sample run • Create empty stack{ } • Push 5 • Push 4 Stack sample run • Create empty stack{ } • Push 5 • Push 4 • Push 8 • Pop (returns 8) • Push 7 • Pop (returns 7) (returns 4) Lecture G - Collections { 5{ { 5 4 8{ { 5 4 7{ { 5 4{ { 5{ Slide 16 of 45.

Evaluating expressions • 5 * (( 6 + 3) / ( 7 – 2 Evaluating expressions • 5 * (( 6 + 3) / ( 7 – 2 * 2)) § § § Push 5 Push 6 Push 3 + Push 7 Push 4 Push 1 * / * {5} {5 6 3} push( pop() + pop() ) { 5 9 } {5 9 7 2} {5 9 7 2 2} push( pop() * pop() ) { 5 9 7 4 } push( pop() - pop() ) { 5 9 3 } push( pop() / pop() ) { 5 3 } push( pop() * pop() ) { 15 } Lecture G - Collections Slide 17 of 45.

Handling return addresses class A { void a() { // … B. b() 1 Handling return addresses class A { void a() { // … B. b() 1 // … B. c() 5 // … } } class B { public void b() { // … c(); 2 // … } 4 public void c() { // … 3 6 } } A. a : B. b : 1 ret : A. a(1) 2 A. a : B. b : B. c : Lecture G - Collections 5 4 A. a : ret : A. a(1) ret : B. b(2) B. c : ret : . a(5) 6 3 Slide 18 of 45.

Allocating parameters and local variables void a() { int x, y; b(5); 1 c(6, Allocating parameters and local variables void a() { int x, y; b(5); 1 c(6, 7); 5 } void b(int z) { int w; c(8, 9); 2 } 4 void c(int q, int r) { int s; 3 6 } a: b: a: params : z=5 locals: w 2 a: b: c: Lecture G - Collections 1 5 4 a: params : z=5 c: locals: w params : q=8, r=9 params : r=6, q=7 locals: s 6 3 locals: s Slide 19 of 45.

The Method Frame void a() { int x, y; b(5); 1 } void b(int The Method Frame void a() { int x, y; b(5); 1 } void b(int z) { int w; w = 1 + 5 * c(8, 9); } a: 1 a: b: 2 params : z=5 2 return : a(1) a: locals: w comp: w=1+5*c(8, 9) int c(int q, int r) { int s; return 7 + q ; 3 } b: params : z=5 return : a(1) locals: w comp: w=1+5*c(8, 9) params : q=8, r=9 3 return : b(2) locals: s comp: 7+q Lecture G - Collections Slide 20 of 45.

Array based Stack implementation • We will show an array based implementation § We Array based Stack implementation • We will show an array based implementation § We will ignore overflow – exceeding the array size • Can be corrected using resize() § We will ignore underflow – popping from an empty stack • This is the caller’s error – should be an exception • All methods run in O(1) time • Later we will show another implementation Lecture G - Collections Slide 21 of 45.

Stack public class Stack { private int[] elements; private int top; private final static Stack public class Stack { private int[] elements; private int top; private final static int DEFAULT_MAX_SIZE = 10; public Stack(int max. Size){ elements = new int[max. Size]; top=0; } public Stack() { this(DEFAULT_MAX_SIZE); } public void push(int x) { elements[top++] =x; } public int pop() { return elements[--top]; } public boolean is. Empty() { return top == 0; } } Lecture G - Collections Slide 22 of 45.

Lecture G - Collections Unit G 3 – Linked List structure Lecture G - Lecture G - Collections Unit G 3 – Linked List structure Lecture G - Collections Slide 23 of 45.

Linked Data Structures How to build and use recursive data structures Lecture G - Linked Data Structures How to build and use recursive data structures Lecture G - Collections Slide 24 of 45.

Recursive data structures • An object may have fields of its own type • Recursive data structures • An object may have fields of its own type • This does not create a problem since the field only has a reference to the other object • This allows building complicated linked data structures For each person, a set of friends § For each house on a street, its two neighbors § For each vertex in a binary tree, its two sons § For each web page, the pages that it links to § • There are several common structures Lists (singly linked, doubly linked) § Trees (binary, n-ary, search, …) § Graphs (directed, undirected) § Lecture G - Collections Slide 25 of 45.

Person public class Person { private Person best. Friend; private String name; public Person(String Person public class Person { private Person best. Friend; private String name; public Person(String name) { this. name = name; } public String get. Name() { return name; } public Person get. Best. Friend() { return best. Friend; } public void set. Best. Friend(Person friend) { best. Friend = friend; } } Lecture G - Collections Slide 26 of 45.

Person Usage Example (add animation( Danny Person a = new person(“Alice”); Carol d Person Person Usage Example (add animation( Danny Person a = new person(“Alice”); Carol d Person b = new Person(“Bob”); c Person c = new Person(“Carol”); Person d = new Person(“Danny”); Bob Alice a. set. Best. Friend(b); b a b. set. Best. Friend(a); c. set. Best. Friend(d); d. setbest. Friend(a); Person e = c. get. Best. Friend(); // a. e System. out. println(e. get. Name()); // Alice if (e == b. get. Best. Friend()) { … } // true Lecture G - Collections Slide 27 of 45.

Linked Lists • The simplest linked data structure: a simple linear list. data start Linked Lists • The simplest linked data structure: a simple linear list. data start data null Lecture G - Collections Slide 28 of 45.

List public class List { private int data; private List next; public List(int data, List public class List { private int data; private List next; public List(int data, List next) { this. data = data; this. next = next; } public int get. Data() { return data; } public List get. Next() { return next; } } Lecture G - Collections Slide 29 of 45.

Implementing a Set public class Set { private List elements; public Set() { elements Implementing a Set public class Set { private List elements; public Set() { elements = null; } public boolean contains(int x) { for (List c = elements ; c != null ; c = c. get. Next()) if (c. get. Data() == x) return true; return false; } public void insert(int x) { if (!contains(x)) elements = new List(x, elements); } } Lecture G - Collections Slide 30 of 45.

Sample run 1 Set s = new Set(); s. insert(3); 2 s. insert(5) 3 Sample run 1 Set s = new Set(); s. insert(3); 2 s. insert(5) 3 if (s. contains(3)) { … } null elements 3 null 1 2 elements 5 3 null 3 Lecture G - Collections Slide 31 of 45.

Sample run 1 Set s = new Set(); s. insert(3); 2 s. insert(5) 3 Sample run 1 Set s = new Set(); s. insert(3); 2 s. insert(5) 3 if (s. contains(3)) { … } elements 4 5 3 null 4 if(c. get. Data()==3) c Lecture G - Collections Slide 32 of 45.

Sample run 1 Set s = new Set(); s. insert(3); 2 s. insert(5) 3 Sample run 1 Set s = new Set(); s. insert(3); 2 s. insert(5) 3 if (s. contains(3)) { … } elements 4 5 3 null 4 if(c. get. Data()==3) c Lecture G - Collections Slide 33 of 45.

List iterator public class List. Iterator { private List current; public List. Iterator(List list) List iterator public class List. Iterator { private List current; public List. Iterator(List list) { current = list; } public boolean has. Next() { return current != null; } public int next. Item() { int item = current. get. Data(); current = current. get. Next(); return item; } } Lecture G - Collections Slide 34 of 45.

Using Iterators public class Set { private List elements; // constructor and insert() as Using Iterators public class Set { private List elements; // constructor and insert() as before public boolean contains(int x) { for (List. Iterator l = new List. Iterator(elements); l. has. Next(); ) if (l. next. Item() == x) return true; return false; } public String to. String() { String. Buffer answer = new Stringbuffer(“{“); for (List. Iterator l = new List. Iterator(elements); l. has. Next(); ) answer. append(“ “ + l. next. Item() + “ “); answer. append(“}”); return answer. to. String(); } } Lecture G - Collections Slide 35 of 45.

Implementing a Stack public class Stack { private List elements; public Stack() { elements Implementing a Stack public class Stack { private List elements; public Stack() { elements = null; } public void push(int x) { elements = new List(x, elements); } public boolean is. Empty() { return elements == null; } public int pop() { int x = elements. get. Data(); elements = elements. get. Next(); return x; } } Lecture G - Collections Slide 36 of 45.

Lecture G 4 - Collections Unit G 4 - List Represnetation of Naturals Lecture Lecture G 4 - Collections Unit G 4 - List Represnetation of Naturals Lecture G - Collections Slide 37 of 45.

Recursion on lists An example of using lists and recursion for implementing the natural Recursion on lists An example of using lists and recursion for implementing the natural numbers Lecture G - Collections Slide 38 of 45.

Foundations of Logic and Computation • What are the very basic axioms of mathematics? Foundations of Logic and Computation • What are the very basic axioms of mathematics? Usually the axioms of Zermello-Frenkel set theory § Only sets exists in this theory § How are natural numbers handled? § Built recursively from sets: 0 = {} , 1 = { {} } , 2 = { {} } } , … § • What is the simplest computer? § § § Usually one takes the “Turing Machine” What can it do? Everything that any other computer can do How? It can simulate any other computer Lecture G - Collections Slide 39 of 45.

Successor representation of the naturals • Mathematically, the only basic notions you need in Successor representation of the naturals • Mathematically, the only basic notions you need in order to define the natural numbers are 1 (the first natural) § Successor (succ(x) = x + 1) § Induction § • We will provide a Java representation of the naturals using the successor notion in a linked list § Mathematical Operations will be defined using recursion 3: null Lecture G - Collections Slide 40 of 45.

Natural public class Natural { private Natural predecessor; public static final Natural ZERO = Natural public class Natural { private Natural predecessor; public static final Natural ZERO = null; public Natural(Natural predecessor) { this. predecessor = predecessor; } public Natural minus 1() { return predecessor; } public Natural plus 1() { return new Natural(this); } public boolean equals 1() { return predecessor == ZERO; } Lecture G - Collections Slide 41 of 45.

< , = , - , + public Natural plus(Natural y) { if (y < , = , - , + public Natural plus(Natural y) { if (y == ZERO) return this; return this. plus 1(). plus(y. minus 1()); } /** returns max(this-y, 0) */ public Natural minus(Natural y) { if (y == ZERO) return this; if (y. equals 1()) return ZERO; return this. minus 1(). minus(y. minus 1()); } public boolean ge(Natural y) { return y. minus(this) == ZERO; } public boolean eq(Natural y) {return this. ge(y) && y. ge(this); } public boolean gt(Natural y) { return !y. ge(this); } Lecture G - Collections Slide 42 of 45.

, / , * mod public Natural times(Natural y) { if (y == ZERO) , / , * mod public Natural times(Natural y) { if (y == ZERO) return ZERO; return this. plus(this. times(y. minus 1())); } public Natural div(Natural y) { if ( y. gt(this) ) return ZERO; return ((this. minus(y)). div(y)). plus 1(); } public Natural mod(Natural y) { return this. minus((this. div(y)). times(y)); } Lecture G - Collections Slide 43 of 45.

printing final static Natural ONE = new Natural(ZERO); final static Natural TEN= ONE. plus printing final static Natural ONE = new Natural(ZERO); final static Natural TEN= ONE. plus 1(); private char last. Digit() { Natrual digit = this. mod(TEN); if ( digit == ZERO ) return ‘ 0’; if ( (digit = digit. minus 1()) == ZERO ) return ‘ 1’; if ( (digit = digit. minus 1()) == ZERO ) return ‘ 2’; // … if ( (digit = digit. minus 1()) == ZERO ) return ‘ 8’; return ‘ 9’; } public String to. String() { return (TEN. gt(this) ? “” : this. div(TEN). to. String()) +last. Digit(); }} Lecture G - Collections Slide 44 of 45.

Lecture G - Collections Slide 45 of 45. Lecture G - Collections Slide 45 of 45.