DS6 ABSTRACT DATA TYPES Flashcards

1
Q

INTRO

A

differentiate mechanism from use with layers of abstraction
bit -> assembly -> high level programming language -> algorithm design
abstract data types are accessed through interfaces
interfaces describe the type and methods we can use but dont reveal the internal mechanism

with abstraction we can have multiple clients
we can optimize the implementation to a specific system using modular code

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

EXAMPLE: POINT CLASS

A

user program has interface:
public interface Point{
Point();
Point(double, double);
double x();
double y();
double r();
double theta();
double distance(Point);
public String toString();}

the class Point can be implemented with both cartesian and polar coords and the client will see no difference

code can be optimized without affecting the program

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

QUEUES

A

general basic ds structures
collection of items
insert remove isempty
on the bases of queues we have priority queues fifo and lifo queues etc
its is a little abstract

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

PUSHDOWN STACK

A

is a LIFO queue (last in first out)
insert = push in from top
remove = pop from top

lifo queue is best for:
-algorithms that need to be able to return to previous steps (undo and back functions)
-backtracking algorithms that form a partial solution check it stepwise and undo if it doesnt work
this can be an optimized alternative to brute force case checking

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

FIFO QUEUE

A

first in first out
insert = put on top (or enqueue)
remove = get from bottom (or deque)

! note there can be a hybrid where we can push and pop and put and get

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

INFIX TO POSTFIX USING STACK

A

infix is the way we write mathematical expressions(3+57-6)
postfix is when the operators are at the end of the expression(357
+6-)
input: infix expression
output: postfix expression
use a stack
when operand print
when operator push to stack
when left parenthesis do nothing
when right parenthesis pop operator and print

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

CALCULATE EXPRESSION USING STACK

A

in postfix
use stack for operands
when operand push in stack
when operator pop twice calculate and push result

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

STACK WITH ARRAY

A

class intStack{
~private int[] s;
~private int N; counter for items in stack
~intStack(int maxN){ s = new int[maxN]; N = 0;}
~boolean isEmpty(){return (N == 0);)}
~void push(int item){s[N++] = item;}
~int pop(){return s[–N];}
}
ideally automatic size change

when N == maxN -> maxN *= 2
when N<= maxN/4 -> maxN/=2
since it is array (static) we need to copy to different array to change the size

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

STACK WITH LIST

A

to solve size change problem
reference handling load
(in memory stack with array should be better since memory is limited anyways, if data segment is predetermined we can know the max size our systems allows)

class intStack{
~private Node head;
~private class Node{
~~int item; Node next;
~~Node(int item, Node next){this.item = item; this.next = next;}}
~intStack(int maxN){head = null;}
~boolean isEmpty(){return (head == null);}
~void push(int item){head = new Node(item, head);}
~int pop(){int v = head.item; Node t = head.next;
head = t;//delete head// return v;}
}

maxN is never used
code is very similar for most methods
push and pop are o(1) unless array resize is needed
exception handling is necessary but omitted for simplicity

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

GENERALIZED IMPLEMENTATION

A

item or array is Object class
problems with type conversion (wrapper classes, type checking etc)
default types like ints require wrapper class to be used and therefore are slower than they should be

solution stack<T> where T is the type
where T is used its replaced with type of choice</T>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

FIFO QUEUE WITH LIST

A

class intQ{
~private Node head, tail;
~private class Node{
~~int item;
~~Node next;
~~Node(int item){this.item = item; next = null;}
~intQ(int max){head = null; tail = null;}
~boolean isEmpty(){return head == null;}
~void put(int item){
~~if(isEmpty()){
~~~tail = new Node(item);
~~~head = tail;}
~~else{
~~~Node t = tail;
~~~tail = new Node(item);
~~~t.next = tail;}
~int get(){
~~int v = head.item;
~~Node t = head.next;
~~head = t;
~~return v;}
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

FIFO QUEUE WITH ARRAY

A

instead of moving items we will move the head and tail as indexes of the queue
n size array
wrap at 0
head is first item unless head ==N
head % ++
tail ++ %
empty: head%N == tail
full: (head-1)%N == tail

class Q{
private int q[];
private int N, head = 0, tail = 0;
~Q(int maxN){
~~q = new int[maxN+1];
~~N = maxN +1;
~~head = N; tail = 0;}
~boolean isEmpty(){return head%N==tail;}
~boolean isFull(){return (head-1)%N ==tail;}
~void enq(int item){q[tail++]=item; tail = tail%N}// cause when tail = N we need it to go to 0
~int deq(){head = head%N; return q[head++];}
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly