(balanced) binary search treeslecture 5 scheduling and binary search trees 6.006 fall 2011 lecture...

79
Introduction Radboud University Nijmegen (Balanced) Binary Search Trees Alexandra Silva [email protected] http://www.cs.ru.nl/ ~ alexandra Institute for Computing and Information Sciences Radboud University Nijmegen 14th October 2014 Alexandra 14th October 2014 Lesson 6 1 / 21

Upload: others

Post on 17-Jul-2020

14 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: (Balanced) Binary Search TreesLecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011 Lecture 5: Scheduling and Binary Search Trees Lecture Overview ¥ Runway reservation system

Introduction Radboud University Nijmegen

(Balanced) Binary Search Trees

Alexandra Silva

[email protected]

http://www.cs.ru.nl/~alexandra

Institute for Computing and Information SciencesRadboud University Nijmegen

14th October 2014

Alexandra 14th October 2014 Lesson 6 1 / 21

Page 2: (Balanced) Binary Search TreesLecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011 Lecture 5: Scheduling and Binary Search Trees Lecture Overview ¥ Runway reservation system

Introduction Radboud University Nijmegen

Recap

Up to now

• Basic algorithm analysis: sequential, recursive.

• O-notation, simplified model of computation.

• Dynamic Programming.

• This week: data structures, importance of a good DS.

Alexandra 14th October 2014 Lesson 6 2 / 21

Page 3: (Balanced) Binary Search TreesLecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011 Lecture 5: Scheduling and Binary Search Trees Lecture Overview ¥ Runway reservation system

Introduction Radboud University Nijmegen

Motivation

• Airport runaway reservation system

• Simplified scenario: only one runaway available.

• Safety issues associated with landing planes, and planes takingoff.

• Build these constraints in– and the checks for theseconstraints– into your data structure.

Alexandra 14th October 2014 Lesson 6 3 / 21

Page 4: (Balanced) Binary Search TreesLecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011 Lecture 5: Scheduling and Binary Search Trees Lecture Overview ¥ Runway reservation system

Introduction Radboud University Nijmegen

Parameters

• Reserve request: specifies landing time t.

• Add t to the set R of landing times if no other landings arescheduled within k minutes.

• k can vary: let’s assume it is statically set (e.g. 3 min).

• After landing, remove request from R.

What operations do we need in the data structure?

• Adding requests. If they satisfy constraint!

• Removing requests.

• Notion of time, checks every m seconds to update thestructure.

• Nutshell: we need a data structure that allows for insertionand removal of elements.

• Additional requirement: operations in O(lg n)

Alexandra 14th October 2014 Lesson 6 4 / 21

Page 5: (Balanced) Binary Search TreesLecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011 Lecture 5: Scheduling and Binary Search Trees Lecture Overview ¥ Runway reservation system

Introduction Radboud University Nijmegen

Parameters

• Reserve request: specifies landing time t.

• Add t to the set R of landing times if no other landings arescheduled within k minutes.

• k can vary: let’s assume it is statically set (e.g. 3 min).

• After landing, remove request from R.

What operations do we need in the data structure?

• Adding requests.

If they satisfy constraint!

• Removing requests.

• Notion of time, checks every m seconds to update thestructure.

• Nutshell: we need a data structure that allows for insertionand removal of elements.

• Additional requirement: operations in O(lg n)

Alexandra 14th October 2014 Lesson 6 4 / 21

Page 6: (Balanced) Binary Search TreesLecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011 Lecture 5: Scheduling and Binary Search Trees Lecture Overview ¥ Runway reservation system

Introduction Radboud University Nijmegen

Parameters

• Reserve request: specifies landing time t.

• Add t to the set R of landing times if no other landings arescheduled within k minutes.

• k can vary: let’s assume it is statically set (e.g. 3 min).

• After landing, remove request from R.

What operations do we need in the data structure?

• Adding requests. If they satisfy constraint!

• Removing requests.

• Notion of time, checks every m seconds to update thestructure.

• Nutshell: we need a data structure that allows for insertionand removal of elements.

• Additional requirement: operations in O(lg n)

Alexandra 14th October 2014 Lesson 6 4 / 21

Page 7: (Balanced) Binary Search TreesLecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011 Lecture 5: Scheduling and Binary Search Trees Lecture Overview ¥ Runway reservation system

Introduction Radboud University Nijmegen

Parameters

• Reserve request: specifies landing time t.

• Add t to the set R of landing times if no other landings arescheduled within k minutes.

• k can vary: let’s assume it is statically set (e.g. 3 min).

• After landing, remove request from R.

What operations do we need in the data structure?

• Adding requests. If they satisfy constraint!

• Removing requests.

• Notion of time, checks every m seconds to update thestructure.

• Nutshell: we need a data structure that allows for insertionand removal of elements.

• Additional requirement: operations in O(lg n)

Alexandra 14th October 2014 Lesson 6 4 / 21

Page 8: (Balanced) Binary Search TreesLecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011 Lecture 5: Scheduling and Binary Search Trees Lecture Overview ¥ Runway reservation system

Introduction Radboud University Nijmegen

Example

Lecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011

Lecture 5: Scheduling and Binary Search

Trees

Lecture Overview

• Runway reservation system

– Definition

– How to solve with lists

• Binary Search Trees

– Operations

Readings

CLRS Chapter 10, 12.1-3

Runway Reservation System

• Airport with single (very busy) runway (Boston 6 ! 1)

• “Reservations” for future landings

• When plane lands, it is removed from set of pending events

• Reserve req specify “requested landing time” t

• Add t to the set if no other landings are scheduled within k minutes either way.

Assume that k can vary.

– else error, don’t schedule

Example

37 41 46 49 56time (mins)

now x x x x

Figure 1: Runway Reservation System Example

Let R denote the reserved landing times: R = (41, 46, 49, 56) and k = 3

1

What happens for

• 44

not allowed (because 46 is in R)

• 53 X

• 20 not allowed (time has passed)

• How do we do it efficiently in O(lg n)?

Alexandra 14th October 2014 Lesson 6 5 / 21

Page 9: (Balanced) Binary Search TreesLecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011 Lecture 5: Scheduling and Binary Search Trees Lecture Overview ¥ Runway reservation system

Introduction Radboud University Nijmegen

Example

Lecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011

Lecture 5: Scheduling and Binary Search

Trees

Lecture Overview

• Runway reservation system

– Definition

– How to solve with lists

• Binary Search Trees

– Operations

Readings

CLRS Chapter 10, 12.1-3

Runway Reservation System

• Airport with single (very busy) runway (Boston 6 ! 1)

• “Reservations” for future landings

• When plane lands, it is removed from set of pending events

• Reserve req specify “requested landing time” t

• Add t to the set if no other landings are scheduled within k minutes either way.

Assume that k can vary.

– else error, don’t schedule

Example

37 41 46 49 56time (mins)

now x x x x

Figure 1: Runway Reservation System Example

Let R denote the reserved landing times: R = (41, 46, 49, 56) and k = 3

1

What happens for

• 44 not allowed (because 46 is in R)

• 53

X

• 20 not allowed (time has passed)

• How do we do it efficiently in O(lg n)?

Alexandra 14th October 2014 Lesson 6 5 / 21

Page 10: (Balanced) Binary Search TreesLecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011 Lecture 5: Scheduling and Binary Search Trees Lecture Overview ¥ Runway reservation system

Introduction Radboud University Nijmegen

Example

Lecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011

Lecture 5: Scheduling and Binary Search

Trees

Lecture Overview

• Runway reservation system

– Definition

– How to solve with lists

• Binary Search Trees

– Operations

Readings

CLRS Chapter 10, 12.1-3

Runway Reservation System

• Airport with single (very busy) runway (Boston 6 ! 1)

• “Reservations” for future landings

• When plane lands, it is removed from set of pending events

• Reserve req specify “requested landing time” t

