\Mankinds's progress is measured by the number of. Elementary data structures such as stacks, queues,



Similar documents
Introduction to Data Structures and Algorithms

22c:31 Algorithms. Ch3: Data Structures. Hantao Zhang Computer Science Department

Questions 1 through 25 are worth 2 points each. Choose one best answer for each.

QUEUES. Primitive Queue operations. enqueue (q, x): inserts item x at the rear of the queue q

Data Structures and Algorithm Analysis (CSC317) Intro/Review of Data Structures Focus on dynamic sets

DATA STRUCTURES USING C

Linked Lists, Stacks, Queues, Deques. It s time for a chainge!

Queues and Stacks. Atul Prakash Downey: Chapter 15 and 16

Quiz 4 Solutions EECS 211: FUNDAMENTALS OF COMPUTER PROGRAMMING II. 1 Q u i z 4 S o l u t i o n s

Krishna Institute of Engineering & Technology, Ghaziabad Department of Computer Application MCA-213 : DATA STRUCTURES USING C

Linked Lists Linked Lists, Queues, and Stacks

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

Common Data Structures

5. A full binary tree with n leaves contains [A] n nodes. [B] log n 2 nodes. [C] 2n 1 nodes. [D] n 2 nodes.

Data Structures Using C++ 2E. Chapter 5 Linked Lists

MAX = 5 Current = 0 'This will declare an array with 5 elements. Inserting a Value onto the Stack (Push)

Analysis of a Search Algorithm

Data Structures and Algorithms Stacks and Queues

What is a Stack? Stacks and Queues. Stack Abstract Data Type. Java Interface for Stack ADT. Array-based Implementation

PES Institute of Technology-BSC QUESTION BANK

10CS35: Data Structures Using C

CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team

Linear ADTs. Restricted Lists. Stacks, Queues. ES 103: Data Structures and Algorithms 2012 Instructor Dr Atul Gupta

1. The memory address of the first element of an array is called A. floor address B. foundation addressc. first address D.

Algorithms and Data Structures

Abstract Data Type. EECS 281: Data Structures and Algorithms. The Foundation: Data Structures and Abstract Data Types

1/1 7/4 2/2 12/7 10/30 12/25

ECE 250 Data Structures and Algorithms MIDTERM EXAMINATION /5:15-6:45 REC-200, EVI-350, RCH-106, HH-139

Module 2 Stacks and Queues: Abstract Data Types

Data Structure [Question Bank]

Binary Search Trees. A Generic Tree. Binary Trees. Nodes in a binary search tree ( B-S-T) are of the form. P parent. Key. Satellite data L R

Data Structures and Data Manipulation

CSE373: Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks/Queues. Linda Shapiro Spring 2016

Java Software Structures

Sequential Data Structures

Sample Questions Csci 1112 A. Bellaachia

CSE 326, Data Structures. Sample Final Exam. Problem Max Points Score 1 14 (2x7) 2 18 (3x6) Total 92.

Data Structure and Algorithm I Midterm Examination 120 points Time: 9:10am-12:10pm (180 minutes), Friday, November 12, 2010

Universidad Carlos III de Madrid

Analysis of Algorithms I: Binary Search Trees

6 March Array Implementation of Binary Trees

Big O and Limits Abstract Data Types Data Structure Grand Tour.

AP Computer Science AB Syllabus 1

A binary heap is a complete binary tree, where each node has a higher priority than its children. This is called heap-order property

EE2204 DATA STRUCTURES AND ALGORITHM (Common to EEE, EIE & ICE)

Queues Outline and Required Reading: Queues ( 4.2 except 4.2.4) COSC 2011, Fall 2003, Section A Instructor: N. Vlajic

Data Structures. Level 6 C Module Descriptor

A binary search tree is a binary tree with a special property called the BST-property, which is given as follows:

Cpt S 223. School of EECS, WSU

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

Data Structures Fibonacci Heaps, Amortized Analysis

INTERNATIONAL JOURNAL OF PURE AND APPLIED RESEARCH IN ENGINEERING AND TECHNOLOGY

