Download DOCX

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Basis (linear algebra) wikipedia , lookup

Birkhoff's representation theorem wikipedia , lookup

Group (mathematics) wikipedia , lookup

Corecursion wikipedia , lookup

Transcript
Data Structures , Spring 2013
Practical Session #4 - ADTs: Array, Queue, Stack,
Linked List
Basic Data Structures and Abstract Data Types
ADT
Abstract Data Type
A collection of data-storing entities with operations to create, access,
change, etc.
Vector(Array)
A linear sequence of elements that supports access to its elements by
their indexes.
A vector ADT supports the following basic methods:
 elementAt(i) – returns the element at index i
 replaceAt(i, element) – replace and return the element that in
index i with new given element.
 insertAt(i, element) – insert new element at index i
 removeAt(i) – remove the element in index i
 size() – returns the size of the vector
 isEmpty() - returns true if the vector is empty
FIFO - First In First Out
ADT that supports the following operations:



Queue
Enqueue - insert new element at the tail of the queue
Dequeue - remove element from the head of the queue
isEmpty - returns true if the queue is empty
LIFO - Last In First Out
ADT that supports the following operations:



Stack
Push - add element to the head of the stack
Pop - remove element from the head of the stack
isEmpty - returns true if the stack is empty
Linked List
A data structure with linear access to the elements in it.
Each element has a pointer to the next element in the list
Doubly-Linked
List
A data structure with linear access to the elements in it.
Each element has 2 pointers, one to the next element in the list and
the other to the previous element

Important! – ADT’s can be implemented in a variety of possibilities. Below we
describe ADT’s and demonstrate their implementations.
Question 1
S is a set of at most n elements, where n is given.
Each element has a unique key in the range [0...n-1]. (i.e. no 2 different elements have
the same key)
Find a data structure of size O(n), that supports the following operations in the given
times:
Init(n)
Operation
Time
Initialize S (S is an empty set at the beginning)
O(n)
isElement(k) Check if S has an element whose key equals to k
O(1)
Insert(x)
Add element x to S
O(1)
Remove(k)
Remove element whose key equals to k
O(1)
isEmpty()
Check if S is empty
O(1)
hasAll
Check if S contains all the elements whose keys are
O(1)
in the range [0...n-1]
Question 2
Suggest 2 ways to implement a Queue using 2 Stacks.
Question 3:
L1 and L2 are two linked lists which have a common part of size k.
The length of L1, L2 until the 1st common node is n, m respectively.
Suggest a way to find the 1st common node in O(n+m+k) time.
(k,n,m are variables, not given)
Question 4:
L is a singly linked list. Starting from the first element, suggest a way to traverse forward
and backward from one element to its next or previous element at O(1). You can use only
O(1) extra memory.
Question 5:
A is a Boolean (1 or 0 values) matrix of size n*n.
Find a data structure that supports the following operations in the given time:
Operation
Time
init(n)
Initialize A with the value 1
O(n2)
flip(i,j)
A[i,j]=!A[i,j]
O(1)
hasRowOf1 Return true iff A has a row that contains only 1-s O(1)
hasRowOf0 Return true iff A has a row that contains only 0-s O(1)
Question 6:
Suggest a way to implement an array of integers so that assignment of some value
INIT_VAL to all cells of an array will take O(1) time.
Remember that reading A[i] or writing to A[i] takes O(1) time.
Question 7:
:‫נגדיר "מחסנית מינימום" כמבנה נתונים התומך בפעולות הבאות‬
.‫ – אתחול מבנה הנתונים‬Create() 
.‫ למבנה‬x ‫ – הכנסת איבר‬Insert(x) 
.‫ – הוצאת האיבר שהוכנס אחרון‬RemoveLast() 
.)‫ – החזרת האיבר הקטן ביותר במבנה (ללא הוצאתו‬Min() 
.k – ‫ – שינוי ערך האיבר הקטן ביותר במבנה ל‬ChangeMin(k) 
.‫ כל האיברים שונים זה מזה‬:‫הנחה‬
,O(1) ‫ כאשר סיבוכיות הזמן הנדרשת לארבע הפעולות הראשונות היא‬,"‫הציעו מימוש ל"מחסנית מינימום‬
‫ הינו מספר האיברים במבנה שהוכנסו אחרי האיבר המינימלי (הכוונה‬t – ‫ כש‬,O(t) ChangeMin ‫ולפעולה‬
.)‫ בוצעה‬ChangeMin ‫לאיבר המינימלי לפני שהפעולה‬