Binary Search Trees (I)

Slides:



Advertisements
Similar presentations
Chapter 7. Binary Search Trees
Advertisements

2014-T2 Lecture 25 School of Engineering and Computer Science, Victoria University of Wellington  Lindsay Groves, Marcus Frean, Peter Andreae, and Thomas.
A Binary Search Tree Implementation Chapter 25 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
1 Section 9.2 Tree Applications. 2 Binary Search Trees Goal is implementation of an efficient searching algorithm Binary Search Tree: –binary tree in.
Chapter 19 - basic definitions - order statistics ( findkth( ) ) - balanced binary search trees - Java implementations Binary Search Trees 1CSCI 3333 Data.
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.
COMP 103 Introduction to Trees.
2014-T2 Lecture 24 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and Thomas.
2014-T2 Lecture 21 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, John Lewis,
2013-T2 Lecture 22 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and Thomas.
CS 162 Intro to Programming II Searching 1. Data is stored in various structures – Typically it is organized on the type of data – Optimized for retrieval.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington ArraySet and Binary Search.
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.
2014-T2 Lecture 19 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
2013-T2 Lecture 18 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
CSS446 Spring 2014 Nan Wang.  to study trees and binary trees  to understand how binary search trees can implement sets  to learn how red-black trees.
1 Binary Trees and Binary Search Trees Based on Dale & Co: Object-Oriented Data Structures using C++ (graphics)
2014-T2 Lecture 29 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae and Thomas.
2015-T2 Lecture 27 School of Engineering and Computer Science, Victoria University of Wellington  Lindsay Groves, Marcus Frean, Peter Andreae, and Thomas.
2015-T2 Lecture 21 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and Thomas.
2015-T2 Lecture 20 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, John Lewis,
2014-T2 Lecture 27 School of Engineering and Computer Science, Victoria University of Wellington  Lindsay Groves, Marcus Frean, Peter Andreae, and Thomas.
2015-T2 Lecture 19 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
2014-T2 Lecture 18 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
(c) University of Washington20c-1 CSC 143 Binary Search Trees.
A Binary Search Tree Implementation Chapter 25 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions.
2015-T2 Lecture 28 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae and Thomas.
(c) University of Washington20-1 CSC 143 Java Trees.
COMP 103 Binary Search Trees II Marcus Frean 2014-T2 Lecture 26
Topic 2: binary Trees COMP2003J: Data Structures and Algorithms 2
Chapter 12 – Data Structures
Trees Chapter 15.
COMP 53 – Week Fourteen Trees.
CSCE 3110 Data Structures & Algorithm Analysis
CSCE 3110 Data Structures & Algorithm Analysis
Fundamentals of Programming II Overview of Collections
COMP 103 Linked Structures Marcus Frean 2014-T2 Lecture 17
Recursive Objects (Part 4)
A Binary Search Tree Implementation
COMP 103 Introduction to Trees Thomas Kuehne 2013-T2 Lecture 19
CISC220 Fall 2009 James Atlas Lecture 13: Binary Trees.
Sections 8.7 – 8.8 Balancing a Binary Search Tree.
COMP 103 Tree Traversals Lindsay Groves 2016-T2 Lecture 22
Binary Search Tree Chapter 10.
COMP 103 SORTING Lindsay Groves 2016-T2 Lecture 26
COMP 103 Sorting with Binary Trees: Tree sort, Heap sort Alex Potanin
COMP 103 HeapSort Thomas Kuehne 2013-T1 Lecture 27
More complexity analysis & Binary Search
COMP 103 Binary Search Trees.
Depth-first vs breadth-first traversals
Chapter 16 Tree Implementations
Binary Search 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.
structures and their relationships." - Linus Torvalds
original list {67, 33,49, 21, 25, 94} pass { } {67 94}
Adapted from Pearson Education, Inc.
Searching.
Chapter 16 Tree Implementations
Podcast Ch18b Title: STree Class
A Robust Data Structure
Lecture 12 CS203 1.
Searching CLRS, Sections 9.1 – 9.3.
Binary Search Trees.
Linear Search Binary Search Tree
CSC 143 Java Trees.
CSC 143 Binary Search Trees.
Yan Shi CS/SE 2630 Lecture Notes
Data Structures Using C++ 2E
structures and their relationships." - Linus Torvalds
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Presentation transcript:

