34
Binary Search Trees: Introduction Daniel Kane Department of Computer Science and Engineering University of California, San Diego Data Structures Data Structures and Algorithms

Binary Search Trees: Introduction - WordPress.com · 08/07/2016  · Binary Search Trees: Introduction DanielKane Department of Computer Science and Engineering University of California,

  • Upload
    others

  • View
    5

  • Download
    0

Embed Size (px)

Citation preview

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

Need Something New

ProblemPrevious data structures won’t work. Weneed something new.