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
  45. pBinTree = ^BinTree;
  46. BinTree = object
  47. private
  48. root : pNode;
  49. public
  50. procedure init;
  51. procedure pushNode(data : integer);
  52. function popNode(var data : integer) : boolean;
  53. end;
  54.  
  55.  
  56. procedure BinTree.init;
  57. begin
  58. root := nil
  59. end;
  60.  
  61. procedure BinTree.pushNode(data : integer);
  62. begin
  63. push(root, data);
  64. end;
  65.  
  66. function BinTree.popNode(var data : integer) : boolean;
  67. begin
  68. popNode := pop(root, data);
  69. end;
  70.  
  71. const
  72. N = 10;
  73. MAX = 1000;
  74. var
  75. root : pBinTree;
  76. data, i : integer;
  77. begin
  78. new(root);
  79. root^.init;
  80. randomize;
  81. i := 0;
  82. while (i < 10) do begin
  83. data := random(MAX);
  84. root^.pushNode(data);
  85. i := i + 1
  86. end;
  87. while (root^.popNode(data) = true) do
  88. writeln(data);
  89. dispose(root);
  90. end.
  91. { end }
  92.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty