java 二叉查找树_用Java实现二叉查找树

1 /**

2 *@author: wenhx3 * @date: Created in 2019/10/8 19:41 (之前)4 * @description: 二叉查找树的实现5 */

6 public class BinarySearchTree>{7 ​8 /**

9 * 树的根节点10 */

11 private BinaryNoderoot;12 ​13 /**

14 * 定义树的节点(内部类)15 */

16 private static class BinaryNode{17 ​18 AnyType element; //元素值

19 BinaryNode left; //左孩子

20 BinaryNode right; //右孩子

21 ​22 //节点的构造器:初始化一个树的节点

23 BinaryNode(AnyType theElement) {24 this(theElement, null, null);25 }26 ​27 BinaryNode(AnyType theElement, BinaryNode lt, BinaryNodert) {28 element =theElement;29 left =lt;30 right =rt;31 }32 }33 ​34 /**

35 * 二叉排序树的构造器:初始化根节点36 */

37 publicBinarySearchTree() {38 root = null;39 }40 ​41 /**

42 * 置空43 */

44 public voidmakeEmpty() {45 root = null;46 }47 ​48 /**

49 * 判空50 */

51 public booleanisEmpty() {52 return root == null;53 }54 ​55 /**

56 * 寻找最小值57 */

58 publicAnyType findMin() {59 if(isEmpty()) {60 throw newRuntimeException();61 }62 returnfindMin(root).element;63 }64 ​65 /**

66 * 寻找最大值67 */

68 publicAnyType findMax() {69 if(isEmpty()) {70 throw newRuntimeException();71 }72 returnfindMax(root).element;73 }74 ​75 ​76 /**

77 * 是否存在元素x78 */

79 public booleancontains(AnyType x) {80 returncontains(x, root);81 }82 ​83 /**

84 * 插入元素x85 */

86 public voidinsert(AnyType x) {87 root =insert(x, root);88 }89 ​90 /**

91 * 删除元素x92 */

93 public voidremove(AnyType x) {94 root =remove(x, root);95 }96 ​97 /**

98 * 遍历此二叉树99 */

100 public voidprintTree() {101 if(isEmpty()) {102 System.out.println("Empty tree");103 } else{104 printTree(root);105 }106 }107 ​108 /**

109 * 寻找最小值(内部方法):此处用递归实现110 */

111 private BinaryNode findMin(BinaryNodet) {112 if (t == null) {113 return null;114 } else if (t.left == null) {115 returnt;116 }117 returnfindMin(t.left);118 }119 ​120 /**

121 * 寻找最大值(内部方法):此处用非递归实现122 */

123 private BinaryNode findMax(BinaryNodet) {124 if (t != null) {125 while (t.right != null) {126 t =t.right;127 }128 }129 returnt;130 }131 ​132 /**

133 * 是否存在元素x(内部方法)134 */

135 private boolean contains(AnyType x, BinaryNodet) {136 /**

137 * 跳出递归的条件138 */

139 if (t == null) {140 return false;141 }142 ​143 /**

144 * 如果x小于节点值,则递归到左孩子;145 * 如果x大于节点值,则递归到右孩子;146 * 如果x等于节点值,则找到。147 */

148 int compareResult =x.compareTo(t.element);149 ​150 if (compareResult < 0) {151 returncontains(x, t.left);152 } else if (compareResult > 0) {153 returncontains(x, t.right);154 } else{155 return true;156 }157 ​158 }159 ​160 /**

161 * 插入元素x(内部方法)162 */

163 private BinaryNode insert(AnyType x, BinaryNodet) {164 /**

165 * 跳出递归的条件166 */

167 if (t == null) {168 return new BinaryNode<>(x, null, null);169 }170 ​171 /**

172 * 如果x小于节点值,则递归到左孩子;173 * 如果x大于节点值,则递归到右孩子;174 * 如果x等于节点值,则说明已有元素x,无需操作。175 */

176 int compareResult =x.compareTo(t.element);177 ​178 if (compareResult < 0) {179 t.left =insert(x, t.left);180 } else if (compareResult > 0) {181 t.right =insert(x, t.right);182 } else{183 }184 returnt;185 ​186 }187 ​188 /**

189 * 删除元素x(内部方法)190 */

191 private BinaryNode remove(AnyType x, BinaryNodet) {192 /**

193 * 跳出递归的条件194 */

195 if (t == null) {196 return t; //Item not found; do nothing

197 }198 ​199 /**

200 * 如果x小于节点值,则递归到左孩子;201 * 如果x大于节点值,则递归到右孩子;202 * 如果x等于节点值,则要删除此节点。203 */

204 int compareResult =x.compareTo(t.element);205 ​206 if (compareResult < 0) {207 t.left =remove(x, t.left);208 } else if (compareResult > 0) {209 t.right =remove(x, t.right);210 } else if (t.left != null && t.right != null) {211 //要删除的节点有两个孩子(可选用右孩子最小元素/左孩子最大元素上调)

212 t.element =findMin(t.right).element;213 t.right =remove(t.element, t.right);214 } else{215 //要删除的节点有一个孩子或者没有孩子

216 t = (t.left != null) ?t.left : t.right;217 }218 returnt;219 }220 ​221 /**

222 * 遍历此二叉树(内部方法)223 */

224 private void printTree(BinaryNodet) {225 //中序遍历-->即递增顺序

226 if (t != null) {227 printTree(t.left);228 System.out.println(t.element);229 printTree(t.right);230 }231 }232 ​233 /**

234 * 求树的深度(内部方法)235 */

236 private int height(BinaryNodet) {237 if (t == null) {238 return -1;239 } else{240 return 1 +Math.max(height(t.left), height(t.right));241 }242 }243 ​244 /**

245 * 主方法用来测试246 */

247 public static voidmain(String[] args) {248 BinarySearchTree t = new BinarySearchTree<>();249 t.insert(6);250 t.insert(3);251 t.insert(9);252 t.insert(2);253 t.insert(5);254 t.insert(8);255 t.insert(10);256 t.printTree();257 t.insert(4);258 }259 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值