Borrar filtros
Borrar filtros

Extract elements from a heap

3 visualizaciones (últimos 30 días)
Ken
Ken el 2 de Nov. de 2016
Comentada: Ken el 6 de Nov. de 2016
I am trying an assignment on Heaps i.e. how to extract elements and bubble them down:
% binary min-heap container
% let us assume the heap contains the following elements already and
% is in a consistent state
BinMinHeap = [];
BinMinHeap(1).pos = [3,3]; BinMinHeap(1).key = 1;
BinMinHeap(2).pos = [4,2]; BinMinHeap(2).key = 3;
BinMinHeap(3).pos = [3,2]; BinMinHeap(3).key = 2;
BinMinHeap(4).pos = [3,1]; BinMinHeap(4).key = 4;
BinMinHeap(5).pos = [4,3]; BinMinHeap(5).key = 5;
% extract the minimal element from the heap and store it in "ExtractedElement"
ExtractedElement = ...;
%make heap consistent again by first moving the bottom element
%to the top and then down the binary tree
consistent = false;
while ~consistent
...;
end
  4 comentarios
Jan
Jan el 3 de Nov. de 2016
Editada: Jan el 3 de Nov. de 2016
Now please explain, how "minimal" is measured. The minimal key, norm of pos, minimal x or y value or what?
What does "extracting" mean? Do you want to get the value or should the value be removed from the data?
Finally define "consistent". We cannot suggest a method without knowing, what this term means in your special case.
Ken
Ken el 3 de Nov. de 2016
Editada: Ken el 3 de Nov. de 2016
1. Minimal: Each entry is from a graph with x,y cords. The entry contains a key at that point. Minimal is the minimum of the key .e. the element with least key value goes on top. So, in the heap, the minimum-key is on top of the heap, then the next min is below it etc. 2. Extracting: the 5 heap elements are popped out from the heap one-by-one 3. Consistent: The elements in the heap are arranged in a way that the minimum key element is on top, next min. below it etc., so the max key element is at the bottom of the heap.
Thanks

Iniciar sesión para comentar.

Respuesta aceptada

James Tursa
James Tursa el 3 de Nov. de 2016
I am assuming you have a typo in your code above and are really starting with keys in sorted "consistent" order ... i.e., I assume you really have this to start with:
BinMinHeap(2).key = 2;
BinMinHeap(3).key = 3;
To extract the min from the heap, simply this:
ExtractedValue = BinMinHeap(1); % <-- extract the current minimum element
Then all you have to do is "bubble" the others up into their new spots. I.e., write some code to shift element 2 into element 1 spot, then element 3 into element 2 spot, etc. After the shifting is done delete the last element. You can do this using a loop, or using an all-at-once vectorized statement.
  7 comentarios
James Tursa
James Tursa el 4 de Nov. de 2016
Editada: James Tursa el 4 de Nov. de 2016
You missed this change:
n = 1; % <-- need to start comparisons at the top
Also, you need to move that BinMinHeap(nheap) = [] line out of the loop
Ken
Ken el 6 de Nov. de 2016
Thanks James, works fine.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by