CSE 373: Data Structures and Algorithms

Slides:



Advertisements
Similar presentations
S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
Advertisements

CSE 143 Lecture 18 Binary Trees read slides created by Marty Stepp and Hélène Martin
CSE 143 Lecture 19 Binary Trees read slides created by Marty Stepp
CSE 143 Lecture 19 Binary Search Trees read 17.3 slides created by Marty Stepp
CSE 143 Lecture 7 Sets and Maps reading: ; 13.2 slides created by Marty Stepp
Version TCSS 342, Winter 2006 Lecture Notes Trees Binary Trees Binary Search Trees.
Data Structures - CSCI 102 Binary Tree In binary trees, each Node can point to two other Nodes and looks something like this: template class BTNode { public:
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.
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.
Building Java Programs Binary Search Trees; TreeSet.
CSE 143 Lecture 20 Binary Search Trees continued; Tree Sets read slides created by Marty Stepp and Hélène Martin
CSE 143 Lecture 18 Binary Trees read slides created by Marty Stepp
Binary Search Trees Binary Search Trees (BST)  the tree from the previous slide is a special kind of binary tree called a binary.
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.
CSE 143 Lecture 22 Binary Search Trees continued; Tree Sets read slides adapted from Marty Stepp and Hélène Martin
Building Java Programs Binary Trees reading: 17.1 – 17.3.
CSE 143 Lecture 21 Binary Search Trees, continued read slides created by Marty Stepp
CSE 143 Lecture 19 Binary Trees read slides created by Marty Stepp
CSE 373 Data Structures and Algorithms Lecture 9: Set ADT / Trees.
CSE 143 Lecture 20 Binary Search Trees read 17.3 slides created by Marty Stepp
Lecture 19: Binary Trees reading: 17.1 – 17.3
CSE 143 Lecture 20 Binary Search Trees continued; Tree Sets
CSE 373 Binary search trees; tree height and balance
Building Java Programs
Wednesday Notecards. Wednesday Notecards Wednesday Notecards.
Efficiency of in Binary Trees
UNIT III TREES.
Week 6 - Wednesday CS221.
Trees.
Trees.
COMP 103 Binary Search Trees.
Tree data structure.
Binary Trees, Binary Search Trees
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
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.
Week 2: 10/1-10/5 Monday Tuesday Wednesday Thursday Friday
Building Java Programs Chapter 17
slides created by Marty Stepp and Alyssa Harding
Building Java Programs
Tree data structure.
Find in a linked list? first last 7  4  3  8 NULL
Binary Trees Based on slides by Alyssa Harding & Marty Stepp
CSE 373: Data Structures and Algorithms
EE 422C Sets.
CSE 373: Data Structures and Algorithms
CSE 143 Lecture 20 Binary Search Trees, continued read 17.3
CSE 332: Data Abstractions Binary Search Trees
CSE 373 Data Structures and Algorithms
CSE 373: Data Structures and Algorithms
Binary Search Trees.
Exercise Write a program that counts the number of unique words in a large text file (say, Moby Dick or the King James Bible). Store the words in a collection.
Design and Analysis of Algorithms
Binary Search Trees continued; Tree Sets
Lecture 21: Binary Search Trees; TreeSet
CSE 143 Lecture 25 Set ADT implementation; hashing read 11.2
CSE 373 Data Structures and Algorithms
Building Java Programs
CSC 143 Binary Search Trees.
slides created by Marty Stepp
Building Java Programs
Lecture 21: Binary Search Trees; TreeSet
CSE 143 Lecture 15 Sets and Maps; Iterators reading: ; 13.2
Hashing based on slides by Marty Stepp
Trees.
CSE 373 Java Collection Framework reading: Weiss Ch. 3, 4.8
Lecture 9: Intro to Trees
Binary Search Trees Based on slides by Marty Stepp & Alyssa Harding
Tree (new ADT) Terminology: A tree is a collection of elements (nodes)
Presentation transcript:

CSE 373: Data Structures and Algorithms Lecture 9: Trees

Set ADT set: A collection that does not allow duplicates We don't think of a set as having indexes; we just add things to the set in general and don't worry about 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 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 email addresses

Using Sets 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]" List<String> list = new ArrayList<String>(); ... Set<Integer> set = new TreeSet<Integer>(); // empty Set<String> set2 = new HashSet<String>(list); can construct an empty set, or one based on a given collection

More Set operations addAll(collection) containsAll(coll) equals(set) retainAll removeAll 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

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

Sets and ordering HashSet : elements are stored in an unpredictable order Set<String> names = new HashSet<String>(); 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<String> names = new TreeSet<String>(); ... // [Jake, Kasey, Marisa, Robert]

Implementing Set ADT Insert Remove Search Unsorted array (n) (log(n)+n) (log(n) + n) (log(n)) Linked list

Trees 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. (The left and/or right subtree could be empty.) 7 6 3 2 1 5 4 root The definition of a tree shown here is recursive.

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