Binary Heaps. CSE 373 Data Structures

Binary Search Trees CMPSC 122

CompuScholar, Inc. Alignment to Utah's Computer Programming II Standards

Chapter 8: Bags and Sets

Algorithms Chapter 12 Binary Search Trees

ADTs,, Arrays, Linked Lists

STACKS,QUEUES, AND LINKED LISTS

CHAPTER 4 ESSENTIAL DATA STRUCTRURES

Unordered Linked Lists

Algorithms. Margaret M. Fleck. 18 October 2010

Chapter 3: Restricted Structures Page 1

St S a t ck a ck nd Qu Q eue 1

A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and:

Chapter 14 The Binary Search Tree

Algorithms and Abstract Data Types

External Sorting. Chapter 13. Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1

Lecture 2: Data Structures Steven Skiena. skiena

DATA STRUCTURE - QUEUE

CIS 631 Database Management Systems Sample Final Exam

1.00 Lecture 35. Data Structures: Introduction Stacks, Queues. Reading for next time: Big Java: Data Structures

Data Structures UNIT III. Model Question Answer

LINKED DATA STRUCTURES

Home Page. Data Structures. Title Page. Page 1 of 24. Go Back. Full Screen. Close. Quit

Data Structures and Algorithms

Algorithms and Data Structures Written Exam Proposed SOLUTION

Ordered Lists and Binary Trees

G. H. RAISONI COLLEGE OF ENGG NAGPUR-16 Session DEPARTMENT CSE Semester IV SUBJECT DSPD

Binary Heaps * * * * * * * / / \ / \ / \ / \ / \ * * * * * * * * * * * / / \ / \ / / \ / \ * * * * * * * * * *

Class Overview. CSE 326: Data Structures. Goals. Goals. Data Structures. Goals. Introduction

Converting a Number from Decimal to Binary

International Journal of Software and Web Sciences (IJSWS)

This lecture. Abstract data types Stacks Queues. ADTs, Stacks, Queues Goodrich, Tamassia

7.1 Our Current Model

Cpt S 223. School of EECS, WSU

Atmiya Infotech Pvt. Ltd. Data Structure. By Ajay Raiyani. Yogidham, Kalawad Road, Rajkot. Ph : ,

Data Structures. Jaehyun Park. CS 97SI Stanford University. June 29, 2015

Operations: search;; min;; max;; predecessor;; successor. Time O(h) with h height of the tree (more on later).

CmpSci 187: Programming with Data Structures Spring 2015

GUJARAT TECHNOLOGICAL UNIVERSITY, AHMEDABAD, GUJARAT. Course Curriculum. DATA STRUCTURES (Code: )

Binary Search Trees. Each child can be identied as either a left or right. parent. right. A binary tree can be implemented where each node

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING LESSON PLAN

Exam study sheet for CS2711. List of topics

Data Structures, Practice Homework 3, with Solutions (not to be handed in)

Heaps & Priority Queues in the C++ STL 2-3 Trees

Binary Heap Algorithms

Binary search algorithm

Introduction to Object-Oriented Programming

Transcription:

Elementary Data Structures \Mankinds's progress is measured by the number of things we can do without thinking." Elementary data structures such as stacks, queues, lists, and heaps will be the \of-the-shelf" components we build our algorithm from. There are two aspects to any data structure: The abstract operations which it supports. The implementatiton of these operations. The fact that we can describe the behavior of our data structures in terms of abstract operations explains why we can use them without thinking, while the fact that we have dierent implementation of the same abstract operations enables us to optimize performance.

Stacks and Queues Sometimes, the order in which we retrieve data is independant of its content, being only a function of when it arrived. A stack supports last-in, rst-out operations: push and pop. A queue supports rst-in, rst-out operations: enqueue and dequeue. A deque is a double ended queue and supports all four operations: push, pop, enqueue, dequeue. Lines in banks are based on queues, while food in my refrigerator is treated as a stack. Both can be used to traverse a tree, but the order is completely dierent. 1 1 2 3 5 2 4 5 6 7 Queue 7 6 4 3 Stack Which order is better for WWW crawler robots?

