Lecture 10 ESO207 InventingBinarySearchTrees
Lecture 10 ESO207 InventingBinarySearchTrees
(ESO207A)
Lecture 10
Inventing a new Data Structure with
• Flexibility of lists for updates
• Efficiency of arrays for search
1
Recap of the last lecture
2
Important Notice
There are basically two ways of introducing a new/innovative solution of a
problem.
1. One way is to just explain it without giving any clue as to how the person
who invented the concept came up with this solution.
2. Another way is to start from scratch and take a journey of the route
which the inventor might have followed to arrive at the solution.
This journey goes through various hurdles and questions,
each hinting towards a better insight into the problem
if we have patience and open mind.
Lists Array
6
Restructuring doubly linked list
head head
2 5 46 83 96
1 2 n/2 n-1 n
head
46
Since it is sorted n/2 arrangement that
Can we modify Observation:
thesearching
list so53that
2 5
facilitates 41
efficient in we need so 83
an array, 96
1 2 Smaller
to IDs can
traverse now
n/2at be nodes
-1 most searched quickly,
n/2 +1 of the inbut for
list also n-1 n
What structure let
emergesus keep
if you the elements
extend this of the list
idea further
larger
the IDs,
worstwe mayfor
case have
any to traverse
search whole ?list.
operation ?
Imagine
…
sorted according to unique ID numbers of
persons.
head
7
A new data structure emerges
To analyze it
head mathematically, let us
remove irrelevant
46 details for some time.
28 67
5 35 49 83
2 25 31 41 48 53 73 96
8
A new data structure emerges
To analyze it mathematically, remove irrlevant details
Can we relate it to
head something we might
have seen in the real
46 world ?
28 67
5 35 49 83
2 25 31 41 48 53 73 96
9
Nature :
a great source of inspiration
leaves
joints
branches
root
10
Nature :
a great source of inspiration
11
Nature :
a great source of inspiration
root
branches
joints
leaves
12
Nature :
a great source of inspiration
root
Nodes edges
Binary Tree 13
Nature :
a great source of inspiration
Binary Tree 14
Binary Tree: A mathematical model
Which of these
are not binary
trees ?
15
Binary Tree: some terminologies
p subtree(y)
subtree(x) 16
Varieties of Binary trees
T T’
u u
x
x v
y
y
q p z r w
w
p
17
Height of a perfectly balanced Binary tree
= ? ≤ 𝟏+ 𝑯 𝑛 (2)
𝑛 𝑛
≤ ≤
2 2
𝑛
18
Height of a perfectly balanced Binary tree
= ? ≤ 𝟏+ 𝑯 𝑛 (2)
𝑛 𝑖
≤
4
𝑛
19
Implementing a Binary tree
v value(v)
left(v) right(v)
20
Binary Search Tree (BST)
head
46
28 67
5 35 49 83
2 25 31 41 48 53 73 96
28 67
5 35 49 83
2 25 31 41 48 53 73 96
28 67
5 35 49 83
2 25 31 41 48 53 73 96
50