avltreetest.pp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. Program AvlTreeTest;
  2. {
  3. This file is a demo of the Free Component Library (FCL)
  4. Copyright (c) 2009 by Marco van de Voort.
  5. A demo/test of straightforward unit Avl_Tree usage.
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright. Alternately you may also
  8. use this file under a BSD license.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  12. **********************************************************************}
  13. // Simple test of avl_tree unit.
  14. // It adds 1000 randomly generated numbers to both a TBits and an avltree,
  15. // no dupes are allowed in the avltree. Then it compares and deallocates.
  16. //
  17. // While doing this, it counts the total number of compares.
  18. {$mode ObjFPC}{$H+}
  19. Uses avl_tree,Sysutils,Classes;
  20. Const
  21. NumberOfValues = 10000;
  22. Type TDataObject = Class
  23. Name:String;
  24. value : integer;
  25. end;
  26. Var objcompares : integer =0;
  27. keycompares : integer =0;
  28. Function CompareProcObj(Node1, Node2: Pointer): integer;
  29. begin
  30. inc(objcompares);
  31. Result := CompareStr(TDataObject(Node2).name,TDataObject(Node1).name);
  32. end;
  33. Function CompareProcKey(Node1:pointer; Node2: Pointer): integer;
  34. begin
  35. inc(keycompares);
  36. Result := CompareStr(TDataObject(Node2).name,ansistring(Node1));
  37. end;
  38. var
  39. Tree : TAVLTree;
  40. i,value : Integer;
  41. valueStr : String;
  42. Allocated : TBits;
  43. obj : TDataObject;
  44. AVLNode : TAVLTreeNode;
  45. begin
  46. Randomize;
  47. Tree :=TAVLTree.Create(@CompareProcObj);
  48. Allocated:=TBits.Create(NumberOfValues);
  49. // note that the compareproc is different from above.
  50. For I:=0 to NumberOfValues-1 do
  51. begin
  52. value:=Random(NumberOfValues);
  53. valueStr:=inttostr(Value);
  54. If not assigned(Tree.FindKey(pointer(valueStr),@CompareProcKey)) Then
  55. begin
  56. obj:=TDataObject.Create;
  57. obj.name:=ValueStr;
  58. obj.value:=value;
  59. Tree.Add(Obj);
  60. Allocated[value]:=true;
  61. end;
  62. end;
  63. // Key compares (which should be about sum(n=1..NumberOfValues,log(n)/log(2))
  64. // seems to be about 2log(n)-2. I haven't calculated the limit yet.
  65. writeln(Tree.Count, ' unique nodes in the tree');
  66. Writeln('object compares (insert):',objcompares,' (/Tree.count): ',floattostrf(objcompares/tree.count,fffixed,10,2));
  67. Writeln('key compares (find ):',keycompares,' (/',NumberOfValues,'): ',floattostrf(keycompares/NumberOfValues,fffixed,10,2));
  68. // iterating and comparing with the TBits.
  69. AVLNode:=Tree.FindLowest;
  70. while (AVLNode<>nil) do
  71. begin
  72. value:=TDataObject(AVLNode.Data).value;
  73. if not Allocated[value] then
  74. writeln('Oops, missed:',value);
  75. AVLNode:=Tree.FindSuccessor(AVLNode)
  76. end;
  77. // Iterating is compareless as it should be, despite
  78. // the "FINDsuccessor" method name.
  79. Writeln('object compares (insert):',objcompares);
  80. Writeln('key compares (find ):',keycompares);
  81. // Clean up.
  82. Tree.FreeAndClear;
  83. FreeAndNil(Tree);
  84. FreeAndNil(Allocated);
  85. End.