• Add t to the set if no other landings are scheduled within k minutes either way.

Assume that k can vary.

– else error, don’t schedule

Example

37 41 46 49 56time (mins)

now x x x x

Figure 1: Runway Reservation System Example

Let R denote the reserved landing times: R = (41, 46, 49, 56) and k = 3

1

What happens for

• 44 not allowed (because 46 is in R)

• 53 X

• 20

not allowed (time has passed)

• How do we do it efficiently in O(lg n)?

Alexandra 14th October 2014 Lesson 6 5 / 21

Page 11: (Balanced) Binary Search TreesLecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011 Lecture 5: Scheduling and Binary Search Trees Lecture Overview ¥ Runway reservation system

Introduction Radboud University Nijmegen

Example

Lecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011

Lecture 5: Scheduling and Binary Search

Trees

Lecture Overview

• Runway reservation system

– Definition

– How to solve with lists

• Binary Search Trees

– Operations

Readings

CLRS Chapter 10, 12.1-3

Runway Reservation System

• Airport with single (very busy) runway (Boston 6 ! 1)

• “Reservations” for future landings

• When plane lands, it is removed from set of pending events

• Reserve req specify “requested landing time” t

• Add t to the set if no other landings are scheduled within k minutes either way.

Assume that k can vary.

– else error, don’t schedule

Example

37 41 46 49 56time (mins)

now x x x x

Figure 1: Runway Reservation System Example

Let R denote the reserved landing times: R = (41, 46, 49, 56) and k = 3

1

What happens for

• 44 not allowed (because 46 is in R)

• 53 X

• 20 not allowed (time has passed)

• How do we do it efficiently in O(lg n)?

Alexandra 14th October 2014 Lesson 6 5 / 21

Page 12: (Balanced) Binary Search TreesLecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011 Lecture 5: Scheduling and Binary Search Trees Lecture Overview ¥ Runway reservation system

Introduction Radboud University Nijmegen

Example

Lecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011

Lecture 5: Scheduling and Binary Search

Trees

Lecture Overview

• Runway reservation system

– Definition

– How to solve with lists

• Binary Search Trees

– Operations

Readings

CLRS Chapter 10, 12.1-3

Runway Reservation System

• Airport with single (very busy) runway (Boston 6 ! 1)

• “Reservations” for future landings

• When plane lands, it is removed from set of pending events

• Reserve req specify “requested landing time” t

• Add t to the set if no other landings are scheduled within k minutes either way.

Assume that k can vary.

– else error, don’t schedule

Example

37 41 46 49 56time (mins)

now x x x x

Figure 1: Runway Reservation System Example

Let R denote the reserved landing times: R = (41, 46, 49, 56) and k = 3

1

What happens for

• 44 not allowed (because 46 is in R)

• 53 X

• 20 not allowed (time has passed)

• How do we do it efficiently in O(lg n)?

Alexandra 14th October 2014 Lesson 6 5 / 21

Page 13: (Balanced) Binary Search TreesLecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011 Lecture 5: Scheduling and Binary Search Trees Lecture Overview ¥ Runway reservation system

Introduction Radboud University Nijmegen

Example

Lecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011

Lecture 5: Scheduling and Binary Search

Trees

Lecture Overview

• Runway reservation system

– Definition

– How to solve with lists

• Binary Search Trees

– Operations

Readings

CLRS Chapter 10, 12.1-3

Runway Reservation System

• Airport with single (very busy) runway (Boston 6 ! 1)

• “Reservations” for future landings

• When plane lands, it is removed from set of pending events

• Reserve req specify “requested landing time” t

• Add t to the set if no other landings are scheduled within k minutes either way.

Assume that k can vary.

– else error, don’t schedule

Example

37 41 46 49 56time (mins)

now x x x x

Figure 1: Runway Reservation System Example

Let R denote the reserved landing times: R = (41, 46, 49, 56) and k = 3

1

What happens for

• 44 not allowed (because 46 is in R)

• 53 X

• 20 not allowed (time has passed)

• How do we do it efficiently in O(lg n)?

Alexandra 14th October 2014 Lesson 6 5 / 21

Page 14: (Balanced) Binary Search TreesLecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011 Lecture 5: Scheduling and Binary Search Trees Lecture Overview ¥ Runway reservation system

Introduction Radboud University Nijmegen

Options for data structures

• Unsorted list/array: good?

most operations are in O(n).Insertion can be in O(1)

• Sorted list: Appending and sorting takes O(nlgn) time.Insertion takes O(n) time. A k minute check can be done inO(1) once the insertion point is found.

• Sorted array: Binary search to find place to insert in O(lgn)time. Looks good? Insertion is still in O(n).

• We almost had it in the sorted list/array.

• Key point: We need fast insertion into a sorted list.

• What if the time would be in whole minutes (instead ofallowing 56.4, etc)?

• A large array indexed by time!

Alexandra 14th October 2014 Lesson 6 6 / 21

Page 15: (Balanced) Binary Search TreesLecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011 Lecture 5: Scheduling and Binary Search Trees Lecture Overview ¥ Runway reservation system

Introduction Radboud University Nijmegen

Options for data structures

• Unsorted list/array: good? most operations are in O(n).Insertion can be in O(1)

• Sorted list: Appending and sorting takes O(nlgn) time.Insertion takes O(n) time. A k minute check can be done inO(1) once the insertion point is found.

• Sorted array: Binary search to find place to insert in O(lgn)time. Looks good? Insertion is still in O(n).

• We almost had it in the sorted list/array.

• Key point: We need fast insertion into a sorted list.

• What if the time would be in whole minutes (instead ofallowing 56.4, etc)?

• A large array indexed by time!

Alexandra 14th October 2014 Lesson 6 6 / 21

Page 16: (Balanced) Binary Search TreesLecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011 Lecture 5: Scheduling and Binary Search Trees Lecture Overview ¥ Runway reservation system

Introduction Radboud University Nijmegen

Options for data structures

• Unsorted list/array: good? most operations are in O(n).Insertion can be in O(1)

• Sorted list: Appending and sorting takes O(nlgn) time.Insertion takes O(n) time.

A k minute check can be done inO(1) once the insertion point is found.

• Sorted array: Binary search to find place to insert in O(lgn)time. Looks good? Insertion is still in O(n).

• We almost had it in the sorted list/array.

• Key point: We need fast insertion into a sorted list.

• What if the time would be in whole minutes (instead ofallowing 56.4, etc)?

• A large array indexed by time!

Alexandra 14th October 2014 Lesson 6 6 / 21

Page 17: (Balanced) Binary Search TreesLecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011 Lecture 5: Scheduling and Binary Search Trees Lecture Overview ¥ Runway reservation system

Introduction Radboud University Nijmegen

Options for data structures

• Unsorted list/array: good? most operations are in O(n).Insertion can be in O(1)

• Sorted list: Appending and sorting takes O(nlgn) time.Insertion takes O(n) time. A k minute check can be done inO(1) once the insertion point is found.

• Sorted array: Binary search to find place to insert in O(lgn)time. Looks good? Insertion is still in O(n).

• We almost had it in the sorted list/array.

• Key point: We need fast insertion into a sorted list.

• What if the time would be in whole minutes (instead ofallowing 56.4, etc)?

• A large array indexed by time!

Alexandra 14th October 2014 Lesson 6 6 / 21

Page 18: (Balanced) Binary Search TreesLecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011 Lecture 5: Scheduling and Binary Search Trees Lecture Overview ¥ Runway reservation system

Introduction Radboud University Nijmegen

Options for data structures

• Unsorted list/array: good? most operations are in O(n).Insertion can be in O(1)

• Sorted list: Appending and sorting takes O(nlgn) time.Insertion takes O(n) time. A k minute check can be done inO(1) once the insertion point is found.

• Sorted array: Binary search to find place to insert in O(lgn)time.

Looks good? Insertion is still in O(n).

