Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.59 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class PE_TREE {
  4.  
  5. public static void main(String[] args) {
  6. Scanner in = new Scanner(System.in);
  7. BTree bTree = new BTree();
  8.  
  9. while(in.hasNextLine())
  10. {
  11. String s = in.nextLine();
  12. if (s.length() < 1) break;
  13. bTree.add(Integer.parseInt(s));
  14. }
  15.  
  16. bTree.postOrder(bTree.root);
  17. }
  18. }
  19.  
  20. class BTree {
  21. TNode root = null;
  22. public void add(int value)
  23. {
  24. if (root == null)
  25. root = new TNode(value);
  26. else
  27. {
  28. TNode current = this.root;
  29. while(true)
  30. {
  31. if (value < current.data)
  32. {
  33. if (current._lft == null)
  34. {
  35. current._lft = new TNode(value);
  36. return;
  37. }
  38. else
  39. current = current._lft;
  40. }
  41. else
  42. {
  43. if (current._rft == null)
  44. {
  45. current._rft = new TNode(value);
  46. return;
  47. }
  48. else
  49. current = current._rft;
  50. }
  51. }
  52. }
  53. }
  54.  
  55. public void postOrder(TNode node)
  56. {
  57. if (node != null)
  58. {
  59. postOrder(node._lft);
  60. postOrder(node._rft);
  61. System.out.println(node.data);
  62. }
  63. }
  64. }
  65.  
  66. class TNode {
  67. int data;
  68. TNode _lft;
  69. TNode _rft;
  70. TNode (int data) {
  71. this.data = data;
  72. }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement