Binary Search Trees: Introduction - WordPress.com · 08/07/2016 · Binary Search Trees:...
Transcript of Binary Search Trees: Introduction - WordPress.com · 08/07/2016 · Binary Search Trees:...
![Page 1: Binary Search Trees: Introduction - WordPress.com · 08/07/2016 · Binary Search Trees: Introduction DanielKane Department of Computer Science and Engineering University of California,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528c422fed22084f7148ad/html5/thumbnails/1.jpg)
Binary Search Trees:Introduction
Daniel KaneDepartment of Computer Science and Engineering
University of California, San Diego
Data StructuresData Structures and Algorithms
![Page 2: Binary Search Trees: Introduction - WordPress.com · 08/07/2016 · Binary Search Trees: Introduction DanielKane Department of Computer Science and Engineering University of California,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528c422fed22084f7148ad/html5/thumbnails/2.jpg)
Learning Objectives
Provide examples of the sorts ofproblems we hope to solve with BinarySearch Trees.Show why data structures that we havealready covered are insufficient.
![Page 3: Binary Search Trees: Introduction - WordPress.com · 08/07/2016 · Binary Search Trees: Introduction DanielKane Department of Computer Science and Engineering University of California,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528c422fed22084f7148ad/html5/thumbnails/3.jpg)
Outline
1 Local Search
2 Attempts
![Page 4: Binary Search Trees: Introduction - WordPress.com · 08/07/2016 · Binary Search Trees: Introduction DanielKane Department of Computer Science and Engineering University of California,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528c422fed22084f7148ad/html5/thumbnails/4.jpg)
Dictionary SearchFind all words that start with some givenstring.
![Page 5: Binary Search Trees: Introduction - WordPress.com · 08/07/2016 · Binary Search Trees: Introduction DanielKane Department of Computer Science and Engineering University of California,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528c422fed22084f7148ad/html5/thumbnails/5.jpg)
Date Ranges
Find all emails received in a given period.
![Page 6: Binary Search Trees: Introduction - WordPress.com · 08/07/2016 · Binary Search Trees: Introduction DanielKane Department of Computer Science and Engineering University of California,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528c422fed22084f7148ad/html5/thumbnails/6.jpg)
Closest HeightFind the person in your class whose height isclosest to yours.
![Page 7: Binary Search Trees: Introduction - WordPress.com · 08/07/2016 · Binary Search Trees: Introduction DanielKane Department of Computer Science and Engineering University of California,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528c422fed22084f7148ad/html5/thumbnails/7.jpg)
Local Search
DefinitionA Local Search Datastructure stores anumber of elements each with a key comingfrom an ordered set. It supports operations:
RangeSearch(x , y): Returns allelements with keys between x and y .NearestNeighbors(z): Returns theelement with keys on either side of z .
![Page 8: Binary Search Trees: Introduction - WordPress.com · 08/07/2016 · Binary Search Trees: Introduction DanielKane Department of Computer Science and Engineering University of California,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528c422fed22084f7148ad/html5/thumbnails/8.jpg)
Example
RangeSearch(5, 12)
NearestNeighbors(3)
![Page 9: Binary Search Trees: Introduction - WordPress.com · 08/07/2016 · Binary Search Trees: Introduction DanielKane Department of Computer Science and Engineering University of California,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528c422fed22084f7148ad/html5/thumbnails/9.jpg)
Example
RangeSearch(5, 12)
NearestNeighbors(3)
![Page 10: Binary Search Trees: Introduction - WordPress.com · 08/07/2016 · Binary Search Trees: Introduction DanielKane Department of Computer Science and Engineering University of California,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528c422fed22084f7148ad/html5/thumbnails/10.jpg)
Example
RangeSearch(5, 12)
NearestNeighbors(3)
![Page 11: Binary Search Trees: Introduction - WordPress.com · 08/07/2016 · Binary Search Trees: Introduction DanielKane Department of Computer Science and Engineering University of California,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528c422fed22084f7148ad/html5/thumbnails/11.jpg)
Dynamic Data Structure
We would also like to be able to modify thedata structure as we go.
Insert(x): Adds a element with key x .Delete(x): Removes the element withkey x .
![Page 12: Binary Search Trees: Introduction - WordPress.com · 08/07/2016 · Binary Search Trees: Introduction DanielKane Department of Computer Science and Engineering University of California,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528c422fed22084f7148ad/html5/thumbnails/12.jpg)
Example
Insert(3)
Delete(10)
![Page 13: Binary Search Trees: Introduction - WordPress.com · 08/07/2016 · Binary Search Trees: Introduction DanielKane Department of Computer Science and Engineering University of California,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528c422fed22084f7148ad/html5/thumbnails/13.jpg)
Example
Insert(3)
Delete(10)
![Page 14: Binary Search Trees: Introduction - WordPress.com · 08/07/2016 · Binary Search Trees: Introduction DanielKane Department of Computer Science and Engineering University of California,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528c422fed22084f7148ad/html5/thumbnails/14.jpg)
Example
Insert(3)
Delete(10)
![Page 15: Binary Search Trees: Introduction - WordPress.com · 08/07/2016 · Binary Search Trees: Introduction DanielKane Department of Computer Science and Engineering University of California,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528c422fed22084f7148ad/html5/thumbnails/15.jpg)
ProblemIf an empty data structure is given thesecommands what does it output at the end?
Insert(3)Insert(8)Insert(5)Insert(10)Delete(8)Insert(12)NearestNeighbors(7)
![Page 16: Binary Search Trees: Introduction - WordPress.com · 08/07/2016 · Binary Search Trees: Introduction DanielKane Department of Computer Science and Engineering University of California,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528c422fed22084f7148ad/html5/thumbnails/16.jpg)
Answer
![Page 17: Binary Search Trees: Introduction - WordPress.com · 08/07/2016 · Binary Search Trees: Introduction DanielKane Department of Computer Science and Engineering University of California,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528c422fed22084f7148ad/html5/thumbnails/17.jpg)
Outline
1 Local Search
2 Attempts
![Page 18: Binary Search Trees: Introduction - WordPress.com · 08/07/2016 · Binary Search Trees: Introduction DanielKane Department of Computer Science and Engineering University of California,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528c422fed22084f7148ad/html5/thumbnails/18.jpg)
Hash TableRangeSearch: Impossible ×
NearestNeighbors: Impossible ×Insert: O(1) XDelete: O(1) X
![Page 19: Binary Search Trees: Introduction - WordPress.com · 08/07/2016 · Binary Search Trees: Introduction DanielKane Department of Computer Science and Engineering University of California,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528c422fed22084f7148ad/html5/thumbnails/19.jpg)
Hash TableRangeSearch: Impossible ×NearestNeighbors: Impossible ×
Insert: O(1) XDelete: O(1) X
![Page 20: Binary Search Trees: Introduction - WordPress.com · 08/07/2016 · Binary Search Trees: Introduction DanielKane Department of Computer Science and Engineering University of California,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528c422fed22084f7148ad/html5/thumbnails/20.jpg)
Hash TableRangeSearch: Impossible ×NearestNeighbors: Impossible ×Insert: O(1) X
Delete: O(1) X
![Page 21: Binary Search Trees: Introduction - WordPress.com · 08/07/2016 · Binary Search Trees: Introduction DanielKane Department of Computer Science and Engineering University of California,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528c422fed22084f7148ad/html5/thumbnails/21.jpg)
Hash TableRangeSearch: Impossible ×NearestNeighbors: Impossible ×Insert: O(1) XDelete: O(1) X
![Page 22: Binary Search Trees: Introduction - WordPress.com · 08/07/2016 · Binary Search Trees: Introduction DanielKane Department of Computer Science and Engineering University of California,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528c422fed22084f7148ad/html5/thumbnails/22.jpg)
ArrayRangeSearch: O(n) ×
NearestNeighbors: O(n) ×Insert: O(1) XDelete: O(1) X
![Page 23: Binary Search Trees: Introduction - WordPress.com · 08/07/2016 · Binary Search Trees: Introduction DanielKane Department of Computer Science and Engineering University of California,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528c422fed22084f7148ad/html5/thumbnails/23.jpg)
ArrayRangeSearch: O(n) ×NearestNeighbors: O(n) ×
Insert: O(1) XDelete: O(1) X
![Page 24: Binary Search Trees: Introduction - WordPress.com · 08/07/2016 · Binary Search Trees: Introduction DanielKane Department of Computer Science and Engineering University of California,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528c422fed22084f7148ad/html5/thumbnails/24.jpg)
ArrayRangeSearch: O(n) ×NearestNeighbors: O(n) ×Insert: O(1) X
Delete: O(1) X
![Page 25: Binary Search Trees: Introduction - WordPress.com · 08/07/2016 · Binary Search Trees: Introduction DanielKane Department of Computer Science and Engineering University of California,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528c422fed22084f7148ad/html5/thumbnails/25.jpg)
ArrayRangeSearch: O(n) ×NearestNeighbors: O(n) ×Insert: O(1) XDelete: O(1) X
![Page 26: Binary Search Trees: Introduction - WordPress.com · 08/07/2016 · Binary Search Trees: Introduction DanielKane Department of Computer Science and Engineering University of California,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528c422fed22084f7148ad/html5/thumbnails/26.jpg)
Sorted ArrayRangeSearch: O(log(n)) X
NearestNeighbors: O(log(n)) XInsert: O(n) ×Delete: O(n) ×
![Page 27: Binary Search Trees: Introduction - WordPress.com · 08/07/2016 · Binary Search Trees: Introduction DanielKane Department of Computer Science and Engineering University of California,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528c422fed22084f7148ad/html5/thumbnails/27.jpg)
Sorted ArrayRangeSearch: O(log(n)) XNearestNeighbors: O(log(n)) X
Insert: O(n) ×Delete: O(n) ×
![Page 28: Binary Search Trees: Introduction - WordPress.com · 08/07/2016 · Binary Search Trees: Introduction DanielKane Department of Computer Science and Engineering University of California,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528c422fed22084f7148ad/html5/thumbnails/28.jpg)
Sorted ArrayRangeSearch: O(log(n)) XNearestNeighbors: O(log(n)) XInsert: O(n) ×
Delete: O(n) ×
![Page 29: Binary Search Trees: Introduction - WordPress.com · 08/07/2016 · Binary Search Trees: Introduction DanielKane Department of Computer Science and Engineering University of California,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528c422fed22084f7148ad/html5/thumbnails/29.jpg)
Sorted ArrayRangeSearch: O(log(n)) XNearestNeighbors: O(log(n)) XInsert: O(n) ×Delete: O(n) ×
![Page 30: Binary Search Trees: Introduction - WordPress.com · 08/07/2016 · Binary Search Trees: Introduction DanielKane Department of Computer Science and Engineering University of California,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528c422fed22084f7148ad/html5/thumbnails/30.jpg)
Linked ListRangeSearch: O(n) ×
NearestNeighbors: O(n) ×Insert: O(1) XDelete: O(1) X
![Page 31: Binary Search Trees: Introduction - WordPress.com · 08/07/2016 · Binary Search Trees: Introduction DanielKane Department of Computer Science and Engineering University of California,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528c422fed22084f7148ad/html5/thumbnails/31.jpg)
Linked ListRangeSearch: O(n) ×NearestNeighbors: O(n) ×
Insert: O(1) XDelete: O(1) X
![Page 32: Binary Search Trees: Introduction - WordPress.com · 08/07/2016 · Binary Search Trees: Introduction DanielKane Department of Computer Science and Engineering University of California,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528c422fed22084f7148ad/html5/thumbnails/32.jpg)
Linked ListRangeSearch: O(n) ×NearestNeighbors: O(n) ×Insert: O(1) X
Delete: O(1) X
![Page 33: Binary Search Trees: Introduction - WordPress.com · 08/07/2016 · Binary Search Trees: Introduction DanielKane Department of Computer Science and Engineering University of California,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528c422fed22084f7148ad/html5/thumbnails/33.jpg)
Linked ListRangeSearch: O(n) ×NearestNeighbors: O(n) ×Insert: O(1) XDelete: O(1) X
![Page 34: Binary Search Trees: Introduction - WordPress.com · 08/07/2016 · Binary Search Trees: Introduction DanielKane Department of Computer Science and Engineering University of California,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528c422fed22084f7148ad/html5/thumbnails/34.jpg)
Need Something New
ProblemPrevious data structures won’t work. Weneed something new.