fork download
  1. using System;
  2.  
  3. class Node {
  4. private int data;
  5. private Node left, right;
  6. public static void push(ref Node root, int data) {
  7. if (root == null) {
  8. Node p = new Node();
  9. p.data = data;
  10. p.left = p.right = null;
  11. root = p;
  12. } else if (data < root.data)
  13. push(ref root.left, data);
  14. else
  15. push(ref root.right, data);
  16. }
  17. public static bool pop(ref Node root, ref int data) {
  18. if (root == null)
  19. return false;
  20. if (root.left != null)
  21. return pop(ref root.left, ref data);
  22. else {
  23. data = root.data;
  24. root = root.right;
  25. return true;
  26. }
  27. }
  28. }
  29.  
  30. class BinTree {
  31. private Node root;
  32. public BinTree() { root = null; }
  33. public void pushNode(int data) { Node.push(ref root, data); }
  34. public bool popNode(ref int data) { return Node.pop(ref root, ref data); }
  35. }
  36.  
  37. class Sample {
  38. public static void Main() {
  39. const int n = 10;
  40. const int max = 1000;
  41. const int seed = 31415926;
  42. Random r = new Random(seed);
  43. BinTree root = new BinTree();
  44. for (int i = 0; i < n; i++)
  45. root.pushNode(r.Next(max));
  46. int data = 0;
  47. Console.WriteLine("C#");
  48. while (root.popNode(ref data))
  49. Console.WriteLine(data);
  50. }
  51. }
  52. /* end */
  53.  
Success #stdin #stdout 0.03s 37064KB
stdin
Standard input is empty
stdout
C#
72
202
293
325
354
362
504
578
594
931