Java写一个二叉查找树

围巾🧣 2020年03月21日 600次浏览

简介

二叉树成为二叉查找树的性质是,对于树中的每个节点X,它的左子树中所有项的值小于X中的项,而它的右子树中所有项的值大于X中的项。

要求

  • 要有节点类、实现比较器,使得节点可以compareTo,使用泛型表节点数据
  • 成员变量:一个根节点
  • 成员方法:BinarySearchTree、contains、findMin、findMax、insert、remove

代码

  1. public class BinarySearchTree<AnyType extends Comparable<? super AnyType>> {

  2. private BinaryNode<AnyType> root;

  3. public BinarySearchTree() {
  4. this.root = null;
  5. }


  6. public void makeEmpty() {
  7. root = null;
  8. }

  9. public boolean isEmpty() {
  10. return root == null;
  11. }

  12. public boolean contains(AnyType x) {
  13. return contains(x, root);
  14. }

  15. public AnyType findMin() {
  16. if (isEmpty()) {
  17. return null;
  18. }
  19. return findMin(root).element;
  20. }

  21. public AnyType findMax() {
  22. if (isEmpty()) {
  23. return null;
  24. }
  25. return findMax(root).element;
  26. }

  27. public void insert(AnyType x) {
  28. root = insert(x, root);
  29. }

  30. public void remove(AnyType x) {
  31. root = remove(x, root);
  32. }

  33. public void printTree() {

  34. }

  35. /**
  36. * 内部方法查找子树中的项。
  37. *
  38. * @param x 搜索项目
  39. * @param t 子树的根节点
  40. * @return 如果项目被发现,则为真;否则为假
  41. */
  42. private boolean contains(AnyType x, BinaryNode<AnyType> t) {

  43. if (t == null) {
  44. return false;
  45. }
  46. int compareResult = x.compareTo(t.element);

  47. if (compareResult < 0) {
  48. return contains(x, t.left);
  49. } else if (compareResult > 0) {
  50. return contains(x, t.right);
  51. } else {
  52. return true;
  53. }
  54. }

  55. /**
  56. * 递归查找
  57. *
  58. * @param t 子树的根节点
  59. * @return 找到的值
  60. */
  61. private BinaryNode<AnyType> findMin(BinaryNode<AnyType> t) {
  62. if (t == null) {
  63. return null;
  64. } else if (t.left == null) {
  65. return t;
  66. }
  67. return findMin(t.left);
  68. }

  69. private BinaryNode<AnyType> findMax(BinaryNode<AnyType> t) {
  70. if (t != null) {
  71. while (t.right != null) {
  72. t = t.right;
  73. }
  74. }
  75. return t;
  76. }

  77. /**
  78. * 内部方法插入到子树中
  79. *
  80. * @param x 要插入的项
  81. * @param t 子树的根节点
  82. * @return the new root of the subtree
  83. */
  84. private BinaryNode<AnyType> insert(AnyType x, BinaryNode<AnyType> t) {
  85. if (t == null) {
  86. return new BinaryNode<>(x, null, null);
  87. }

  88. // 对比,-1小于,0相等,1大于
  89. int compareResult = x.compareTo(t.element);
  90. if (compareResult < 0) {
  91. t.left = insert(x, t.left);
  92. } else if (compareResult > 0) {
  93. t.right = insert(x, t.right);
  94. } else { // Duplicate; do nothing

  95. }
  96. return t;
  97. }

  98. /**
  99. * 从一颗子树移除
  100. *
  101. * @param x 被移除的项
  102. * @param t 子树的根节点
  103. * @return 新的值
  104. */
  105. private BinaryNode<AnyType> remove(AnyType x, BinaryNode<AnyType> t) {
  106. if (t == null) {
  107. return t;
  108. }
  109. int compareResult = x.compareTo(t.element);

  110. if (compareResult < 0) {
  111. t.left = remove(x, t.left);
  112. } else if (compareResult > 0) {
  113. t.right = remove(x, t.right);
  114. } else if (t.left != null && t.right != null) {
  115. t.element = findMin(t.right).element;
  116. t.right = remove(t.element, t.right);
  117. } else {
  118. t = (t.left != null) ? t.left : t.right;
  119. }
  120. return t;
  121. }

  122. private void printTree(BinaryNode<AnyType> t) {

  123. }


  124. private static class BinaryNode<AnyType> {
  125. AnyType element;
  126. BinaryNode<AnyType> left;
  127. BinaryNode<AnyType> right;

  128. public BinaryNode(AnyType element) {
  129. this.element = element;
  130. }

  131. public BinaryNode(AnyType element, BinaryNode<AnyType> left, BinaryNode<AnyType> right) {
  132. this.element = element;
  133. this.left = left;
  134. this.right = right;
  135. }
  136. }
  137. }