The Singleton Pattern II

Slides:



Advertisements
Similar presentations
Chapter 7. Binary Search Trees
Advertisements

The Singleton Pattern II Recursive Linked Structures.
CSE 143 Lecture 18 Binary Trees read slides created by Marty Stepp and Hélène Martin
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary trees Reading: L&C 9.1 – 9.7.
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.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 26 Binary Search Trees.
Version TCSS 342, Winter 2006 Lecture Notes Trees Binary Trees Binary Search Trees.
CS 146: Data Structures and Algorithms June 18 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
1 CSC 321: Data Structures Fall 2012 Proofs & trees  proof techniques –direct proof, proof by contradiction, proof by induction  trees  tree recursion.
Thought for the Day “To become truly great, one has to stand with people, not above them.” – Charles de Montesquieu.
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary search trees.
CM0551 Exam Prep. What are an algorithm’s time and space complexity? (2 marks) Answer: The growth rate of the algorithm’s time requirement and the computer.
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.
Searching: Binary Trees and Hash Tables CHAPTER 12 6/4/15 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education,
Binary Trees Chapter 10. Introduction Previous chapter considered linked lists –nodes connected by two or more links We seek to organize data in a linked.
Trees CSCI Objectives Define trees as data structures Define the terms associated with trees Discuss the possible implementations of trees Analyze.
CMSC 341 Introduction to Trees. 8/3/2007 UMBC CMSC 341 TreeIntro 2 Tree ADT Tree definition  A tree is a set of nodes which may be empty  If not empty,
A Binary Search Tree Binary Search Trees.
CSE 143 Lecture 18 Binary Trees read slides created by Marty Stepp
Chapter 4 Data Structures ADT Examples  Stack  Queue  Trees.
Binary Search Trees Binary Search Trees (BST)  the tree from the previous slide is a special kind of binary tree called a binary.
Binary Search Trees Nilanjan Banerjee. 2 Goal of today’s lecture Learn about Binary Search Trees Discuss the first midterm.
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."
Lecture – Searching a Tree Neil Ghani University of Strathclyde.
CS-2852 Data Structures LECTURE 11 Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
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.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use at Midwestern State University Chapter.
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.
1 Binary Trees and Binary Search Trees Based on Dale & Co: Object-Oriented Data Structures using C++ (graphics)
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
Binary Search Trees (BST)
Traversing a tree means visiting each node in a specified order. There are different ways to traverse a tree. Tree Traversing Depth first search Pre-orderIn-orderPost-order.
Chapter 8 Binary Search Trees. Chapter 8: Binary Search Trees 8.1 – Trees 8.2 – The Logical Level 8.3 – The Application Level 8.4 – The Implementation.
CSE 143 Lecture 19 Binary Trees read slides created by Marty Stepp
CSE 373 Data Structures and Algorithms Lecture 9: Set ADT / Trees.
Binary Search Trees (BST) Let’s look at some pics …and some code.
(c) University of Washington20-1 CSC 143 Java Trees.
Binary Search Trees Chapter 7 Objectives
Lecture 19: Binary Trees reading: 17.1 – 17.3
Chapter 25 Binary Search Trees
CSE 373 Binary search trees; tree height and balance
Recursive Definition of Tree Structures
Recursive Objects (Part 4)
Building Java Programs
Week 6 - Wednesday CS221.
Binary Search Tree (BST)
Software Development Inheritance and Composition
Trees Tree nomenclature Implementation strategies Traversals
Trees.
Topic 18 Binary Search Trees
Binary Search Trees Definition (recursive):
Chapter 20: Binary 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.
Depict the Tree Structure in a picture
Topic 19 Binary Search Trees
Chapter 21: Binary Trees.
slides created by Alyssa Harding
Binary Trees Based on slides by Alyssa Harding & Marty Stepp
Trees.
Lecture 12 CS203 1.
Binary Trees and Binary Search Trees
CSE 143 Lecture 25 Set ADT implementation; hashing read 11.2
Building Java Programs
CSC 143 Java Trees.
Hashing based on slides by Marty Stepp
Trees.
Presentation transcript:

The Singleton Pattern II Recursive Linked Structures

