Data Structures II AP Computer Science

Slides:



Advertisements
Similar presentations
Chapter 7. Binary Search Trees
Advertisements

 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. DCS – SWC 2 Binary Trees Sets and Maps in Java are also available in tree-based implementations A Tree is – in this context – a data structure.
A Binary Search Tree Implementation Chapter Chapter Contents Getting Started An Interface for the Binary Search Tree Duplicate Entries Beginning.
© 2006 Pearson Addison-Wesley. All rights reserved12 B-1 Chapter 12 (continued) Tables and Priority Queues.
1 Trees most slides taken from Mike Scott, UT Austin.
CS 307 Fundamentals of Computer Science 1 Abstract Data Types many slides taken from Mike Scott, UT Austin.
Fall 2007CS 2251 Iterators and Tree Traversals. Fall 2007CS 2252 Binary Trees In a binary tree, each node has at most two subtrees A set of nodes T is.
Algorithm Programming Containers in Java Bar-Ilan University תשס " ו by Moshe Fresko.
24-Jun-15 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
Data Structures & Java Collections Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
The Collections Framework A Brief Introduction. Collections A collection is a structured group of objects –An array is a kind of collection –A Vector.
Java's Collection Framework
SEG4110 – Advanced Software Design and Reengineering TOPIC G Java Collections Framework.
Sets and Maps Part of the Collections Framework. The Set interface A Set is unordered and has no duplicates Operations are exactly those for Collection.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Lecture Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion to process trees.
Chapter 3 List Stacks and Queues. Data Structures Data structure is a representation of data and the operations allowed on that data. Data structure is.
Data Structures and Abstract Data Types "Get your data structures correct first, and the rest of the program will write itself." - David Jones.
DataStructures1 Barb Ericson Georgia Tech July 2008.
Collections in Java. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection with no duplicates SortedSet.
1 Sets and Maps Starring: keySet Co-Starring: Collections.
© 2011 Pearson Addison-Wesley. All rights reserved 11 B-1 Chapter 11 (continued) Trees.
Topic 17 Introduction to Trees
Sets and Maps Chris Nevison. Set Interface Models collection with no repetitions subinterface of Collection –has all collection methods has a subinterface.
Topic 15 The Binary Search Tree ADT Binary Search Tree A binary search tree (BST) is a binary tree with an ordering property of its elements, such.
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."
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,
Georgia Institute of Technology Workshop for CS-AP Teachers Data Structures Barb Ericson June 2006.
Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 6 Data Structures.
SETS AND MAPS Collections of Data. Advanced Data Structures Often referred to as the Java Collections Framework…. Set and map data types Hash tables Binary.
Week 15 – Wednesday.  What did we talk about last time?  Review up to Exam 1.
Sets and Maps Part of the Collections Framework. 2 The Set interface A Set is unordered and has no duplicates Operations are exactly those for Collection.
3-1 Java's Collection Framework Another use of polymorphism and interfaces Rick Mercer.
Tables and Priority Queues
Trees Chapter 11 (continued)
Trees Chapter 11 (continued)
Efficiency of in Binary Trees
Binary Trees "The best time to plant a tree is twenty years ago. The second best time is now." -Chinese proverb Real programmmers always confuse Christmas.
Data Structures TreeMap HashMap.
Binary Trees "A tree may grow a thousand feet tall, but its leaves will return to its roots." -Chinese Proverb.
Trees.
Week 11 - Friday CS221.
abstract containers sequence/linear (1 to 1) hierarchical (1 to many)
COMP 103 Binary Search Trees.
Binary Search Trees -Monty Python and The Holy Grail
Topic 18 Binary Search Trees
Binary Tree and General Tree
Binary Trees.
Data Structures and Database Applications Binary Trees in C#
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
structures and their relationships." - Linus Torvalds
TreeSet TreeMap HashMap
JAVA Collections Framework Set Interfaces & Implementations
Part of the Collections Framework
Topic 18 Binary Trees "A tree may grow a thousand feet tall, but its leaves will return to its roots." -Chinese Proverb.
Collections in Java The objectives of this lecture are:
Lecture 12 CS203 1.
CSC 143 Binary Search Trees.
Hashing in java.util
Trees.
Workshop for CS-AP Teachers
structures and their relationships." - Linus Torvalds
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Presentation transcript:

Data Structures II AP Computer Science "A tree may grow a thousand feet tall, but its leaves will return to its roots." -Chinese Proverb Data Structures II