• We almost had it in the sorted list/array.

• Key point: We need fast insertion into a sorted list.

• What if the time would be in whole minutes (instead ofallowing 56.4, etc)?

• A large array indexed by time!

Alexandra 14th October 2014 Lesson 6 6 / 21

Page 19: (Balanced) Binary Search TreesLecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011 Lecture 5: Scheduling and Binary Search Trees Lecture Overview ¥ Runway reservation system

Introduction Radboud University Nijmegen

Options for data structures

• Unsorted list/array: good? most operations are in O(n).Insertion can be in O(1)

• Sorted list: Appending and sorting takes O(nlgn) time.Insertion takes O(n) time. A k minute check can be done inO(1) once the insertion point is found.

• Sorted array: Binary search to find place to insert in O(lgn)time. Looks good?

Insertion is still in O(n).

• We almost had it in the sorted list/array.

• Key point: We need fast insertion into a sorted list.

• What if the time would be in whole minutes (instead ofallowing 56.4, etc)?

• A large array indexed by time!

Alexandra 14th October 2014 Lesson 6 6 / 21

Page 20: (Balanced) Binary Search TreesLecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011 Lecture 5: Scheduling and Binary Search Trees Lecture Overview ¥ Runway reservation system

Introduction Radboud University Nijmegen

Options for data structures

• Unsorted list/array: good? most operations are in O(n).Insertion can be in O(1)

• Sorted list: Appending and sorting takes O(nlgn) time.Insertion takes O(n) time. A k minute check can be done inO(1) once the insertion point is found.

• Sorted array: Binary search to find place to insert in O(lgn)time. Looks good? Insertion is still in O(n).

• We almost had it in the sorted list/array.

• Key point: We need fast insertion into a sorted list.

• What if the time would be in whole minutes (instead ofallowing 56.4, etc)?

• A large array indexed by time!

Alexandra 14th October 2014 Lesson 6 6 / 21

Page 21: (Balanced) Binary Search TreesLecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011 Lecture 5: Scheduling and Binary Search Trees Lecture Overview ¥ Runway reservation system

Introduction Radboud University Nijmegen

Options for data structures

• Unsorted list/array: good? most operations are in O(n).Insertion can be in O(1)

• Sorted list: Appending and sorting takes O(nlgn) time.Insertion takes O(n) time. A k minute check can be done inO(1) once the insertion point is found.

• Sorted array: Binary search to find place to insert in O(lgn)time. Looks good? Insertion is still in O(n).

• We almost had it in the sorted list/array.

• Key point: We need fast insertion into a sorted list.

• What if the time would be in whole minutes (instead ofallowing 56.4, etc)?

• A large array indexed by time!

Alexandra 14th October 2014 Lesson 6 6 / 21

Page 22: (Balanced) Binary Search TreesLecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011 Lecture 5: Scheduling and Binary Search Trees Lecture Overview ¥ Runway reservation system

Introduction Radboud University Nijmegen

Options for data structures

• Unsorted list/array: good? most operations are in O(n).Insertion can be in O(1)

• Sorted list: Appending and sorting takes O(nlgn) time.Insertion takes O(n) time. A k minute check can be done inO(1) once the insertion point is found.

• Sorted array: Binary search to find place to insert in O(lgn)time. Looks good? Insertion is still in O(n).

• We almost had it in the sorted list/array.

• Key point: We need fast insertion into a sorted list.

• What if the time would be in whole minutes (instead ofallowing 56.4, etc)?

• A large array indexed by time!

Alexandra 14th October 2014 Lesson 6 6 / 21

Page 23: (Balanced) Binary Search TreesLecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011 Lecture 5: Scheduling and Binary Search Trees Lecture Overview ¥ Runway reservation system

Introduction Radboud University Nijmegen

Options for data structures

• Unsorted list/array: good? most operations are in O(n).Insertion can be in O(1)

• Sorted list: Appending and sorting takes O(nlgn) time.Insertion takes O(n) time. A k minute check can be done inO(1) once the insertion point is found.

• Sorted array: Binary search to find place to insert in O(lgn)time. Looks good? Insertion is still in O(n).

• We almost had it in the sorted list/array.

• Key point: We need fast insertion into a sorted list.

• What if the time would be in whole minutes (instead ofallowing 56.4, etc)?

• A large array indexed by time!

Alexandra 14th October 2014 Lesson 6 6 / 21

Page 24: (Balanced) Binary Search TreesLecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011 Lecture 5: Scheduling and Binary Search Trees Lecture Overview ¥ Runway reservation system

Introduction Radboud University Nijmegen

Binary Search trees

• Each node x in the binary tree has a key key(x).

• Nodes other than the root have a parent p(x).

• Nodes may have a left child left(x) and/or a right childright(x).

• The invariant of BSTs: for any node x , for all nodes y in theleft subtree of x , key(y) ≤ key(x). For all nodes y in theright subtree of x key(y) ≥ key(x).

Alexandra 14th October 2014 Lesson 6 7 / 21

Page 25: (Balanced) Binary Search TreesLecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011 Lecture 5: Scheduling and Binary Search Trees Lecture Overview ¥ Runway reservation system

Introduction Radboud University Nijmegen

Operations – Insert

Insert(val)

• Follow left and right children till you find the position (or seethe value). [Example on the board]

• How about the within k = 3 check?

• During insertion: if you find on the path from the root anelement that is within k = 3 of what you are inserting, thenabort and do not insert. [Example on the board]

Complexity: O(h), h is the height of the tree.

Alexandra 14th October 2014 Lesson 6 8 / 21

Page 26: (Balanced) Binary Search TreesLecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011 Lecture 5: Scheduling and Binary Search Trees Lecture Overview ¥ Runway reservation system

Introduction Radboud University Nijmegen

Operations – Insert

Insert(val)

• Follow left and right children till you find the position (or seethe value). [Example on the board]

• How about the within k = 3 check?

• During insertion: if you find on the path from the root anelement that is within k = 3 of what you are inserting, thenabort and do not insert. [Example on the board]

Complexity: O(h), h is the height of the tree.

Alexandra 14th October 2014 Lesson 6 8 / 21

Page 27: (Balanced) Binary Search TreesLecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011 Lecture 5: Scheduling and Binary Search Trees Lecture Overview ¥ Runway reservation system

Introduction Radboud University Nijmegen

Operations – Insert

Insert(val)

• Follow left and right children till you find the position (or seethe value). [Example on the board]

• How about the within k = 3 check?

• During insertion: if you find on the path from the root anelement that is within k = 3 of what you are inserting, thenabort and do not insert. [Example on the board]

Complexity: O(h), h is the height of the tree.

Alexandra 14th October 2014 Lesson 6 8 / 21

Page 28: (Balanced) Binary Search TreesLecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011 Lecture 5: Scheduling and Binary Search Trees Lecture Overview ¥ Runway reservation system

Introduction Radboud University Nijmegen

Operations – Insert

Insert(val)

• Follow left and right children till you find the position (or seethe value). [Example on the board]

• How about the within k = 3 check?

• During insertion: if you find on the path from the root anelement that is within k = 3 of what you are inserting, thenabort and do not insert. [Example on the board]

Complexity:

O(h), h is the height of the tree.

Alexandra 14th October 2014 Lesson 6 8 / 21

Page 29: (Balanced) Binary Search TreesLecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011 Lecture 5: Scheduling and Binary Search Trees Lecture Overview ¥ Runway reservation system

Introduction Radboud University Nijmegen

Operations – Insert

Insert(val)

• Follow left and right children till you find the position (or seethe value). [Example on the board]

• How about the within k = 3 check?

• During insertion: if you find on the path from the root anelement that is within k = 3 of what you are inserting, thenabort and do not insert. [Example on the board]

Complexity: O(h), h is the height of the tree.

Alexandra 14th October 2014 Lesson 6 8 / 21