Binary Search Trees (I) COMP 103 Binary Search Trees (I) Lindsay Groves, Marcus Frean, Peter Andreae, and Thomas Kuehne, VUW Lindsay Groves School of Engineering and Computer Science, Victoria University of Wellington 2016-T2 Lecture 17

RECAP-TODAY RECAP Part I: Collections, Linear structures, Efficiency, Trees, Binary Trees, Recursion TODAY Part II: Binary search trees, General trees, Partially ordered trees/heaps, Sorting, Hashing Introduction to Binary Search Trees Using hierarchical access to unstructured data Reading: Chapter 17.1

Remember: Efficiency Challenge Unsorted Arrays access: O(n) modification: O(n) Sorted Arrays access: O(log(n)) Linear Linked Structures access: O(n) modification: O(1) [provided we know the position] Can we have the best of both worlds? yes, using trees!

Recall Divide & Conquer Optimal strategy when playing “20 questions” we want to eliminate as many as possible in each step but don’t know the answer beforehand... so ask a “question” whose answer you’re maximally uncertain about Mammal Egg Laying Feline Canine Bird Reptile Toby Tiger Lea Lion Bully Bulldog Tanja Tui Kurt Kaka Tim Turtle Sally Snake

Divide & Conquer Strategy ask a “question” whose answer you’re maximally uncertain about what does this remind you of? can we improve on the below? what numbers do we choose? <19 >19 <10 >10 <51 >51 1 5 13 20 50 52 99

Divide & Conquer Strategy use existing elements for decision nodes 1 5 13 20 50 52 99

Divide & Conquer Strategy use existing elements for decision nodes now we need less nodes can get “lucky” on the way down 20 5 52 1 13 50 99

Binary Search Again Searching “50” 20 5 52 1 13 50 99 1 5 13 20 50 52 mid low hi

“≤” if duplicates are allowed Binary Search Trees “≤” if duplicates are allowed Properties For every node: all items in left subtree < item item < all items in right subtree Ascending order obtained by ___________ traversal 20 5 52 1 13 50 99 51 8

BSTNode.contains Pseudocode (recursive) Very similar to binary search! public boolean contains (E value) { if value is at root return true; if value < item Look in left subtree, if non-empty else Look in right subtree, if non-empty return false }

BSTNode.contains Pseudocode (recursive) Follow path from root to place where it would be if it were there. public boolean contains (E value) { if value is at root return true; if value < item at root if there is a left child return whether left child contains value else if there is a right child return whether right child contains value return false } To be more flexible, use a Comparator -- later

BSTSet.contains Pseudocode (iterative) Follow path from root to place where it would be if it were there. public boolean contains(BSTNode node, E value) { while node points to a node if node.item equals value return true; if value < node.item set node to left child of node else set node to right child of node return false } modifying parameter variable…

BSTNode.add pseudocode (recursive) Notice we follow path just as in “contains”, to find the insertion point public boolean add (E value) { if value equals item return false // item already present if value < item // belongs on left if there is no left child insert as a new left child, and return true else call add(value) on left child else // belongs on right if there is no right child insert as a new right child, and return true else call add(value) on right child }

BSTNode.add pseudocode (iterative) public boolean add (E value) { forever if value equals item in parentNode // value already in set return false; if value < item in parentNode // belongs on left if parentNode has no left child insert as left child, increase count, and return true else set parentNode to left child of parentNode else // belongs on right if parentNode has no right child insert as right child, increase count, and return true else set parentNode to right child of parentNode

What’s next? Code these algorithms in Java Consider their cost How does the order of adding values affect the cost? How can we remove an item?