Binary Search Trees A special case of a Binary Tree

Slides:



Advertisements
Similar presentations
Binary Search Trees Briana B. Morrison Adapted from Alan Eugenio.
Advertisements

Binary Search Trees CMSC 132 Chapter 8.1 Nelson Padua-Perez Bill Pugh.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 26 Binary Search Trees.
A Binary Search Tree Implementation Chapter Chapter Contents Getting Started An Interface for the Binary Search Tree Duplicate Entries Beginning.
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.
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.
Week 7 - Wednesday.  What did we talk about last time?  Recursive running time  Master Theorem  Introduction to trees.
CS-2851 Dr. Mark L. Hornick 1 Tree Maps and Tree Sets The JCF Binary Tree classes.
Binary Search Trees. BST Properties Have all properties of binary tree Items in left subtree are smaller than items in any node Items in right subtree.
CSCE 3110 Data Structures & Algorithm Analysis Binary Search Trees Reading: Chap. 4 (4.3) Weiss.
Dictionaries CS 105. L11: Dictionaries Slide 2 Definition The Dictionary Data Structure structure that facilitates searching objects are stored with search.
Chapter 19: Binary Trees Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
Balanced search trees: 2-3 trees. 2-3 trees allow us to process ordered lists in more efficient way than binary trees with an ordering property. Recall.
Trees Chapter 8. 2 Tree Terminology A tree consists of a collection of elements or nodes, organized hierarchically. The node at the top of a tree is called.
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.
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."
1 Trees, Trees, and More Trees. 2 By looking at forests of terms, awesome animations, and complete examples, we hope to get at the root of trees. Hopefully,
1 Searching Searching in a sorted linked list takes linear time in the worst and average case. Searching in a sorted array takes logarithmic time in the.
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.
Binary Search Trees CS340. Overview of a Binary Search Tree A set of nodes T is a binary search tree if either of the following is true T is empty If.
Dictionaries CS 110: Data Structures and Algorithms First Semester,
CS261 Data Structures Binary Search Trees Concepts.
A Binary Search Tree Implementation Chapter 25 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions.
AA Trees.
CSCE 3110 Data Structures & Algorithm Analysis
Data Structures – LECTURE Balanced trees
Chapter 25 Binary Search Trees
CSE 373 Binary search trees; tree height and balance
CSC 427: Data Structures and Algorithm Analysis
Recursive Objects (Part 4)
A Binary Search Tree Implementation
BST Trees
Week 6 - Wednesday CS221.
Chapter 10 Search Trees 10.1 Binary Search Trees Search Trees
Multiway search trees and the (2,4)-tree
Binary Search Tree Chapter 10.
Lecture 22 Binary Search Trees Chapter 10 of textbook
Trees.
Lecture 18. Basics and types of Trees
Hashing Exercises.
COMP 103 Binary Search Trees.
Binary Search Trees -Monty Python and The Holy Grail
ITEC 2620M Introduction to Data Structures
Topic 18 Binary Search Trees
A Binary Search Tree Implementation
Binary Tree and General Tree
Binary Search Trees.
(edited by Nadia Al-Ghreimil)
Topic 19 Binary Search Trees
Wednesday, April 18, 2018 Announcements… For Today…
CS 367 – Introduction to Data Structures
Search Sorted Array: Binary Search Linked List: Linear Search
Binary Search Trees (BST)
A Robust Data Structure
Lecture 12 CS203 1.
Binary Trees CS-2851 Dr. Mark L. Hornick.
Balanced Binary Search Trees
CMSC 202 Trees.
Binary Search Trees.
(edited by Nadia Al-Ghreimil)
Self-Balancing Search Trees
CSC 143 Binary Search Trees.
Mark Redekopp David Kempe
Trees.
Search Sorted Array: Binary Search Linked List: Linear Search
Trees Trees.
A Binary Search Tree Implementation
Balanced search trees: 2-3 trees.
CS210- Lecture 13 June 28, 2005 Agenda Heaps Complete Binary Tree
Presentation transcript:

Binary Search Trees A special case of a Binary Tree CS-2851 Dr. Mark L. Hornick

Binary Search Trees have some special properties Each element in the left subtree of an element is less than the root element of that subtree And each element in the right subtree is greater than the root element In a Set, no two elements are ever the same that is, no duplicate elements The left and right subtrees are thus themselves Binary Search Trees CS-2851 Dr. Mark L. Hornick

Not a Binary Search Tree Why? 50 80 30 100 20 40 60 CS-2851 Dr. Mark L. Hornick

Not a Binary Search Tree 50 80 30 100 20 40 60 “60” cannot be in the right branch of 80, since it is less than 80. CS-2851 Dr. Mark L. Hornick

A binary search tree need not be full, complete or a two-tree, but it could be any of those If a binary search tree is full or complete, its height is logarithmic (base 2) in n If a binary search tree is a chain, its height is linear in n. CS-2851 Dr. Mark L. Hornick

Another Linear Height example Even binary search trees that are not chains may have height that is linear in n. In the example at right, there are exactly two elements at level 1, level 2, etc. O(n)=n/2 CS-2851 Dr. Mark L. Hornick

Repeat: Each element in the left subtree is less than the root element of that subtree “less than” is obvious for integer elements What about other types of elements? We assume that the elements contained within the Entries of a BinarySearchTree are objects in a class that implements the Comparable interface: public interface Comparable<E> { int compareTo(E obj); } CS-2851 Dr. Mark L. Hornick

Adding to a Binary Search Tree Beginning at the root of the tree, repeat the following recursive pseudo-code to add target to the tree current = root // begin at the root add(current, target) { if target LESS THAN current add( left child, target) break; if target GREATER THAN current add( right child, target) break; If target == current // can’t have duplicates! break; } The inserted element always becomes a new leaf in the tree CS-2851 Dr. Mark L. Hornick

Add() time performance For adding an element, what is the worst case? What is the worst height? The worstTime (n) is linear in n if the tree is a chain What is the average height? The averageTime (n) is logarithmic in n if the tree is “bushy” CS-2851 Dr. Mark L. Hornick

Searching a Binary Search Tree Beginning at the root of the tree, recursively perform the following to determine if the tree contains the target current = root // begin at the root boolean contains( current, target) { if current EQUALS target return true; if target LESS THAN current AND left child not null return contains( left child, target); if target GREATER THAN current AND right child not null return contains( right child, target); } CS-2851 Dr. Mark L. Hornick

Search performance The averageTime(n) for a successful search: The average height of a complete binary search tree is logarithmic in n; so: averageTime(n) is O(log n). The worstTime(n) occurs if the tree is a chain. So worstTime(n) is ???? CS-2851 Dr. Mark L. Hornick