Introduction to Algorithms, Third Edition



Download 4,84 Mb.
Pdf ko'rish
bet105/618
Sana07.04.2022
Hajmi4,84 Mb.
#534272
1   ...   101   102   103   104   105   106   107   108   ...   618
Bog'liq
Introduction-to-algorithms-3rd-edition

Figure 6.1
A max-heap viewed as
(a)
a binary tree and
(b)
an array. The number within the circle
at each node in the tree is the value stored at that node. The number above a node is the corresponding
index in the array. Above and below the array are lines showing parent-child relationships; parents
are always to the left of their children. The tree has height three; the node at index 4 (with value 8)
has height one.
P
ARENT
.i /
1
return
b
i=2
c
L
EFT
.i /
1
return
2i
R
IGHT
.i /
1
return
2i
C
1
On most computers, the L
EFT
procedure can compute
2i
in one instruction by
simply shifting the binary representation of
i
left by one bit position. Similarly, the
R
IGHT
procedure can quickly compute
2i
C
1
by shifting the binary representation
of
i
left by one bit position and then adding in a
1
as the low-order bit. The
P
ARENT
procedure can compute
b
i=2
c
by shifting
i
right one bit position. Good
implementations of heapsort often implement these procedures as “macros” or “in-
line” procedures.
There are two kinds of binary heaps: max-heaps and min-heaps. In both kinds,
the values in the nodes satisfy a
heap property
, the specifics of which depend on
the kind of heap. In a
max-heap
, the
max-heap property
is that for every node
i
other than the root,

P
ARENT
.i /
AŒi ;
that is, the value of a node is at most the value of its parent. Thus, the largest
element in a max-heap is stored at the root, and the subtree rooted at a node contains


6.1
Heaps
153
values no larger than that contained at the node itself. A

Download 4,84 Mb.

Do'stlaringiz bilan baham:
1   ...   101   102   103   104   105   106   107   108   ...   618




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish