Data Structures Amihood Amir. מדעיהמחשב מדעי ביולוגיה פיסיקה כימיה מתמטיקה מדעי הרוח מדעי החברה מדעי המדינה מדעי ההתנהגות.

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

Data Structure HKOI training /4/2010 So Pak Yeung.
Chapter 6 Structures By C. Shing ITEC Dept Radford University.
CS252: Systems Programming Ninghui Li Program Interview Questions.
File Processing - Organizing file for Performance MVNC1 Organizing Files for Performance Chapter 6 Jim Skon.
Stacks  a data structure which stores data in a Last-in First-out manner (LIFO)  has a pointer called TOP  can be implemented by either Array or Linked.
ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”
Elementary Data Structures CS 110: Data Structures and Algorithms First Semester,
Today’s Agenda  Stacks  Queues  Priority Queues CS2336: Computer Science II.
 Abstract Data Type Abstract Data Type  What is the difference? What is the difference?  Stacks Stacks  Stack operations Stack operations  Parsing.
1 Chapter 24 Lists Stacks and Queues. 2 Objectives F To design list with interface and abstract class (§24.2). F To design and implement a dynamic list.
CS102 – Data Structures Lists, Stacks, Queues, Trees, Hash Collections Framework David Davenport Spring 2002.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (4) Data Structures 11/18/2008 Yang Song.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
Organizing files for performance Chapter Data compression Advantages of reduced file size Redundancy reduction: state code example Repeating sequences:
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.
Lists CSE1303 Part A Data Structures and Algorithms.
EXPANDING STACKS AND QUEUES CS16: Introduction to Data Structures & Algorithms 1 Tuesday, February 10, 2015.
ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”
Chapter 7: Runtime Environment –Run time memory organization. We need to use memory to store: –code –static data (global variables) –dynamic data objects.
Comp 249 Programming Methodology Chapter 15 Linked Data Structure - Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Chapter 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Data Structures Winter What is a Data Structure? A data structure is a method of organizing data. The study of data structures is particularly important.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights.
1 CSE 1342 Programming Concepts Lists. 2 Basic Terminology A list is a finite sequence of zero or more elements. –For example, (1,3,5,7) is a list of.
Final Exam Review CS Total Points – 60 Points Writing Programs – 50 Points Tracing Algorithms, determining results, and drawing pictures – 50.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Cousin of the Stack.  An abstract data type (container class) in which items are entered at one end and removed from the other end  First In First.
Standard Template Library The Standard Template Library was recently added to standard C++. –The STL contains generic template classes. –The STL permits.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
 200 Total Points ◦ 75 Points Writing Programs ◦ 60 Points Tracing Algorithms and determining results ◦ 35 Points Short Answer ◦ 30 Points Multiple Choice.
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.
Data Structures for Midterm 2. C++ Data Structure Runtimes.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
CISC220 Fall 2009 James Atlas Dec 07: Final Exam Review.
Linear Data Structures
Data Structures David Kauchak cs302 Spring Data Structures What is a data structure? Way of storing data that facilitates particular operations.
Final Exam Review CS Total Points – 20 Points Writing Programs – 65 Points Tracing Algorithms, determining results, and drawing pictures – 50.
Lecture 10 b Stacks b Queues. 2 Stacks b A stack ADT is linear b Items are added and removed from only one end of a stack b It is therefore LIFO: Last-In,
Stacks Chapter 3 Objectives Upon completion you will be able to
BITS Pilani Pilani Campus Data Structure and Algorithms Design Dr. Maheswari Karthikeyan Lecture3.
Data Structures and Algorithm Analysis Dr. Ken Cosh Linked Lists.
List Structures What is a list? A homogeneous collection of elements with a linear relationship between the elements linear relationship - each element.
Mohammed I DAABO COURSE CODE: CSC 355 COURSE TITLE: Data Structures.
Review Array Array Elements Accessing array elements
CSC317 Selection problem q p r Randomized‐Select(A,p,r,i)
Cpt S 122 – Data Structures Abstract Data Types
Queues.
G.PULLAIAH COLLEGE OF ENGINEERING AND TECHNOLOGY
Data Structure By Amee Trivedi.
Chapter 12 – Data Structures
Top 50 Data Structures Interview Questions
September 29 – Stacks and queues
Program based on queue & their operations for an application
Data Structure Interview Question and Answers
Chapter 15 Lists Objectives
CS 1114: Implementing Search
COP3530- Data Structures Advanced Lists
Data Structures Interview / VIVA Questions and Answers
Week 15 – Monday CS221.
CMSC 341 Lecture 5 Stacks, Queues
Stacks, Queues, and Deques
Lesson Objectives Aims
אחסון (אירגון) מידע DATA DATA DATA Link Link Link … …
Mutable Data (define mylist (list 1 2 3)) (bind ((new (list 4)))
EE 312 Final Exam Review.
CSCS-200 Data Structure and Algorithms
CS 2308 Final Exam Review.
Presentation transcript:

Data Structures Amihood Amir

מדעיהמחשב מדעי ביולוגיה פיסיקה כימיה מתמטיקה מדעי הרוח מדעי החברה מדעי המדינה מדעי ההתנהגות

מדעיהמחשב INPUT OUTPUT

מדעיהמחשב INPUT OUTPUT

מדעימכונת כביסה INPUT OUTPUT

מדעיהטרירמה

מדעיהמחשב רכילותאפליקציותרכילותאפליקציותתאוריה מתודולוגיה שפות תכנות מערכות הפעלה בינה מלאכותית עיבוד נתונים מסדי נתונים הנדסת תכנה... מתודולוגיה מה ניתן לחישוב? חישוביות מה ניתן לחישוב? מה ניתן לחישוב יעיל? ואיך? מורכבות אלגוריתמים מבני נתונים

What Does Data Structures Mean? EXAMPLE: Input: Text T=T[1], …, T[n] of words. Query: Find occurrences of word P. Time: O(n|P|).

Can we do better? Concordance (index) : Construct a table C of pairs: Sort the table lexicographically by word.

EXAMPLE: Let T= boa, aba, xavier, abracadabra, wonderful C = Do binary search on C : Time: O((log n)|P|).

What Happened? We constructed an external structure (in the example, a table) That enabled answering our question faster. In this course we will see some basic such structures and the type of problems they enable to solve more efficiently.

Another Example Let’s play a game: Players: Two people. Input: A pile of gogoim. Moves: Take gogoim out of the pile. First player takes out 1or 2 gogoim. Then alternately, can not exceed the number already taken out. Winner: The player who takes out the last gogoim.

Example pile Total taken I WIN !!!!

What is the strategy? Note that if there are n elements in the pile, and player A manages to bring the gogoim taken out of the pile to the number Then player B still can not win, but no matter what B does, he will bring the number of gogoim to more than half so A will then be able to win in the next move.

Now recurse… If player A bring the outside gogoim to number then he wins. So recurse with the same strategy. Make sure that in the previous move the ouside gogoim were

The LIFO The data structure we need is the LIFO – Last In First Out – Or stack (מחסנית).

Using the LIFO Let L be a stack, V a variable. Basic Operations: Push(L,V) -- pushes V into stack L. Pop(L,V) -- pops the top value out of stack L and puts it in variable V

A Winning Algorithm for our Game Our data structures: V = the current number of gogoim outside. L = the stack of “winning” number of gogoim outside. Initialization: V L empty.

A Winning Algorithm (2) Find winning sizes: While V > 2 do: PUSH(L,V) V End While You start. If V=2 take 2 gogoim. If V=1 take 1.

A Winning Algorithm (3) The Game: Variables: S = number of gogoim taken out so far. Initialize: S 0. The Game moves: While pile not empty do: POP(L,V) If this is first pop then take out V gogoim. S V else take out V-S gogoim. S V. Opponent makes his move. End While

A Sample Run n = /2 - 1 = /2 - 1 = /2 - 1 = 54 54/2 - 1 = 26 26/2 - 1 = 12 12/2 - 1 = 5 5/2 - 1 = 2

Stack Implementation Option 1: Array S[1],…,S[n]. First element in S[1]. Pointer top points to last element. Advantages: Simple. Disadvantages: Need to keep a long array for stack. When it fills, need to copy everything to a longer array.

Stack Implementation (2) Option 2: Linked list of records. A record has a number of data fields and a pointer to the next record.

Stack Implementation (3) In our case: Stack = May be implemented in memory as: Top

Stack Implementation (4) Advantages: Efficient for dynamic allocation of records. Disadvantages: More complicated to maintain.

Linked List Top

Linked List Operations Insertion: (if we know where to insert) 3 7 5

Linked List Operations Deletion: Take care: properly maintain the free space

Doubly Linked Lists Insertion and Deletion Time: constant.

Can we do Binary Search on Linked List? For example to have a dynamic concordance. No  No constant time random access on linked lists Searching a linked list of length n : O(n) time.

Other Data Structures for which Linked Lists are Suitable FIFO – First In First Out – Queue

FIFO Implementation Head End

FIFO Operations Front Rear 1) ENQUEUE (x,Q) : INSERT (x,END (Q),Q) 2) DEQUEUE (x,Q) : x (FIRST (Q)) Remove(FIRST(Q),Q)

Double Ended Queue Can get in or out at head or end of queue.

Linked Lists used for Compression Sparse Arrays: Similar scheme for multidimensions Top