Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Structures and Abstract Data Types "Get your data structures correct first, and the rest of the program will write itself." - David Jones.

Similar presentations


Presentation on theme: "Data Structures and Abstract Data Types "Get your data structures correct first, and the rest of the program will write itself." - David Jones."— Presentation transcript:

1 Data Structures and Abstract Data Types "Get your data structures correct first, and the rest of the program will write itself." - David Jones

2 Abstract Data Types  Abstract Data Types (aka ADTs) are descriptions of how a data type will work without implementation details  Description can be a formal, mathematical description  Java interfaces are a form of ADTs –some implementation details start to creep in CS 221 - Computer Science II

3 Data Structures  A Data Structure is: –an implementation of an abstract data type and –"An organization of information, usually in computer memory", for better algorithm efficiency." aList List Object size myElements 5 A C E B A 0 1 2 3 4 5 6 7 8 9 10

4 CS 221 - Computer Science II Data Structure Concepts  Data Structures are containers: –they hold other data –arrays are a data structure –... so are lists  Other types of data structures: –stack, queue, tree, binary search tree, hash table, dictionary or map, set, and on and on –en.wikipedia.org/wiki/List_of_data_structuresen.wikipedia.org/wiki/List_of_data_structures  Different types of data structures are optimized for certain types of operations

5 CS 221 - Computer Science II Core Operations  Data Structures have three core operations –a way to add things –a way to remove things –a way to access things  Details of these operations depend on the data structure –Example: List, add at the end, access by location, remove by location  More operations added depending on what data structure is designed to do

6 Implementing ADTs  When implementing an ADT, the operations and behaviors are already specified –think Java interface  But what use as the internal storage container for the concrete data type? –the internal storage container is used to hold the items in the collection –initially slim pickings for choice of storage containers: arrays anyone? –later add linked structures –now often an implementation of an ADT (which use arrays or linked structures) CS 221 - Computer Science II

7 Bags and Sets  Simplest ADT is a Bag –items can be added, removed, accessed –no implied order to the items –duplicates allowed  Set –same as a bag, except duplicate elements not allowed –union, intersection, difference, subset CS 221 - Computer Science II

8 Lists  Items have a position in this Collection –Random access or not?  Array Lists –internal storage container is native array  Linked Lists public class Node {private Object data; private Node next; } first last CS 221 - Computer Science II

9 Stacks  Collection with access only to the last element inserted –Last in first out –push (insert) –pop (remove) –peek (top item) –make empty TopData4 Data3 Data2 Data1 CS 221 - Computer Science II

10 Queues  Collection with access only to the item that has been present the longest –first in,first out or last in, last out –enqueue (insert) –dequeue (remove) –front Data4Data3Data2Data1 Front Back CS 221 - Computer Science II

11 Stacks and Queues in the Java Collection API  No queue in the Java collections ADT  Stack extends Vector (which is almost exactly like ArrayList) –Hmmm?  One reason the Java Collections Library is often said to be broken  no Queue in Collection API CS 221 - Computer Science II

12 Trees  Similar to a linked list public class TreeNode {private Object data; private TreeNode left; private TreeNode right; } Root CS 221 - Computer Science II

13 Other Types of Trees  Binary Search Trees –sorted values  Heaps –sorted via a different algorithm  AVL and Red-Black Trees –binary search trees that stay balanced  Splay Trees  B Trees CS 221 - Computer Science II

14 HashTables  Take a key, apply function  f(key) = hash value  store data or object based on hash value  Sorting O(N), access O(1) if a perfect hash function and enough memory for table  How deal with collisions? CS 221 - Computer Science II

15 Other ADTs  Maps –a.k.a. Dictionary –Collection of items with a key and associated values –similar to hash tables, and hash tables often used to implement Maps  Graphs –Nodes with unlimited connections between other nodes  Sparse vectors and sparse matrices CS 221 - Computer Science II

16 Generic Containers  ADTs or Collection classes should be generic –only write them once, hold lots or all types of data –Java achieves genericity through inheritance and polymorphism  ADTs have an internal storage container –What is storing the stuff, –implementation vs. abstraction –in Java, usually holds Objects. Why? CS 221 - Computer Science II

17

18 ADTs and Data Structures in Programming Languages  Modern programming languages usually have a library of data structures –Java collections frameworkJava collections framework –C++ standard template libraryC++ standard template library –.Net framework (small portion of VERY large library).Net framework –Python lists and tuples –Lisp lists

19 CS 221 - Computer Science II Data Structures in Java  Part of the Java Standard Library is the Collections Framework –In class we will create our own data structures and discuss the data structures that exist in Java  A library of data structures  Built on two interfaces –Collection –Iterator  http://java.sun.com/j2se/1.5.0/docs/guide/coll ections/index.html http://java.sun.com/j2se/1.5.0/docs/guide/coll ections/index.html

20 CS 221 - Computer Science II The Java Collection Interface  A generic collection  Can hold any object data type  Which type a particular collection will hold is specified when declaring an instance of a class that implements the Collection interface  Helps guarantee type safety at compile time

21 CS 221 - Computer Science II Methods in the Collection interface public interface Collection {public boolean add(E o) public boolean addAll(Collection c) public void clear() public boolean contains(Object o) public boolean containsAll(Collection c) public boolean equals(Object o) public int hashCode() public boolean isEmpty() public Iterator iterator() public boolean remove(Object o) public boolean removeAll(Collection c) public boolean retainAll(Collection c) public int size() public Object[] toArray() public T[] toArray(T[] a) }

22 CS 221 - Computer Science II The Java ArrayList Class  Implements the List interface and uses an array as its internal storage container  It is a list, not an array  The array that actual stores the elements of the list is hidden, not visible outside of the ArrayList class  All actions on ArrayList objects are via the methods  ArrayLists are generic. –They can hold objects of any type!

23 CS 221 - Computer Science II ArrayList's (Partial) Class Diagram AbstractCollection Object AbstractList ArrayList Iterable Collection List

24 ArrayList Specification


Download ppt "Data Structures and Abstract Data Types "Get your data structures correct first, and the rest of the program will write itself." - David Jones."

Similar presentations


Ads by Google