0% found this document useful (0 votes)
31 views

Lecture 10 ESO207 InventingBinarySearchTrees

The tree on the right is not a binary tree because node u has 3 children violating the property that each node can have at most 2 children.

Uploaded by

Iitian
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

Lecture 10 ESO207 InventingBinarySearchTrees

The tree on the right is not a binary tree because node u has 3 children violating the property that each node can have at most 2 children.

Uploaded by

Iitian
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

Data Structures and Algorithms

(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.

Which of these two ways is better ?

I believe that the second way is better and more effective.


The current lecture is based on this way. The data structure we shall invent is
called a Binary Search Tree. This is the most fundamental and versatile data
structure. We shall realize this fact many times during the course …
3
Doubly Linked List based implementation versus array
based implementation of “List”
Operation Time Complexity per Time Complexity per
operation for array based operation for doubly linked
implementation list based implementation

IsEmpty(L) O(1) O(1)


Search(x,L) O() O()
Successor(p,L) O(1) O(1)
Predecessor(p,L) O(1) O(1)
CreateEmptyList(L) O(1) O(1)
Insert(x,p,L) O() O(1)
Delete(p,L) O() O(1)
MakeListEmpty(L) O(1) O(1)

Arrays are very rigid 4


Problem
Maintain a telephone directory Array based Linked list based
Operations: solution solution
• Search the phone # of a person with ID no. x O()
name O()
Log
O() O(1)
• Insert a new record (ID no., phone #,…)

Can we achieve the best of the two


data structure simultaneously ?

We shall together invent such a novel data structure today


5
Inventing a new data structure

New Data structure

Lists are flexible, so let us try


modifying the linked list structure
to achieve fast search time.

Head Too Rigid for updates

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

Good. Now can we46 modify the list so that


we need to traverse
n/2 at most nodes of
28 the list in the worst case for any search67
n/4 operation ? 3n/4
2 25 31 41 53 65 73 96

1 n/4-1 n/4+1 n/2 -1 n/2 +1 3n/4-1 3n/4+1 n

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

There is no need to distinguish a node from a leaf. So …


leaves

Binary Tree 13
Nature :
a great source of inspiration

Binary Tree 14
Binary Tree: A mathematical model

Definition: A collection of nodes is said to form a binary tree if


1. There is exactly one node with no incoming edge.
This node is called the root of the tree.
2. Every node other than root node has exactly one incoming edge.
3. Each node has at most … two outgoing edges.

Which of these
are not binary
trees ?

15
Binary Tree: some terminologies

• If there is an edge from node u to node v,


then u is called parent of v ,and v is called child of u.
• The Height of a Binary tree T is the maximum number of edges from the
root to any leaf node in the tree T.
T
parent(y) = ?? x u
parent(v) = ?? u
children(y) = ?? {r} x v
children(x) = ?? {y,q}
height(T) = ?? 4 y q subtree(v) z

p subtree(y)
subtree(x) 16
Varieties of Binary trees

We call it Perfectly balanced skewed

T T’
u u

x
x v
y
y
q p z r w
w
p

For every node, the number of nodes in v


the subtrees of its two children differ at
at most by 1.
z

17
Height of a perfectly balanced Binary tree

: Height of a perfectly balanced binary


tree on nodes.
= ? 0

= ? ≤ 𝟏+ 𝑯 𝑛 (2)

𝑛 𝑛
≤ ≤
2 2
𝑛

18
Height of a perfectly balanced Binary tree

: Height of a perfectly balanced binary


tree on nodes.
= ? 0

= ? ≤ 𝟏+ 𝑯 𝑛 (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

Definition: A Binary Tree T storing values is said to be Binary Search Tree


if for each node v in T
• If left(v) <> NULL, then …
value(v) > value of every node in subtree(left(v)).
• If right(v)<>NULL, then …
value(v) < value of every node in subtree(right(v)).
21
Search(T,x)
Searching in a Binary Search Tree
T
Search(T,33) :
Searching for 33 in T.
46

28 67

5 35 49 83

2 25 31 41 48 53 73 96

Homework: Write a neat pseudocode for this operation.


22
Insert(T,x)
Insertion in a Binary Search Tree
T
Insert(T,50) :
Inserting 50 into T.
46

28 67

5 35 49 83

2 25 31 41 48 53 73 96

50

Homework: Write a neat pseudocode for this operation.


23

You might also like