Stack Implementation Although this implementation uses an array, a linked list would eliminate the need to declare the array size in advance. STACK-EMPTY(S) if top[s] = 0 then return TRUE else return FALSE PUSH(S, x) top[s] top[s] +1 S[top[S] x POP(S) if STACK-EMPTY(S) then error \underow" else top[s] top[s] ; 1 return S[top[S] + 1] 4 top 3 2 1 All are O(1) time operations.

Queue Implementation A circular queue implementation requires pointers to the head and tail elements, and wraps around to reuse array elements. ENQUEUE(Q, x) Q[tail[Q]] x if tail[q] = length[q] then tail[q] 1 else tail[q] tail[q] + 1 X X X tail head DEQUEUE(Q) x=q[head[q]] if head[q] = length[q] then head[q] = 1 else head[q] = head[q] + 1 return x A list-based implementation would eliminate the possibility ofoverow. All are O(1) time operations.

Dynamic Set Operations Perhaps the most important class of data structures maintain a set of items, indexed by keys. There are a variety of implementations of these dictionary operations, each of which yield dierent time bounds for various operations. Search(S,k) { A query that, given a set S and a key value k, returns a pointer x to an element in S such that key[x] = k, or nilifno such element belongs to S. Insert(S,x) {A modifying operation that augments the set S with the element x. Delete(S,x) { Given a pointer x to an element in the set S, remove x from S. Observe we are given a pointer to an element x, not a key value. Min(S), Max(S) {Returns the element of the totally ordered set S which has the smallest (largest) key. Next(S,x), Previous(S,x) { Given an element x whose key is from a totally ordered set S, returns the next largest (smallest) element in S, or NIL if x is the maximum (minimum) element.

Pointer Based Implementation We canalsomaintain a directory in either a singly or doubly linked list. L A B C D E F L A B C D E F We gain extra exibility on predecessor queries at a cost of doubling the number of pointers by using doublylinked lists. Since the extra big-oh costs of doubly-linkly lists is zero, we will usually assume they are, althought it might not be necessary. Singly linked to doubly-linked list is as a Conga line is to a Can-Can line.

Array Based Sets Unsorted Arrays Search(S,k) - sequential search, O(n) Insert(S,x) - place in rst empty spot, O(1) Delete(S,x) - copy nth item to the xth spot, O(1) Min(S,x), Max(S,x) - sequential search, O(n) Successor(S,x), Predecessor(S,x) - sequential search, O(n) Sorted Arrays Search(S,k) - binary search, O(lgn) Insert(S,x) - search, then move to make space, O(n) Delete(S,x) - move to ll up the hole, O(n) Min(S,x), Max(S,x) - rst or last element, O(1) Successor(S,x), Predecessor(S,x) - Add or subtract 1 from pointer, O(1) What are the costs for a heap?

Unsorted List Implementation LIST-SEARCH(L, k) x = head[l] while x<>nil and key[x] <> k do x =next[x] return x Note: the while loop might require two lines in some programming languages. HEAD(L) X DELETION X INSERTION LIST-INSERT(L, x) next[x] = head[l] if head[l] <> NIL then prev[head[l]] = x head[l] = x prev[x] = NIL LIST-DELETE(L, x) if prev[x] <> NIL then next[prev[x]] = next[x] else head[l] = next[x] if next[x] <> NIL then prev[next[x]] = prev[x]

Sentinels Boundary conditions can be eliminated using a sentinel element which doesn't go away. NIL LIST-SEARCH'(L, k) x = next[nil[l]] while x<>nil[l] and key[x] <> k do x =next[x] return x LIST-INSERT'(L, x) next[x] = next[nil[l]] prev[next[nil[l]]] = x next[nil[l]] = x prev[x] = NIL[L] LIST-DELETE'(L, x) next[prev[x]] <> next[x] next[prev[x]] = prev[x]