Data Abstraction and Basic Data Structures Abstract Data Type ADT
Total Page:16
File Type:pdf, Size:1020Kb
Data Abstraction and Basic Data Structures Abstract Data type • Improving efficiency by building better 4i is an instance of type T, i ∈ T 4 Data Structure 4e is an element of set S, e ∈ S 4o is an object of class C, o ∈ C • Object IN 4 Abstract Data Type • Abstract Data Type 4Structures: data structure declarations f Specification f Design 4Functions: operation definitions 4 Architecture [Structure, Function] • An ADT is identified as a Class • Abstract Data Types 4in languages such as C++ and Java 4 Lists, Trees • Designing algorithms and 4 Stacks, Queues TECH proving correctness of algorithms 4 Priority Queue, Union-Find 4based on ADT operations and specifications 4 Dictionary Computer Science ADT Specification Operations for ADT • The specification of an ADT describe how the operations • Constructors (functions, procedures, or methods) behave 4create a new object and return a reference to it 4 in terms of Inputs and Outputs • A specification of an operation consists of: • Access functions 4 Calling prototype 4return information about an object, but do not modify it 4 Preconditions • Manipulation procedures 4 Postconditions 4modify an object, but do not return information • The calling prototype includes • State of an object 4 name of the operation 4 parameters and their types 4current values of its data 4 return value and its types • Describing constructors and manipulation procedures • The preconditions are statements 4in terms of Access functions 4 assumed to be true when the operation is called. • Recursive ADT • The postconditions are statements 4if any of its access functions returns 4 assumed to be true when the operation returns. the same class as the ADT ADT Design e.g. Lists Binary Tree 4Every computable function can be computed using • A binary tree T is a set of elements, called nodes, Lists as the only data structure! that is empty or satisfies: • IntList cons(int newElement, IntList oldList) 41. There is a distinguished node r called the root 4Precondition: None. 42. The remaining nodes are divided into two disjoint 4Postconditions: If x = cons(newElement, oldList) then subsets, L and R, each of which is a binary tree. 1. x refers to a newly created object; 2. x != nil; L is called the left subtree of T and R is called the right 3. first(x) = newElement; subtree of T. 4. rest(x) = oldList • There are at most 2d nodes at depth d of a binary tree. • int first(IntList aList) // access function • A binary tree with n nodes has height at least 4Precondition: aList != nil Ceiling[lg(n+1)] – 1. • IntList rest(IntList aList) // access function • A binary tree with height h has at most 2h+1 –1 nodes 4Precondition: aList != nil • IntList nil //constant denoting the empty list. 1 Stacks Queue • A stack is a linear structure in which insertions and • A queue is a linear structure in which deletions are always make at one end, called the top. 4all insertions are done at one end, called the rear or • This updating policy is call last in, first out (LIFO) back, and 4all deletions are done at the other end, called the front. • This updating policy is called first in, first out (FIFO) Priority Queue Union-Find ADT for Disjoint Sets • A priority queue is a structure with some aspects of • Through a Union operation, two (disjoint) sets can be FIFO queue but combined. 4in which element order is related to each element’s 4(to insure the disjoint property of all existing sets, the priority, original two sets are removed and the new set is added) 4rather than its chronological arrival time. 4Let the set id of the original two set be, s and t, s != t • As each element is inserted into a priority queue, 4Then, new set has one unique set id that is either s or t. conceptually it is inserted in order of its priority • Through a Find operation, the current set id of an • The one element that can be inspected and removed is element can be retrieved. the most important element currently in the priority queue. • Often elements are integers and 4a cost viewpoint: the smallest priority 4the set id is some particular element in the set, 4a profit viewpoint: the largest priority called the leader, as in the next e.g. Union-Find ADT e.g. Dictionary ADT • UnionFind create(int n) • A dictionary is a general associative storage structure. 4// create a set (called sets) of n singleton disjoint sets • Items in a dictionary {{1},{2},{3},…,{n}} 4have an identifier, and • int find(UnionFind sets, e) 4associated information that needs to be stored and 4// return the set id for e retrieved. • void makeSet(unionFind sets, int e) 4no order implied for identifiers in a dictionary ADT 4//union one singleton set {e} (e not already in the sets) into the exiting sets • void union(UnionFind sets, int s, int t) 4// s and t are set ids, s != t 4// a new set is created by union of set [s] and set [t] 4// the new set id is either s or t, in some case min(s, t) 2.