Page 30: (Balanced) Binary Search TreesLecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011 Lecture 5: Scheduling and Binary Search Trees Lecture Overview ¥ Runway reservation system

Introduction Radboud University Nijmegen

Operations

Finding a value in the BST if it exists: find(val)

• Follow left and right pointers until you find it or hit NIL.

Finding the minimum element in a BST: findmin()

• Key is to just go left till you cannot go left anymore.

• findmax?

Complexity: O(h), h is the height of the tree.

Alexandra 14th October 2014 Lesson 6 9 / 21

Page 31: (Balanced) Binary Search TreesLecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011 Lecture 5: Scheduling and Binary Search Trees Lecture Overview ¥ Runway reservation system

Introduction Radboud University Nijmegen

Operations

Finding a value in the BST if it exists: find(val)

• Follow left and right pointers until you find it or hit NIL.

Finding the minimum element in a BST: findmin()

• Key is to just go left till you cannot go left anymore.

• findmax?

Complexity: O(h), h is the height of the tree.

Alexandra 14th October 2014 Lesson 6 9 / 21

Page 32: (Balanced) Binary Search TreesLecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011 Lecture 5: Scheduling and Binary Search Trees Lecture Overview ¥ Runway reservation system

Introduction Radboud University Nijmegen

Operations

Finding a value in the BST if it exists: find(val)

• Follow left and right pointers until you find it or hit NIL.

Finding the minimum element in a BST: findmin()

• Key is to just go left till you cannot go left anymore.

• findmax?

Complexity: O(h), h is the height of the tree.

Alexandra 14th October 2014 Lesson 6 9 / 21

Page 33: (Balanced) Binary Search TreesLecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011 Lecture 5: Scheduling and Binary Search Trees Lecture Overview ¥ Runway reservation system

Introduction Radboud University Nijmegen

Operations

Finding a value in the BST if it exists: find(val)

• Follow left and right pointers until you find it or hit NIL.

Finding the minimum element in a BST: findmin()

• Key is to just go left till you cannot go left anymore.

• findmax?

Complexity: O(h), h is the height of the tree.

Alexandra 14th October 2014 Lesson 6 9 / 21

Page 34: (Balanced) Binary Search TreesLecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011 Lecture 5: Scheduling and Binary Search Trees Lecture Overview ¥ Runway reservation system

Introduction Radboud University Nijmegen

Operations

Finding a value in the BST if it exists: find(val)

• Follow left and right pointers until you find it or hit NIL.

Finding the minimum element in a BST: findmin()

• Key is to just go left till you cannot go left anymore.

• findmax?

Complexity: O(h), h is the height of the tree.

Alexandra 14th October 2014 Lesson 6 9 / 21

Page 35: (Balanced) Binary Search TreesLecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011 Lecture 5: Scheduling and Binary Search Trees Lecture Overview ¥ Runway reservation system

Introduction Radboud University Nijmegen

Operations

Finding a value in the BST if it exists: find(val)

• Follow left and right pointers until you find it or hit NIL.

Finding the minimum element in a BST: findmin()

• Key is to just go left till you cannot go left anymore.

• findmax?

Complexity: O(h), h is the height of the tree.

Alexandra 14th October 2014 Lesson 6 9 / 21

Page 36: (Balanced) Binary Search TreesLecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011 Lecture 5: Scheduling and Binary Search Trees Lecture Overview ¥ Runway reservation system

Introduction Radboud University Nijmegen

Operations

Finding a value in the BST if it exists: find(val)

• Follow left and right pointers until you find it or hit NIL.

Finding the minimum element in a BST: findmin()

• Key is to just go left till you cannot go left anymore.

• findmax?

Complexity:

O(h), h is the height of the tree.

Alexandra 14th October 2014 Lesson 6 9 / 21

Page 37: (Balanced) Binary Search TreesLecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011 Lecture 5: Scheduling and Binary Search Trees Lecture Overview ¥ Runway reservation system

Introduction Radboud University Nijmegen

Operations

Finding a value in the BST if it exists: find(val)

• Follow left and right pointers until you find it or hit NIL.

Finding the minimum element in a BST: findmin()

• Key is to just go left till you cannot go left anymore.

• findmax?

Complexity: O(h), h is the height of the tree.

Alexandra 14th October 2014 Lesson 6 9 / 21

Page 38: (Balanced) Binary Search TreesLecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011 Lecture 5: Scheduling and Binary Search Trees Lecture Overview ¥ Runway reservation system

Introduction Radboud University Nijmegen

New requirement

• Problem specification does not stay the same. . .

• New requirement: Rank(t): How many planes are scheduledto land at times ≤ t?

• How to do it?

• Augmented BST.

Lecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011

New Requirement

Rank(t): How many planes are scheduled to land at times t? The new requirement

necessitates a design amendment.

Cannot solve it e�ciently with what we have but can augment the BST structure.

79

49

46

43 64 83

6

2

1

3

1 1

what lands before 79?

keep track of size of subtrees, during insert and delete

Figure 5: Augmenting the BST Structure

Summarizing from Fig. 5, the algorithm for augmentation is as follows:

1. Walk down tree to find desired time

2. Add in nodes that are smaller

3. Add in subtree sizes to the left

In total, this takes O(h) time.

6

• Insert and deletion modify numbers. How? Add-in on smallernodes.

Complexity: O(h), h is the height of the tree.

Alexandra 14th October 2014 Lesson 6 10 / 21

Page 39: (Balanced) Binary Search TreesLecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011 Lecture 5: Scheduling and Binary Search Trees Lecture Overview ¥ Runway reservation system

Introduction Radboud University Nijmegen

New requirement

• Problem specification does not stay the same. . .

• New requirement: Rank(t): How many planes are scheduledto land at times ≤ t?

• How to do it?

• Augmented BST.

Lecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011

New Requirement

Rank(t): How many planes are scheduled to land at times t? The new requirement

necessitates a design amendment.

Cannot solve it e�ciently with what we have but can augment the BST structure.

79

49

46

43 64 83

6

2

1

3

1 1

what lands before 79?

keep track of size of subtrees, during insert and delete

Figure 5: Augmenting the BST Structure

Summarizing from Fig. 5, the algorithm for augmentation is as follows:

1. Walk down tree to find desired time

2. Add in nodes that are smaller

3. Add in subtree sizes to the left

In total, this takes O(h) time.

6

• Insert and deletion modify numbers. How? Add-in on smallernodes.

Complexity: O(h), h is the height of the tree.

Alexandra 14th October 2014 Lesson 6 10 / 21

Page 40: (Balanced) Binary Search TreesLecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011 Lecture 5: Scheduling and Binary Search Trees Lecture Overview ¥ Runway reservation system

Introduction Radboud University Nijmegen

New requirement

• Problem specification does not stay the same. . .

• New requirement: Rank(t): How many planes are scheduledto land at times ≤ t?

• How to do it?

• Augmented BST.

Lecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011

New Requirement

Rank(t): How many planes are scheduled to land at times t? The new requirement

necessitates a design amendment.

Cannot solve it e�ciently with what we have but can augment the BST structure.

79

49

46

43 64 83

6

2

1

3

1 1

what lands before 79?

keep track of size of subtrees, during insert and delete

Figure 5: Augmenting the BST Structure

Summarizing from Fig. 5, the algorithm for augmentation is as follows:

1. Walk down tree to find desired time

2. Add in nodes that are smaller

3. Add in subtree sizes to the left

In total, this takes O(h) time.

6

• Insert and deletion modify numbers. How? Add-in on smallernodes.

Complexity: O(h), h is the height of the tree.

Alexandra 14th October 2014 Lesson 6 10 / 21

Page 41: (Balanced) Binary Search TreesLecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011 Lecture 5: Scheduling and Binary Search Trees Lecture Overview ¥ Runway reservation system

Introduction Radboud University Nijmegen

New requirement

• Problem specification does not stay the same. . .

• New requirement: Rank(t): How many planes are scheduledto land at times ≤ t?

• How to do it?

• Augmented BST.

Lecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011

New Requirement

Rank(t): How many planes are scheduled to land at times t? The new requirement

necessitates a design amendment.

Cannot solve it e�ciently with what we have but can augment the BST structure.

79

49

46

43 64 83

6

2

1

3

1 1

what lands before 79?

keep track of size of subtrees, during insert and delete

Figure 5: Augmenting the BST Structure

Summarizing from Fig. 5, the algorithm for augmentation is as follows:

1. Walk down tree to find desired time

2. Add in nodes that are smaller

3. Add in subtree sizes to the left

In total, this takes O(h) time.

6

• Insert and deletion modify numbers.

How? Add-in on smallernodes.

Complexity: O(h), h is the height of the tree.

Alexandra 14th October 2014 Lesson 6 10 / 21

Page 42: (Balanced) Binary Search TreesLecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011 Lecture 5: Scheduling and Binary Search Trees Lecture Overview ¥ Runway reservation system

Introduction Radboud University Nijmegen

New requirement

• Problem specification does not stay the same. . .

• New requirement: Rank(t): How many planes are scheduledto land at times ≤ t?

• How to do it?

• Augmented BST.

Lecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011

New Requirement

Rank(t): How many planes are scheduled to land at times t? The new requirement

necessitates a design amendment.

Cannot solve it e�ciently with what we have but can augment the BST structure.

79

49

46

43 64 83

6

2

1

3

1 1

what lands before 79?

keep track of size of subtrees, during insert and delete

Figure 5: Augmenting the BST Structure

Summarizing from Fig. 5, the algorithm for augmentation is as follows:

1. Walk down tree to find desired time

2. Add in nodes that are smaller

3. Add in subtree sizes to the left

In total, this takes O(h) time.

6

• Insert and deletion modify numbers. How?

Add-in on smallernodes.

Complexity: O(h), h is the height of the tree.

Alexandra 14th October 2014 Lesson 6 10 / 21

Page 43: (Balanced) Binary Search TreesLecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011 Lecture 5: Scheduling and Binary Search Trees Lecture Overview ¥ Runway reservation system

Introduction Radboud University Nijmegen

New requirement

• Problem specification does not stay the same. . .

• New requirement: Rank(t): How many planes are scheduledto land at times ≤ t?

• How to do it?

• Augmented BST.

Lecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011

New Requirement

Rank(t): How many planes are scheduled to land at times t? The new requirement

necessitates a design amendment.

Cannot solve it e�ciently with what we have but can augment the BST structure.

79

49

46

43 64 83

6

2

1

3

1 1

what lands before 79?

keep track of size of subtrees, during insert and delete

Figure 5: Augmenting the BST Structure

Summarizing from Fig. 5, the algorithm for augmentation is as follows:

1. Walk down tree to find desired time

2. Add in nodes that are smaller

3. Add in subtree sizes to the left

In total, this takes O(h) time.

6

• Insert and deletion modify numbers. How? Add-in on smallernodes.

Complexity: O(h), h is the height of the tree.

Alexandra 14th October 2014 Lesson 6 10 / 21

Page 44: (Balanced) Binary Search TreesLecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011 Lecture 5: Scheduling and Binary Search Trees Lecture Overview ¥ Runway reservation system

Introduction Radboud University Nijmegen

New requirement

• Problem specification does not stay the same. . .

• New requirement: Rank(t): How many planes are scheduledto land at times ≤ t?

• How to do it?

• Augmented BST.

Lecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011

New Requirement

Rank(t): How many planes are scheduled to land at times t? The new requirement

necessitates a design amendment.

Cannot solve it e�ciently with what we have but can augment the BST structure.

79

49

46

43 64 83

6

2

1

3

1 1

what lands before 79?

keep track of size of subtrees, during insert and delete

Figure 5: Augmenting the BST Structure

Summarizing from Fig. 5, the algorithm for augmentation is as follows:

1. Walk down tree to find desired time

2. Add in nodes that are smaller

3. Add in subtree sizes to the left

In total, this takes O(h) time.

6

• Insert and deletion modify numbers. How? Add-in on smallernodes.

Complexity:

O(h), h is the height of the tree.

Alexandra 14th October 2014 Lesson 6 10 / 21

Page 45: (Balanced) Binary Search TreesLecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011 Lecture 5: Scheduling and Binary Search Trees Lecture Overview ¥ Runway reservation system

Introduction Radboud University Nijmegen

New requirement

• Problem specification does not stay the same. . .

• New requirement: Rank(t): How many planes are scheduledto land at times ≤ t?

• How to do it?

• Augmented BST.

Lecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011

New Requirement

Rank(t): How many planes are scheduled to land at times t? The new requirement

necessitates a design amendment.

Cannot solve it e�ciently with what we have but can augment the BST structure.

79

49

46

43 64 83

6

2

1

3

1 1

what lands before 79?

keep track of size of subtrees, during insert and delete

Figure 5: Augmenting the BST Structure

Summarizing from Fig. 5, the algorithm for augmentation is as follows:

1. Walk down tree to find desired time

2. Add in nodes that are smaller

3. Add in subtree sizes to the left

In total, this takes O(h) time.

6

• Insert and deletion modify numbers. How? Add-in on smallernodes.

Complexity: O(h), h is the height of the tree.Alexandra 14th October 2014 Lesson 6 10 / 21

Page 46: (Balanced) Binary Search TreesLecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011 Lecture 5: Scheduling and Binary Search Trees Lecture Overview ¥ Runway reservation system

Introduction Radboud University Nijmegen

Rank(t)

Lecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011

New Requirement

Rank(t): How many planes are scheduled to land at times t? The new requirement

necessitates a design amendment.

Cannot solve it e�ciently with what we have but can augment the BST structure.

79

49

46

43 64 83

6

2

1

3

1 1

what lands before 79?

keep track of size of subtrees, during insert and delete

Figure 5: Augmenting the BST Structure

Summarizing from Fig. 5, the algorithm for augmentation is as follows:

1. Walk down tree to find desired time

2. Add in nodes that are smaller

3. Add in subtree sizes to the left

In total, this takes O(h) time.

6

One option:

• Walk down tree to find the desired time.

• Add in the nodes that are smaller.

• Add in the subtree sizes to the left.

• Example: Rank(79) = 1 + 2 + 1 + 1 = 5

Alexandra 14th October 2014 Lesson 6 11 / 21

Page 47: (Balanced) Binary Search TreesLecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011 Lecture 5: Scheduling and Binary Search Trees Lecture Overview ¥ Runway reservation system

Introduction Radboud University Nijmegen

Rank(t)

Lecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011

New Requirement

Rank(t): How many planes are scheduled to land at times t? The new requirement

necessitates a design amendment.

Cannot solve it e�ciently with what we have but can augment the BST structure.

79

49

46

43 64 83

6

2

1

3

1 1

what lands before 79?

keep track of size of subtrees, during insert and delete

Figure 5: Augmenting the BST Structure

Summarizing from Fig. 5, the algorithm for augmentation is as follows:

1. Walk down tree to find desired time

2. Add in nodes that are smaller

3. Add in subtree sizes to the left

In total, this takes O(h) time.

6

One option:

• Walk down tree to find the desired time.

• Add in the nodes that are smaller.

• Add in the subtree sizes to the left.

• Example: Rank(79) =

1 + 2 + 1 + 1 = 5

Alexandra 14th October 2014 Lesson 6 11 / 21

Page 48: (Balanced) Binary Search TreesLecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011 Lecture 5: Scheduling and Binary Search Trees Lecture Overview ¥ Runway reservation system

Introduction Radboud University Nijmegen

Rank(t)

Lecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011

New Requirement

Rank(t): How many planes are scheduled to land at times t? The new requirement

necessitates a design amendment.

