Abstract Data Types - School of Electrical Engineering...

45
1 Abstract Data Types CptS 223 – Advanced Data Structures Larry Holder School of Electrical Engineering and Computer Science Washington State University

Transcript of Abstract Data Types - School of Electrical Engineering...

Page 1: Abstract Data Types - School of Electrical Engineering ...holder/courses/CptS223/spr09/slides/adt.pdf · Abstract Data Types. CptS 223 ... to node just before X Doubly-linked list

1111

Abstract Data Types

CptS 223 – Advanced Data Structures

Larry HolderSchool of Electrical Engineering and Computer Science

Washington State University

Page 2: Abstract Data Types - School of Electrical Engineering ...holder/courses/CptS223/spr09/slides/adt.pdf · Abstract Data Types. CptS 223 ... to node just before X Doubly-linked list

222

Purpose

Abstract Data Types (ADTs)ListsStacksQueues

Page 3: Abstract Data Types - School of Electrical Engineering ...holder/courses/CptS223/spr09/slides/adt.pdf · Abstract Data Types. CptS 223 ... to node just before X Doubly-linked list

33

Abstract Data Types (ADTs)ADT is a set of objects together with a set of operations.Abstract in that implementation of operations not specified in ADT definitionE.g., List

Insert, delete, search, sortC++ class perfect for ADTsCan change ADT implementation details without breaking code using ADT

Page 4: Abstract Data Types - School of Electrical Engineering ...holder/courses/CptS223/spr09/slides/adt.pdf · Abstract Data Types. CptS 223 ... to node just before X Doubly-linked list

44

The List ADT

List of size N: A0, A1, …, AN-1

Each element Ak has a unique position k in the listElements can be arbitrarily complexOperations

insert(X,k), remove(k), find(X), findKth(k), printList()

Page 5: Abstract Data Types - School of Electrical Engineering ...holder/courses/CptS223/spr09/slides/adt.pdf · Abstract Data Types. CptS 223 ... to node just before X Doubly-linked list

55

Lists Using Arrays

Simple array vs. Vector classEstimating maximum size

Operationsinsert(X,k) – O(N)remove(k) – O(N)find(X) – O(N)findKth(k) – O(1)printList() – O(N)

Page 6: Abstract Data Types - School of Electrical Engineering ...holder/courses/CptS223/spr09/slides/adt.pdf · Abstract Data Types. CptS 223 ... to node just before X Doubly-linked list

66

Linked Lists

Elements not stored in contiguous memoryNodes in list consist of data element and next pointer

node

data next nullpointer

Page 7: Abstract Data Types - School of Electrical Engineering ...holder/courses/CptS223/spr09/slides/adt.pdf · Abstract Data Types. CptS 223 ... to node just before X Doubly-linked list

77

Linked Lists

OperationsInsert(X,A) – O(1)

Remove(A) – O(1)

Page 8: Abstract Data Types - School of Electrical Engineering ...holder/courses/CptS223/spr09/slides/adt.pdf · Abstract Data Types. CptS 223 ... to node just before X Doubly-linked list

88

Linked Lists

Operationsfind(X) – O(N)findKth(k) – O(N)printList() – O(N)

Page 9: Abstract Data Types - School of Electrical Engineering ...holder/courses/CptS223/spr09/slides/adt.pdf · Abstract Data Types. CptS 223 ... to node just before X Doubly-linked list

99

Doubly-Linked List

Singly-linked listinsert(X,A) and remove(X) require pointer to node just before X

Doubly-linked listAlso keep pointer to previous node

prev next

Page 10: Abstract Data Types - School of Electrical Engineering ...holder/courses/CptS223/spr09/slides/adt.pdf · Abstract Data Types. CptS 223 ... to node just before X Doubly-linked list

1010

Doubly-Linked List

Insert(X,A)

Remove(X)

Problems with operations at ends of list

newA = new Node(A);newA->prev = X->prev;newA->next = X;X->prev->next = newA;X->prev = newA;

X->prev->next = X->next;X->next->prev = X->prev;

Page 11: Abstract Data Types - School of Electrical Engineering ...holder/courses/CptS223/spr09/slides/adt.pdf · Abstract Data Types. CptS 223 ... to node just before X Doubly-linked list

1111

Sentinel NodesDummy head and tail nodes to avoid special cases at ends of listDoubly-linked list with sentinel nodes

Empty doubly-linked list with sentinel nodes

Page 12: Abstract Data Types - School of Electrical Engineering ...holder/courses/CptS223/spr09/slides/adt.pdf · Abstract Data Types. CptS 223 ... to node just before X Doubly-linked list

12

C++ Standard Template Library (STL)

Implementation of common data structures

List, stack, queue, …Generally called containers

WWW resourceswww.sgi.com/tech/stl/www.cppreference.com/cppstl.html

12

Page 13: Abstract Data Types - School of Electrical Engineering ...holder/courses/CptS223/spr09/slides/adt.pdf · Abstract Data Types. CptS 223 ... to node just before X Doubly-linked list

13

Lists using STLvector<Object>

Array-based implementationfindKth – O(1)insert and remove – O(N)

Unless change at end of vector

list<Object>Doubly-linked list with sentinel nodesfindKth – O(N)insert and remove – O(1)

If position of change is known

Both require O(N) for search

13

Page 14: Abstract Data Types - School of Electrical Engineering ...holder/courses/CptS223/spr09/slides/adt.pdf · Abstract Data Types. CptS 223 ... to node just before X Doubly-linked list

14

Container Methods

int size() constReturn number of elements in container

void clear()Remove all elements from container

bool empty() constReturn true if container has no elements, otherwise return false

14

Page 15: Abstract Data Types - School of Electrical Engineering ...holder/courses/CptS223/spr09/slides/adt.pdf · Abstract Data Types. CptS 223 ... to node just before X Doubly-linked list

15

Vector and List Methodsvoid push_back (const Object & x)

Add x to end of listvoid pop_back ()

Remove object at end of listconst Object & back () const

Return object at end of listconst Object & front () const

Return object at front of list

15

Page 16: Abstract Data Types - School of Electrical Engineering ...holder/courses/CptS223/spr09/slides/adt.pdf · Abstract Data Types. CptS 223 ... to node just before X Doubly-linked list

16

List-only Methodsvoid push_front (const Object & x)

Add x to front of listvoid pop_front ()

Remove object at front of list

16

Page 17: Abstract Data Types - School of Electrical Engineering ...holder/courses/CptS223/spr09/slides/adt.pdf · Abstract Data Types. CptS 223 ... to node just before X Doubly-linked list

17

Vector-only MethodsObject & operator[] (int idx)

Return object at index idx in vector with no bounds-checking

Object & at (int idx)Return object at index idx in vector with bounds-checking

int capacity () constReturn internal capacity of vector

void reserve (int newCapacity)Set new capacity for vector (avoid expansion)

17

Page 18: Abstract Data Types - School of Electrical Engineering ...holder/courses/CptS223/spr09/slides/adt.pdf · Abstract Data Types. CptS 223 ... to node just before X Doubly-linked list

18

Iterators

Represents position in containerGetting an iteratoriterator begin ()

Return appropriate iterator representing first item in container

iterator end ()Return appropriate iterator representing end marker in containerPosition after last item in container

18

Page 19: Abstract Data Types - School of Electrical Engineering ...holder/courses/CptS223/spr09/slides/adt.pdf · Abstract Data Types. CptS 223 ... to node just before X Doubly-linked list

19

Iterator Methodsitr++ and ++itr

Advance iterator itr to next location

*itrReturn reference to object stored at iterator itr’s location

itr1 == itr2Return true if itr1 and itr2 refer to same location; otherwise return false

itr1 != itr2Return true if itr1 and itr2 refer to different locations; otherwise return false

19

Page 20: Abstract Data Types - School of Electrical Engineering ...holder/courses/CptS223/spr09/slides/adt.pdf · Abstract Data Types. CptS 223 ... to node just before X Doubly-linked list

20

Example: printList

20

template <typename Container>void printList (const Container & lst){

for (typename Container::const_iterator itr = lst.begin();itr != lst.end();++itr)

{cout << *itr << endl;

}}

Page 21: Abstract Data Types - School of Electrical Engineering ...holder/courses/CptS223/spr09/slides/adt.pdf · Abstract Data Types. CptS 223 ... to node just before X Doubly-linked list

21

Constant Iteratorsiterator begin ()const_iterator begin () constiterator end ()const_iterator end () const

Appropriate version above returned based on whether container is constIf const_iterator used, then *itr cannot appear on left-hand side of assignment (e.g., *itr=0 )

21

