Simple-Minded Insertions into a Binary Search Tree G B G B Insertion order B B E M J G>E T L G>F P W F insert(M) V C K insert(T) B>A insert(J) G A D insert(P) insert(L) B insert(E) • That’s it; simple minded insertion is pretty simple; the fact that it • The tree as you see it now, above, insert(W)preserves the binary search tree property is.

Download Report

Transcript Simple-Minded Insertions into a Binary Search Tree G B G B Insertion order B B E M J G>E T L G>F P W F insert(M) V C K insert(T) B>A insert(J) G A D insert(P) insert(L) B insert(E) • That’s it; simple minded insertion is pretty simple; the fact that it • The tree as you see it now, above, insert(W)preserves the binary search tree property is.

Slide 1

Simple-Minded Insertions into a
Binary Search Tree
GBGBInsertion
order

BB
E

M
J

G>E

T
L

G>F

P

W

F
insert(M)
V
C
K
insert(T)
B>A
insert(J)
G
A
D
insert(P)
insert(L)
B
insert(E)
• That’s it; simple minded
insertion
is pretty
simple;
the fact that it

The
tree
as
you
see
it
now,
above,
insert(W)preserves the binary search tree property is nice (vital, in fact) but the fact
was build
by performing
the
insertions
insert(Q) • A “simple-minded”
insertion
isshape
based
only
onispreserving
the
that
it
doesn’t
preserve
any
particular
constraint
going
to
be
aa
insert(C)
shown
on the left
(M wasregard
first, then
T,
binary
search
tree
property,
without
to
shape
AVL
treessotoon)
fix
insert(F) problem that we’ll need
then
J, and
insert(K) • The new node becomes the correct child of some existing node so as
• Worst case here, the
insertion
is O(h),
where h is
the
height
of the tree

Now
lets
see
an
animation
of
the
next
insert(A)
to preserve the binary search tree property and such that the pointer
insert(D)
couple
of insertions
• In general
(after
just
simple
minded
and
in this case
from the
new
parent
is the
only insertions),
link adjusted
to certainly
do the insertion
insert(G)here, the tree will not be almost perfect, so we have no idea what the Oinsert(G)
insert(B)
insert(B)
notation would be in terms of n, the number of nodes; worst case would
be O(n), if the tree has only one node at each level