Cannot solve it e�ciently with what we have but can augment the BST structure.

79

49

46

43 64 83

6

2

1

3

1 1

what lands before 79?

keep track of size of subtrees, during insert and delete

Figure 5: Augmenting the BST Structure

Summarizing from Fig. 5, the algorithm for augmentation is as follows:

1. Walk down tree to find desired time

2. Add in nodes that are smaller

3. Add in subtree sizes to the left

In total, this takes O(h) time.

6

One option:

• Walk down tree to find the desired time.

• Add in the nodes that are smaller.

• Add in the subtree sizes to the left.

• Example: Rank(79) = 1 + 2 + 1 + 1 = 5

Alexandra 14th October 2014 Lesson 6 11 / 21

Page 49: (Balanced) Binary Search TreesLecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011 Lecture 5: Scheduling and Binary Search Trees Lecture Overview ¥ Runway reservation system

Introduction Radboud University Nijmegen

Have we solved the original problem?

• We have a data structure with operation in O(h).

• The original problem required O(lg n).

• Are we there?

Lecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011

49 461 + 2 + 1 + 1 = 5

79 64subtree

subtree

Figure 6: Augmentation Algorithm Example

All the Python code for the Binary Search Trees discussed here are available at this

link

Have we accomplished anything?

Height h of the tree should be O(lg n).

46

43

49

55

Figure 7: Insert into BST in sorted order

The tree in Fig. 7 looks like a linked list. We have achieved O(n) not O(lg n)!!

Balanced BSTs to the rescue in the next lecture!

7

• Bad news: BSTs can look like lists.

• Good news: we have a solution. Balanced BSTs.

Alexandra 14th October 2014 Lesson 6 12 / 21

Page 50: (Balanced) Binary Search TreesLecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011 Lecture 5: Scheduling and Binary Search Trees Lecture Overview ¥ Runway reservation system

Introduction Radboud University Nijmegen

Have we solved the original problem?

• We have a data structure with operation in O(h).

• The original problem required O(lg n).

• Are we there?

Lecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011

49 461 + 2 + 1 + 1 = 5

79 64subtree

subtree

Figure 6: Augmentation Algorithm Example

All the Python code for the Binary Search Trees discussed here are available at this

link

Have we accomplished anything?

Height h of the tree should be O(lg n).

46

43

49

55

Figure 7: Insert into BST in sorted order

The tree in Fig. 7 looks like a linked list. We have achieved O(n) not O(lg n)!!

Balanced BSTs to the rescue in the next lecture!

7

• Bad news: BSTs can look like lists.

• Good news: we have a solution. Balanced BSTs.

Alexandra 14th October 2014 Lesson 6 12 / 21

Page 51: (Balanced) Binary Search TreesLecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011 Lecture 5: Scheduling and Binary Search Trees Lecture Overview ¥ Runway reservation system

Introduction Radboud University Nijmegen

Have we solved the original problem?

• We have a data structure with operation in O(h).

• The original problem required O(lg n).

• Are we there?

Lecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011

49 461 + 2 + 1 + 1 = 5

79 64subtree

subtree

Figure 6: Augmentation Algorithm Example

All the Python code for the Binary Search Trees discussed here are available at this

link

Have we accomplished anything?

Height h of the tree should be O(lg n).

46

43

49

55

Figure 7: Insert into BST in sorted order

The tree in Fig. 7 looks like a linked list. We have achieved O(n) not O(lg n)!!

Balanced BSTs to the rescue in the next lecture!

7

• Bad news: BSTs can look like lists.

• Good news: we have a solution. Balanced BSTs.

Alexandra 14th October 2014 Lesson 6 12 / 21

Page 52: (Balanced) Binary Search TreesLecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011 Lecture 5: Scheduling and Binary Search Trees Lecture Overview ¥ Runway reservation system

Introduction Radboud University Nijmegen

Have we solved the original problem?

• We have a data structure with operation in O(h).

• The original problem required O(lg n).

• Are we there?

Lecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011

49 461 + 2 + 1 + 1 = 5

79 64subtree

subtree

Figure 6: Augmentation Algorithm Example

All the Python code for the Binary Search Trees discussed here are available at this

link

Have we accomplished anything?

Height h of the tree should be O(lg n).

46

43

49

55

Figure 7: Insert into BST in sorted order

The tree in Fig. 7 looks like a linked list. We have achieved O(n) not O(lg n)!!

Balanced BSTs to the rescue in the next lecture!

7

• Bad news: BSTs can look like lists.

• Good news: we have a solution. Balanced BSTs.

Alexandra 14th October 2014 Lesson 6 12 / 21

Page 53: (Balanced) Binary Search TreesLecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011 Lecture 5: Scheduling and Binary Search Trees Lecture Overview ¥ Runway reservation system

Introduction Radboud University Nijmegen

Have we solved the original problem?

• We have a data structure with operation in O(h).

• The original problem required O(lg n).

• Are we there?

Lecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011

49 461 + 2 + 1 + 1 = 5

79 64subtree

subtree

Figure 6: Augmentation Algorithm Example

All the Python code for the Binary Search Trees discussed here are available at this

link

Have we accomplished anything?

Height h of the tree should be O(lg n).

46

43

49

55

Figure 7: Insert into BST in sorted order

The tree in Fig. 7 looks like a linked list. We have achieved O(n) not O(lg n)!!

Balanced BSTs to the rescue in the next lecture!

7

• Bad news: BSTs can look like lists.

• Good news: we have a solution. Balanced BSTs.

Alexandra 14th October 2014 Lesson 6 12 / 21

Page 54: (Balanced) Binary Search TreesLecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011 Lecture 5: Scheduling and Binary Search Trees Lecture Overview ¥ Runway reservation system

Introduction Radboud University Nijmegen

Balanced BSTs

• Operations of BSTs were in O(h). [What is h?]

• h can vary between lg n and n:

Lecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011

New Requirement

Rank(t): How many planes are scheduled to land at times t? The new requirement

necessitates a design amendment.

Cannot solve it e�ciently with what we have but can augment the BST structure.

79

49

46

43 64 83

6

2

1

3

1 1

what lands before 79?

keep track of size of subtrees, during insert and delete

Figure 5: Augmenting the BST Structure

Summarizing from Fig. 5, the algorithm for augmentation is as follows:

1. Walk down tree to find desired time

2. Add in nodes that are smaller

3. Add in subtree sizes to the left

In total, this takes O(h) time.

6

vs

Lecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011

49 461 + 2 + 1 + 1 = 5

79 64subtree

subtree

Figure 6: Augmentation Algorithm Example

All the Python code for the Binary Search Trees discussed here are available at this

link

Have we accomplished anything?

Height h of the tree should be O(lg n).

46

43

49

55

Figure 7: Insert into BST in sorted order

The tree in Fig. 7 looks like a linked list. We have achieved O(n) not O(lg n)!!

Balanced BSTs to the rescue in the next lecture!

7

perfectly balanced path

• Balanced BSTs maintain h = O(lg n)⇒ operations performin O(lg n).

Alexandra 14th October 2014 Lesson 6 13 / 21

Page 55: (Balanced) Binary Search TreesLecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011 Lecture 5: Scheduling and Binary Search Trees Lecture Overview ¥ Runway reservation system

Introduction Radboud University Nijmegen

Balanced BSTs

• Operations of BSTs were in O(h). [What is h?]

• h can vary between lg n and n:

Lecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011

New Requirement

Rank(t): How many planes are scheduled to land at times t? The new requirement

necessitates a design amendment.

Cannot solve it e�ciently with what we have but can augment the BST structure.

79

49

46

43 64 83

6

2

1

3

1 1

what lands before 79?

keep track of size of subtrees, during insert and delete

Figure 5: Augmenting the BST Structure

Summarizing from Fig. 5, the algorithm for augmentation is as follows:

1. Walk down tree to find desired time

