Verfasst am: 23.04.2015, 21:18
Titel: Arbeiten mit Klassen/Unterklassen in Octave
Hi ich würde gerne mit der Klasse Heaps in octave arbeiten. Nun habe ich folgende Matlab implementierungen der Klassen gefunden:
die Unterklasse :MinHeap:
Code:
classdef MinHeap < Heap
%--------------------------------------------------------------------------
% Class: MinHeap < Heap (& handle) % % Constructor: H = MinHeap(n); % H = MinHeap(n,x0); % % Properties: (none) % % Methods: H.InsertKey(key); % sx = H.Sort(); % min = H.ReturnMin(); % min = H.ExtractMin(); % count = H.Count(); % capacity = H.Capacity(); % bool = H.IsEmpty(); % bool = H.IsFull(); % H.Clear(); % % Description: This class implements a min-heap of numeric keys % % Author: Brian Moore % brimoor@umich.edu % % Date: January 16, 2014
%--------------------------------------------------------------------------
%
% Public methods
%
methods(Access = public)
%
% Constructor
%
function this = MinHeap(varargin)
%----------------------- Constructor --------------------------
% Syntax: H = MinHeap(n); % H = MinHeap(n,x0); % % Inputs: n is the maximum number of keys that H can hold % % x0 is a vector (of length <= n) of numeric keys % to insert into the heap during initialization % % Description: Creates a min-heap with capacity n
%--------------------------------------------------------------
% Call base class constructor
this=this@Heap(varargin{:});
%
% Insert key
%
function InsertKey(this,key)
%------------------------ InsertKey ---------------------------
% Syntax: H.InsertKey(key); % % Inputs: key is a number % % Description: Inserts key into H
%--------------------------------------------------------------
this.SetLength(this.k + 1);
this.x(this.k) = inf;
this.DecreaseKey(this.k,key);
end
%
% Sort the heap
%
function sx = Sort(this)
%-------------------------- Sort ------------------------------
% Syntax: sx = H.Sort(); % % Outputs: sx is a vector taht contains the sorted % (ascending order) keys in H % % Description: Returns the sorted values in H
%--------------------------------------------------------------
% Sort the heap
nk = this.k; % virtual heap size during sorting procedure for i = this.k:-1:2
this.Swap(1,i);
nk = nk - 1;
this.MinHeapify(1,nk);
end
this.x(1:this.k) = flipud(this.x(1:this.k));
sx = this.x(1:this.k);
end
%
% Return minimum element
%
functionmin = ReturnMin(this)
%------------------------ ReturnMin ---------------------------
% Syntax: min = H.ReturnMin(); % % Outputs: min is the minimum key in H % % Description: Returns the minimum key in H
%--------------------------------------------------------------
%
% Extract minimum element
%
functionmin = ExtractMin(this)
%------------------------ ExtractMin --------------------------
% Syntax: min = H.ExtractMin(); % % Outputs: min is the minimum key in H % % Description: Returns the minimum key in H and extracts it % from the heap
%--------------------------------------------------------------
this.SetLength(this.k - 1);
min = this.x(1);
this.x(1) = this.x(this.k + 1);
this.MinHeapify(1);
end end
%
% Private methods
%
methods(Access = private)
%
% Decrease key at index i
%
function DecreaseKey(this,i,key) if(i > this.k) % Index overflow error
MinHeap.IndexOverflowError();
elseif(key > this.x(i)) % Decrease key error
MinHeap.DecreaseKeyError();
end
this.x(i) = key;
while((i > 1) && (this.x(Heap.parent(i)) > this.x(i)))
this.Swap(i,Heap.parent(i));
i = Heap.parent(i);
end end
%
% Build the min heap
%
function BuildMinHeap(this) for i = floor(this.k / 2):-1:1
this.MinHeapify(i);
end end
%
% Maintain the min heap property at a given node
%
function MinHeapify(this,i,size) % Parse inputs if(nargin < 3) size = this.k;
end
ll = Heap.left(i);
rr = Heap.right(i);
if((ll <= size) && (this.x(ll) < this.x(i)))
smallest = ll;
else
smallest = i;
end if((rr <= size) && (this.x(rr) < this.x(smallest)))
smallest = rr;
end if(smallest ~= i)
this.Swap(i,smallest);
this.MinHeapify(smallest,size);
end end end
%
% Private static methods
%
methods(Access = private, Static = true)
%
% Decrease key error
%
function DecreaseKeyError() error('You can only decrease keys in MinHeap');
end
%
% Index overflow error
%
function IndexOverflowError() error('MinHeap index overflow');
end end end
classdef Heap < handle
%
% Abstract superclass for all heap classes
%
% Note: You cannot instantiate Heap objects directly; use MaxHeap or % MinHeap
%
%
% Protected properties
%
properties(Access = protected)
k; % current number of elements
n; % heap capacity
x; % heap array end
%
% Public methods
%
methods(Access = public)
%
% Constructor
%
function this = Heap(n,x0) % Initialize heap if(n == 0)
Heap.ZeroCapacityError();
end
this.n = n;
this.x = nan(n,1);
if((nargin == 2) && ~isempty(x0)) % Insert given elements
k0 = numel(x0);
if(k0 > n) % Heap overflow
Heap.OverflowError();
else
this.x(1:k0) = x0(:);
this.SetLength(k0);
end else % Empty heap
this.Clear();
end end
%
% Return number of elements in heap
%
function count = Count(this)
%-------------------------- Count -----------------------------
% Syntax: count = H.Count(); % % Outputs: count is the number of values in H % % Description: Returns the number of values in H
%--------------------------------------------------------------
%
% Return heap capacity
%
function capacity = Capacity(this)
%------------------------- Capacity ---------------------------
% Syntax: capacity = H.Capacity(); % % Outputs: capacity is the size of H % % Description: Returns the maximum number of values that can % fit in H
%--------------------------------------------------------------
%
% Check for full heap
%
function bool = IsFull(this)
%-------------------------- IsFull ----------------------------
% Syntax: bool = H.IsFull(); % % Outputs: bool = {true,false} % % Description: Determines if H is full
%--------------------------------------------------------------
Habe diese zwei klassen in den selben ordner gesteckt wie meinen Algorithmus, und wenn ich darin dann MinHeap definieren will kommt folgende Fehlermeldung:
Code:
>> H=MinHeap(n)
parse error near line49 of file C:\Users\.....\Documents\OCTAVE\MinHeap.m
Du kannst Beiträge in dieses Forum schreiben. Du kannst auf Beiträge in diesem Forum antworten. Du kannst deine Beiträge in diesem Forum nicht bearbeiten. Du kannst deine Beiträge in diesem Forum nicht löschen. Du kannst an Umfragen in diesem Forum nicht mitmachen. Du kannst Dateien in diesem Forum posten Du kannst Dateien in diesem Forum herunterladen
MATLAB, Simulink, Stateflow, Handle Graphics, Real-Time Workshop, SimBiology, SimHydraulics, SimEvents, and xPC TargetBox are registered trademarks and The MathWorks, the L-shaped membrane logo, and Embedded MATLAB are trademarks of The MathWorks, Inc.