![]() |
Thats Insane - wrong memory setting by recursion???
thats insane, try to run this...you will get the most highest parent (node 0) as result, but try to run it with ShowMessage('val '+ValToStr+' res'+inttostr(result)); and you will get the right result...also you get the right result by adding result:=result;
but WHY????????????????????????????????????? OMG WHY???????????????????
Delphi-Quellcode:
type TBinTree=class
Value:integer; Left,Right: TBinTree; ... function FindMin(init:boolean):integer; end; function TBinTree.FindMin(init:boolean):integer; begin //ShowMessage('val '+ValToStr+' res'+inttostr(result)); //result:=result; if value<result then begin result:=value; end; if init then result:=self.Value; if not(left=nil) then result:=left.FindMin(false); if not(right=nil) then result:=right.FindMin(false); end; in prog -> ShowMessage(IntToStr(BinTree.MindMin(true))); HOW CAN SHOWMESSAGE CHANGE THE RESULT OF A RECURSION????? AND WHY HAVE RESULT:=RESULT; THE SAME EFFECT?? |
Re: Thats Insane - wrong memory setting by recursion???
Line 17: there is an "exit" missing -_-
Dust Signs |
Re: Thats Insane - wrong memory setting by recursion???
iam sorry iam not getting it " Line 17: there is an "exit" missing -_- " Can you explain?
|
Re: Thats Insane - wrong memory setting by recursion???
//EDIT: Sorry, I was wrong.
Dust Signs |
Re: Thats Insane - wrong memory setting by recursion???
Zitat:
greetz Mike |
Re: Thats Insane - wrong memory setting by recursion???
Hello,
one problem (but not the only one I guess) with your code is that you haven't set the Result prior to comparing it to "Value". So "Result" is undefined. I think you were trying to do something like this:
Delphi-Quellcode:
xaromz
function TBinTree.FindMin: Integer;
begin Result := Value; // Assume current Value is lowest if Assigned(Left) then Result := Min(Left.FindMin, Result); // Get lowest value [current, left] if Assigned(Right) then Result := Min(Right.FindMin, Result); // Get lowest value [current, right] end; |
Re: Thats Insane - wrong memory setting by recursion???
yes your code is great xaromz, thats what i wanted
but i initialized the value of result, thnaks the init boolean (by the first run, the value of result was set to current), so it wasnt undefined, iam not getting it :( |
Re: Thats Insane - wrong memory setting by recursion???
You're not initializing the local variables at the very beginning of your method.. ;)
|
Re: Thats Insane - wrong memory setting by recursion???
Hello,
Zitat:
Zitat:
Delphi-Quellcode:
Here Result wasn't initialized. Your initializeation followed after that comparison.
if value<result then
Greets xaromz |
Re: Thats Insane - wrong memory setting by recursion???
ok but ist the same also when i put
Delphi-Quellcode:
value is always inicializes and result now too, but why the result changes besides on ShowMessage (again it gives the right value when i dont hide the showmessage dialog)
function TBinTree.FindMin(init:boolean):integer;
begin if init then result:=self.Value; //ShowMessage('val '+ValToStr+' res'+inttostr(result)); if value<result then begin result:=value; end; if not(left=nil) then result:=left.FindMin(false); if not(right=nil) then result:=right.FindMin(false); end; |
Alle Zeitangaben in WEZ +1. Es ist jetzt 17:30 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz