|
| 1 | +package com.coding.basic; |
| 2 | + |
| 3 | +/** |
| 4 | + * BST 二叉排序树 实现 第14小组 296933284 |
| 5 | + * |
| 6 | + * @author Tonnyson |
| 7 | + * |
| 8 | + */ |
| 9 | +public class BinarySearchTree implements Comparable { |
| 10 | + |
| 11 | + private Object data; |
| 12 | + private BinarySearchTree leftChild; |
| 13 | + private BinarySearchTree rightChild; |
| 14 | + |
| 15 | + public BinarySearchTree() { |
| 16 | + super(); |
| 17 | + this.data = null; |
| 18 | + this.leftChild = null; |
| 19 | + this.rightChild = null; |
| 20 | + } |
| 21 | + |
| 22 | + public BinarySearchTree(Object data) { |
| 23 | + this(); |
| 24 | + this.data = data; |
| 25 | + } |
| 26 | + |
| 27 | + public Object getData() { |
| 28 | + return data; |
| 29 | + } |
| 30 | + |
| 31 | + public void setData(Object data) { |
| 32 | + this.data = data; |
| 33 | + } |
| 34 | + |
| 35 | + public BinarySearchTree getLeftChild() { |
| 36 | + return leftChild; |
| 37 | + } |
| 38 | + |
| 39 | + public void setLeftChild(BinarySearchTree leftChild) { |
| 40 | + this.leftChild = leftChild; |
| 41 | + } |
| 42 | + |
| 43 | + public BinarySearchTree getRightChild() { |
| 44 | + return rightChild; |
| 45 | + } |
| 46 | + |
| 47 | + public void setRightChild(BinarySearchTree rightChild) { |
| 48 | + this.rightChild = rightChild; |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * 向树中插入节点 |
| 53 | + * |
| 54 | + * @param obj |
| 55 | + * 节点值 |
| 56 | + */ |
| 57 | + public void insert(Object obj) { |
| 58 | + insert(obj, this); |
| 59 | + } |
| 60 | + |
| 61 | + private boolean insert(Object obj, BinarySearchTree node) { |
| 62 | + |
| 63 | + BinarySearchTree bstNode = new BinarySearchTree(obj); |
| 64 | + |
| 65 | + if (node == null) { |
| 66 | + node = bstNode; |
| 67 | + return true; |
| 68 | + } else if (node.compareTo(obj) == 0) { |
| 69 | + return true; |
| 70 | + } else if (node.compareTo(obj) > 0) { |
| 71 | + |
| 72 | + if (node.getLeftChild() != null) { |
| 73 | + return insert(obj, node.getLeftChild()); |
| 74 | + } |
| 75 | + |
| 76 | + node.leftChild = bstNode; |
| 77 | + |
| 78 | + } else if (node.compareTo(obj) < 0) { |
| 79 | + |
| 80 | + if (node.getRightChild() != null) { |
| 81 | + return insert(obj, node.getRightChild()); |
| 82 | + } |
| 83 | + |
| 84 | + node.rightChild = bstNode; |
| 85 | + } |
| 86 | + |
| 87 | + return false; |
| 88 | + |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * 中序遍历 BST 的节点,使之有序输出 |
| 93 | + */ |
| 94 | + public void inOrder(BinarySearchTree node) { |
| 95 | + |
| 96 | + if (node != null) { |
| 97 | + inOrder(node.getLeftChild()); |
| 98 | + visit(node); |
| 99 | + inOrder(node.getRightChild()); |
| 100 | + } |
| 101 | + |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * 层序遍历 BST 的节点值 |
| 106 | + */ |
| 107 | + public void levelOrder(BinarySearchTree node) { |
| 108 | + Queue queue = new Queue(); |
| 109 | + BinarySearchTree bstNode = null; |
| 110 | + queue.enQueue(node); |
| 111 | + |
| 112 | + while (!queue.isEmpty()) { |
| 113 | + bstNode = (BinarySearchTree) queue.deQueue(); |
| 114 | + visit(bstNode); |
| 115 | + |
| 116 | + if (bstNode.getLeftChild() != null) { |
| 117 | + queue.enQueue(bstNode.getLeftChild()); |
| 118 | + } |
| 119 | + |
| 120 | + if (bstNode.getRightChild() != null) { |
| 121 | + queue.enQueue(bstNode.getRightChild()); |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * 访问指定节点值 |
| 128 | + * |
| 129 | + * @param node |
| 130 | + */ |
| 131 | + public void visit(BinarySearchTree node) { |
| 132 | + System.out.println(node.getData()); |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * 比较 BST 节点值大小 |
| 137 | + */ |
| 138 | + @Override |
| 139 | + public int compareTo(Object obj) { |
| 140 | + int result = 0; |
| 141 | + |
| 142 | + if (obj instanceof Integer) { |
| 143 | + Integer value = (Integer) obj; |
| 144 | + Integer thisValue = (Integer) this.data; |
| 145 | + result = thisValue.compareTo(value); |
| 146 | + } else { |
| 147 | + String value = obj.toString(); |
| 148 | + result = this.data.toString().compareTo(value); |
| 149 | + } |
| 150 | + |
| 151 | + return result; |
| 152 | + } |
| 153 | + |
| 154 | +} |
0 commit comments