1
0
mirror of synced 2024-09-16 16:46:09 +00:00

Fixed: LookupNode would sometimes return nil after creating a node and balancing the tree

Fixed: SetCursor in LookupNode was not implemented
Fixed: Next followed an incorrect path
This commit is contained in:
Mark van Renswoude 2004-09-01 19:31:28 +00:00
parent f92c475873
commit bc366730a2
5 changed files with 84 additions and 55 deletions

View File

@ -12,14 +12,15 @@ uses
type
TfrmBTree = class(TForm)
ocTree: TdxOrgChart;
procedure ocTreeDblClick(Sender: TObject);
private
FTree: TX2UtCustomBTree;
FTree: TX2CustomBTree;
protected
procedure BuildTree(const ARoot: PX2UtBTreeNode;
procedure BuildTree(const ARoot: PX2BTreeNode;
const AParent: TdxOcNode = nil);
public
class procedure Execute(const ATree: TX2UtCustomBTree;
class procedure Execute(const ATree: TX2CustomBTree;
const AShowModal: Boolean = True);
end;
@ -30,7 +31,7 @@ uses
TypInfo;
type
THackBTree = class(TX2UtCustomBTree);
THackBTree = class(TX2CustomBTree);
{$R *.dfm}
@ -67,7 +68,7 @@ procedure TfrmBTree.BuildTree;
end;
function CreateNode(const AParent: TdxOcNode;
const ANode: PX2UtBTreeNode): TdxOcNode;
const ANode: PX2BTreeNode): TdxOcNode;
begin
Result := CreateTextNode(AParent, IntToStr(ANode^.Index) + #13#10 +
IntToStr(ANode^.Balance));
@ -117,7 +118,7 @@ begin
if Assigned(pNode) and Assigned(pNode.Data) then
begin
FTree.Delete(PX2UtBTreeNode(pNode.Data)^.Index);
FTree.Delete(PX2BTreeNode(pNode.Data)^.Index);
ocTree.Clear();
BuildTree(THackBTree(FTree).Root);

View File

@ -32,6 +32,6 @@
-M
-$M16384,1048576
-K$00400000
-LE"c:\delphi6\Projects\Bpl"
-LN"c:\delphi6\Projects\Bpl"
-LE"c:\program files\borland\delphi6\Projects\Bpl"
-LN"c:\program files\borland\delphi6\Projects\Bpl"
-DmadExcept

View File

@ -56,10 +56,6 @@ HostApplication=
Launcher=
UseLauncher=0
DebugCWD=
[Language]
ActiveLang=
ProjectLang=
RootDir=
[Version Info]
IncludeVerInfo=0
AutoIncBuild=0
@ -86,27 +82,35 @@ ProductName=
ProductVersion=1.0.0.0
Comments=
[Excluded Packages]
c:\program files\borland\delphi6\Projects\Bpl\X2CLGLD.bpl=X²CL GraphicList (Designtime)
c:\program files\borland\delphi6\Bin\dclnet60.bpl=Borland Internet Components
c:\program files\borland\delphi6\Projects\Bpl\VirtualTreesD6D.bpl=Virtual Treeview
C:\PROGRA~1\Borland\Delphi6\Projects\Bpl\JvCoreD6D.bpl=JVCL Core Components
C:\PROGRA~1\Borland\Delphi6\Projects\Bpl\JvStdCtrlsD6D.bpl=JVCL Standard Controls
C:\Program Files\Borland\Delphi6\Projects\Bpl\JvCtrlsD6D.bpl=JVCL Visual Controls
c:\program files\borland\delphi6\Projects\Bpl\dclIndyCore60.bpl=Indy 10 Core Design Time
c:\program files\borland\delphi6\Projects\Bpl\dclIndyProtocols60.bpl=Indy 10 Protocols Design Time
c:\program files\borland\delphi6\Projects\Bpl\BalloonD6.bpl=Balloon 2.0
c:\program files\borland\delphi6\Projects\Bpl\SysILS.bpl=(untitled)
c:\program files\borland\delphi6\Projects\Bpl\DragDropD6.bpl=Drag and Drop Component Suite
C:\Program Files\Borland\Delphi6\Projects\Bpl\JvCustomD6D.bpl=JVCL Custom Controls
C:\Program Files\Borland\Delphi6\Projects\Bpl\JvWizardD6D.bpl=JVCL Wizard Design Time Package
C:\PROGRA~1\Borland\Delphi6\Projects\Bpl\GR32_D6.bpl=Graphics32
c:\program files\borland\delphi6\Projects\Bpl\GR32_DSGN_D6.bpl=Graphics32 Design Time Package
C:\PROGRA~1\Borland\Delphi6\Projects\Bpl\PNGImage_D6.bpl=PNGImage
C:\PROGRA~1\Borland\Delphi6\Projects\Bpl\tbx_d6.bpl=Toolbar2000 -- TBX Extensions (Alex Denisov)
C:\PROGRA~1\Borland\Delphi6\Projects\Bpl\tb2k_d6.bpl=Toolbar2000 Components (Jordan Russell)
c:\program files\borland\delphi6\Projects\Bpl\tbxdsgn_d6.bpl=Toolbar2000 -- TBX Extensions Design Package (Alex Denisov)
C:\PROGRA~1\Borland\Delphi6\Projects\Bpl\tb2kdsgn_d6.bpl=Toolbar2000 Design Package (Jordan Russell)
c:\program files\borland\delphi6\Bin\applet60.bpl=Borland Control Panel Applet Package
F:\Delphi\Components\DevExpress\OrgChart Suite\Lib\dcldxOrgCD6.bpl=ExpressOrgChart by Developer Express Inc.
F:\Delphi\Components\DevExpress\OrgChart Suite\Lib\dcldxDBOrD6.bpl=ExpressDBOrgChart by Developer Express Inc.
F:\Delphi\Components\madCollection\madExcept\Delphi 6\madExceptIde_.bpl=madExceptIde 1.0b · www.madshi.net
c:\program files\borland\delphi6\Bin\dclemacsedit60.bpl=Borland Editor Emacs Enhancements
c:\program files\borland\delphi6\Projects\Bpl\TntUnicodeVcl_D60.bpl=Tnt Unicode Controls
C:\Program Files\Borland\Delphi6\Projects\Bpl\JvJansD6D.bpl=JVCL Jans Components
c:\program files\borland\delphi6\Projects\Bpl\dclIndyCore60.bpl=Indy 10 Core Design Time
c:\program files\borland\delphi6\Projects\Bpl\dclIndyProtocols60.bpl=Indy 10 Protocols Design Time
c:\program files\borland\delphi6\Projects\Bpl\P164_D60.bpl=TurboPower OfficePartner 1.64 Design-time package - VCL60
c:\program files\borland\delphi6\Projects\Bpl\v103_d60.bpl=TurboPower VisualPlanIt 1.03 designtime package - VCL60
c:\program files\borland\delphi6\Projects\Bpl\BalloonD6.bpl=Balloon 2.0
c:\program files\borland\delphi6\Projects\Bpl\SysILS.bpl=(untitled)
c:\program files\borland\delphi6\Projects\Bpl\DragDropD6.bpl=Drag and Drop Component Suite
C:\Program Files\Borland\Delphi6\Projects\Bpl\JvCustomD6D.bpl=JVCL Custom Controls
C:\Program Files\Borland\Delphi6\Projects\Bpl\JvCtrlsD6D.bpl=JVCL Visual Controls
C:\Program Files\Borland\Delphi6\Projects\Bpl\JvWizardD6D.bpl=JVCL Wizard Design Time Package
c:\program files\borland\delphi6\Projects\Bpl\dclIndy60.bpl=Internet Direct (Indy) for D6 Property and Component Editors
c:\program files\borland\delphi6\Bin\dclnet60.bpl=Borland Internet Components
c:\program files\borland\delphi6\Bin\dclsoap60.bpl=Borland SOAP Components
c:\program files\borland\delphi6\Projects\Bpl\ColorPickerButtonD6.bpl=ColorPickerButton
C:\Program Files\Borland\Delphi6\Projects\Bpl\ThreadNameExpert60.bpl=JCL Thread Name IDE expert for Delphi 6
@ -135,7 +139,6 @@ C:\Program Files\Borland\Delphi6\Projects\Bpl\JvXPCtrlsD6D.bpl=JVCL XP Controls
c:\program files\borland\delphi6\Projects\Bpl\GJLSoftwareD5.bpl=GJL Software ExDBGrid Components
c:\program files\borland\delphi6\Projects\Bpl\FREEREP6.bpl=FreeReport 2.32 Components
c:\program files\borland\delphi6\Projects\Bpl\PageControlExD6.bpl=PageControlEx
F:\Delphi\Components\madCollection\madExcept\Delphi 6\madExceptIde_.bpl=madExceptIde 1.0b · www.madshi.net
c:\program files\borland\delphi6\Projects\Bpl\ZCore.bpl=Zeos Core Classes and Intefaces
c:\program files\borland\delphi6\Projects\Bpl\ZParse.bpl=Zeos Parsing Classes and Intefaces
c:\program files\borland\delphi6\Projects\Bpl\ZParseSql.bpl=Zeos SQL Parsing Classes and Intefaces
@ -145,16 +148,12 @@ c:\program files\borland\delphi6\Projects\Bpl\ZComponent.bpl=Zeos Database Compo
c:\program files\borland\delphi6\Projects\Bpl\IconXPD6.bpl=IconXP
c:\program files\borland\delphi6\Projects\Bpl\NLDVDBT_D6D.bpl=NLDVirtualDBTree
c:\program files\borland\delphi6\Projects\Bpl\ff2_d60.bpl=TurboPower FlashFiler Designtime Package - VCL60
C:\PROGRA~1\Borland\Delphi6\Projects\Bpl\GR32_D6.bpl=Graphics32
c:\program files\borland\delphi6\Projects\Bpl\GR32_DSGN_D6.bpl=Graphics32 Design Time Package
c:\program files\borland\delphi6\Projects\Bpl\PNGImage_D6.bpl=PNGImage
c:\program files\borland\delphi6\Projects\Bpl\SynEdit_D6.bpl=SynEdit component suite
c:\program files\borland\delphi6\Projects\Bpl\DelphiX_for5.bpl=DelphiX - DirectX components for Delphi
c:\program files\borland\delphi6\Projects\Bpl\NLDMBD6D.bpl=NLDMessageBox
c:\program files\borland\delphi6\Bin\dcl31w60.bpl=Delphi 1.0 Compatibility Components
c:\program files\borland\delphi6\Projects\Bpl\NLDTrayIconD6.bpl=NLDTrayIcon donated by SVG_1986
c:\program files\borland\delphi6\Projects\Bpl\ServerListD6D.bpl=SOF2Manager - Server List Designtime
C:\WINDOWS\System32\ibevnt60.bpl=Borland Interbase Event Alerter Component
F:\Delphi\Components\madCollection\madBasic\Delphi 6\madHelp_.bpl=madHelp 1.1 · www.madshi.net
F:\Delphi\Components\madCollection\madRemote\Delphi 6\madRemote_.bpl=madRemote 1.1a · www.madshi.net
F:\Delphi\Components\madCollection\madKernel\Delphi 6\madKernel_.bpl=madKernel 1.2z · www.madshi.net
@ -163,7 +162,6 @@ F:\Delphi\Components\madCollection\madSecurity\Delphi 6\madSecurity_.bpl=madSecu
F:\Delphi\Components\madCollection\madShell\Delphi 6\madShell_.bpl=madShell 1.3i · www.madshi.net
c:\program files\borland\delphi6\Projects\Bpl\BMSpinEditD6.bpl=BMSpinEdit
c:\program files\borland\delphi6\Projects\Bpl\GLScene6.bpl=GLScene - OpenGL 3D library
c:\program files\borland\delphi6\Projects\Bpl\TntUnicodeVcl_D60.bpl=Tnt Unicode Controls
c:\program files\borland\delphi6\Bin\dclsmp60.bpl=Borland Sample Components
c:\program files\borland\delphi6\Projects\Bpl\X2MultiMon_D6D.bpl=X2MultiMon Designtime Package
F:\Delphi\Components\ZipForge\Lib\Delphi 6\dclZipForged6.bpl=ZipForge Package
@ -179,7 +177,6 @@ c:\program files\borland\delphi6\Bin\dcltqr60.bpl=TeeChart for QuickReport Compo
c:\program files\borland\delphi6\Bin\dclclxdb60.bpl=Borland CLX Database Components
C:\Program Files\Borland\Delphi6\Bin\dclclxstd60.bpl=Borland CLX Standard Components
c:\program files\borland\delphi6\Bin\dclmcn60.bpl=Borland DataSnap Connection Components
c:\program files\borland\delphi6\Bin\dclemacsedit60.bpl=Borland Editor Emacs Enhancements
c:\program files\borland\delphi6\Bin\dclshlctrls60.bpl=Shell Control Property and Component Editors
c:\program files\borland\delphi6\Bin\DBWEBXPRT.BPL=Borland Web Wizard Package
c:\program files\borland\delphi6\Bin\dclwbm60.bpl=Borland InternetExpress Components
@ -188,4 +185,19 @@ c:\program files\borland\delphi6\Bin\dclwebsnap60.bpl=Borland WebSnap Components
c:\program files\borland\delphi6\Bin\dclite60.bpl=Borland Integrated Translation Environment
c:\program files\borland\delphi6\Bin\dcldbx60.bpl=Borland dbExpress Components
c:\program files\borland\delphi6\Bin\dcldbxcds60.bpl=Borland Local DBX ClientDataset Components
F:\Delphi\Components\DevExpress\OrgChart Suite\Lib\dcldxDBOrD6.bpl=ExpressDBOrgChart by Developer Express Inc.
H:\Downloads\commentexpert.bpl=Comment Expert v1.0 Alpha
c:\program files\borland\delphi6\Projects\Bpl\dclIndy60.bpl=Internet Direct (Indy) for D6 Property and Component Editors
C:\WINDOWS\System32\ibevnt60.bpl=Borland Interbase Event Alerter Component
c:\program files\borland\delphi6\Projects\Bpl\PngComponentsDesigntime.bpl=Png components designtime
[Included Packages]
C:\Program Files\Borland\Delphi6\Bin\dclstd60.bpl=Borland Standard Components
F:\Delphi\Components\madCollection\madBasic\Delphi 6\madBasic_.bpl=madBasic 1.1f · www.madshi.net
F:\Delphi\Components\madCollection\madDisAsm\Delphi 6\madDisAsm_.bpl=madDisAsm 2.0a · www.madshi.net
F:\Delphi\Components\madCollection\madExcept\Delphi 6\madExcept_.bpl=madExcept 2.6a · www.madshi.net
F:\Delphi\Components\madCollection\madExcept\Delphi 6\madExceptWizard_.bpl=madExceptWizard 2.6 · www.madshi.net
c:\program files\borland\delphi6\Bin\dclsmpedit60.bpl=Borland Editor Script Enhancements
C:\PROGRA~1\Borland\Delphi6\Projects\Bpl\ThemeManager6.bpl=Windows XP Theme Manager
C:\Program Files\Borland\Delphi6\Bin\dcldb60.bpl=Borland Database Components
C:\Program Files\Borland\Delphi6\Bin\dclact60.bpl=Borland ActionBar Components
c:\program files\borland\delphi6\Bin\dclcds60.bpl=Borland Base Cached ClientDataset Component
C:\Program Files\Borland\Delphi6\Bin\dclmid60.bpl=Borland MyBase DataAccess Components

View File

@ -12,32 +12,36 @@ uses
var
shData: TX2UtStringHash;
btTest: TX2UtStringBTree;
shData: TX2StringHash;
btTest: TX2StringBTree;
iItem: Integer;
pItem: PX2UtBTreeNode;
pItem: PX2BTreeNode;
begin
// Test binary tree
{
btTest := TX2UtStringBTree.Create();
btTest := TX2StringBTree.Create();
try
Randomize();
for iItem := 0 to 61 do
btTest[Random(500)] := 'bla';
btTest[40] := 'bla2';
btTest[50] := 'bla3';
TfrmBTree.Execute(btTest);
if btTest.Exists(40, True) then
WriteLn(btTest.CurrentValue);
WriteLn;
btTest.Reset();
while btTest.Next() do
WriteLn(btTest.CurrentIndex, ' - ', btTest.CurrentValue);
TfrmBTree.Execute(btTest);
finally
FreeAndNil(btTest);
//ReadLn;
ReadLn;
end;
}
(*
shData := TX2UtStringHash.Create();
try
shData['thisakslhalskdjfhaslkdfjhaslkfjh'] := 'is';
@ -55,4 +59,5 @@ begin
FreeAndNil(shData);
//ReadLn;
end;
*)
end.

View File

@ -528,15 +528,27 @@ begin
pBalance := pCurrent;
if AIndex > pCurrent^^.Index then
begin
// Continue on the right side
pCurrent := @pCurrent^^.Children[CRight]
else if AIndex < pCurrent^^.Index then
pCurrent := @pCurrent^^.Children[CRight];
if ASetCursor then
FParents.Push(pCurrent^, CRight);
end else if AIndex < pCurrent^^.Index then
begin
// Continue on the left side
pCurrent := @pCurrent^^.Children[CLeft]
else
pCurrent := @pCurrent^^.Children[CLeft];
if ASetCursor then
FParents.Push(pCurrent^, CLeft);
end else
begin
// Found it!
Result := pCurrent^;
if ASetCursor then
FCursor := Result;
break;
end;
end else if ACanCreate then
@ -544,6 +556,7 @@ begin
// Create new node
InitNode(pCurrent^);
pCurrent^^.Index := AIndex;
Result := pCurrent^;
// Update balance factors
pLast := pCurrent^;
@ -566,7 +579,6 @@ begin
if Assigned(pBalance) then
BalanceInsert(pBalance^);
Result := pCurrent^;
break;
end else
break;
@ -911,7 +923,7 @@ end;
function TX2CustomBTree.Exists;
begin
Result := Assigned(LookupNode(AIndex, False, True));
Result := Assigned(LookupNode(AIndex, False, ASetCursor));
end;
@ -937,12 +949,13 @@ procedure TX2CustomBTree.Reset;
begin
Cursor := Root;
IsReset := True;
Parents.Clear();
end;
function TX2CustomBTree.Next;
var
iDirection: Integer;
pParent: PX2BTreeNode;
pCurrent: PX2BTreeNode;
begin
Result := False;
@ -958,37 +971,35 @@ begin
if Assigned(Cursor^.Children[CLeft]) then
begin
// Valid left path, follow it
Parents.Push(Cursor);
Parents.Push(Cursor, CLeft);
Cursor := Cursor^.Children[CLeft];
Result := True;
end else if Assigned(Cursor^.Children[CRight]) then
begin
// Valid right path, follow it
Parents.Push(Cursor);
Parents.Push(Cursor, CRight);
Cursor := Cursor^.Children[CRight];
Result := True;
end else
begin
// Neither is valid, traverse back up the parent stack until
// a node if found with a sibling
pCurrent := Cursor;
pParent := Parents.Pop();
// a node is found with a sibling
pParent := Parents.Pop(iDirection);
ClearCursor();
while Assigned(pParent) do
begin
if Assigned(pParent^.Children[CRight]) and
(pParent^.Children[CRight] <> pCurrent) then
if (iDirection = CLeft) and
Assigned(pParent^.Children[CRight]) then
begin
// Parent has a sibling, follow it
Parents.Push(pParent);
Parents.Push(pParent, CRight);
Cursor := pParent^.Children[CRight];
Result := True;
break;
end;
pCurrent := pParent;
pParent := Parents.Pop();
pParent := Parents.Pop(iDirection);
end;
end;
end else