Other Data Structures in APCS Binary Search Trees TreeNode Maps HashMap TreeMap Sets TreeSet HashSet Heaps (not covering today) Data Structures II

Binary Search Trees A binary tree is a tree where each node has at most two children, referred to as the left and right child A binary search tree is a binary tree where every node's left subtree has values less than the node's value, and every right subtree has values greater. A new node is added as a leaf. root parent 17 11 19 right child left child Data Structures II

Performance of Binary Trees For the three core operations (add, access, remove) a binary search tree (BST) has an average case performance of O(log N) Even when using the naïve insertion / removal algorithms no checks to maintain balance balance achieved based on the randomness of the data inserted Data Structures II

Sample Insertion 100, 164, 130, 189, 244, 42, 141, 231, 20, 153 (from HotBits: www.fourmilab.ch/hotbits/) 100 42 164 20 130 189 244 141 141 231 If you insert 1000 random numbers into a BST using the naïve algorithm what is the expected height of the tree? (Number of links from root to deepest leaf.) Data Structures II

Worst Case Performance In the worst case a BST can degenerate into a singly linked list. Performance goes to O(N) 2 3 5 7 11 13 17 2 3 5 7 11 13 17 Data Structures II

Properties of a BST The minimum value is in the left most node The maximum value is in the right most node useful when removing an element from the BST An inorder traversal of a BST provides the elements of the BST in ascending order There are 4 traditional types of traversals preorder traversal: process the root, then process all sub trees (left to right) in order traversal: process the left sub tree, process the root, process the right sub tree post order traversal: process the left sub tree, process the right sub tree, then process the root level order traversal: starting from the root of a tree, process all nodes at the same depth from left to right, then proceed to the nodes at the next depth. Data Structures II

Results of Traversals To determine the results of a traversal on a given tree draw a path around the tree. start on the left side of the root and trace around the tree. The path should stay close to the tree. 100 pre order: process when pass down left side of node 100 42 20 72 164 in order: process when pass underneath node 20 42 72 100 164 post order: process when pass up right side of node 20 72 42 164 100 42 164 20 72 Data Structures II

Implementing BSTs There is not a standalone binary search tree class in the Java standard library The TreeMap and TreeSet classes use BSTs as the internal storage container (also called the backing collection) To formulate questions regarding binary trees and binary search trees APCS development committee has provided a TreeNode class there has been a binary tree question on every test since 1999. Not always BST Data Structures II

The TreeNode class public class TreeNode { private Object value; private TreeNode left; private TreeNode right; public TreeNode(Object initValue) { value = initValue; left = null; right = null; } public TreeNode(Object initValue, TreeNode initLeft, TreeNode initRight) { value = initValue; left = initLeft; right = initRight; } public Object getValue() { return value; } public TreeNode getLeft() { return left; } public TreeNode getRight() { return right; } public void setValue(Object theNewValue) { value = theNewValue; } public void setLeft(TreeNode theNewLeft) { left = theNewLeft; } public void setRight(TreeNode theNewRight) { right = theNewRight; } } Data Structures II