2. Add in nodes that are smaller

3. Add in subtree sizes to the left

In total, this takes O(h) time.

6

vs

Lecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011

49 461 + 2 + 1 + 1 = 5

79 64subtree

subtree

Figure 6: Augmentation Algorithm Example

All the Python code for the Binary Search Trees discussed here are available at this

link

Have we accomplished anything?

Height h of the tree should be O(lg n).

46

43

49

55

Figure 7: Insert into BST in sorted order

The tree in Fig. 7 looks like a linked list. We have achieved O(n) not O(lg n)!!

Balanced BSTs to the rescue in the next lecture!

7

perfectly balanced path

• Balanced BSTs maintain h = O(lg n)⇒ operations performin O(lg n).

Alexandra 14th October 2014 Lesson 6 13 / 21

Page 56: (Balanced) Binary Search TreesLecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011 Lecture 5: Scheduling and Binary Search Trees Lecture Overview ¥ Runway reservation system

Introduction Radboud University Nijmegen

Balanced BSTs

• Operations of BSTs were in O(h). [What is h?]

• h can vary between lg n and n:

Lecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011

New Requirement

Rank(t): How many planes are scheduled to land at times t? The new requirement

necessitates a design amendment.

Cannot solve it e�ciently with what we have but can augment the BST structure.

79

49

46

43 64 83

6

2

1

3

1 1

what lands before 79?

keep track of size of subtrees, during insert and delete

Figure 5: Augmenting the BST Structure

Summarizing from Fig. 5, the algorithm for augmentation is as follows:

1. Walk down tree to find desired time

2. Add in nodes that are smaller

3. Add in subtree sizes to the left

In total, this takes O(h) time.

6

vs

Lecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011

49 461 + 2 + 1 + 1 = 5

79 64subtree

subtree

Figure 6: Augmentation Algorithm Example

All the Python code for the Binary Search Trees discussed here are available at this

link

Have we accomplished anything?

Height h of the tree should be O(lg n).

46

43

49

55

Figure 7: Insert into BST in sorted order

The tree in Fig. 7 looks like a linked list. We have achieved O(n) not O(lg n)!!

Balanced BSTs to the rescue in the next lecture!

7

perfectly balanced path

• Balanced BSTs maintain h = O(lg n)⇒ operations performin O(lg n).

Alexandra 14th October 2014 Lesson 6 13 / 21

Page 57: (Balanced) Binary Search TreesLecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011 Lecture 5: Scheduling and Binary Search Trees Lecture Overview ¥ Runway reservation system

Introduction Radboud University Nijmegen

AVL trees

• Introduced by Adelson-Velskii and Landis 1962.

• For every node require heights of left and right children by atmost ±1.

• Each node stores its height. (enough to store the difference inheights)

• What is the the height of a node? [Example on the board]

• height(n) = max{left(n), right(n)}+ 1

• Empty tree has height −1 (why? if we want leaves to haveheight 0. If we want them to have height 1 we choose theheight of the empty tree to be 0.).

• Augmentation: every operation will update the key in thenode. (constant overhead)

Alexandra 14th October 2014 Lesson 6 14 / 21

Page 58: (Balanced) Binary Search TreesLecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011 Lecture 5: Scheduling and Binary Search Trees Lecture Overview ¥ Runway reservation system

Introduction Radboud University Nijmegen

AVL trees

• Introduced by Adelson-Velskii and Landis 1962.

• For every node require heights of left and right children by atmost ±1.

• Each node stores its height. (enough to store the difference inheights)

• What is the the height of a node? [Example on the board]

• height(n) = max{left(n), right(n)}+ 1

• Empty tree has height −1 (why?

if we want leaves to haveheight 0. If we want them to have height 1 we choose theheight of the empty tree to be 0.).

• Augmentation: every operation will update the key in thenode. (constant overhead)

Alexandra 14th October 2014 Lesson 6 14 / 21

Page 59: (Balanced) Binary Search TreesLecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011 Lecture 5: Scheduling and Binary Search Trees Lecture Overview ¥ Runway reservation system

Introduction Radboud University Nijmegen

AVL trees

• Introduced by Adelson-Velskii and Landis 1962.

• For every node require heights of left and right children by atmost ±1.

• Each node stores its height. (enough to store the difference inheights)

• What is the the height of a node? [Example on the board]

• height(n) = max{left(n), right(n)}+ 1

• Empty tree has height −1 (why? if we want leaves to haveheight 0. If we want them to have height 1 we choose theheight of the empty tree to be 0.).

• Augmentation: every operation will update the key in thenode. (constant overhead)

Alexandra 14th October 2014 Lesson 6 14 / 21

Page 60: (Balanced) Binary Search TreesLecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011 Lecture 5: Scheduling and Binary Search Trees Lecture Overview ¥ Runway reservation system

Introduction Radboud University Nijmegen

AVL trees are balanced

• Claim: AVL trees are balanced.

• What is the worst case?

• Nh = min nodes in a tree of height h.

• Nh = Nh−1 + Nh−2 + 1 > 2Nh−2.

• Nh > 2W ⇒ h < 2 lgNh.

Alternative proof:

• Nh > Fh (fibonacci number, yay!).

• Nh = Fn+2 − 1.

• Fh = ϕh√5

, where ϕ is the golden ratio (≈ 1.618).

• max h ≈ logϕ n ≈ 1.440 lg n

Alexandra 14th October 2014 Lesson 6 15 / 21

Page 61: (Balanced) Binary Search TreesLecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011 Lecture 5: Scheduling and Binary Search Trees Lecture Overview ¥ Runway reservation system

Introduction Radboud University Nijmegen

AVL trees are balanced

• Claim: AVL trees are balanced.

• What is the worst case?

• Nh = min nodes in a tree of height h.

• Nh = Nh−1 + Nh−2 + 1 > 2Nh−2.

• Nh > 2W ⇒ h < 2 lgNh.

Alternative proof:

• Nh > Fh (fibonacci number, yay!).

• Nh = Fn+2 − 1.

• Fh = ϕh√5

, where ϕ is the golden ratio (≈ 1.618).

• max h ≈ logϕ n ≈ 1.440 lg n

Alexandra 14th October 2014 Lesson 6 15 / 21

Page 62: (Balanced) Binary Search TreesLecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011 Lecture 5: Scheduling and Binary Search Trees Lecture Overview ¥ Runway reservation system

Introduction Radboud University Nijmegen

AVL trees are balanced

• Claim: AVL trees are balanced.

• What is the worst case?

• Nh = min nodes in a tree of height h.

• Nh =

Nh−1 + Nh−2 + 1 > 2Nh−2.

• Nh > 2W ⇒ h < 2 lgNh.

Alternative proof:

• Nh > Fh (fibonacci number, yay!).

• Nh = Fn+2 − 1.

• Fh = ϕh√5

, where ϕ is the golden ratio (≈ 1.618).

• max h ≈ logϕ n ≈ 1.440 lg n

Alexandra 14th October 2014 Lesson 6 15 / 21

Page 63: (Balanced) Binary Search TreesLecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011 Lecture 5: Scheduling and Binary Search Trees Lecture Overview ¥ Runway reservation system

Introduction Radboud University Nijmegen

AVL trees are balanced

• Claim: AVL trees are balanced.

• What is the worst case?

• Nh = min nodes in a tree of height h.

• Nh = Nh−1 + Nh−2 + 1

> 2Nh−2.

• Nh > 2W ⇒ h < 2 lgNh.

Alternative proof:

• Nh > Fh (fibonacci number, yay!).

• Nh = Fn+2 − 1.

• Fh = ϕh√5

, where ϕ is the golden ratio (≈ 1.618).

• max h ≈ logϕ n ≈ 1.440 lg n

Alexandra 14th October 2014 Lesson 6 15 / 21

