Binary Trees Like a list, a (binary) tree can be empty or non-empty. In class we will explore a state-based implementation, similar to the LRStruct You.

Slides:



Advertisements
Similar presentations
Chapter 7. Binary Search Trees
Advertisements

Treaps.  Good (logarithmic) performance with random data  Linear performance if the data is sorted ◦ Solutions – Splay Trees Amortized O( lg n) performance.
Binary Trees Chapter 6. Linked Lists Suck By now you realize that the title to this slide is true… By now you realize that the title to this slide is.
© 2006 Pearson Addison-Wesley. All rights reserved11 B-1 Chapter 11 (continued) Trees.
Talk tonight Richard Stallman Norton 112 6:00 PM.
How do visitors work? This set of slides traces through the execution of a visitor we’ve seen before (the lengthVisitor) on a short list. It shows how.
Tree Traversals A traversal is a way of walking the tree structure Some common traversals: –pre-order traversal –in-order traversal –post-order traversal.
1 Section 9.2 Tree Applications. 2 Binary Search Trees Goal is implementation of an efficient searching algorithm Binary Search Tree: –binary tree in.
Binary Trees Like a list, a (binary) tree can be empty or non-empty. In class we will explore a state-based implementation, similar to the LRS You are.
Building Java Programs Binary Search Trees reading: 17.3 – 17.4.
1 BST Trees A binary search tree is a binary tree in which every node satisfies the following: the key of every node in the left subtree is.
1 Joe Meehean.  Important and common problem  Given a collection, determine whether value v is a member  Common variation given a collection of unique.
CSCE 3110 Data Structures & Algorithm Analysis Binary Search Trees Reading: Chap. 4 (4.3) Weiss.
Sorted Array What is BigO for sorted list implemented as: ArrayList: – Search : – Insert(value) : – Remove(value) : LinkedList: – Search : – Insert(value)
© 2011 Pearson Addison-Wesley. All rights reserved 11 B-1 Chapter 11 (continued) Trees.
CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists.
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.
Binary Search Trees Binary Search Trees (BST)  the tree from the previous slide is a special kind of binary tree called a binary.
1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.
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."
CS 361 – Chapter 3 Sorted dictionary ADT Implementation –Sorted array –Binary search tree.
Chapter 11 B Trees. © 2004 Pearson Addison-Wesley. All rights reserved 11 B-2 The ADT Binary Search Tree A deficiency of the ADT binary tree which is.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use at Midwestern State University Chapter.
Computer Science and Software Engineering University of Wisconsin - Platteville 10. Binary Search Tree Yan Shi CS/SE 2630 Lecture Notes Partially adopted.
Binary Search Trees (BSTs) 18 February Binary Search Tree (BST) An important special kind of binary tree is the BST Each node stores some information.
Trees 3 The Binary Search Tree Section 4.3. Binary Search Tree Also known as Totally Ordered Tree Definition: A binary tree B is called a binary search.
1 Binary Trees and Binary Search Trees Based on Dale & Co: Object-Oriented Data Structures using C++ (graphics)
Binary Search Trees (BST)
Tree Data Structures. Heaps for searching Search in a heap? Search in a heap? Would have to look at root Would have to look at root If search item smaller.
COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables I.
2014-T2 Lecture 27 School of Engineering and Computer Science, Victoria University of Wellington  Lindsay Groves, Marcus Frean, Peter Andreae, and Thomas.
CSE 143 Lecture 20 Binary Search Trees read 17.3 slides created by Marty Stepp
(c) University of Washington20c-1 CSC 143 Binary Search Trees.
(c) University of Washington20-1 CSC 143 Java Trees.
CSCE 3110 Data Structures & Algorithm Analysis
CSCE 3110 Data Structures & Algorithm Analysis
Trees Chapter 11 (continued)
CSE 373 Binary search trees; tree height and balance
Binary Trees and Binary Search Trees
Recursive Objects (Part 4)
Trees Chapter 11 (continued)
BST Trees
CISC220 Fall 2009 James Atlas Lecture 13: Binary Trees.
Binary Search Tree (BST)
Chapter 10.1 Binary Search Trees
Trees.
COMP 103 Binary Search Trees.
Binary Search Trees.
Binary Search Trees Definition (recursive):
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
Binary Search Trees.
Rick Mercer, Allison Obourn, Marty Stepp
Find in a linked list? first last 7  4  3  8 NULL
A Robust Data Structure
Lecture 12 CS203 1.
Ch. 12 Tables and Priority Queues
Binary Search Trees Chapter 9 2/22/2019 B.Ramamurthy.
Binary Search Trees Chapter 9 2/24/2019 B.Ramamurthy.
CSE 373 Data Structures and Algorithms
CSC 143 Java Trees.
CSC 143 Binary Search Trees.
Building Java Programs
Yan Shi CS/SE 2630 Lecture Notes
Trees.
Trees Trees.
Binary Search Trees Based on slides by Marty Stepp & Alyssa Harding
Instructor: Dr. Michael Geiger Spring 2017 Lecture 30: Sorting & heaps
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Tree (new ADT) Terminology: A tree is a collection of elements (nodes)
Presentation transcript:

