Advertisement
ShadowZek

Untitled

Mar 26th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.48 KB | None | 0 0
  1. public class StrBSTree {
  2. private class Node
  3. {
  4. private String data;
  5. private Node leftChild;
  6. private Node rightChild;
  7. public Node(String aData)
  8. {
  9. data = aData;
  10. leftChild = null;
  11. rightChild = null;
  12. }
  13. }
  14. private Node root;
  15. public StrBSTree()
  16. {
  17. root = null;
  18. }
  19. public void insert(String aData)
  20. {
  21. if (root == null)
  22. {
  23. root = new Node(aData);
  24. }
  25. else
  26. {
  27. insert(root, aData);
  28. }
  29. }
  30. private Node insert(Node aNode, String aData)//Recursive Method
  31. {
  32. if (aNode == null)//Found a leaf
  33. {
  34. aNode = new Node(aData);
  35. }
  36. else if (aData.compareTo(aNode.data) < 0)//Go left
  37. {
  38. aNode.leftChild = insert(aNode.leftChild, aData);
  39. }
  40. else if (aData.compareTo(aNode.data) >= 0)//Go right
  41. {
  42. aNode.rightChild = insert(aNode.rightChild, aData);
  43. }
  44. return aNode;
  45. }
  46. public void remove(String aData)
  47. {
  48. root = remove(root, aData);
  49. }
  50. private Node remove(Node aNode, String aData)
  51. {
  52. //Find the node
  53. if (aNode == null)
  54. {
  55. return null;
  56. }
  57. if (aData.compareTo(aNode.data) < 0)//Left
  58. {
  59. aNode.leftChild = remove(aNode.leftChild, aData);
  60. }
  61. else if (aData.compareTo(aNode.data) > 0)//Right
  62. {
  63. aNode.rightChild = remove(aNode.rightChild, aData);
  64. }
  65. else//Found it
  66. {
  67. if (aNode.rightChild == null)//None or only left child
  68. {
  69. return aNode.leftChild;
  70. }
  71. if (aNode.leftChild == null)//Right child only
  72. {
  73. return aNode.rightChild;
  74. }
  75. //Two children
  76. Node min = findMinInTree(aNode.rightChild);
  77. aNode.data = min.data;
  78. aNode.rightChild = remove(aNode.rightChild, min.data);
  79. }
  80. return aNode;
  81. }
  82. private Node findMinInTree(Node aNode)
  83. {
  84. if (aNode == null)
  85. {
  86. return null;
  87. }
  88. if (aNode.leftChild == null)
  89. {
  90. return aNode;
  91. }
  92. else
  93. {
  94. return findMinInTree(aNode.leftChild);
  95. }
  96. }
  97. public void printPreOrder()
  98. {
  99. printPreOrder(root);
  100. }
  101. private void printPreOrder(Node aNode)
  102. {
  103. if (aNode == null)
  104. {
  105. return;
  106. }
  107. System.out.println(aNode.data);//Process
  108. if (aNode.leftChild != null)//Left
  109. {
  110. printPreOrder(aNode.leftChild);
  111. }
  112. if (aNode.rightChild != null)//Right
  113. {
  114. printPreOrder(aNode.rightChild);
  115. }
  116. return;
  117. }
  118. public int getHeight()
  119. {
  120. return getHeight(root, 0);
  121. }
  122. private int getHeight(Node aNode, int count)
  123. {
  124. if (aNode == null)
  125. {
  126. return count;
  127. }
  128. count++;
  129. return Math.max(getHeight(aNode.leftChild, count), getHeight(aNode.rightChild, count));
  130. }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement