473,387 Members | 1,619 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

heap doesn't appear to work as described

My book says that in a heap, a value at position i will be smaller
than the values at positions 2*i and 2*i + 1. To test that, I ran
this program:
----------
from heapq import *
from random import shuffle

data = range(10)
shuffle(data)
heap = []
for n in data:
heappush(heap, n)
print heap

heappush(heap, .5)
print heap

-----output:------
[0, 1, 3, 4, 2, 6, 7, 9, 8, 5]
[0, 0.5, 3, 4, 1, 6, 7, 9, 8, 5, 2]
----------

In the second heap, at position i=2 there is the value 3. At position
2*i=4, there is the value 1. 3 is not less than 1, which my book
claims should be the case.

Apr 3 '07 #1
7 1528
>My book says that in a heap, a value at position i will be smaller
than the values at positions 2*i and 2*i + 1.
Check the heapq docs for the constraints the Python heapq module maintains:

http://docs.python.org/dev/lib/module-heapq.html

They are different than what you stated above:

Heaps are arrays for which heap[k] <= heap[2*k+1] and heap[k] <=
heap[2*k+2] for all k, counting elements from zero.

Skip
Apr 3 '07 #2
On Apr 3, 5:27 pm, s...@pobox.com wrote:
>My book says that in a heap, a value at position i will be smaller
>than the values at positions 2*i and 2*i + 1.

Check the heapq docs for the constraints the Python heapq module maintains:

http://docs.python.org/dev/lib/module-heapq.html

They are different than what you stated above:

Heaps are arrays for which heap[k] <= heap[2*k+1] and heap[k] <=
heap[2*k+2] for all k, counting elements from zero.

Skip
Thanks

Apr 4 '07 #3

<sk**@pobox.comwrote in message
news:17***********************@montanaro.dyndns.or g...
|
| >My book says that in a heap, a value at position i will be smaller
| >than the values at positions 2*i and 2*i + 1.

I am sure your book either uses 1-based arrays or a 0-based arrays with the
first not used. The need to keep these alternatives in mind is an
unfortunate fact of programming life.

| Check the heapq docs for the constraints the Python heapq module
maintains:
|
| http://docs.python.org/dev/lib/module-heapq.html
|
| They are different than what you stated above:
|
| Heaps are arrays for which heap[k] <= heap[2*k+1] and heap[k] <=
| heap[2*k+2] for all k, counting elements from zero.

If you shift indexes down and substitute i = k+1, then i, 2*i, 2*i+1 are
transformed to (k+1)-1, 2*(k+1)-1, 2*(k+1)+1-1 == k, 2*k+1, 2*(k+1). So
the Python invariant is the same thing in different 'coordinates'.

In the first case h1 is smaller than h2,h3; h2 smaller than h4,h5; etc.
In the second, h0 is smaller than h1,h2; h1 smaller than h3,h4, etc.

Terry Jan Reedy

Apr 4 '07 #4
On Apr 3, 10:30 pm, "Terry Reedy" <tjre...@udel.eduwrote:
<s...@pobox.comwrote in message

news:17***********************@montanaro.dyndns.or g...
|
| >My book says that in a heap, a value at position i will be smaller
| >than the values at positions 2*i and 2*i + 1.

I am sure your book either uses 1-based arrays or a 0-based arrays with the
first not used. The need to keep these alternatives in mind is an
unfortunate fact of programming life.
My book uses lists.

Apr 4 '07 #5

skipCheck the heapq docs for the constraints the Python heapq module
skipmaintains:

s/constraints/invariants/

Skip
Apr 4 '07 #6
>>>My book says that in a heap, a value at position i will be than the
values at positions 2*i and 2*i + 1.
>I am sure your book either uses 1-based arrays or a 0-based arrays
with the first not used. The need to keep these alternatives in mind
is an unfortunate fact of programming life.
My book uses lists.
Yes, but is the first element of the list addressed as element 1 or element
0? Terry was doing the transformation from 1-based to 0-based indexing to
demonstrate that the invariants you described were the same as those
maintained by Python's heapq module.

Skip

Apr 4 '07 #7
On Apr 4, 7:05 am, s...@pobox.com wrote:
>>>My book says that in a heap, a value at position i will be than the
>>>values at positions 2*i and 2*i + 1.
>I am sure your book either uses 1-based arrays or a 0-based arrays
>with the first not used. The need to keep these alternatives in mind
>is an unfortunate fact of programming life.
My book uses lists.

Yes, but is the first element of the list addressed as element 1 or element
0?
Here is the example:
---------
from heapq import *
from random import shuffle

data = range(10)
shuffle(data)
heap = []
for n in data:
heappush(heap, n)
print heap
heappush(heap, 0.5)
print heap

#my test:
print heap[0] #output: 0
-------

It's from Beginning Python: Novice to Professional, which I think is a
really poorly written book chock full of mistakes and ambiguous
example code.


Apr 4 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: ANt | last post by:
Hi, we have some major GC issues at present with a system we're trying to put live. It's a live calculation engine that's distributed across about 30 Java server processes. A set of processes...
14
by: Kevin Grigorenko | last post by:
Hello, I couldn't find an obvious answer to this in the FAQ. My basic question, is: Is there any difference in allocating on the heap versus the stack? If heap or stack implementation is not...
17
by: Jonas Rundberg | last post by:
Hi I just started with c++ and I'm a little bit confused where stuff go... Assume we have a class: class test { private: int arr; };
7
by: S. A. Hussain | last post by:
Where Global variables created in STACK or HEAP in C/C++? ve##tolimitsyahoocom, delete ##
149
by: Christopher Benson-Manica | last post by:
(Followups set to comp.std.c. Apologies if the crosspost is unwelcome.) strchr() is to strrchr() as strstr() is to strrstr(), but strrstr() isn't part of the standard. Why not? --...
1
by: Chris Mullins | last post by:
We've been using the SSLStream class found in System.Net.Security to build a giant Sockets server that provides TLS encryption at the channel leve. Before .Net 2.0, we used an open-source...
53
by: fdmfdmfdm | last post by:
This is an interview question and I gave out my answer here, could you please check for me? Q. What are the memory allocation for static variable in a function, an automatic variable and global...
0
by: JosAH | last post by:
Greetings, I was asked to write a Tip Of the Week; so here goes: a lot of topics are started here in this forum (and a lot of other forums too) mentioning a problem about sorting data. ...
9
by: coder_lol | last post by:
Thanks everybody for helping me with the Syntax confusion! The implicit conversion stuff really got me :) I have one more question... Array<int32ia; Does the above use the default...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.