CSE 373 Data Structures and Algorithms Lecture 9: Set ADT / Trees.

Slides:



Advertisements
Similar presentations
Chapter 7. Binary Search Trees
Advertisements

CSE 143 Lecture 18 Binary Trees read slides created by Marty Stepp and Hélène Martin
CS 171: Introduction to Computer Science II
InOrder Traversal Algorithm // InOrder traversal algorithm inOrder(TreeNode n) { if (n != null) { inOrder(n.getLeft()); visit(n) inOrder(n.getRight());
CSci 143 Sets & Maps Adapted from Marty Stepp, University of Washington
CSE 143 Lecture 19 Binary Trees read slides created by Marty Stepp
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.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (4) Data Structures 11/18/2008 Yang Song.
Fundamentals of Python: From First Programs Through Data Structures
CSE 143 Lecture 19 Binary Search Trees read 17.3 slides created by Marty Stepp
CSE 143 Lecture 15 Sets and Maps reading: ; 13.2 slides created by Marty Stepp
CSE 143 Lecture 7 Sets and Maps reading: ; 13.2 slides created by Marty Stepp
Binary Search Trees Chapter 7 Objectives
Version TCSS 342, Winter 2006 Lecture Notes Trees Binary Trees Binary Search Trees.
By : Budi Arifitama Pertemuan ke Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation.
Trees Chapter 8. 2 Tree Terminology A tree consists of a collection of elements or nodes, organized hierarchically. The node at the top of a tree is called.
Trees. Tree Terminology Chapter 8: Trees 2 A tree consists of a collection of elements or nodes, with each node linked to its successors The node at the.
Advanced Algorithms Analysis and Design Lecture 8 (Continue Lecture 7…..) Elementry Data Structures By Engr Huma Ayub Vine.
Sorted Array What is BigO for sorted list implemented as: ArrayList: – Search : – Insert(value) : – Remove(value) : LinkedList: – Search : – Insert(value)
Building Java Programs Chapter 17 Binary Trees. 2 Creative use of arrays/links Some data structures (such as hash tables and binary trees) are built around.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Templatized Tree.
CS Data Structures Chapter 15 Trees Mehmet H Gunes
S EARCHING AND T REES COMP1927 Computing 15s1 Sedgewick Chapters 5, 12.
CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists.
Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
Tree (new ADT) Terminology:  A tree is a collection of elements (nodes)  Each node may have 0 or more successors (called children)  How many does a.
CSE 143 Lecture 18 Binary Trees read slides created by Marty Stepp
Building Java Programs Chapter 11 Lecture 11-1: Sets and Maps reading:
Binary Search Trees Binary Search Trees (BST)  the tree from the previous slide is a special kind of binary tree called a binary.
Trees Chapter 8. 2 Tree Terminology A tree consists of a collection of elements or nodes, organized hierarchically. The node at the top of a tree is called.
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.
Binary Search Trees Nilanjan Banerjee. 2 Goal of today’s lecture Learn about Binary Search Trees Discuss the first midterm.
Computer Science: A Structured Programming Approach Using C Trees Trees are used extensively in computer science to represent algebraic formulas;
Binary Search Tree Traversal Methods. How are they different from Binary Trees?  In computer science, a binary tree is a tree data structure in which.
CS-2852 Data Structures LECTURE 11 Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
1 Chapter 7 Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation of the binary search tree.
Building Java Programs Binary Trees reading: 17.1 – 17.3.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
CSE 143 Lecture 19 Binary Trees read slides created by Marty Stepp
CSE 143 Lecture 11: Sets and Maps reading:
CSE 143 Lecture 20 Binary Search Trees read 17.3 slides created by Marty Stepp
18-1 Chapter 18 Binary Trees Data Structures and Design in Java © Rick Mercer.
(c) University of Washington20-1 CSC 143 Java Trees.
TREES From root to leaf. Trees  A tree is a non-linear collection  The elements are in a hierarchical arrangement  The elements are not accessible.
Binary Search Trees Chapter 7 Objectives
Lecture 19: Binary Trees reading: 17.1 – 17.3
CSE 373 Binary search trees; tree height and balance
Recursive Objects (Part 4)
Building Java Programs
CISC220 Fall 2009 James Atlas Lecture 13: Binary Trees.
Binary Search Tree (BST)
Trees.
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.
Building Java Programs Chapter 17
slides created by Marty Stepp and Alyssa Harding
Binary Trees Based on slides by Alyssa Harding & Marty Stepp
CSE 143 Lecture 20 Binary Search Trees, continued read 17.3
CSE 373: Data Structures and Algorithms
Design and Analysis of Algorithms
CSE 143 Lecture 25 Set ADT implementation; hashing read 11.2
Building Java Programs
CSC 143 Java Trees.
slides created by Marty Stepp
Trees.
Chapter 20: Binary Trees.
Trees.
CSE 373 Java Collection Framework reading: Weiss Ch. 3, 4.8
Non-Linear data structures
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Presentation transcript:

