27
The Magic of Stacks

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

Embed Size (px)

Citation preview

Page 1: 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

The Magic of Stacks

Page 2: 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

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

Page 3: 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

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.

Page 4: 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

Stack – When do I use a Stack?

Expressions within other expressions Methods that call other methods Traversing directories and

subdirectories

Page 5: 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

The Magic of Queues

Page 6: 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

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

Page 7: 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

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.

Page 8: 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

Queue – When do I use a queue?

Going back to the beginning and retracing steps.

Simulating lines.

Page 9: 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

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.

Page 10: 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

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.

Page 11: 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

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)

Page 12: 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

The Magic of Binary Trees

Page 13: 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

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.

Page 14: 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

Binary Tree - Example

Page 15: 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

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

Page 16: 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

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

Page 17: 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

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

Page 18: 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

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.

Page 19: 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

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);}

Page 20: 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

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);}

Page 21: 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

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.

Page 22: 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

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));}

}}

}

Page 23: 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

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;}

Page 24: 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

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

Page 25: 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

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());

}}

Page 26: 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

Binary Expression Tree

*

-

8 3

+

4 2

(8-3) * (4 + 2)*-83+4283-42+*

Page 27: 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

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)