lecture 6: binary search trees csci 700 - algorithms i - cuny
TRANSCRIPT
![Page 1: Lecture 6: Binary Search Trees CSCI 700 - Algorithms I - CUNY](https://reader030.vdocuments.net/reader030/viewer/2022021208/62062c8d8c2f7b1730051b9d/html5/thumbnails/1.jpg)
Lecture 6: Binary Search Trees
CSCI 700 - Algorithms I
Andrew Rosenberg
![Page 2: Lecture 6: Binary Search Trees CSCI 700 - Algorithms I - CUNY](https://reader030.vdocuments.net/reader030/viewer/2022021208/62062c8d8c2f7b1730051b9d/html5/thumbnails/2.jpg)
Last Time
Linear Time Sorting
Counting SortRadix SortBucket Sort
![Page 3: Lecture 6: Binary Search Trees CSCI 700 - Algorithms I - CUNY](https://reader030.vdocuments.net/reader030/viewer/2022021208/62062c8d8c2f7b1730051b9d/html5/thumbnails/3.jpg)
Today
Binary Search Trees
![Page 4: Lecture 6: Binary Search Trees CSCI 700 - Algorithms I - CUNY](https://reader030.vdocuments.net/reader030/viewer/2022021208/62062c8d8c2f7b1730051b9d/html5/thumbnails/4.jpg)
Data Structures
Data structure is a set of elements and the relationshipbetween them.
The appropriate data structure for a task is determined by thefunctions it needs to support.
![Page 5: Lecture 6: Binary Search Trees CSCI 700 - Algorithms I - CUNY](https://reader030.vdocuments.net/reader030/viewer/2022021208/62062c8d8c2f7b1730051b9d/html5/thumbnails/5.jpg)
Data Structures
Data structure is a set of elements and the relationshipbetween them.
The appropriate data structure for a task is determined by thefunctions it needs to support.
A dictionary supports (minimally) Insert, Search andDelete.
Other data structures might need Minimum, Maximum, etc.
![Page 6: Lecture 6: Binary Search Trees CSCI 700 - Algorithms I - CUNY](https://reader030.vdocuments.net/reader030/viewer/2022021208/62062c8d8c2f7b1730051b9d/html5/thumbnails/6.jpg)
Data Structures
Data structure is a set of elements and the relationshipbetween them.
The appropriate data structure for a task is determined by thefunctions it needs to support.
A dictionary supports (minimally) Insert, Search andDelete.
Other data structures might need Minimum, Maximum, etc.
For a restricted domain, D = 1, . . . , k, we can use an array,A[1..k]. This allows Insert, Search and Delete to beO(1).
![Page 7: Lecture 6: Binary Search Trees CSCI 700 - Algorithms I - CUNY](https://reader030.vdocuments.net/reader030/viewer/2022021208/62062c8d8c2f7b1730051b9d/html5/thumbnails/7.jpg)
Data Structures
Data structure is a set of elements and the relationshipbetween them.
The appropriate data structure for a task is determined by thefunctions it needs to support.
A dictionary supports (minimally) Insert, Search andDelete.
Other data structures might need Minimum, Maximum, etc.
For a restricted domain, D = 1, . . . , k, we can use an array,A[1..k]. This allows Insert, Search and Delete to beO(1).
Aside: Hashing attempts to map an unrestricted domain to arestricted one.
![Page 8: Lecture 6: Binary Search Trees CSCI 700 - Algorithms I - CUNY](https://reader030.vdocuments.net/reader030/viewer/2022021208/62062c8d8c2f7b1730051b9d/html5/thumbnails/8.jpg)
Binary Search Tree
Binary Search Trees (BSTs) are a simple and efficientimplementation of a dictionary.
A BST is a rooted binary tree.
The keys are at the nodes.
For every node, v , the keys of the left subtree ≤ key(v)
For every node, v , the keys of the right subtree > key(v)
![Page 9: Lecture 6: Binary Search Trees CSCI 700 - Algorithms I - CUNY](https://reader030.vdocuments.net/reader030/viewer/2022021208/62062c8d8c2f7b1730051b9d/html5/thumbnails/9.jpg)
Binary Search Tree
Binary Search Trees (BSTs) are a simple and efficientimplementation of a dictionary.
A BST is a rooted binary tree.
The keys are at the nodes.
For every node, v , the keys of the left subtree ≤ key(v)
For every node, v , the keys of the right subtree > key(v)
Binary Search Trees are not, by definition, balanced.
![Page 10: Lecture 6: Binary Search Trees CSCI 700 - Algorithms I - CUNY](https://reader030.vdocuments.net/reader030/viewer/2022021208/62062c8d8c2f7b1730051b9d/html5/thumbnails/10.jpg)
Binary Search Tree Data Structure
key
parent
left right
![Page 11: Lecture 6: Binary Search Trees CSCI 700 - Algorithms I - CUNY](https://reader030.vdocuments.net/reader030/viewer/2022021208/62062c8d8c2f7b1730051b9d/html5/thumbnails/11.jpg)
Binary Search Tree Example
root 10
null
5 20
81
![Page 12: Lecture 6: Binary Search Trees CSCI 700 - Algorithms I - CUNY](https://reader030.vdocuments.net/reader030/viewer/2022021208/62062c8d8c2f7b1730051b9d/html5/thumbnails/12.jpg)
Binary Search Tree Example
root 20
null
10
8
5
1
![Page 13: Lecture 6: Binary Search Trees CSCI 700 - Algorithms I - CUNY](https://reader030.vdocuments.net/reader030/viewer/2022021208/62062c8d8c2f7b1730051b9d/html5/thumbnails/13.jpg)
Not a Binary Search Tree Example
root 10
null
5 20
81
![Page 14: Lecture 6: Binary Search Trees CSCI 700 - Algorithms I - CUNY](https://reader030.vdocuments.net/reader030/viewer/2022021208/62062c8d8c2f7b1730051b9d/html5/thumbnails/14.jpg)
Not a Binary Search Tree Example
root 10
null
5 208
1
![Page 15: Lecture 6: Binary Search Trees CSCI 700 - Algorithms I - CUNY](https://reader030.vdocuments.net/reader030/viewer/2022021208/62062c8d8c2f7b1730051b9d/html5/thumbnails/15.jpg)
Height of a BST
The height of a BST is at most n − 1.
The height of a BST is at least log n.
![Page 16: Lecture 6: Binary Search Trees CSCI 700 - Algorithms I - CUNY](https://reader030.vdocuments.net/reader030/viewer/2022021208/62062c8d8c2f7b1730051b9d/html5/thumbnails/16.jpg)
Sorting with a BST
Binary Search Trees are sorted.
Constructing a sorted array is Θ(n)
Traverse(T)
if T.root thenTraverse(T.left)Print T.keyTraverse(T.right)
end if
![Page 17: Lecture 6: Binary Search Trees CSCI 700 - Algorithms I - CUNY](https://reader030.vdocuments.net/reader030/viewer/2022021208/62062c8d8c2f7b1730051b9d/html5/thumbnails/17.jpg)
Sorting with a BST
Binary Search Trees are sorted.
Constructing a sorted array is Θ(n)
Traverse(T)
if T.root thenTraverse(T.left)Print T.keyTraverse(T.right)
end if
Can we prove that this is correct and Θ(n)?
![Page 18: Lecture 6: Binary Search Trees CSCI 700 - Algorithms I - CUNY](https://reader030.vdocuments.net/reader030/viewer/2022021208/62062c8d8c2f7b1730051b9d/html5/thumbnails/18.jpg)
Searching a BST
Search(T, x)
if T then
if T.key = x then
return Telse
if T.key < x then
return Search(T.left, x)else
return Search(T.right, x)end if
end if
else
return nullend if
![Page 19: Lecture 6: Binary Search Trees CSCI 700 - Algorithms I - CUNY](https://reader030.vdocuments.net/reader030/viewer/2022021208/62062c8d8c2f7b1730051b9d/html5/thumbnails/19.jpg)
Searching a BST
Search(T,x) is O(height)
If balanced, height = log n, so O(log n).
Worst case scenario, a sequential search, O(n).
![Page 20: Lecture 6: Binary Search Trees CSCI 700 - Algorithms I - CUNY](https://reader030.vdocuments.net/reader030/viewer/2022021208/62062c8d8c2f7b1730051b9d/html5/thumbnails/20.jpg)
Finding specific elements in a BST
Minimum(T) = O(height). Traverse to the left.
Maximum(T) = O(height). Traverse to the right.
Successor(T) - Find the node with smallest key greaterthan T.key.
![Page 21: Lecture 6: Binary Search Trees CSCI 700 - Algorithms I - CUNY](https://reader030.vdocuments.net/reader030/viewer/2022021208/62062c8d8c2f7b1730051b9d/html5/thumbnails/21.jpg)
Successor(T)
Successor(T)
If T has a right child, then return Minimum(T .right)If T has no right child, and is a left child, then return T .parent
If T has no right child and is a right child, then traverse up until aleft child is found - then this node’s parent.Else T has no successor.
Successor(T) = O(height)
![Page 22: Lecture 6: Binary Search Trees CSCI 700 - Algorithms I - CUNY](https://reader030.vdocuments.net/reader030/viewer/2022021208/62062c8d8c2f7b1730051b9d/html5/thumbnails/22.jpg)
Inserting an Element into a BST
Insert(T,x)
if T then
if T.key ≤ x then
Insert(T.left, x)else
Insert(T.right, x)end if
else
T ← NewNode(x).end if
Insert is a lot like Search.
Insert(T,x) = O(height)
![Page 23: Lecture 6: Binary Search Trees CSCI 700 - Algorithms I - CUNY](https://reader030.vdocuments.net/reader030/viewer/2022021208/62062c8d8c2f7b1730051b9d/html5/thumbnails/23.jpg)
Building a BST
To build a BST, Insert n random elements in order to anempty BST.
It takes O(n log n) to build a BST from such a set of randomelements.
Run Insert n times.∑
n
i=1log i = n log n
Expected height = O(log n)
![Page 24: Lecture 6: Binary Search Trees CSCI 700 - Algorithms I - CUNY](https://reader030.vdocuments.net/reader030/viewer/2022021208/62062c8d8c2f7b1730051b9d/html5/thumbnails/24.jpg)
Building a BST
To build a BST, Insert n random elements in order to anempty BST.
It takes O(n log n) to build a BST from such a set of randomelements.
Run Insert n times.∑
n
i=1log i = n log n
Expected height = O(log n)
Can a BST be constructed in less than O(n log n)?
![Page 25: Lecture 6: Binary Search Trees CSCI 700 - Algorithms I - CUNY](https://reader030.vdocuments.net/reader030/viewer/2022021208/62062c8d8c2f7b1730051b9d/html5/thumbnails/25.jpg)
Building a BST
To build a BST, Insert n random elements in order to anempty BST.
It takes O(n log n) to build a BST from such a set of randomelements.
Run Insert n times.∑
n
i=1log i = n log n
Expected height = O(log n)
Can a BST be constructed in less than O(n log n)?
No. It’s equivalent to a comparison sort. Since Comparisonsorting is Ω(n log n), constructing a BST is Ω(n log n).
![Page 26: Lecture 6: Binary Search Trees CSCI 700 - Algorithms I - CUNY](https://reader030.vdocuments.net/reader030/viewer/2022021208/62062c8d8c2f7b1730051b9d/html5/thumbnails/26.jpg)
Delete(T,v)
Delete(T,v)
If v is a leaf, delete v .If v has 1 child, delete v , replace v with its child.If v has 2 children, swap v with Successor(v), then Delete(v).
How long does each case take?
![Page 27: Lecture 6: Binary Search Trees CSCI 700 - Algorithms I - CUNY](https://reader030.vdocuments.net/reader030/viewer/2022021208/62062c8d8c2f7b1730051b9d/html5/thumbnails/27.jpg)
Delete(T,v)
Delete(T,v)
If v is a leaf, delete v .If v has 1 child, delete v , replace v with its child.If v has 2 children, swap v with Successor(v), then Delete(v).
How long does each case take?
How can we be sure Delete(v) terminates?
![Page 28: Lecture 6: Binary Search Trees CSCI 700 - Algorithms I - CUNY](https://reader030.vdocuments.net/reader030/viewer/2022021208/62062c8d8c2f7b1730051b9d/html5/thumbnails/28.jpg)
Delete(T,v)
Delete(T,v)
If v is a leaf, delete v .If v has 1 child, delete v , replace v with its child.If v has 2 children, swap v with Successor(v), then Delete(v).
How long does each case take?
How can we be sure Delete(v) terminates?
Show that this holds the BST properties.
![Page 29: Lecture 6: Binary Search Trees CSCI 700 - Algorithms I - CUNY](https://reader030.vdocuments.net/reader030/viewer/2022021208/62062c8d8c2f7b1730051b9d/html5/thumbnails/29.jpg)
Recap
Binary Search Trees are an efficient, simple dictionary datastructure.
Construction O(n log n)
Insertion O(log n)
Search O(log n)
Deletion O(log n)
Binary Search Trees are sorted representations of data.
![Page 30: Lecture 6: Binary Search Trees CSCI 700 - Algorithms I - CUNY](https://reader030.vdocuments.net/reader030/viewer/2022021208/62062c8d8c2f7b1730051b9d/html5/thumbnails/30.jpg)
Bye
Next time
Heaps.