The implementation of this class is testable on the AP CS AB exam. Stacks are last in first out....
-
Upload
crystal-gumbel -
Category
Documents
-
view
214 -
download
0
Transcript of The implementation of this class is testable on the AP CS AB exam. Stacks are last in first out....
The Magic of Stacks
Stack
The implementation of this class is testable on the AP CS AB exam.
Stacks are last in first out. LIFO. A stack is a sequence of items of the
same type. These items can only be added and
removed at one end. Stack operations are either push or
pop
Stack – Methods
boolean isEmpty() Are there any elements in the stack?
E push(E item) Add or push item onto to the stack. Returns the item.
E pop() Return and Remove or pop last element pushed
from the stack. EmptyStackLocation is thrown if empty.
E peek() Return last element pushed from the stack. EmptyStackLocation is thrown if empty.
Stack – When do I use a Stack?
Expressions within other expressions Methods that call other methods Traversing directories and
subdirectories
The Magic of Queues
Queue
This interface is testable on the AB exam.
Queues are first in first out. FIFO. A queue is a line of items of the
same type. These items can only be added and
removed at one end. Stack operations are either add or
remove
Queue - Methods
boolean isEmpty() Are there any elements in the queue?
boolean add(E item) Put an element into the queue.
E remove() Returns first element in the queue. NoSuchElement is thrown if the queue is empty.
E peek() Returns the first element in the queue. Returns null if empty.
Queue – When do I use a queue?
Going back to the beginning and retracing steps.
Simulating lines.
Priority Queue
Queue with a priority field. Elements in a priority queue must
implement Comparable. Does not allow insertion of null elements.
A NullPointerException is thrown if null. Priority Queues allow for:
Rapid insertion of elements that arrive in arbitrary order.
Rapid retrieval of the element with highest priority.
Priority Queue – When do I use it?
A database of patients awaiting liver transplants, where the sickest patients have the highest priority
Scheduling events. Events are generated in random order and each event has a time stamp denoting when the event will occur. The scheduler retrieves the events in the order they will occur.
Stack, Queue and Priority Queue runtime operations.
Algorithm Stack Queue Priority Queue
Insert O(1) O(1) O(log n)
Remove O(1) O(1) O(log n)
Peek O(1) O(1) O(1)
The Magic of Binary Trees
Binary Trees
The implementation of this class is testable on the AP CS AB exam.
A binary tree is a finite set of elements that is either empty or contains a single element called the root.
Each node in the tree has a Left and Right.
Binary Tree - Example
Binary Tree
•Root•Node•Leaf•Children•Parent•Descendant•Ancestor•Depth•Level of a node•Level of a tree•Height•Balanced Tree•Perfect Binary Tree•Complete Binary Tree
Balanced Binary Tree
A balanced binary tree is where the depth of all the leaves differs by at most 1.
A
B
D E
H
C
F G
Perfect Binary Tree
A perfect binary tree is a full binary tree in which all leaves are at the same depth or same level.
A
B
D E
C
F G
Complete Binary Tree
A
B
D E
C
F
A complete binary tree is either perfect or perfect through the next-to-last level, with leaves as far left as possible in the last level.
Binary Tree Node Class
public class TreeNode{
private Object value;private TreeNode left, right;
public TreeNode(Object initValue);
public TreeNode(Object initValue, TreeNode initLeft, TreeNode initRight);
public Object getValue();
public TreeNode getLeft();
public TreeNode getRight();
public void setValue(Object theNewValue);
public void setLeft(TreeNode theNewLeft);
public void setRight(TreeNode theNewRight);}
Binary Tree Class
public abstract class BinaryTree{
private TreeNode root;
public BinaryTree(){
root = null;}
public TreeNode getRoot();
public void setRoot(TreeNode theNewNode);
public boolean isEmpty(){
return root == null;}
public abstract void insert(Comparable item);
public abstract TreeNode find(Comparable key);}
Binary Search Tree
The implementation of this class is testable on the AP CS AB exam.
A binary search tree is a binary tree that stores elements in an ordered way that makes it efficient to find a given element and easy to access the elements in sorted order.
Binary Search Tree
public class BinarySearchTree extends BinaryTree{
public void insert(Comparable item){
if(getRoot() == null){
setRoot(new TreeNode(item));}else{
TreeNode p = null, q = getRoot();
while(g != null){
p = q;
if(item.compareTo(p.getValue()) < 0){
q = p.getLeft();}else{
q = p.getRight();}
}if(item.compareTo(p.getValue()) < 0){
p.setLeft(new TreeNode(item));}else{
p.setRight(new TreeNode(item));}
}}
}
Binary Search Tree - Findpublic TreeNode find(Comparable key){
TreeNode p = getRoot();
while(p != null && key.compareTo(p.getValue()) != 0){if(key.compareTo(p.getValue()) < 0){p = p.getLeft();}else{p = p.getRight();}}
return p;}
Tree Traversal
A
B C
In order BAC
Preorder ABC
Post order BCA
In order Left-Root-RightPreorder Root-Left-RightPost order Left-Right-Root
Recursive Methods in BSTpublic void postorder(){
doPostOrder(root);}
private void doPostOrder(TreeNode t){
if(t != null){
doPostOrder(t.getLeft());doPostOrder(t.getRight());System.out.println(t.getValue());
}}
Binary Expression Tree
*
-
8 3
+
4 2
(8-3) * (4 + 2)*-83+4283-42+*
Binary Search Tree runtime operations
Operation Balanced BST Unbalanced BST
Insert 1 element O(log n) O(n)
Insert n into empty BST
O(n log n) O(n^3)
Search for key O(log n) O(n)
Traverse Tree O(n) O(n)