Page 64: (Balanced) Binary Search TreesLecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011 Lecture 5: Scheduling and Binary Search Trees Lecture Overview ¥ Runway reservation system

Introduction Radboud University Nijmegen

AVL trees are balanced

• Claim: AVL trees are balanced.

• What is the worst case?

• Nh = min nodes in a tree of height h.

• Nh = Nh−1 + Nh−2 + 1 > 2Nh−2.

• Nh > 2W ⇒ h < 2 lgNh.

Alternative proof:

• Nh > Fh (fibonacci number, yay!).

• Nh = Fn+2 − 1.

• Fh = ϕh√5

, where ϕ is the golden ratio (≈ 1.618).

• max h ≈ logϕ n ≈ 1.440 lg n

Alexandra 14th October 2014 Lesson 6 15 / 21

Page 65: (Balanced) Binary Search TreesLecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011 Lecture 5: Scheduling and Binary Search Trees Lecture Overview ¥ Runway reservation system

Introduction Radboud University Nijmegen

AVL trees are balanced

• Claim: AVL trees are balanced.

• What is the worst case?

• Nh = min nodes in a tree of height h.

• Nh = Nh−1 + Nh−2 + 1 > 2Nh−2.

• Nh > 2W ⇒ h < 2 lgNh.

Alternative proof:

• Nh > Fh (fibonacci number, yay!).

• Nh = Fn+2 − 1.

• Fh = ϕh√5

, where ϕ is the golden ratio (≈ 1.618).

• max h ≈ logϕ n ≈ 1.440 lg n

Alexandra 14th October 2014 Lesson 6 15 / 21

Page 66: (Balanced) Binary Search TreesLecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011 Lecture 5: Scheduling and Binary Search Trees Lecture Overview ¥ Runway reservation system

Introduction Radboud University Nijmegen

AVL trees are balanced

• Claim: AVL trees are balanced.

• What is the worst case?

• Nh = min nodes in a tree of height h.

• Nh = Nh−1 + Nh−2 + 1 > 2Nh−2.

• Nh > 2W ⇒ h < 2 lgNh.

Alternative proof:

• Nh > Fh (fibonacci number, yay!).

• Nh = Fn+2 − 1.

• Fh = ϕh√5

, where ϕ is the golden ratio (≈ 1.618).

• max h ≈ logϕ n ≈ 1.440 lg n

Alexandra 14th October 2014 Lesson 6 15 / 21

Page 67: (Balanced) Binary Search TreesLecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011 Lecture 5: Scheduling and Binary Search Trees Lecture Overview ¥ Runway reservation system

Introduction Radboud University Nijmegen

Operations on AVL trees

• How do we maintain the property?

• Several operations: insert, delete, . . .

• How to do insert?

• First insert as in BSTs, then fix AVL property. (easy ;-))

• Examples board.

Alexandra 14th October 2014 Lesson 6 16 / 21

Page 68: (Balanced) Binary Search TreesLecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011 Lecture 5: Scheduling and Binary Search Trees Lecture Overview ¥ Runway reservation system

Introduction Radboud University Nijmegen

Operations on AVL trees

• How do we maintain the property?

• Several operations: insert, delete, . . .

• How to do insert?

• First insert as in BSTs, then fix AVL property. (easy ;-))

• Examples board.

Alexandra 14th October 2014 Lesson 6 16 / 21

Page 69: (Balanced) Binary Search TreesLecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011 Lecture 5: Scheduling and Binary Search Trees Lecture Overview ¥ Runway reservation system

Introduction Radboud University Nijmegen

Operations on AVL trees

• How do we maintain the property?

• Several operations: insert, delete, . . .

• How to do insert?

• First insert as in BSTs, then fix AVL property. (easy ;-))

• Examples board.

Alexandra 14th October 2014 Lesson 6 16 / 21

Page 70: (Balanced) Binary Search TreesLecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011 Lecture 5: Scheduling and Binary Search Trees Lecture Overview ¥ Runway reservation system

Introduction Radboud University Nijmegen

Rotations

• Examples show only fixing of one violation.

• There might be several violations, which we fix upwards.

• Start with x lowest node violating AVL property.

Alexandra 14th October 2014 Lesson 6 17 / 21

Page 71: (Balanced) Binary Search TreesLecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011 Lecture 5: Scheduling and Binary Search Trees Lecture Overview ¥ Runway reservation system

Introduction Radboud University Nijmegen

Rotations

• x right heavy (left case symmetric).

Scan

ned

by C

amSc

anne

r

What about y balanced?

Alexandra 14th October 2014 Lesson 6 18 / 21

Page 72: (Balanced) Binary Search TreesLecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011 Lecture 5: Scheduling and Binary Search Trees Lecture Overview ¥ Runway reservation system

Introduction Radboud University Nijmegen

Rotations

• x right heavy (left case symmetric).

Scan

ned

by C

amSc

anne

r

What about y balanced?Alexandra 14th October 2014 Lesson 6 18 / 21

Page 73: (Balanced) Binary Search TreesLecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011 Lecture 5: Scheduling and Binary Search Trees Lecture Overview ¥ Runway reservation system

Introduction Radboud University Nijmegen

Rotations

• If y is left heavy we need two rotations.

Scan

ned

by C

amSc

anne

r

Alexandra 14th October 2014 Lesson 6 19 / 21

Page 74: (Balanced) Binary Search TreesLecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011 Lecture 5: Scheduling and Binary Search Trees Lecture Overview ¥ Runway reservation system

Introduction Radboud University Nijmegen

Rotations

• If y is left heavy we need two rotations.

Scan

ned

by C

amSc

anne

r

Alexandra 14th October 2014 Lesson 6 19 / 21

Page 75: (Balanced) Binary Search TreesLecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011 Lecture 5: Scheduling and Binary Search Trees Lecture Overview ¥ Runway reservation system

Introduction Radboud University Nijmegen

AVL sort

• Insert n items.

O(n lg n)

• In-order traversal.

O(n)

Alexandra 14th October 2014 Lesson 6 20 / 21

Page 76: (Balanced) Binary Search TreesLecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011 Lecture 5: Scheduling and Binary Search Trees Lecture Overview ¥ Runway reservation system

Introduction Radboud University Nijmegen

AVL sort

• Insert n items.

O(n lg n)

• In-order traversal. O(n)

Alexandra 14th October 2014 Lesson 6 20 / 21

Page 77: (Balanced) Binary Search TreesLecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011 Lecture 5: Scheduling and Binary Search Trees Lecture Overview ¥ Runway reservation system

Introduction Radboud University Nijmegen

AVL sort

• Insert n items. O(n lg n)

• In-order traversal. O(n)

Alexandra 14th October 2014 Lesson 6 20 / 21

Page 78: (Balanced) Binary Search TreesLecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011 Lecture 5: Scheduling and Binary Search Trees Lecture Overview ¥ Runway reservation system

Introduction Radboud University Nijmegen

Abstract data type

Many balanced search trees:

• AVL trees

• B-trees/ 2-3-4 trees

• red-black trees

• scapegoat trees

• . . .

Desired operations/ specification (abstract data type):

• Insert and delete

• Min

• Successor / Predecessor

Different data structures can correspond to same specification(AVL trees, heaps, etc).

Alexandra 14th October 2014 Lesson 6 21 / 21

Page 79: (Balanced) Binary Search TreesLecture 5 Scheduling and Binary Search Trees 6.006 Fall 2011 Lecture 5: Scheduling and Binary Search Trees Lecture Overview ¥ Runway reservation system

Introduction Radboud University Nijmegen

Abstract data type

Many balanced search trees:

• AVL trees

• B-trees/ 2-3-4 trees

• red-black trees

• scapegoat trees

• . . .

Desired operations/ specification (abstract data type):

• Insert and delete

• Min

• Successor / Predecessor

Different data structures can correspond to same specification(AVL trees, heaps, etc).

Alexandra 14th October 2014 Lesson 6 21 / 21