BST Interface public interface BST<E extends Comparable<E>> extends Collection<E>{ // Traversals public List<E> preorder(); public List<E> inorder(); public List<E> postorder(); }

LinkedBST Class public class LinkedBST<E extends Comparable<E>> extends AbstractCollection<E> implements BST<E>{ private TreeNode<E> root = null; // Join the Collection family public boolean add(E item){return true;} public int size(){return 0;} public Iterator<E> iterator(){return this.preorder().iterator();} // Tree-like methods public boolean contains(Object item){return true;} public String toString(){return "";} // Traversals public List<E> preorder(){return new ArrayList<E>();} public List<E> inorder(){return new ArrayList<E>();} public List<E> postorder(){return new ArrayList<E>();} }

The TreeNode Class root “A” public class TreeNode<E extends Comparable<E>>{ public E data; public TreeNode<E> left, right; public TreeNode(E data){ this.data = data; this.left = null; this.right = null; } TreeNode<String> root = new TreeNode<>("A"); root “A”

Recursive Data Structures A recursive data structure is either a basic value, such as null, or contains a value of the same type of data structure The TreeNode class is a good example

Recursive Data Structures A linked structure is either empty (null) or contains a data value followed by other linked structures (left, right) of exactly the same form

Recursive Algorithms When processing a linked structure Handle the case of empty (null) Do something with the data value and then “recurse” with the linked structure (left, right) of exactly the same form

Some Recursive Patterns: Preorder Traversal if the linked structure is not empty process the value process the linked structure on the left process the linked structure on the right Note that there is an if statement rather than a while loop The process just quits when the base case (the end of the structure) is reached

Some Recursive Patterns: Preorder Traversal // In the LinkedBST class public List<E> preorder(){ List<E> list = new ArrayList<>(); preorderTraverse(this.root, list); return list; } private preorderTraverse(TreeNode<E> node, List<E> list){ if (node != null){ list.add(node.data); preorderTraverse(node.left, list); preorderTraverse(node.right, list);

Some Recursive Patterns: Search if the linked structure is empty return false else if the target equals the value return true else return the result of searching the rest of the linked structure

Some Recursive Patterns: Search public boolean contains(Object item){ return containsTraverse(this.root, (Comparable)item); } private boolean containsTraverse(TreeNode<E> node, Comparable<E> item){ if (node == null) return false; else{ int relation = item.compareTo(node.data); if (relation == 0) return true; else if (relation < 0) return containsTraverse(node.left, item); else return containsTraverse(node.right, item);

Some Recursive Patterns: size if the linked structure is empty return 0 else return 1 + the size of the rest of the linked structure

Some Recursive Patterns: size public int size(){ return sizeTraverse(this.root); } public int sizeTraverse(TreeNode<E> node){ if (node == null) return 0; else return 1 + sizeTraverse(node.left) + sizeTraverse(node.right);

Problems With Current Version Uses null for the case of an empty structure, so we must check for null with if statements in every method

Problems With Current Version A linked structure should be able to compute its own length But a recursive implementation of this would be pretty hard TreeNode n = new TreeNode("Ken"); int myLength = n.length();

Our View of the Current Version A linked structure is either empty (null) or contains a data value followed by another linked structure (next) of exactly the same form

A Better View A linked structure is either or empty (an empty node) compound (a compound node that contains a data value and another linked structure)

From Data to Algorithms Two concrete classes of nodes that implement the same interface The empty node’s methods handle the base cases The compound node’s methods handle the recursive ones Use polymorphism to make the choices (no explicit if statements in the code!)

From Data to Algorithms TreeNode CompoundNode EmptyNode

The TreeNode Interface public interface TreeNode<E extends Comparable<E>>{ public void preorder(List<E> list); public boolean contains(Comparable<E> item); public int size(); // etc. }

Node Classes, Etc. EmptyNode – The class of an empty node CompoundNode – The class of compound nodes EmptyNode.THE_EMPTY_NODE – The single instance used for any empty node

Using the New Implementation // In the class LinkedBST public List<E> preorder(){ List<E> list = new ArrayList<>(); this.root.preorderTraverse(list); return list; } public boolean contains(Object item){ return this.root.contains((Comparable)item); public int size(){ return this.root.size();

The EmptyNode Class The singleton instance is created just once public class EmptyNode<E> implements TreeNode<E>{ public void preorder(List<E> list){return;} public boolean contains(Comparable<E> item){return false} public int size(){return 0;} // etc. other instance methods go here // Constructor to prevent other instantiations private EmptyNode(){} static public final EmptyNode<E> THE_EMPTY_NODE = new EmptyNode<E>(); } The singleton instance is created just once

The CompoundNode Class public class CompoundNode<E> implements TreeNode<E>{ private E data; private CompoundNode<E> left, right; public CompoundNode(E data){ this.data = data; this.left = EmptyNode.THE_EMPTY_NODE; this.right = EmptyNode.THE_EMPTY_NODE; } public void preorder(List<E> list){ list.add(this.data) left.preorder(list); # Not recursion, but right.preorder(list); # polymorphism! public int size(){ return 1 + this.left.size() + this.right.size();

The CompoundNode Class public boolean contains(Comparable<E> item){ relation = item.compareTo(node.data); if (relation == 0) return true; else if (relation < 0) return this.left.contains(item); else return this.right.contains(item); }

Recursive Objects Instead of using explicit if statements to handle choices, try using polymorphism, which automatically handles them Divide the cases into different types of objects that obey the same interface (avoid using null for the simple case) Use simple objects for the base cases and recursive objects for the recursive ones