Terminology 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 7 6 3 2 1 5 4 root

StringTreeNode class // 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 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. 91 60 87 29 55 42 -3 overall root

Exercise Which of the trees shown are legal binary search trees? x k q m e b 18 10 11 5 8 4 2 7 20 42 21.3 8.1 9.6 1.9 7.2 -7 -1 -5

Programming with Binary Trees Many tree algorithms are recursive Process current node, recur on subtrees Base case is 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) // 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;

Implementing Set with BST Each Set entry adds a node to tree Node contains String element, references to left/right subtree Tree organized for binary search Quickly search or place to insert/remove element

Implementing Set with BST (cont.) public interface StringSet { public boolean add(String value); public boolean contains(String value); public void print(); public boolean remove(String value); public int size(); }

StringTreeSet class Client code talks to the // A StringTreeSet represents a Set of Strings. public class StringTreeSet { private StringTreeNode root; // null for an empty set methods } Client code talks to the StringTreeSet, not to the node objects inside it Methods of the StringTreeSet create and manipulate the nodes, their data and links between them "X" "M" "P" "I" "L" "J" "C" root

Set implementation: contains (search) public boolean contains(String value) { return contains(root, value); } private boolean contains(StringTreeNode root, String value) { if (root == null) { return false; // not in set } else if (root.data.compareTo(value) == 0) { return true; // found! } else if (root.data.compareTo(value) > 0) { return contains(root.left, value); // search left } else { return contains(root.right, value); // search right

Set implementation: insert Starts like contains Trace out path where node should be Add node as new leaf Don't change any other nodes or references Correct place to maintain binary search tree property

Set implementation: insert public boolean add(String value) { int oldSize = size(); this.root = add(root, value); return oldSize != size(); } private StringTreeNode add(StringTreeNode root, String value) { if (root == null) { root = new StringTreeNode(value); numElements++; } else if (root.data.compareTo(value) == 0) { return root; } else if (root.data.compareTo(value) > 0) { root.left = add(root.left, value); } else { root.right = add(root.right, value); }

Set implementation: remove Possible states for the node to be removed: a leaf: replace with null a node with a left child only: replace with left child a node with a right child only: replace with right child a node with both children: replace with min value from right "X" "M" "P" "I" "L" "J" "C" root "X" "P" "I" "M" "J" "C" root set.remove("L");

Set implementation: remove public boolean remove(String value) { int oldSize = size(); root = remove(root, value); return oldSize != size(); } private StringTreeNode remove(StringTreeNode root, String value) { if (root == null) { return root; } else if (root.data.compareTo(value) > 0) { root.left = remove(root.left, value); } else if (root.data.compareTo(value) < 0) { root.right = remove(root.right, value); } else { numElements--; if (root.right != null && root.left != null) { root.data = findMin(root.right).data; root.right = remove(root.right, root.data); } else if (root.right != null) { root = root.right; } else { root = root.left; } return root;

Evaluate Set as BST Space used Runtime Overhead of two references per entry BST adds nodes as needed; no excess capacity Runtime add, contains take time proportional to tree height height expected to be O(log N)

A Balanced Tree Values: 2 8 14 15 18 20 21 Order added: 15, 8, 2, 20, 21, 14, 18 Different tree structures possible Depends on order inserted 7 nodes, expected height log 7 ≈ 3 Perfectly balanced 21 18 20 8 15 14 2 root

Mostly Balanced Tree Same Values: 2 8 14 15 18 20 21 Order added: 20, 8, 21, 18, 14, 15, 2 Mostly balanced, height 4/5 root 15 14 21 8 20 18 2

Degenerate Tree Same Values: 2 8 14 15 18 20 21 Order added: 2, 8, 14, 15, 18, 20, 21 Totally unbalanced, height 7 root 2 8 14 15 18 20 21

Binary Trees: Some Numbers 2/15/2019 Binary Trees: Some Numbers Recall: height of a tree = length of longest path from the root to a leaf. For binary tree of height h: max # of leaves: max # of nodes: min # of leaves: min # of nodes: 2h, for perfect tree 2h 2(h + 1) - 1 2h+1 – 1, for perfect tree Some tree calculations, for tree of height h: - Max number of leaves (perfect tree): 2^h - Max number of nodes (perfect tree): 2^(h+1) - 1 - Min number of nodes/leaves (degenerate tree): h-1/1 - What fraction of the tree is located in the last level of a perfect tree? 1/2 - Average depth for N nodes: sqrt(N) (We won’t go into this, but if you take N nodes and assume all distinct trees of the nodes are equally likely, you get an average depth of SQRT(N). Is that bigger or smaller than log n? Bigger, so it’s not good enough!) 1 1, for “list” tree h + 1 h+1, for “list” tree We’re not going to do better than log(n) height, and we need something to keep us away from n.

Implementing Set ADT (Revisited) Insert Remove Search Unsorted array (1) (n) Sorted array (log(n)+n) (log(n) + n) (log(n)) Linked list BST (if balanced) O(log n)