Page 22: Abstract Data Types - School of Electrical Engineering ...holder/courses/CptS223/spr09/slides/adt.pdf · Abstract Data Types. CptS 223 ... to node just before X Doubly-linked list

22

Better printList

22

Page 23: Abstract Data Types - School of Electrical Engineering ...holder/courses/CptS223/spr09/slides/adt.pdf · Abstract Data Types. CptS 223 ... to node just before X Doubly-linked list

23

Container Operations Requiring Iterators

iterator insert (iterator pos, const Object & x)Add x into list, prior to position given by iterator posReturn iterator representing position of inserted itemO(1) for lists, O(N) for vectors

iterator erase (iterator pos)Remove object whose position is given by iterator posReturn iterator representing position of item following posThis operation invalidates posO(1) for lists, O(N) for vectors

iterator erase (iterator start, iterator end)Remove all items beginning at position start, up to, but not including end

23

Page 24: Abstract Data Types - School of Electrical Engineering ...holder/courses/CptS223/spr09/slides/adt.pdf · Abstract Data Types. CptS 223 ... to node just before X Doubly-linked list

24

Implementation of Vector

24

copy constructor

destructor

operator=

Page 25: Abstract Data Types - School of Electrical Engineering ...holder/courses/CptS223/spr09/slides/adt.pdf · Abstract Data Types. CptS 223 ... to node just before X Doubly-linked list

25

Implementation of Vector

25

Automaticresize

Page 26: Abstract Data Types - School of Electrical Engineering ...holder/courses/CptS223/spr09/slides/adt.pdf · Abstract Data Types. CptS 223 ... to node just before X Doubly-linked list

26

Implementation of Vector

26

Iterators

Page 27: Abstract Data Types - School of Electrical Engineering ...holder/courses/CptS223/spr09/slides/adt.pdf · Abstract Data Types. CptS 223 ... to node just before X Doubly-linked list

27

Implementation of List

27

Nested class

Page 28: Abstract Data Types - School of Electrical Engineering ...holder/courses/CptS223/spr09/slides/adt.pdf · Abstract Data Types. CptS 223 ... to node just before X Doubly-linked list

28

Implementation of List

28

Page 29: Abstract Data Types - School of Electrical Engineering ...holder/courses/CptS223/spr09/slides/adt.pdf · Abstract Data Types. CptS 223 ... to node just before X Doubly-linked list

29

Implementation of List

29

Empty list

Page 30: Abstract Data Types - School of Electrical Engineering ...holder/courses/CptS223/spr09/slides/adt.pdf · Abstract Data Types. CptS 223 ... to node just before X Doubly-linked list

30

Implementation of List

30

Allows inheriting classes to access these.

Gives List class access to constructor.

Page 31: Abstract Data Types - School of Electrical Engineering ...holder/courses/CptS223/spr09/slides/adt.pdf · Abstract Data Types. CptS 223 ... to node just before X Doubly-linked list

31

Implementation of List

31

Page 32: Abstract Data Types - School of Electrical Engineering ...holder/courses/CptS223/spr09/slides/adt.pdf · Abstract Data Types. CptS 223 ... to node just before X Doubly-linked list

32

Implementation of List

32

Page 33: Abstract Data Types - School of Electrical Engineering ...holder/courses/CptS223/spr09/slides/adt.pdf · Abstract Data Types. CptS 223 ... to node just before X Doubly-linked list

33

Implementation of List

33

Page 34: Abstract Data Types - School of Electrical Engineering ...holder/courses/CptS223/spr09/slides/adt.pdf · Abstract Data Types. CptS 223 ... to node just before X Doubly-linked list

34

Implementation of List

34

Page 35: Abstract Data Types - School of Electrical Engineering ...holder/courses/CptS223/spr09/slides/adt.pdf · Abstract Data Types. CptS 223 ... to node just before X Doubly-linked list

35

Stack ADTStack is a list where insert and remove take place only at the “top”Operations

Push (insert) element on top of stackPop (remove) element from top of stackTop: return element at top of stack

LIFO (Last In First Out)

Page 36: Abstract Data Types - School of Electrical Engineering ...holder/courses/CptS223/spr09/slides/adt.pdf · Abstract Data Types. CptS 223 ... to node just before X Doubly-linked list

36

Stack Implementation

template <typename Object>class stack{public:stack () {}void push (Object & x){ s.push_front (x); }

void pop (){ s.pop_front (); }

Object & top (){ s.front (); }

private:list<Object> s;

}

