CSE 143 Lecture 20 Binary Search Trees continued; Tree Sets

Slides:



Advertisements
Similar presentations
CSE 143 Lecture 18 Binary Trees read slides created by Marty Stepp and Hélène Martin
Advertisements

CSE 143 Lecture 22: Advanced List Implementation (ADTs; interfaces; abstract classes; inner classes; generics; iterators)
CSE 143 Lecture 17 Binary Search Trees and Comparable slides created by Ethan Apter
CSE 143 Lecture 19 Binary Trees read slides created by Marty Stepp
Building Java Programs Inner classes, generics, abstract classes reading: 9.6, 15.4,
CSE 143 Lecture 19 Binary Search Trees read 17.3 slides created by Marty Stepp
Building Java Programs Binary Search Trees reading: 17.3 – 17.4.
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.
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 Nilanjan Banerjee. 2 Goal of today’s lecture Learn about Binary Search Trees Discuss the first midterm.
CSE 143 Lecture 24 Advanced collection classes (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, , slides.
Building Java Programs Bonus Slides Hashing. 2 Recall: ADTs (11.1) abstract data type (ADT): A specification of a collection of data and the operations.
1 CSE 331 Generics (Parametric Polymorphism) slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia
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 143 Lecture 20 Binary Search Trees read 17.3 slides created by Marty Stepp
CSE 143 Lecture 14 Interfaces; Abstract Data Types (ADTs)
Lecture 19: Binary Trees reading: 17.1 – 17.3
Building Java Programs Chapter 16
CSE 373 Binary search trees; tree height and balance
Building Java Programs
Building Java Programs
A tree set Our SearchTree class is essentially a set.
Binary Search Trees.
Building Java Programs
Building Java Programs Chapter 17
Rick Mercer, Allison Obourn, Marty Stepp
slides created by Marty Stepp and Alyssa Harding
Building Java Programs
Generics (Parametric Polymorphism)
CSE 143 Lecture 6 Interfaces; Complexity (Big-Oh)
Building Java Programs
Lecture 26: Advanced List Implementation
A tree set Our SearchTree class is essentially a set.
CSE 373 Data Structures and Algorithms
slides created by Marty Stepp and Hélène Martin
CSE 373: Data Structures and Algorithms
CSE 373: Data Structures and Algorithms
CSE 143 Lecture 20 Binary Search Trees, continued read 17.3
slides adapted from Marty Stepp and Hélène Martin
Building Java Programs
Building Java Programs
CSE 373: Data Structures and Algorithms
Binary Search Trees continued; Tree Sets
slides created by Marty Stepp
Lecture 21: Binary Search Trees; TreeSet
slides created by Alyssa Harding
CSE 143 Lecture 25 Set ADT implementation; hashing read 11.2
CSE 373: Data Structures and Algorithms
CSE 373 Data Structures and Algorithms
CSE 143 Lecture 25 Advanced collection classes
Building Java Programs
CSC 143 Binary Search Trees.
slides created by Marty Stepp
Building Java Programs
Lecture 21: Binary Search Trees; TreeSet
Binary Search Trees reading: 17.3 – 17.4
Hashing based on slides by Marty Stepp
slides created by Marty Stepp and Hélène Martin
Trees.
Lecture 7: Linked List Basics reading: 16.2
slides adapted from Marty Stepp
Binary Search Trees Based on slides by Marty Stepp & Alyssa Harding
CSE 143 Lecture 21 Advanced List Implementation
slides created by Marty Stepp and Hélène Martin
Presentation transcript:

CSE 143 Lecture 20 Binary Search Trees continued; Tree Sets read 17.3 - 17.5 slides created by Marty Stepp and Hélène Martin http://www.cs.washington.edu/143/

Recall: x = change(x) Methods that modify a tree should have the following pattern: input (parameter): old state of the node output (return): new state of the node In order to actually change the tree, you must reassign: node = change(node, parameters); node.left = change(node.left, parameters); node.right = change(node.right, parameters); overallRoot = change(overallRoot, parameters); node before parameter return your method node after

Add method // Adds the given value to this BST in sorted order. public void add(int value) { overallRoot = add(overallRoot, value); } private IntTreeNode add(IntTreeNode node, int value) { if (node == null) { node = new IntTreeNode(value); } else if (node.data > value) { node.left = add(node.left, value); } else if (node.data < value) { node.right = add(node.right, value); } // else a duplicate return node; 91 60 87 29 55 42 -3 overallRoot What about the case where root is unmodified?

Exercise Add a method getMin to the IntTree class that returns the minimum integer value from the tree. Assume that the elements of the IntTree constitute a legal binary search tree. Throw a NoSuchElementException if the tree is empty. int min = tree.getMin(); // -3 91 60 87 29 55 42 -3 overall root

Exercise solution // Returns the minimum value from this BST. // Throws a NoSuchElementException if the tree is empty. public int getMin() { if (overallRoot == null) { throw new NoSuchElementException(); } return getMin(overallRoot); private int getMin(IntTreeNode root) { if (root.left == null) { return root.data; } else { return getMin(root.left); 91 60 87 29 55 42 -3 overallRoot What about the case where root is unmodified?

Exercise Add a method remove to the IntTree class that removes a given integer value from the tree, if present. Remove the value in such a way as to maintain BST ordering. tree.remove(73); tree.remove(29); tree.remove(87); tree.remove(55); 91 60 87 29 55 42 36 overall root 73

Cases for removal 1 1. a leaf: replace with null 2. a node with a left child only: replace with left child 3. a node with a right child only: replace with right child 29 55 42 -3 overall root 29 55 42 overall root 29 42 overall root 42 overall root tree.remove(29); tree.remove(-3); tree.remove(55);

Cases for removal 2 4. a node with both children: replace with min from right (replacing with max from left would also work) 91 60 87 29 55 42 -3 overall root 91 87 29 60 42 -3 overall root tree.remove(55);

Exercise solution // Removes the given value from this BST, if it exists. public void remove(int value) { overallRoot = remove(overallRoot, value); } private IntTreeNode remove(IntTreeNode root, int value) { if (root == null) { return null; } else if (root.data > value) { root.left = remove(root.left, value); } else if (root.data < value) { root.right = remove(root.right, value); } else { // root.data == value; remove this node if (root.right == null) { return root.left; // no R child; replace w/ L } else if (root.left == null) { return root.right; // no L child; replace w/ R } else { // both children; replace w/ min from R root.data = getMin(root.right); root.right = remove(root.right, root.data); return root; What about the case where root is unmodified?

Searching BSTs The BSTs below contain the same elements. 14 19 4 11 9 What orders are "better" for searching? 14 19 4 11 overall root 9 7 6 4 7 11 6 overall root 19 14 9 19 11 14 6 9 7 4 overall root

Trees and balance balanced tree: One whose subtrees differ in height by at most 1 and are themselves balanced. A balanced tree of N nodes has a height of ~ log2 N. A very unbalanced tree can have a height close to N. The runtime of adding to / searching a BST is closely related to height. Some tree collections (e.g. TreeSet) contain code to balance themselves as new nodes are added. 19 7 14 6 9 8 4 overall root height = 4 (balanced)

Implementing a Tree Set read 17.4 - 17.5

A tree set Our SearchTree class is essentially a set. operations: add, remove, contains, size, isEmpty similar to the TreeSet class in java.util Let's actually turn it into a full set implementation. step 1: create ADT interface; implement it step 2: get rid of separate node class file step 3: make tree capable of storing any type of data (not just int) y m p e k g c overallRoot

Recall: ADTs (11.1) abstract data type (ADT): A specification of a collection of data and the operations that can be performed on it. Describes what a collection does, not how it does it. Java's collection framework describes ADTs with interfaces: Collection, Deque, List, Map, Queue, Set, SortedMap An ADT can be implemented in multiple ways by classes: ArrayList and LinkedList implement List HashSet and TreeSet implement Set LinkedList , ArrayDeque, etc. implement Queue

An IntSet interface // Represents a list of integers. public interface IntSet { public void add(int value); public boolean contains(int value); public boolean isEmpty(); public void remove(int value); public int size(); } public class IntTreeSet implements IntSet { ...

To get rid of our separate node file, we can use an inner class. Inner classes To get rid of our separate node file, we can use an inner class. inner class: A class defined inside of another class. inner classes are hidden from other classes (encapsulated) inner objects can access/modify the fields of the outer object

Inner class syntax // outer (enclosing) class public class name { ... // inner (nested) class private class name { } Only this file can see the inner class or make objects of it. Each inner object is associated with the outer object that created it, so it can access/modify that outer object's methods/fields. If necessary, can refer to outer object as OuterClassName.this

Recall: Type Parameters ArrayList<Type> name = new ArrayList<Type>(); When constructing a java.util.ArrayList, you specify the type of elements it will contain in < and >. ArrayList accepts a type parameter; it is a generic class. ArrayList<String> names = new ArrayList<String>(); names.add("Marty Stepp"); names.add("Helene Martin"); names.add(42); // compiler error

Implementing generics // a parameterized (generic) class public class name<Type> { ... } Forces any client that constructs your object to supply a type. Don't write an actual type such as String; the client does that. Instead, write a type variable name such as E or T. You can require multiple type parameters separated by commas. The rest of your class's code can refer to that type by name.

Generics and inner classes public class Foo<T> { private class Inner<T> {...} // incorrect private class Inner {...} // correct } If an outer class declares a type parameter, inner classes can also use that type parameter. The inner class should NOT redeclare the type parameter. (If you do, it will create a second type param with the same name.)

Issues with generic objects public class TreeSet<E> { ... public void example(E value1, E value2) { // BAD: value1 == value2 (they are objects) // GOOD: value1.equals(value2) // BAD: value1 < value2 // GOOD: value1.compareTo(value2) < 0 } When testing objects of type E for equality, must use equals When testing objects of type E for < or >, must use compareTo Problem: By default, compareTo doesn't compile! What's wrong!

Type constraints // a parameterized (generic) class public class name<Type extends Class/Interface> { ... } A type constraint forces the client to supply a type that is a subclass of a given superclass or implements a given interface. Then the rest of your code can assume that the type has all of the methods in that superclass / interface and can call them.

Generic set interface // Represents a list of values. public interface Set<E> { public void add(E value); public boolean isEmpty(); public boolean contains(E value); public void remove(E value); public int size(); } public class TreeSet<E extends Comparable<E>> implements Set<E> { ...