fork download
  1. program main;
  2.  
  3. type
  4. Node = record
  5. data : integer;
  6. left : ^Node;
  7. right : ^Node
  8. end;
  9. pNode = ^Node;
  10.  
  11. procedure push(var root : pNode; data : integer);
  12. var
  13. r : pNode;
  14. begin
  15. if root = nil then begin
  16. new(r);
  17. r^.data := data;
  18. r^.left := nil;
  19. r^.right := nil;
  20. root := r
  21. end else if data < root^.data then
  22. push(root^.left, data)
  23. else
  24. push(root^.right, data)
  25. end;
  26.  
  27. function pop(var root : pNode; var data : integer) : boolean;
  28. var
  29. r : pNode;
  30. begin
  31. if root = nil then
  32. pop := false
  33. else if root^.left <> nil then
  34. pop := pop(root^.left, data)
  35. else begin
  36. r := root;
  37. root := r^.right;
  38. data := r^.data;
  39. dispose(r);
  40. pop := true
  41. end
  42. end;
  43.  
  44. type BinTree = class
  45. private
  46. root : pNode;
  47. public
  48. constructor Create();
  49. procedure pushNode(data : integer);
  50. function popNode(var data : integer) : boolean;
  51. end;
  52.  
  53. constructor BinTree.Create();
  54. begin
  55. root := nil
  56. end;
  57.  
  58. procedure BinTree.pushNode(data : integer);
  59. begin
  60. push(root, data);
  61. end;
  62.  
  63. function BinTree.popNode(var data : integer) : boolean;
  64. begin
  65. popNode := pop(root, data);
  66. end;
  67.  
  68. const
  69. N = 10;
  70. MAX = 1000;
  71. var
  72. root : BinTree;
  73. data, i : integer;
  74. begin
  75. root := BinTree.Create;
  76. randomize;
  77. i := 0;
  78. while (i < 10) do begin
  79. data := random(MAX);
  80. root.pushNode(data);
  81. i := i + 1
  82. end;
  83. while (root.popNode(data) = true) do
  84. writeln(data);
  85. root.Free
  86. end.
  87. { end }
  88.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty