 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.

Slides:



Advertisements
Similar presentations
PRIORITY QUEUES AND HEAPS Lecture 19 CS2110 Spring
Advertisements

©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
ITEC200 – Week08 Trees. 2 Chapter Objectives Students can: Describe the Tree abstract data type and use tree terminology such as.
© 2006 Pearson Addison-Wesley. All rights reserved11 B-1 Chapter 11 (continued) Trees.
1 Trees most slides taken from Mike Scott, UT Austin.
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (4) Data Structures 11/18/2008 Yang Song.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L12 (Chapter 20) Lists, Stacks,
Marc Smith and Jim Ten Eyck
Version TCSS 342, Winter 2006 Lecture Notes Trees Binary Trees Binary Search Trees.
1 Chapter 25 Trees Iterators Heaps Priority Queues.
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary search trees.
Tree. Basic characteristic Top node = root Left and right subtree Node 1 is a parent of node 2,5,6. –Node 2 is a parent of node.
CS Data Structures Chapter 15 Trees Mehmet H Gunes
CS 1031 Tree Traversal Techniques; Heaps Tree Traversal Concept Tree Traversal Techniques: Preorder, Inorder, Postorder Full Trees Almost Complete Trees.
Information and Computer Sciences University of Hawaii, Manoa
© 2011 Pearson Addison-Wesley. All rights reserved 11 B-1 Chapter 11 (continued) Trees.
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),
DATA STRUCTURES AND ALGORITHMS Lecture Notes 5 Prepared by İnanç TAHRALI.
COSC 1030 Lecture 9 Binary Trees. Topics Basic Concept and Terminology Applications of Binary Tree Complete Tree Representation Traversing Binary Trees.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Chapter 19: Binary Trees Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
Chapter 4 Data Structures ADT Examples  Stack  Queue  Trees.
Trees N1 A tree is a data structure that starts with a root node that can be linked to 1 or more additional nodes. Furthermore, each node can be linked.
Starting at Binary Trees
Topic 19 Binary Search Trees "Yes. Shrubberies are my trade. I am a shrubber. My name is 'Roger the Shrubber'. I arrange, design, and sell shrubberies."
CS 1031 Trees A Quick Introduction to Graphs Definition of Trees Rooted Trees Binary Trees Binary Search Trees.
1 Trees, Trees, and More Trees. 2 By looking at forests of terms, awesome animations, and complete examples, we hope to get at the root of trees. Hopefully,
Outline Binary Trees Binary Search Tree Treaps. Binary Trees The empty set (null) is a binary tree A single node is a binary tree A node has a left child.
Recursive Data Structures and Grammars  Themes  Recursive Description of Data Structures  Grammars and Parsing  Recursive Definitions of Properties.
Chapter 11 B Trees. © 2004 Pearson Addison-Wesley. All rights reserved 11 B-2 The ADT Binary Search Tree A deficiency of the ADT binary tree which is.
Trees Isaac Sheff. Nodes Building blocks of trees “Parent” node may have “Child” nodes Can be both parent and child Can’t be its own ancestor Can’t have.
COSC2007 Data Structures II Chapter 11 Trees IV. 2 Topics ADT BST Implementations Efficiency TreeSort Save/Restore into/from file General Trees.
Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 6 Data Structures.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 25 Trees, Iterators,
Binary Search Trees Data Structures Ananda Gunawardena
PRIORITY QUEUES AND HEAPS Slides of Ken Birman, Cornell University.
Week 10 - Friday.  What did we talk about last time?  Graph representations  Adjacency matrix  Adjacency lists  Depth first search.
1 Chapter 4 Trees Basic concept How tree are used to implement the file system How tree can be used to evaluate arithmetic expressions How to use trees.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
Week 15 – Wednesday.  What did we talk about last time?  Review up to Exam 1.
1 Trees General Trees  Nonrecursive definition: a tree consists of a set of nodes and a set of directed edges that connect pairs of nodes.
CS 367 Introduction to Data Structures Lecture 8.
COSC 2P03 Week 21 Stacks – review A Last-In First-Out (LIFO) structure Basic Operations: –push : insert data item onto top of stack –pop : remove data.
Course: Programming II - Abstract Data Types HeapsSlide Number 1 The ADT Heap So far we have seen the following sorting types : 1) Linked List sort by.
DS.T.1 Trees Chapter 4 Overview Tree Concepts Traversals Binary Trees Binary Search Trees AVL Trees Splay Trees B-Trees.
Trees Chapter 15.
Trees Chapter 11 (continued)
Stacks – review A Last-In First-Out (LIFO) structure Basic Operations:
Trees Chapter 11 (continued)
Heaps And Priority Queues
Week 6 - Wednesday CS221.
Binary Search Trees -Monty Python and The Holy Grail
Topic 18 Binary Search Trees
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
Ch. 11 Trees 사실을 많이 아는 것 보다는 이론적 틀이 중요하고, 기억력보다는 생각하는 법이 더 중요하다.
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
Topic 19 Binary Search Trees
Stacks – review A Last-In First-Out (LIFO) structure Basic Operations:
Announcements Prelim 1 on Tuesday! A4 will be posted today
Tree.
Data Structures II AP Computer Science
Trees.
Trees Trees.
Presentation transcript:

 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

 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.

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

 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

 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.

 Going back to the beginning and retracing steps.  Simulating lines.

 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.

 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.

AlgorithmStackQueuePriority Queue InsertO(1) O(log n) RemoveO(1) O(log n) PeekO(1)

 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.

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

 A balanced binary tree is where the depth of all the leaves differs by at most 1. ABDEHCFG

 A perfect binary tree is a full binary tree in which all leaves are at the same depth or same level. ABDECFG

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.

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

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

 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.

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

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

ABC In order BAC Preorder ABC Post order BCA In order Left-Root-Right Preorder Root-Left-Right Post order Left-Right-Root

public void postorder() { doPostOrder(root); } private void doPostOrder(TreeNode t) { if(t != null) { doPostOrder(t.getLeft()); doPostOrder(t.getRight()); System.out.println(t.getValue()); }

* (8-3) * (4 + 2) * *

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)