More on Implementation Many ways to implement BSTs Using nodes is just one and even then many options and choices APCS usually implements an empty tree with the root equal to null, as opposed to a dummy node public class BinarySearchTree { private TreeNode root; private int size; public BinarySearchTree() { root = null; size = 0; } Data Structures II

Access an Element public boolean contains(Comparable data) { return conHelp(data, root);} public boolean conHelp(Comparable data, TreeNode t) { boolean result; if( t == null ) // fell off the tree. Item not present result = false; else { // which way do I go? Comparable dataInNode = (Comparable)(t.getValue()); int dir = dataInNode.compareTo( data ); if( dir > 0 ) // data in node > data to insert. go left return conHelp(data, t.getLeft()); else if( dir < 0 ) // data in node < data to insert. go right return conHelp(data, t.getRight()); // found it! result = true; } return result; Data Structures II

Add an Element, Recursive public boolean add(Comparable data) { int oldSize = size; root = addHelp(data, root); return oldSize != size; } public TreeNode addHelp(Comparable data, TreeNode t) { if( t == null ) { // fell off tree. // Create and return new node size++; return new TreeNode(data); } else { // which way do I go? Comparable dataInNode = (Comparable)(t.getValue()); int dir = dataInNode.compareTo( data ); if( dir > 0 ) // data in node > data to insert. go left t.setLeft( addHelp(data, t.getLeft() )); else if( dir < 0 ) // data in node < data to insert. go right t.setRight( addHelp(data, t.getRight())); // else date in node equals data to insert. No action return t; Data Structures II

Add an Element, Iterative public boolean otherAdd(Comparable data) { int oldSize = size; // empty tree? if( root == null ) { root = new TreeNode(data); size++; } else { int dir = -1; TreeNode lead = root; TreeNode trailer = root; while( lead != null && dir != 0) { trailer = lead; dir = ((Comparable)(lead.getValue())).compareTo(data); if( dir > 0 ) lead = trailer.getLeft(); else if( dir < 0 ) lead = trailer.getRight(); if( lead == null ) { if( dir > 0 ) trailer.setLeft( new TreeNode(data) ); trailer.setRight( new TreeNode(data) ); return oldSize != size; Data Structures II

Remove an Element An exercise for you Three cases node is a leaf, 0 children (easy) node has 1 child (easy) node has 2 children (interesting) Data Structures II

Maps - public interface Map A map is an object that maps keys to values A map cannot contain duplicate keys; each key can map to at most one value but those values could be complicated things like Lists, Sets, Stacks, Queues, or other Maps an interesting way of representing a graph -> a map of sets Data Structures II

Methods int size() boolean isEmpty() boolean containsKey(Object key) boolean containsValue(Object value) Object get(Object key) Object put(Object key, Object value) Object remove(Object key) void putAll(Map t) void clear() Set keySet() Collection values() Set entrySet() boolean equals(Obect o) int hashCode() Data Structures II

What to know for AP HashMap TreeMap Methods: put get remove containsKey size keySet Data Structures II

public Object remove(Object key) If the map contains a mapping of this key to a value then the mapping is removed Returns previous value associated with specified key, or null if there was no mapping for key Data Structures II

public boolean containsKey(Object key) Returns true if this map contains a mapping for the specified key Data Structures II

public int size() Returns the number of key-value mappings in the Map Data Structures II

public Set keySet() Returns a set view of the keys contained in this map The set supports element removal, which removes the corresponding mapping from the map, via the Iterator.remove, Set.remove, removeAll retainAll, and clear operations. It does not support the add or addAll operations Data Structures II

Example Map m = new TreeMap(); String[] words = {"blue", "red", "green", "yellow", "black"}; int j = 0; for(int i = 1; i<=5; i++) {m.put(new Integer(i), words[j]); j++;} System.out.println(m); //lets run this then add to it Data Structures II

Example II Create a Frequency Table of Characters in a String public Map createFrequencyTable(String source) { Map result = new TreeMap(); Integer one = new Integer(1); Integer freq; Character temp; for(int i = 0; i < source.length(); i++) { temp = new Character( source.charAt(i)); freq = (Integer)result.get( temp ); if( freq == null ) result.put( temp, one ); else result.put( temp, new Integer( freq.intValue() + 1 )); } return result; Data Structures II

Sets - The Set Interface It is a collection that contains no duplicate elements It has at most one null element Implemented by AbstractSet HashSet LinkedHashSet TreeSet Extends the Collection interface Data Structures II

Methods int size() boolean isEmpty() boolean contains(Object x) Iterator iterator() Object[] toArray() boolean add(Object x) boolean remove(Object x) boolean containsAll(Collection c) boolean addAll(Collection c) // union boolean retainAll(Collection c) // intersection boolean removeAll(Collection c) // difference void clear() boolean equals(Object x) int hashCode() Data Structures II

What to know for AP HashSet – stored in hash table O(1) for add, remove, contains TreeSet – stored in a tree (“ordered”) o(log N) for add, remove, contains Methods: boolean add (Object x) boolean contains (Object x) boolean remove (Object x) int size() Iterator iterator() Data Structures II

boolean add (Object x) Adds the specified Object to the set (if its not already present) Returns false and leaves the set unchanged if the value of x is already in the set Data Structures II

boolean contains (Object x) Returns true if and only if the set contains the specified element Data Structures II

boolean remove (Object x) Removes the specified element from the set (if it exists) Returns true if the set contained the specified element (in other words, if the set changed as a result of the call) Data Structures II

int size() Returns the number of elements in the set Data Structures II

Iterator iterator() Returns an iterator over the elements in this set The elements of the set are in no particular order (unless this set is an instance of some class that provides a guarantee) Data Structures II