Linked List Vector

template <typename Object>class stack{public:stack () {}void push (Object & x){ s.push_back (x); }

void pop (){ s.pop_back (); }

Object & top (){ s.back (); }

private:vector<Object> s;

}

Running Times ?

Page 37: Abstract Data Types - School of Electrical Engineering ...holder/courses/CptS223/spr09/slides/adt.pdf · Abstract Data Types. CptS 223 ... to node just before X Doubly-linked list

37

C++ STL Stack Class

MethodsPush, pop, topEmpty, size

#include <stack>

stack<int> s;

for (int i = 0; i < 5; i++ ){

s.push(i);}while (!s.empty()){

cout << s.top() << endl;s.pop();

}

Page 38: Abstract Data Types - School of Electrical Engineering ...holder/courses/CptS223/spr09/slides/adt.pdf · Abstract Data Types. CptS 223 ... to node just before X Doubly-linked list

38

Stack Applications

Balancing symbols: ((()())(()))stack<char> s;while not end of file{read character cif c = ‘(’then s.push(c)if c = ‘)’then if s.empty()

then errorelse s.pop()

}if (! s.empty())then errorelse okay

Page 39: Abstract Data Types - School of Electrical Engineering ...holder/courses/CptS223/spr09/slides/adt.pdf · Abstract Data Types. CptS 223 ... to node just before X Doubly-linked list

39

Stack ApplicationsPostfix expressions

((1 * 2) + 3) + (4 * 5) = 1 2 * 3 + 4 5 * +HP calculators

Infix to postfix conversionOutput operandsPush operators and left parenthesesWhen right parenthesis, output operators up to left parenthesis

Class PostFixCalculator{public:...void Multiply (){int i1 = s.top();s.pop();int i2 = s.top();s.pop();s.push (i1 * i2);

}private:stack<int> s;

}

Page 40: Abstract Data Types - School of Electrical Engineering ...holder/courses/CptS223/spr09/slides/adt.pdf · Abstract Data Types. CptS 223 ... to node just before X Doubly-linked list

40

Stack Applications

Function callsProgramming languages use stacks to keep track of function callsWhen a function call occurs

Push CPU registers and program counter on to stack (“activation record” or “stack frame”)Upon return, restore registers and program counter from top stack frame and pop

Page 41: Abstract Data Types - School of Electrical Engineering ...holder/courses/CptS223/spr09/slides/adt.pdf · Abstract Data Types. CptS 223 ... to node just before X Doubly-linked list

41

Queue ADT

Queue is a list where insert takes place at the back, but remove takes place at the frontOperations

Enqueue (insert) element at the back of the queueDequeue (remove and return) element from the front of the queueFIFO (First In First Out)

front back

5 7 2 6 3 2 8Enque hereDequeue here

Page 42: Abstract Data Types - School of Electrical Engineering ...holder/courses/CptS223/spr09/slides/adt.pdf · Abstract Data Types. CptS 223 ... to node just before X Doubly-linked list

42

Queue Implementation

template <typename Object>class queue{public:queue () {}void enqueue (Object & x){ q.push_back (x); }

Object & dequeue (){Object & x = q.front ();q.pop_front ();return x;

}private:list<Object> q;

}

Linked List Vector (?)

Running time ?

Page 43: Abstract Data Types - School of Electrical Engineering ...holder/courses/CptS223/spr09/slides/adt.pdf · Abstract Data Types. CptS 223 ... to node just before X Doubly-linked list

43

C++ STL Queue Class

MethodsPush (at back)Pop (from front)Back, frontEmpty, size

#include <queue>

queue<int> q;

for (int i = 0; i < 5; i++ ){

q.push(i);}while (!q.empty()){

cout << q.front() << endl;q.pop();

}

Page 44: Abstract Data Types - School of Electrical Engineering ...holder/courses/CptS223/spr09/slides/adt.pdf · Abstract Data Types. CptS 223 ... to node just before X Doubly-linked list

44

Queue Applications

Job schedulingPriority queuesGraph traversalsQueuing theory

44

Page 45: Abstract Data Types - School of Electrical Engineering ...holder/courses/CptS223/spr09/slides/adt.pdf · Abstract Data Types. CptS 223 ... to node just before X Doubly-linked list

45

Summary

Abstract Data Types (ADTs)Linked listStackQueue

C++ Standard Template Library (STL)Numerous applicationsBuilding blocks for more complex data structures