CSE 373 Data Structures and Algorithms Lecture 9: Set ADT / Trees

Set ADT 2  set: A collection that does not allow duplicates  We don't think of a set as having indices or any order  Basic set operations:  insert: Add an element to the set (order doesn't matter).  remove: Remove an element from the set.  search: Efficiently determine if an element is a member of the set. set.contains("to") true set "the" "of" "from" "to" "she" "you" "him" "why" "in" "down" "by" "if" set.contains("be")false

Sets in computer science 3  Databases:  Set of records in a table  Search engines:  Set of URLs/webpages on the Internet  Real world examples:  Set of all products for sale in a store inventory  Set of friends on Facebook  Set of addresses

Using Set s 4 List list = new ArrayList ();... Set set = new TreeSet (); // empty Set set2 = new HashSet (list);  Can construct an empty set, or one based on a given collection add( value ) adds the given value to the set contains( value ) returns true if the given value is found in this set remove( value ) removes the given value from the set clear() removes all elements of the set size() returns the number of elements in list isEmpty() returns true if the set's size is 0 toString() returns a string such as "[3, 42, -7, 15]"

More Set operations 5 addAll( collection ) adds all elements from the given collection to this set containsAll( coll ) returns true if this set contains every element from given set equals( set ) returns true if given other set contains the same elements iterator() returns an object used to examine set's contents removeAll( coll ) removes all elements in the given collection from this set retainAll( coll ) removes elements not found in given collection from this set toArray() returns an array of the elements in this set addAll retainAllremoveAll

Accessing elements in a Set 6 for ( type name : collection ) { statements ; }  Provides a clean syntax for looping over the elements of a Set, List, array, or other collection Set grades = new TreeSet ();... for (double grade : grades) { System.out.println("Student grade: " + grade); }  needed because sets have no indexes; can't get element i

Sets and ordering 7  HashSet : elements are stored in an unpredictable order Set names = new HashSet (); names.add("Jake"); names.add("Robert"); names.add("Marisa"); names.add("Kasey"); System.out.println(names); // [Kasey, Robert, Jake, Marisa]  TreeSet : elements are stored in their "natural" sorted order Set names = new TreeSet ();... // [Jake, Kasey, Marisa, Robert]

Set Implementation Runtimes 8 InsertRemoveSearch Unsorted array  (n) (n)(n) (n)(n) Sorted array  (log(n)+n)  (log(n)) Linked list  (n) (n)(n) (n)(n)

Trees 9  tree: A directed, acyclic structure of linked nodes.  directed: Has one-way links between nodes.  acyclic: No path wraps back around to the same node twice.  binary tree: One where each node has at most two children.  A binary tree can be defined as either:  empty ( null ), or  a root node that contains:  data  a left subtree and a right subtree  Either (or both) subtrees could be empty root

Trees in computer science 10  folders/files on a computer  family genealogy; organizational charts  AI: decision trees  compilers: parse tree  a = (b + c) * d;  cell phone T9 d+ *a = cb

Terminology 11  node: an object containing a data value and left/right children  root: topmost node of a tree  leaf: a node that has no children  branch: any internal node; neither the root nor a leaf  parent: a node that refers to this one  child: a node that this node refers to  sibling: a node with common parent root

StringTreeNode class 12 // A StringTreeNode object is one node in a binary tree of Strings. public class StringTreeNode { public String data; // data stored at this node public StringTreeNode left; // reference to left subtree public StringTreeNode right; // reference to right subtree // Constructs a leaf node with the given data. public StringTreeNode(String data) { this(data, null, null); } // Constructs a leaf or branch node with the given data and links. public StringTreeNode(String data, StringTreeNode left, StringTreeNode right) { this.data = data; this.left = left; this.right = right; }

Binary search trees 13  binary search tree ("BST"): a binary tree that is either:  empty ( null ), or  a root node R such that:  every element of R's left subtree contains data "less than" R's data,  every element of R's right subtree contains data "greater than" R's,  R's left and right subtrees are also binary search trees.  BSTs store their elements in sorted order, which is helpful for searching/sorting tasks overall root

Exercise 14  Which of the trees shown are legal binary search trees? xk qg m e b

Programming with Binary Trees 15  Many tree algorithms are recursive  Process current node, recurse on subtrees  Base case is usually empty tree ( null )  traversal: An examination of the elements of a tree.  A pattern used in many tree algorithms and methods  Common orderings for traversals:  pre-order: process root node, then its left/right subtrees  in-order: process left subtree, then root node, then right  post-order: process left/right subtrees, then root node

Tree Traversal (in order) 16 // Returns a String representation of StringTreeSet with elements in // their "natural order" (e.g., [Jake, Kasey, Marisa, Robert]). public String toString() { String str = "[" + toString(root); if (str.length() > 1) { str = str.substring(0, str.length()-2); } return str + "]"; } // recursive helper; in-order traversal private String toString(StringTreeNode root) { String str = ""; if (root != null) { str += toString(root.left); str += root.data + ", "; str += toString(root.right); } return str; }