Sie sind auf Seite 1von 10

Final Exam - More Concise Study Guide

FVCproductions - December 2014

Basic Definitions & Phrases


abstract data type
a data structure is the underlying programming construct used to implement a
collection
nowhere in an ADTs definition is there any mention of how the set of operations is
implemented
circular linked list most efficient for implementing a queue
data structure
memory structure with associated representation mapping, e.g. a collection of
programming constructions used to implement a collection
abstractions
ignores or hides certain details at certain times, e.g. collection is abstraction where
details of implementation are hidden
abstract in abstract data type
means that there are many different ways of implementing data type

Arrays
overview
fixed size
can add/remove at any position b/c elements accessed by index
elements must be of same type
all 1 block of memory
predefined in java
Defining an array (character type) of a size 3 called array
char [] array = new char[10]

Ask user to enter 10 characters and assign each of the character into array

for (int i = 0; i < array.length; i++)


array[i] = keyboard.next().charAt(0);

Search For Element


public static int search(int [] numbers, int element) { for (int index = 0; index <
numbers.length; index++) { if (numbers[index] == element) return index; } return 1; }
Number of Occurrences
int numOccur = 0 public static int search(int [] numbers, int element) { for (int index = 0;
index < numbers.length; index++) { if (numbers[index] == element) numOccur += 1; } return
numOccur; }

Properties of Stacks
overview
example of a collection SDT
since stacks are lists, any list implementation will do
Last In, First Out data structure
can be implemented as an array
can only access the top
harder to manage b/c only top can be accessed
defintion
data structure of ordered items such that items can be inserted and removed only at 1
end (called the top )

Stack Code & Algorithms


isEmpty
int top = -1; //or 0
public boolean isEmpty () {
return (top == -1);
}

delete from stack

public int remove () {


if (isEmpty() == true)
System.out.println("Stack empty, cannot delete element.");
else
return stack[top--];
}

1. check if stack is not empty


2. decrement top
3. set return value to top position of array
print in order
public void printOrder() {
while(!isEmpty()) {
int value = remove();
System.out.print(value + " ");
}
}

Properties of Queues
overview
example of a collection SDT
operations: enqueue (add), dequeue (delete)
First In, First Out (FIFO)
add to rear and remove from front
defintion
data structure of ordered items that can be inserted only at end (rear) and removed at
other end (front)

Queue Algorithms
public void enqueue (int item) {
if (isFull())
System.out.println("Queue full, cannot add.");
rear++;
array[back] = item;
size++;
}

enqueue operation
1. save value at first index to item you want to enqueue
2. increment size
3. if array fills up, all n elements have to be copied to a new, larger array
public int dequeue() { if(isEmpty()) System.out.println(Queue empty, cannot
remove.); size; front = array[front]; array[front] = null; front++; return front; }
dequeue operation
1. save value at first index
2. shift all elements left
3. decrement size

How To Choose Stacks Over Queues Or Vice-Versa


implementing a stack as an array is easy, but implementing a queue as an array is more
difficult because you have to dequeue from front and enqueue at end
if you want to be able to add or remove values from any position, arrays are the way to go
because any value can be accessed through indexes
main limitations of arrays is that they cannot be extended or shrunk so you want to use an
array when you know your max upper limit
if youre not sure how many values there will be in your data structure, then its better to not
use an array and stick with a linked list (ergo, queues)
because arrays always have a fixed size

Linked Lists vs Arrays


Arrays
can access any element directly via indexing
all elements grouped together
sitting in 1 block of memory
size is fixed
Linked Lists
each element sits in own block of memory (called node)
nodes can only be accessed in sequential order
appears limited
size varies - nodes allocated on need basis
list elements can be easily inserted/removed without reallocation and at any point in
the list

no random access to data


no efficient form of indexing
Key Differences
underlying layout of data in memory
how individual elements are accessed

Best way to implement queue - Circular Linked List


Linked Lists Properties
overview
a sequence of elements arranged 1 after another with each element connected to
next by a link
one of the simplest and most common data structures
can be used to implement other abstract data types
defintion
data structure of group of nodes that represent a sequence
advantages
dynamic data structure that allocates needed memory
easy implementation for insertion and deletion operations
other data structures can be easily executed with linked lists
reduce access time and can expand without more memory being used
disadvantages
usually wastes memory with pointers which requires extra storage space
nodes must be read sequentially
nodes stored in an in-contiguous manner, so more difficult to access individual nodes
very difficult to reverse traverse

Linked Lists Algorithms


constructor for an integer node in a linked list
public Int_Node (int initialData, Int_node initialLink) {
data = initialData; //integer value
link = initialLink; //reference to next node in list
}

define empty linked list


Int_Node head = null;

pseudocode
1. representing empty list by storing null in head reference
keeping track of front node by using an Int_Node reference variable called
head

add new node to empty list


head = new Int_Node(data, null);

pseudocode
1. create new node for head
2. place data in new nodes data field
3. make head refer to null which is initial head value
4. connect new node to head
add node to front of list
head = new Int_Node(newData, head);

pseudocode
1. create new node
2. place data ( newData ) in new nodes data field
3. connect new node to front of list
4. make original head refer to new head of linked list
removing node at head
head = head.link;

pseudocode
1. directing head to node right next to it ( head.link ) so that original head is
removed
given code for inserting node after node 30 with data value of 25. how?

if (previous.data == 30)
previous.link = new IntNode(25, previous.link);
else {
boolean flag = false;
while(previous != null || !flag) {
if(previous.data == 30)
flag = true;
previous = previous.link;
}
}
if(previous.data == 30)
previous.link = new IntNode(25,previous.link);

given code for inserting node before node 30 with data value of 25. how?
while(previous.data > 25 && !flag) {
if(previous.data > 25) {
previous = new IntNode(25, previous);
flag = true;
}
previous = previous.link;
}

Binary Trees
trees
a structure with unique starting point - root - where each node can have multiple
children nodes, and a unique path exists from the root node to every other node
root
top node of a tree structure, a node with no parent
binary tree
structure where each item is called a node and where each node can have a max of
two children, left and right child node
leaf
node with no children
descendant
child of a node
ancestor
parent of a node
binary search tree
binary tree where value in any node is greater than or equal to value in its left child

and any of descendants (nodes in left subtree) and less than value in its right child
and any of its descendants (nodes in right subtree)
diagram:
full binary tree
binary tree where all of leaves are on same level and every non-leaf node has 2
children
complete binary tree
binary tree that is full or full through next-to-last level, with leaves on last level as far
as possible

balanced tree
left and right subtrees of any node are the same height

preorder
node/root, left, right
inorder
left, node/root, right
postorder
left, right, node/root

How to Delete A Binary Search Tree


no successor
1. if node is leaf, simply removed
2. but if root is leaf, pointer to tree assigned null value
one successor
1. parent node connected to sucessor node
2. deleted node disposed of
two successors
1. find logical predecessor (node in left subtree with largest value)
2. logical predecessor replaces deleted node

Recursion
recursive method is a method that calls itself
in many cases, recursive algorithms are less efficient than iterative algorithms

recursive solutions repetitively


allocate memory for parameters and local variables
store address of where control returns after method terminates

example 1 provided - Mary


public class Mary {
public int getNumber (int num) {
if (num == 1) { //base case
return 1; //if number is 1, returns 1
}
//general recursive rule
else {

return (num + getNumber(num-1)); //call method within itself (num-1) so not a


}
}
public static void main(String[] args) {
Mary myMary = new Mary();
myMary.getNumber(5);
}
}

Tips or Tricks?
Contact me @fvcproductions

//calls method with number

Das könnte Ihnen auch gefallen