How to insert elements in a heap

17 visualizzazioni (ultimi 30 giorni)
Ken
Ken il 31 Ott 2016
Commentato: James Tursa il 1 Nov 2016
I am trying to insert elements on to a heap with the code below but get error "conversion to double from struct not possible"
ElementsToInsert = [];
ElementsToInsert(1).pos = [3,1]; ElementsToInsert(1).key = 4;
ElementsToInsert(2).pos = [3,2]; ElementsToInsert(2).key = 2;
ElementsToInsert(3).pos = [3,3]; ElementsToInsert(3).key = 1;
ElementsToInsert(4).pos = [4,2]; ElementsToInsert(4).key = 3;
BinMinHeap = [];
for n=1:1:length(ElementsToInsert)
%insert ElementsToInsert(n) at the end of the heap
BinMinHeap(n)=ElementsToInsert(n)
while BinMinHeap(n)<=BinMinHeap(n-1)
swap(BinMinHeap(n),BinMinHeap(n-1))
%BinMinHeap(n)=BinMinHeap(n-1)
...;
end
end

Risposta accettata

James Tursa
James Tursa il 31 Ott 2016
You can clear the variables instead of initializing them to empty doubles. E.g.
% ElementsToInsert = [];
clear ElementsToInsert % <-- clear the variable
But then you will run into an error using the index n-1 when n=1 in your loop. So maybe make this change also:
% BinMinHeap = [];
BinMinHeap = ElementsToInsert(1); % <-- Initialize to 1st element
for n=2:1:length(ElementsToInsert) % <-- Starting index changed from 1 to 2
Then you will run into an error on this line for comparing structs:
while BinMinHeap(n)<=BinMinHeap(n-1)
To fix this, I don't know what you really want to compare here. Maybe the key? E.g.,
while BinMinHeap(n).key<=BinMinHeap(n-1).key
But even with this change, I have to wonder why you have this in a while loop. Are you intending to bubble the latest entry up to its appropriate spot? If so, you will need to change the indexing you use in this while loop.
Then this line looks suspicious:
swap(BinMinHeap(n),BinMinHeap(n-1))
It appears you want to swap two element of BinMinHeap, but calling a function for this likely will not do what you intend since functions in general work with copies of the inputs. Maybe you meant this?
% swap(BinMinHeap(n),BinMinHeap(n-1))
temp = BinMinHeap(n);
BinMinHeap(n) = BinMinHeap(n-1);
BinMinHeap(n-1) = temp;
Are you trying to code up a simple insertion or bubble sort?
  10 Commenti
Ken
Ken il 1 Nov 2016
Can't thank you enough - Halloween Treat and all! Will try this and let you know - Regards
James Tursa
James Tursa il 1 Nov 2016
(I've been eating my kid's Halloween candy today and feel generous)

Accedi per commentare.

Più risposte (1)

Alexandra Harkai
Alexandra Harkai il 31 Ott 2016
BinMinHeap(n)=ElementsToInsert(n)
does not work because of this. You can write instead:
BinMinHeap(n)=deal(ElementsToInsert(n))
However, there will be problems with this still, because the comparison is not a valid operation on structs:
BinMinHeap(n)<=BinMinHeap(n-1)
so you could compare the keys instead:
BinMinHeap(n).key<=BinMinHeap(n-1).key
  1 Commento
Ken
Ken il 31 Ott 2016
I want to insert the 4 elements into the heap and then sort them based on their keys i.e. lower keys above higher keys.

Accedi per commentare.

Categorie

Scopri di più su Structures in Help Center e File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by