You are on page 1of 8


Chapter II Object Oriented Design

+ Object Oriented Design Goals
- Robustness
Means producing the right output no matter the input. Handle unexpected
- Adaptability
Software should evolve over time and adapt according to changing
conditions. Software should be portable.
- Reusability
Code should be well written, such that the code can be reused in future
software, perhaps as a component.
+ Object Oriented Design Principles
- Abstraction
The notion of abstraction is to distill a complicated system in to it's
most fundamental parts. Abstraction yields ADTs or Abstract Data Types.
ADTs are structures that specify for the type of data stored and what each
operation does but not how it does it. In java and ADT is an interface
with a set of method declarations but not implementations. ADTs are
realized by concrete data structures. Classes implement interfaces,
because they contain behavior.
- Encapsulation
Different components of a system should not reveal their implementations.
Encapsulation allows programmers to hide their implementations making it
only accessible via an interface or API. Making the code robust and
- Modularity
The separation of different components within in a software system into
different functional units.
+ Design Patterns
- A solution to a typical software design problem.
+ Inheritance
- 'is-a' relationship
- A house is a type of building, a building is not a type of house
- Child classes extend base or parent classes
+ Polymorphism
- Liskov substitution principle, a variable of declared type may be assigned
an instance from any direct or indirect child class of that type.
- Variable may take on many forms.
- Java uses dynamic dispatch to determine the method closest to the actual
+ Interface and Abstract Classes
- In order for objects to interact, classes should present an API that can
be interfaced with.
- Strong typing requires that, that the parameters that are passed to methods
rigidly conform to the type specified in the interface.
- Interfaces are implemented
- Java Classes can implement multiple interfaces.

+ Abstract Classes
- Abstract classes are between interfaces and base classes
- They may define method signatures, known as abstract methods, but they
may also have fields and implemented methods known as concrete methods
- May not be instantiated. Child class must implement abstract methods of
parent class.
+ Casting
- Widening Conversions occur when a type is casted or converted up. This is
implicit casting.
- Narrowing Conversions, casting down or explicit casting. Instanceof should
be used before attempting down casting.
+ Generics
- The goal of generic programming is to write one class that can accommodate
any input type. Avoid many explicit casts.
- Type inference
- Not defined at compile time, defined at run-time.
- Instantiate with actual parameters
Pair<String, ArrayList> = new Pair<String, ArrayList>();
Chapter III Fundamental Data Structures
+ Linked list is collection of nodes that form a linear sequence.
+ The first node in a Linked List is the Head, the last node is known as the
Tail. The Tail's next reference is Null.
+ Link Hopping refers to traversing the list
+ Singly Linked lists only keep track of the node next in the sequence
+ Singly Linked List
------------------------------------------------------------------------------+ Methods
- size()
- isEmpty()
- first()
- last()
- addFirst(e)
- addLast(e)
- removeFirst()
+ Add element to the Head of List
- Create a new node, with new element
- Set the new nodes next to the Head
- Set the Head to the new node
- Increment List Size
- addFirst(e)
tmp = new Node(e); = head;
head = tmp;
size ++;
+ Add element to Tail
- Create new node, with new element
- Set new node next to null
- Set Tail next to new node
- Set Tail to new node
- Increase List size

- addLast(e)
tmp = new Node(e); = Null; = tmp;
Tail = tmp;
+ removeFirst()
head =;
+ Doubly Linked List
------------------------------------------------------------------------------+ Doubly Linked lists keep track of the preceeding and proceeding nodes. Both
next and previous nodes are tracked at each node.
+ Doubly linked lists have nodes at the tail and head that do not contain any
elements. These nodes demarcate the boundaries of the list and are known as
the Sentinel Nodes.
+ Methods
- size()
- isEmpty()
- first()
- last()
- addFirst(e)
- addLast(e)
- removeFirst()
- rmoveLast()
+ addBetween(E e, Node<E> predecessor, Node<E> successor)
Node<E> tmp = new Node<E>(e, predecessor, successor); = tmp;
successor.previous = tmp;
+ E remove(Node<E> node)
Node<E> predecessor = node.previous;
Node<E> successor =; = successor;
successor = predecessor;
return node.getElement();
Chapter IV Algorithm Analysis
+ Seven Functions used in Algorithm Analysis
- Constant Function
- f(n) = c
- Logarithm Function
- x = logn iff b^x =n
- Linear Function
- f(n) = n

- N-log N function
- f(n) = nlog(n)
- Quadratic Function
- f(n) = n^2
- Cubic and other Polynomials
- f(n) = n^3
- Exponential Function
- f(n) = b^n
+ Ordered in increasing Growth Rate
1. 1
2. logn
3. n
4. nlogn
5. n^2
6. n^3
7. 2^n
Chapter V Recursion
+ Recursion can be distilled into the concept; when a method calls itself
+ Types of recursion
- Linear Recursion
- LinearSum(A, n)
if(n == 0)
return A[0]
return LinearSum(A, n-1) + A[n-1]
- Tail Recursion
- Makes recursive call on last step
- Binary Recursion
- Multiple Recursion
Chapter VI Stacks, Queues & Deques
+ Stacks are a collection of objects who are inserted and removed according
to the LIFO principal. Or Last-In, First-Out.
+ Java.util.Stack interface
+ The Stack ADT
------------------------------------------------------------------------------- push(e)
- pop()
- top() //returns element at top
- size()
- isEmpty()
+ Queues are a collection of objects that are inserted according to the FIFO

