Algorithms Part III. Data Structures

Slides:



Advertisements
Similar presentations
Comp 122, Spring 2004 Binary Search Trees. btrees - 2 Comp 122, Spring 2004 Binary Trees  Recursive definition 1.An empty tree is a binary tree 2.A node.
Advertisements

Algorithms, Lists and Pseudocode Dr. Andrew Wallace PhD BEng(hons) EurIng
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 6 Dynamic data structures Motivation Common dynamic ADTs Stacks, queues, lists:
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (4) Data Structures 11/18/2008 Yang Song.
10.Elementary data structures Hsu, Lih-Hsing. Computer Theory Lab. Chapter 11P Stacks and queues Stacks and queues are dynamic set in which element.
Data Structures Dynamic Sets Heaps Binary Trees & Sorting Hashing.
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
Data Structures from Cormen, Leiserson, Rivest & Stein.
Stacks and Queues Dr. Andrew Wallace PhD BEng(hons) EurIng
1 Stack Data : a collection of homogeneous elements arranged in a sequence. Only the first element may be accessed Main Operations: Push : insert an element.
Chapter 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
PRIORITY QUEUES (HEAPS). Queues are a standard mechanism for ordering tasks on a first-come, first-served basis However, some tasks may be more important.
October 18, Algorithms and Data Structures Lecture V Simonas Šaltenis Nykredit Center for Database Research Aalborg University
 2007 Pearson Education, Inc. All rights reserved C Data Structures.
ECE 103 Engineering Programming Chapter 61 Abstract Data Types Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103 material.
Elementary Data Structures Data Structures and Algorithms A. G. Malamos.
October 18, Algorithms and Data Structures Lecture V Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Information and Computer Sciences University of Hawaii, Manoa
2IL50 Data Structures Fall 2015 Lecture 7: Binary Search Trees.
Data Structures Chapter 6. Data Structure A data structure is a representation of data and the operations allowed on that data. Examples: 1.Array 2.Record.
Heaps and basic data structures David Kauchak cs161 Summer 2009.
Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.
Data structures What does that mean? In general there are two aspects: how data will be organized in computer memory what will be the operations that will.
Chapter 10 Elementary data structures Lee, Hsiu-Hui Ack: This presentation is based on the lecture slides from Hsu, Lih-Hsing, as well as various materials.
Data Structures David Kauchak cs302 Spring Data Structures What is a data structure? Way of storing data that facilitates particular operations.
Lecture 21 Data Structures, Algorithms and Complexity Stacks and Queues GRIFFITH COLLEGE DUBLIN.
CS6045: Advanced Algorithms Data Structures. Dynamic Sets Next few lectures will focus on data structures rather than straight algorithms In particular,
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
Binary Search Trees What is a binary search tree?
Review Array Array Elements Accessing array elements
Elementary data structures
Data Structures Using C, 2e
Queues.
Data Structure By Amee Trivedi.
Chapter 12 – Data Structures
12 C Data Structures.
12 C Data Structures.
Chapter 15 Lists Objectives
Heaps And Priority Queues
Binary Search Trees.
Red Black Trees
CPSC 311 Section 502 Analysis of Algorithm
Stacks and Queues.
Lecture 22 Binary Search Trees Chapter 10 of textbook
What does that mean? In general there are two aspects:
Hashing Exercises.
Chapter 8: Data Abstractions
CS200: Algorithms Analysis
CS 583 Analysis of Algorithms
Ch. 12: Binary Search Trees Ming-Te Chi
Elementary Data Structures
CMSC 341 Lecture 5 Stacks, Queues
Lesson Objectives Aims
ITEC 2620M Introduction to Data Structures
Ch. 12: Binary Search Trees Ming-Te Chi
Lesson 6. Types Equality and Identity. Collections.
Priority Queues (Chapter 6.6):
Binary Search Trees (13.1/12.1)
Algorithms and Data Structures Lecture VII
Dynamic Sets (III, Introduction)
CS6045: Advanced Algorithms
Mutable Data (define mylist (list 1 2 3)) (bind ((new (list 4)))
Topic 6: Binary Search Tree Data structure Operations
Binary and Binomial Heaps
Priority Queues (Chapter 6):
Stacks, Queues, and Deques
Binary Search Trees Comp 122, Spring 2004.
Chapter 12&13: Binary Search Trees (BSTs)
Premaster Course Algorithms 1 Chapter 3: Elementary Data Structures
Presentation transcript:

Algorithms Part III. Data Structures Ch. 10: Elementary Data Structures Ming-Te Chi Ch10 Elementary DS

Data Structures Introduction Ch 10. Elementary Data Structures Ch 11. Hash Tables (will not cover) Ch 12. Binary Search Trees Ch 13. Red-Black Trees (will cover briefly) Ch 14. Augmenting Data Structures Ch10 Elementary DS

Introduction Sets manipulated by algorithm can grow, shrink, or change over time. Called dynamic set. Types of operations to be performed on sets: Insert, delete, test-membership (search?), etc. Extract the smallest element, etc. Ch10 Elementary DS

Introduction (continue) Operations to be performed on dynamic sets: queries: return information about the set. Search, minimum, maximum Successor, predecessor. modifying operations: change the set. Insert, delete Data structures that can support any of these operations on a set of size n: O (log n) Ch10 Elementary DS

Overview of Chapters (10-14) Ch 10. Elementary Data Structures Simple data structures such as: Queues, stacks, linked lists, and rooted trees How objects and pointers can be implemented in programming environments that do not support them as primitives. Very elementary. Ch10 Elementary DS

Overview of Chapters (10-14) Ch 11. Hash Tables Introduce hash tables, which support the operations INSERT, DELETE, and SEARCH. Worst-case: O(n) for SEARCH operation. Expected time for hash-table operations: O(1). Analysis relies on probability, but most of the chapter requires no background on this subject. Ch10 Elementary DS

Overview of Chapters (10-14) Ch 12. Binary Search Trees Support all the dynamic-set operations. Worst-case: O(n). Expected time: O(log n). Serve as the basis for many other data structures. Ch10 Elementary DS

Overview of Chapters (10-14) Ch 13. Red-Black Trees A variant of binary search trees. Balanced search tree. Worst-case: O(log n). Ch10 Elementary DS

Overview of Chapters (10-14) Ch 14. Augmenting Data Structures How to augment red-back trees to support operations other than the basic ones. Ch10 Elementary DS

CH. 10. ELEMENTARY DATA STRUCTURES Ch10 Elementary DS

10.1 Stacks and Queues Stacks and Queues Stack Queue dynamic set elements removed from the set by the DELETE operation is pre-specified Stack LIFO policy: Last-In First-Out Delete the element most recently inserted Push (insert), pop (delete) Queue FIFO policy: First-In First-Out Enqueue (insert), dequeue (delete) Ch10 Elementary DS

An array implementation of a stack S Ch10 Elementary DS

empty, underflows, overflows STACK_EMPTY(S ) 1 if S.top == 0 2 return TRUE 3 else return FALSE Ch10 Elementary DS

2 then error “underflow” 3 else S.top = S.top -1 4 return S[S.top + 1] PUSH(S,x) 1 S.top = S.top + 1 2 S[S.top] = x POP(S ) 1 if STACK-EMPTY(S ) 2 then error “underflow” 3 else S.top = S.top -1 4 return S[S.top + 1] Ch10 Elementary DS

An array implementation of a queue Q Ch10 Elementary DS

ENQUEUE(Q, x) 1 Q[Q.tail] = x 2 if Q.tail == Q.length 3 Q.tail = 1 4 else Q.tail = Q.tail +1 Anything wrong? Ch10 Elementary DS

DEQUEUE(Q ) 1 x = Q[Q.head] 2 if Q.head == Q.length 3 Q.head = 1 4 else Q.head = Q.head +1 5 return x Ch10 Elementary DS

10.2 Linked lists Data structure in which the objects are arranged in a linear order. The order in a linked list is determined by a pointer in each object. The order in an array is determined by the array indices. Singly linked list, doubly linked list, circular list. Head and tail. Ch10 Elementary DS

10.2 Linked lists List-Insert(L, x), x.key=25 List-Delete(L, x), x.key=4 Ch10 Elementary DS

O(n) time in the worst case LIST_SEARCH(L,k) 1 x = L.head 2 while x ≠ NIL and x.key ≠ k 3 x = x.next 4 return x O(n) time in the worst case Ch10 Elementary DS

LIST_INSERT(L,x) 1 x.next = L.head 2 if L.head ≠ NIL 3 L.head.prev = x 4 L.head = x 5 x.prev = NIL O(1) Ch10 Elementary DS

LIST_DELETE(L,x) O(1) or O(n) 1 if x.prev ≠ NIL 2 next[prev[x]] = x.next 3 else L.head = x.next 4 if x.next ≠ NIL 5 x.next.prev = x.prev (Call LIST_SEARCH first O(n)) O(1) or O(n) Ch10 Elementary DS

A Sentinel is a dummy object that allows us to simplify boundary conditions, List-Insert’(L, x), x.key=25 List-Delete’(L, x), x.key=4 Ch10 Elementary DS

LIST_DELETE’(L,x) 1 x.prev.next = x.next 2 x.next.prev = x.prev Ch10 Elementary DS

2 while x ≠ L.nil and x.key ≠ k 3 x = x.next 4 return x LIST_SEARCH’(L,k) 1 x = L.nil.next 2 while x ≠ L.nil and x.key ≠ k 3 x = x.next 4 return x Ch10 Elementary DS

LIST_INSERT’(L,x) 1 x.next = L.nil.next 2 L.nil.next.prev = x 3 L.nil.next = x 4 x.prev = L.nil Ch10 Elementary DS

Remarks on sentinels Rarely reduce the asymptotic time bounds of data structure operations. Can only reduce constant factors. Can improve the clarity of code rather than speed. The extra storage used by the sentinels, for small lists, can represent significant wasted memory. Use sentinels only when they truly simplify the code. Ch10 Elementary DS

11.3 Implementing pointers and objects A multiple-array representation of objects Ch10 Elementary DS

A single array representation of objects Ch10 Elementary DS

Allocating and freeing objects: Garbage collector Determining which objects are unused. Free list Singly linked list that keeps the free objects Uses only the “next” pointer. Stack. Ch10 Elementary DS

Allocating and freeing objects--garbage collector Ch10 Elementary DS

Allocate_object( ),LIST_INSERT(L,4),Key(4)=25 Ch10 Elementary DS

LIST_DELETE(L,5), FREE_OBJECT(5) Ch10 Elementary DS

Ch10 Elementary DS

Two link lists Ch10 Elementary DS

10.4 Representing rooted trees Binary trees: parent, left(-child), right(-child) p[x] = NIL  x is the root x has no left child  left[x] = NIL x has no right child  right[x] = NIL Ch10 Elementary DS

Binary trees Ch10 Elementary DS

10.4 Representing rooted trees Rooted trees with unbounded branching: parent, left-child, right-sibling p[x] = NIL  x is the root left-child[x]: points to the leftmost child of x. right-sibling[x]: points to the sibling of x immediately to the right. x has no children  left-child[x] = NIL x is the rightmost child of its parent  right-sibling[x] = NIL Ch10 Elementary DS

Rooted tree with unbounded branching Ch10 Elementary DS

Other tree representations Heap Others Ch10 Elementary DS