Download presentation
Presentation is loading. Please wait.
Published byCrystal Gumbel Modified over 9 years ago
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 type. These items can only be added and removed at one end. Stack operations are either push or pop
3
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.
4
Expressions within other expressions Methods that call other methods Traversing directories and subdirectories
6
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
7
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.
8
Going back to the beginning and retracing steps. Simulating lines.
9
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.
10
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.
11
AlgorithmStackQueuePriority Queue InsertO(1) O(log n) RemoveO(1) O(log n) PeekO(1)
13
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.
15
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
16
A balanced binary tree is where the depth of all the leaves differs by at most 1. ABDEHCFG
17
A perfect binary tree is a full binary tree in which all leaves are at the same depth or same level. ABDECFG
18
ABDECF 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.
19
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); }
20
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); }
21
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.
22
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)); }
23
public 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; }
24
ABC In order BAC Preorder ABC Post order BCA In order Left-Root-Right Preorder Root-Left-Right Post order Left-Right-Root
25
public void postorder() { doPostOrder(root); } private void doPostOrder(TreeNode t) { if(t != null) { doPostOrder(t.getLeft()); doPostOrder(t.getRight()); System.out.println(t.getValue()); }
26
*-83+42 (8-3) * (4 + 2) *-83+42 83-42+*
27
OperationBalanced BSTUnbalanced BST Insert 1 elementO(log n)O(n) Insert n into empty BSTO(n log n)O(n^3) Search for keyO(log n)O(n) Traverse TreeO(n)
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.