principle. Or the First-In, First-Out

+ Java.util.Queue
+ The Queue ADT
------------------------------------------------------------------------------- enqueue(e)
- dequeue()
- first()
- size()
- isEmpty()
+ Deque ADT or Double Ended Queue
- addFirst(e)
- addLast(e)
- removeFirst()
- removeLast()
- first()
- last()
- size()
- isEmpty()
Chapter VII List & Iterator ADT
+ Java.util.List interface
+ The List ADT
------------------------------------------------------------------------------- size()
- isEmpty()
- get(i)
- set(i,e)
- add(i,e)
- remove(i)
+ The ArrayList<E>, or vector ADT - where A[i] stores a reference to element
with index i.
------------------------------------------------------------------------------+ public E remove(i)
if(i < 0 || i > size)
return OutofBounds;
E temp = data[i];
for(int k = 1; k < size - 1; k++)
data[k] = data[k+1];
data[size-1] = null;
return temp;
+ public void add(i, e)
if(i < 0 || i > size)
return OutOfBounds;
if(size == data.length)
return ArrayIsFull
for(int k = size-1; k < size-1; k--)

data[k+1] = data[k];
data[i] = e;
+ Resize array
- Manually
- Create new array larger than current
- Copy contents across
- Dereference old Array
- ArrayCopy Java.lang.System
- Make new destination array, dest[]
- System.arraycopy(Object src, int srcPos, Object dest, int destPos);
+ Position ADT, single method element()
------------------------------------------------------------------------------+ The location of an element within a structure
- getElement()
- first()
- last()
- before(p) //returns the position before p
- after(p) // returns the position after p
- isEmpty()
- size()
- Traversing position list
Position<String> cursor = guests.first();
while(cursor != null)
cursor = guests.after(cursor);
+ Updating a Position List
- addFirst(e)
- addLast(e)
- addBefore(p, e)
- addAfter(p, e)
- set(p, e)
- remove(p) //returns element, removes position
+ Position<E> addBetween(E e, Node<E> pred, Node<E> succ)
Node<E> tmp = new Node<>(e, pred, succ );
pred.setNext = tmp;
succ.setPrev = tmp;
return tmp;
+ Position<E> remove(Position<E> p)
Node<E> predecessor = node.getPrev();
Node<E> successor = node.getNext();
E tmp = node.getElement();
return tmp;
+ Iterator

+ An iterator is a software design pattern that abstracts the process of

scanning through a sequence of elements, one element at a time.
+ Java.util.Iterator
- hasNext() // returns true is a next element
- next() //returns next element
- while(iter.hasNext)
val =;
- for-each loop
- for(ElemntType variable : collection)
- Java.lang.Iterable returns a single method; iterator()
- supports for-each
- cannot remove element, must implement java.lang.Iterator
+ Implementing Iterators
- Snapshot iterator, records the sequence of elements at a given time when the iterator is created. O(n) time
- Lazy iterator, no upfront copy. Traverses structure when next is called.
Chapter VIII Trees
+ A

tree is an abstract model of hierarchical structure

Root, node without a parent
Internal node, node with at least one child
External(leaves) node, node without children
Ancestors of a node, parent, grandparent
Descendant of a node, child, grandchild, grand-grandchild
Subtree, a tree consisting of a node and its descendants
Edge, a pair of nodes (u,v) where u is the parent of v
Path, sequence of nodes such that any two consecutive nodes form an edge

+ Tree ADT using Position ADT

- getElement()
- root() //returns position of root
- parent(p) //returns position of parent
- children(p) //returns iterable collection of children
- numChildren(p)
- isInternal(p) // true if p has at least one child
- isExternal(p) // true if p has no children
- isRoot() // true if p is root
- size()
- isEmpty()
- iterator() //returns Iterator
- positions() //returns Iterable collection
+ Computing Depth, find the depth of the node. The number of
ancestors of p but p itself.
- public int depth(Position<E> p)
return 0
return 1 + depth(parent(p))

+ Computing Height, height is the maximum of depths of the tree

- public int height(Position<E> p)
int h = 0;
for(Position<E> c : children(p))
h = Math.max(h, 1 + height(c));
return h;
+ Tree traversal is a systematic way of visiting all the positions of T
- Preorder Traversal, visits Root and then children is traversed recursively
- preorder(p)
for each child c in children(p)
- PostOrder Traversal, recursively traverses the subtrees rooted at the
- postorder(p)
for each child c in children(p)