How to insert elements in a heap

4 ビュー (過去 30 日間)
Ken
Ken 2016 年 10 月 31 日
コメント済み: James Tursa 2016 年 11 月 1 日
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

採用された回答

James Tursa
James Tursa 2016 年 10 月 31 日
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 件のコメント
Ken
Ken 2016 年 11 月 1 日
Can't thank you enough - Halloween Treat and all! Will try this and let you know - Regards
James Tursa
James Tursa 2016 年 11 月 1 日
(I've been eating my kid's Halloween candy today and feel generous)

サインインしてコメントする。

その他の回答 (1 件)

Alexandra Harkai
Alexandra Harkai 2016 年 10 月 31 日
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 件のコメント
Ken
Ken 2016 年 10 月 31 日
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.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeStructures についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by