Академический Документы
Профессиональный Документы
Культура Документы
Arrays
overview
fixed size
more efficient b/c it uses less memory
easier to manage
collection
linear
can add/remove at any position
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);
Use the data in array to reverse a list called reverse so that the first element in the list
called array is the last element in reverse
for (int i = 0; i < array.length; i++)
reverse[reverse.length i] = array[i];
Write the first use way to use for-loop to print out all elements in the list called reverse
for (int i = 0; i < reverse.length; i++)
System.out.print(reverse[i] + );
Write the second use way to use for-loop to print out all elements in the list called reverse
for (char value : reverse)
System.out.print(value + );
Assign the value for each cell in the array called scores to
columnIndex * 20 / rowIndex
Write a findMax method which finds the maximum value in the data stored in scores
static double findMax(double [][] scores) {
double max = scores[0][0];
for (int row = 0; row < scores.length; row++)
for (int col = 0; col < scores[row].length; col++)
if (scores[row][col] > max)
max = myScores[row][col];
return max;
}
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 )
diagram
conceptual implementation
First In, First Out (FIFO)
add to rear and remove from front
operations
push - adds item to stack
pop - deletes item from stack
peek - looks at top item in stack
size - returns # of elements in stack by examining top
isEmpty - tests for emptiness in stack by examining top
isFull
add to stack
public void add (int element) {
if (isFull() == true)
System.out.println("Stack full, cannot add element.");
else
stack[++top] = element;
}
Properties of Queues
overview
example of a collection SDT
operations: enqueue (add), dequeue (delete)
defintion
data structure of ordered items that can be inserted only at end (rear) and removed at
other end (front)
diagram
conceptual implementation
First In, First Out (FIFO)
add to rear and remove from front
operations
enqueue - adds item to queue
Queue Algorithms
shift queues
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
dequeue operation
1. save value at first index
2. shift all elements left
3. decrement size
circular queues
advance queue indexes front (to delete an item) and back (to insert an item) by
moving them both clockwise around array
Towers of Hanoi
examples of queues
waiting line for a roller coaster
hamburger processing line at Mickey Ds
undo button
vehicles on a toll bridge
luggage checking machine
phone answering system for Apples customer support
above is a linked list with nodes that contain 2 fields - an integer value and a link to next to
the next node; last node linked to terminator symbol to signify end of list/null link
conceptual implementation
each node has data and reference (link) to next node in sequence
allows for insertion of removal of elements from any position in sequence
operations for singly linked list
insertion
deletion
traversal - going through entire sequence until reaching last node
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
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
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
Diagram showing how a node is inserted after an existing node Inserting node before
existing node cannot be done directly - instead you have to keep track of the previous
node and insert a node after that
adding anywhere but front
previous.link = new Int_Node(newData, previous.link);
while (prev.link != null) {
prev = head;
prev = prev.link;
}
pseudocode
1. set a reference named prev (for previous) to refer to node which is just before
new nodes position
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
removing node anywhere
Removing node after given node - to find and remove a particular node, you still have to
keep track of the previous element
traverse through list
Int_Node pointer = head;
while (pointer != null) {
pointer = pointer.link;
}
pseudocode
1. initializing pointer to reference head
2. while loop that keeps going through entire list until pointer (or head ) is null
null implying that its reached the last node because the last node will
always have a null link since theres nothing next to the last node so no
link so null link
3.
pseudocode
same as traversal algorithm but just printing out data of pointer as you go along
the node sequence with pointer.data
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
diagram:
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 nonleaf 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
FUN TIP I
a fast way to remember this is through the following, starting by writing r in the far left
then fill it out with L or R, representing left or right, as follows:
r L R - preorder
L r R - inorder
L R r - postorder
FUN TIP II
in-order traversal is probably easiest to see, because it sorts the values from
smallest to largest (literally)
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
basically works like this:
a base case is established
if matched, method solves it and returns
if base case cannot be solved
method reduces it to smaller problem (recursive case) and calls itself to solve
smaller problem
count(n)
if (n > 0)
print n
count (n-1)
else
print done
count: n --> n = 3
count: (n-1) --> n = 2
count: (n-1) --> n = 1
count: (n-1) --> n = 0
public static int gcd(int x, int y) { // returns greater common denominator of two ar
if (x % y == 0)
return y;
else
return gcd(y, x % y);
}
Tips or Tricks?
Contact me @fvcproductions