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."

Slides:



Advertisements
Similar presentations
Comp 122, Spring 2004 Binary Search Trees. btrees - 2 Comp 122, Spring 2004 Binary Trees  Recursive definition 1.An empty tree is a binary tree 2.A node.
Advertisements

S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
A Binary Search Tree Implementation Chapter Chapter Contents Getting Started An Interface for the Binary Search Tree Duplicate Entries Beginning.
AA Trees another alternative to AVL trees. Balanced Binary Search Trees A Binary Search Tree (BST) of N nodes is balanced if height is in O(log N) A balanced.
1 Trees most slides taken from Mike Scott, UT Austin.
A Binary Search Tree Implementation Chapter Chapter Contents Getting Started An Interface for the Binary Search Tree Duplicate Entries Beginning.
1 Binary Search Trees (BSTs). 2 Consider the search operation FindKey (): find an element of a particular key value in a binary tree. This operation takes.
Chapter 13 Binary Search Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Define a binary search tree abstract.
CS21, Tia Newhall Binary Search Trees (BST) 1.Hierarchical data structure with a single pointer to root node 2.Each node has at most two child nodes (a.
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.
CS 146: Data Structures and Algorithms June 18 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
By : Budi Arifitama Pertemuan ke Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation.
CSCE 3110 Data Structures & Algorithm Analysis Binary Search Trees Reading: Chap. 4 (4.3) Weiss.
Trees, Binary Search Trees, Recursion, Project 2 Bryce Boe 2013/08/01 CS24, Summer 2013 C.
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.
© 2011 Pearson Addison-Wesley. All rights reserved 11 B-1 Chapter 11 (continued) Trees.
Chapter 19: Binary Trees Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
Binary Search Trees Binary Search Trees (BST)  the tree from the previous slide is a special kind of binary tree called a binary.
Binary SearchTrees [CLRS] – Chap 12. What is a binary tree ? A binary tree is a linked data structure in which each node is an object that contains following.
1 Binary Search Trees (BSTs) What is a Binary search tree? Why Binary search trees? Binary search tree implementation Insertion in a BST Deletion from.
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 Tree Traversal Methods. How are they different from Binary Trees?  In computer science, a binary tree is a tree data structure in which.
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
Lec 15 Oct 18 Binary Search Trees (Chapter 5 of text)
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.
10 Binary-Search-Tree Data Structure  Binary-trees and binary-search-trees  Searching  Insertion  Deletion  Traversal  Implementation of sets using.
Chapter 19: Binary Search Trees or How I Learned to Love AVL Trees and Balance The Tree Group 6: Tim Munn.
David Stotts Computer Science Department UNC Chapel Hill.
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.
Week 10 - Friday.  What did we talk about last time?  Graph representations  Adjacency matrix  Adjacency lists  Depth first search.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
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.
1 Introduction to trees Instructor: Dimitrios Kosmopoulos.
Week 15 – Wednesday.  What did we talk about last time?  Review up to Exam 1.
2014-T2 Lecture 27 School of Engineering and Computer Science, Victoria University of Wellington  Lindsay Groves, Marcus Frean, Peter Andreae, and Thomas.
BSTs, AVL Trees and Heaps Ezgi Shenqi Bran. What to know about Trees? Height of a tree Length of the longest path from root to a leaf Height of an empty.
Binary Search Trees Chapter 7 Objectives
CSCE 3110 Data Structures & Algorithm Analysis
CSCE 3110 Data Structures & Algorithm Analysis
Trees Chapter 11 (continued)
Recursive Objects (Part 4)
Trees Chapter 11 (continued)
A Binary Search Tree Implementation
BST Trees
Week 6 - Wednesday CS221.
Binary Search Tree Chapter 10.
Trees.
COMP 103 Binary Search Trees.
Chapter 16 Tree Implementations
Binary Search Trees -Monty Python and The Holy Grail
Topic 18 Binary Search Trees
Binary Search Trees.
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
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.
Topic 19 Binary Search Trees
Lec 12 March 9, 11 Mid-term # 1 (March 21?)
Binary Search Tree AVL Tree
Topic 18 Binary Trees "A tree may grow a thousand feet tall, but its leaves will return to its roots." -Chinese Proverb.
Chapter 16 Tree Implementations
Binary Search Trees A special case of a Binary Tree
Non-Linear Structures
CSC 143 Binary Search Trees.
Data Structures II AP Computer Science
Trees.
Search Sorted Array: Binary Search Linked List: Linear Search
Presentation transcript:

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." - Monty Python and The Holy Grail

CS314 2 The Problem with Linked Lists  Accessing a item from a linked list takes O(N) time for an arbitrary element  Binary trees can improve upon this and reduce access to O(log N) time for the average case  Expands on the binary search technique and allows insertions and deletions  Worst case degenerates to O(N) but this can be avoided by using balanced trees (AVL, Red-Black) Binary Search Trees

CS314 3 Binary Search Trees  A binary search tree is a binary tree in which every node's left subtree holds values less than the node's value, and every right subtree holds values greater than the node's value.  A new node is added as a leaf. parent left child right child root <> Binary Search Trees

BST Insertion  Add the following values one at a time to an initially empty binary search tree using the traditional, naïve algorithm:  What is the resulting tree? CS314Binary Search Trees 4

Traversals  What is the result of an inorder traversal of the resulting tree?  How could a preorder traversal be useful? CS314Binary Search Trees 5

Clicker Question 1  After adding N distinct elements in random order to a Binary Search Tree what is the expected height of the tree? A.O(N 1/2 ) B.O(logN) C.O(N) D.O(NlogN) E. O(N 2 ) CS314 6 Binary Search Trees

CS314 7 Node for Binary Search Trees public class BSTNode { private Comparable myData; private BSTNode myLeft; private BSTNode myRightC; public BinaryNode(E item) {myData = item;} public E getValue() {return myData;} public BinaryNode getLeft() {return myLeft;} public BinaryNode getRight() {return myRight;} public void setLeft(BSTNode b) {myLeft = b;} // setRight not shown } Binary Search Trees

CS314 8 Worst Case Performance  Insert the following values into an initially empty binary search tree using the traditional, naïve algorithm:  What is the height of the tree?  What is the worst case height of a BST? Binary Search Trees

CS314 9 More on Implementation  Many ways to implement BSTs  Using nodes is just one and even then many options and choices public class BinarySearchTree > { private BSTNode root; private int size; Binary Search Trees

CS Add an Element, Recursive Binary Search Trees

CS Add an Element, Iterative Binary Search Trees

Clicker Question 2  What are the best case and worst case order to add N distinct elements, one at a time, to an initially empty binary search tree? BestWorst A.O(N)O(N) B.O(NlogN)O(NlogN) C.O(N)O(NlogN) D.O(NlogN)O(N 2 ) E.O(N 2 )O(N 2 ) CS Binary Search Trees

CS Performance of Binary Trees  For the three core operations (add, access, remove) a binary search tree (BST) has an average case performance of O(log N)  Even when using the naïve insertion / removal algorithms –no checks to maintain balance –balance achieved based on the randomness of the data inserted Binary Search Trees

CS Remove an Element  Three cases –node is a leaf, 0 children (easy) –node has 1 child (easy) –node has 2 children (interesting) Binary Search Trees

CS Properties of a BST  The minimum value is in the left most node  The maximum value is in the right most node –useful when removing an element from the BST Binary Search Trees

CS Alternate Implementation  In class examples of dynamic data structures have relied on null terminated ends. –Use null to show end of list, no children  Alternative form –use structural recursion and polymorphism Binary Search Trees

CS BST Interface public interface BST { public int size(); public boolean contains(Comparable obj); public boolean add(Comparable obj); } Binary Search Trees

CS EmptyBST public class EmptyBST implements BST { private static EmptyBST theOne = new EmptyBST(); private EmptyBST(){} public static EmptyBST getEmptyBST(){ return theOne; } public NEBST add(Comparable obj) { return new NEBST(obj); } public boolean contains(Comparable obj) { return false; } public int size() { return 0; } } Binary Search Trees

CS Non Empty BST – Part 1 public class NEBST implements BST { private Comparable data; private BST left; private BST right; public NEBST(Comparable d){ data = d; right = EmptyBST.getEmptyBST(); left = EmptyBST.getEmptyBST(); } public BST add(Comparable obj) { int val = obj.compareTo( data ); if( val < 0 ) left = left.add( obj ); else if( val > 0 ) right = right.add( obj ); return this; } Binary Search Trees

CS Non Empty BST – Part 2 public boolean contains(Comparable obj){ int val = obj.compareTo(data); if( val == 0 ) return true; else if (val < 0) return left.contains(obj); else return right.contains(obj); } public int size() { return 1 + left.size() + right.size(); } Binary Search Trees