Hi guys, i'm doing a peice of coursework for uni where we are required to implement a Min-heap/priority queue for calculating the Minimum Spanning Tree. In a min heap, the parent's value needs to be smaller than that of its two children. I've tried to implement this,giving the following list as input: test=[17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1].. hoping to get somthing like [1,2,3,4,5,6,7,8,9... etc] as output. I got [1, 2, 6, 3, 8, 7, 4, 10, 9, 15, 16, 12, 5, 11, 14, 17, 13] which is mostly right apart from the positions of elements 2,6 and 12 (0-based numbering). The 1D array represents the heap like so: [root,child1,child2,child1ofchild1,child2ofchild1 etc].
Anyway, i think the algorthim im using is coded right but im not getting the output i should. If anyone could give me some help here i'd be really greatful as ive been fiddling away for ages with this and im pretty sure its somthing small and stupid that's meaning some of the nodes arnt in the right order. Cheers guys.
Im attaching the code as a txt file cos the forum doesnt allow uploading of .py files, just change the extension and it should run!