Binary Trees Like a list, a (binary) tree can be empty or non-empty. In class we will explore a state-based implementation, similar to the LRStruct You are expected to read about alternate implementation strategies in the textbook

Methods of a Binary Recursive Structure (BRStruct) method to grow tree: insertRoot method to shrink tree: removeRoot accessor/mutator pair for datum: setDatum/getDatum accessor/mutator pair for left: setLeft/getLeft accessor/mutator pair for right: setRight/getRight visitor support: execute

State transitions Empty  NonEmpty: insertRoot on an empty tree NonEmpty  Empty: removeRoot from a tree that is a leaf –A leaf is a tree both of whose children are empty. No other state transitions can occur.

Moreover… insertRoot throws an exception if called on a nonEmpty tree removeRoot throws an exception if called on a non-leaf tree Isn’t this very restrictive?

Yes…but It is very restrictive, but for good reasons: –What would it mean to add a root to a tree that already has one? –If you could remove the root from a tree with nonEmpty children, what would become of those children?

Usage of a BRS A BRStruct is not used “raw”, but is used to implement more specialized trees. Consider how we defined the SortedList in terms of the LRStruct: by composition. We will now explore how to define a sorted binary tree (a Binary Search Tree) by composition with a BRStruct.

Binary Search Tree (BST) A binary search tree (BST) is a binary tree which maintains its elements in order. The BST order condition requires that, for every non-empty tree, the value at the root is greater than every value in the tree’s left subtree, and less than every value in the tree’s right subtree.

Example 1 Does this tree satisfy the BST order condition? Fred WilmaBetty BarneyPebbles

No! Barney Fred < Wilma Fred WilmaBetty BarneyPebbles

Example 2 Does this tree satisfy the BST order condition? Fred WilmaBetty BarneyPebbles

Yes! Barney < Betty < Fred < Pebbles < Wilma Fred WilmaBetty BarneyPebbles

Example 3 Does this tree satisfy the BST order condition? Fred WilmaBetty BarneyPebbles

No! Betty > Barney < Fred < Pebbles < Wilma Fred WilmaBetty BarneyPebbles

Example 4 …but if we swap Betty & Barney, we restore order! Fred Wilma Betty Barney Pebbles

Operations on a BST public BST insert(E item) public BST remove(E item) public boolean member(E item) How do we support these? They don’t exist as methods on the BRStruct.

Visitors to the rescue! Visitors on the BRStruct have the same basic structure as on the LRStruct: they deal with two cases: emptyCase and nonEmpty Case. In our example: public BRStruct emptyCase(BRStruct host, E arg) public BRStruct nonEmptyCase(BRStruct host, E arg)

Example: toStringVisitor public class ToStringVisitor extends IAlgo { public static final ToStringVisitor SINGLETON = new ToStringVisitor(); private ToStringVisitor() {} public Object emptyCase(BRS host, Object input) { return "[]"; } public Object nonEmptyCase(BRS host, Object input) { return "[" + host.getLeft().execute(this, null) + " " + host.getRoot().toString() + " " + host.getRight().execute(this, null) + "]"; }

Back to the BST Let’s first consider the insert operation. We must consider two possibilities: –the underlying BRStruct is empty just insertRoot –the underlying BRStruct is nonEmpty we can’t insertRoot, because the BRStruct is nonEmpty compare new item with root – determine whether new item belongs in left or right subtree – insert recursively into correct subtree EXERCISE: DEFINE THIS VISITOR

A first cut at the visitor public class InsertVisitor implements IAlgo { public Object emptyCase(BRStruct host, E item) { return host.insertRoot(item); } public Object nonEmptyCase(BRStruct host, E item) { if (item.compareTo(host.getDatum()) < 0 ) { // item belongs in left subtree return host.getLeft().execute(this, item); } else if (item.compareTo(host.getRoot()) > 0 ) { // item belongs in right subtree return host.getRight().execute(this, item); } else { // item is already in tree (Note UNIQUENESS ASSUMPTION) return host; }

How about determining membership for an item? We must consider two possibilities: –the underlying BRStruct is empty just item was not found –the underlying BRStruct is nonEmpty compare new item with root – determine whether new item has been found, or whether it would be in left or right subtree – look recursively into correct subtree EXERCISE: DEFINE THIS VISITOR

A first cut at the visitor public class MemberVisitor implements IAlgo { public Boolean emptyCase(BRStruct host, E item) { return false; } public Boolean nonEmptyCase(BRStruct host, E item) { if (item.compareTo(host.getDatum()) < 0 ) { // item belongs in left subtree return host.getLeft().execute(this, item); } else if (item.compareTo(host.getRoot()) > 0 ) { // item belongs in right subtree return host.getRight().execute(this, item); } else { // item is in tree return true; }

Did you notice similarity? The structure of the insert and membership visitors was the same. A small number of details differed. Let’s unify! EXERCISE: DEFINE THIS VISITOR

The find visitor public class MemberVisitor implements IAlgo > { public BRStruct emptyCase(BRStruct host, E item) { // item is in not in tree, but would belong in host return host; } public BRStruct nonEmptyCase(BRStruct host, E item) { if (item.compareTo(host.getDatum()) < 0 ) { // item belongs in left subtree return host.getLeft().execute(this, item); } else if (item.compareTo(host.getRoot()) > 0 ) { // item belongs in right subtree return host.getRight().execute(this, item); } else { // item appears in tree as datum of host return host; }

Look at BST implementation insert, remove and member ALL make use of the same FindVisitor: –first find the insertionPoint, –then do the right thing.

The insert method public BST insert(E item) { BRStruct tree = _tree.execute(new FindVisitor (), item); only insert item into insertPoint if empty (duplicates ignored) return this; } EXERCISE: SOLVE PURPLE PROBLEM

The insert method public BST insert(E item) { BRStruct tree = _tree.execute(new FindVisitor (), item); tree.execute(new IAlgo () { public Object emptyCase(BRStruct host, E input) { host.insertRoot(input); return null; } public Object nonEmptyCase(BRStruct host, E input) { return null; } }, item); return this; }

The member method public BST member(E item) { BRStruct tree = _tree.execute(new FindVisitor (), item); return whether insertPoint is empty or nonEmpty } EXERCISE: SOLVE PURPLE PROBLEM

The member method public BST member(E item) { BRStruct tree = _tree.execute(new FindVisitor (), item); return tree.execute(new IAlgo () { public Boolean emptyCase(BRS host, Object _) { return false; } public Boolean nonEmptyCase(BRS host, Object _) { return true; } }, null); }

The remove method? Next time!