|
| 1 | +package cn.edu.tju.rico.BinarySearchTree; |
| 2 | + |
| 3 | +public class BinarySearchTree { |
| 4 | + |
| 5 | + private TreeNode root; |
| 6 | + |
| 7 | + /** |
| 8 | + * @description 根据已知序列构建二叉搜索树 |
| 9 | + * @author rico |
| 10 | + * @created 2017年6月3日 下午6:15:54 |
| 11 | + * @param input |
| 12 | + */ |
| 13 | + public BinarySearchTree(int[] input) { |
| 14 | + createBinarySearchTree(input); |
| 15 | + } |
| 16 | + |
| 17 | + /** |
| 18 | + * @description 根据已知序列构建二叉搜索树 |
| 19 | + * @author rico |
| 20 | + * @created 2017年6月3日 下午6:15:06 |
| 21 | + * @param input |
| 22 | + */ |
| 23 | + public void createBinarySearchTree(int[] input) { |
| 24 | + if (input != null) { |
| 25 | + for (int i = 0; i < input.length; i++) { |
| 26 | + root = insert(input[i], root); |
| 27 | + } |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * @description 二叉搜索树的搜索算法,递归算法 |
| 33 | + * @author rico |
| 34 | + * @created 2017年6月3日 下午3:27:43 |
| 35 | + * @param target |
| 36 | + * 目标值 |
| 37 | + * @param root |
| 38 | + * 二叉搜索树的根结点 |
| 39 | + * @return |
| 40 | + */ |
| 41 | + public TreeNode search(int target, TreeNode root) { |
| 42 | + TreeNode result = null; |
| 43 | + if (root != null) { // 递归终止条件 |
| 44 | + if (target == root.data) { // 递归终止条件 |
| 45 | + result = root; |
| 46 | + return result; |
| 47 | + } else if (target < root.data) { // 目标值小于根结点值,从左子树查找 |
| 48 | + result = search(target, root.left); |
| 49 | + } else { // 目标值大于根结点值,从右子树查找 |
| 50 | + result = search(target, root.right); |
| 51 | + } |
| 52 | + } |
| 53 | + return result; |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * @description 二叉搜索树的插入操作 |
| 58 | + * @author rico |
| 59 | + * @created 2017年6月3日 下午5:55:05 |
| 60 | + * @param target |
| 61 | + * @param node |
| 62 | + * @return |
| 63 | + */ |
| 64 | + public TreeNode insert(int target, TreeNode node) { |
| 65 | + if (search(target, node) == null) { |
| 66 | + if (node == null) { |
| 67 | + return new TreeNode(target); |
| 68 | + } else { |
| 69 | + if (target < node.data) { |
| 70 | + node.left = insert(target, node.left); |
| 71 | + } else { |
| 72 | + node.right = insert(target, node.right); |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + return node; |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * @description 删除搜索二叉树的制定结点 |
| 81 | + * @author rico |
| 82 | + * @created 2017年6月3日 下午8:43:29 |
| 83 | + * @param target |
| 84 | + * @param node |
| 85 | + * @return |
| 86 | + */ |
| 87 | + public TreeNode remove(int target, TreeNode node) { |
| 88 | + TreeNode tmp = null; |
| 89 | + if (node != null) { |
| 90 | + if (target < node.data) { // 从左子树删除 |
| 91 | + node.left = remove(target, node.left); |
| 92 | + } else if (target > node.data) { // 从右子树删除 |
| 93 | + node.right = remove(target, node.right); |
| 94 | + } else if (node.left != null && node.right != null) { // 找到待删除结点,且其左右子树不为空 |
| 95 | + // 找到以待删除结点右子树的中序遍历第一个结点(最小结点) |
| 96 | + tmp = node.right; |
| 97 | + while (tmp.left != null) { |
| 98 | + tmp = tmp.left; |
| 99 | + } |
| 100 | + |
| 101 | + // 用最小结点补位待删除结点 |
| 102 | + node.data = tmp.data; |
| 103 | + |
| 104 | + // 删除待删除结点右子树上补位结点 |
| 105 | + node.right = remove(node.data, node.right); |
| 106 | + } else { |
| 107 | + if (node.left == null) { |
| 108 | + node = node.right; |
| 109 | + } else { |
| 110 | + node = node.left; |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + return node; |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * @description 中序遍历二叉搜索树,递归算法,升序排序 |
| 119 | + * @author rico |
| 120 | + * @created 2017年6月3日 下午3:52:54 |
| 121 | + * @param root |
| 122 | + */ |
| 123 | + public void inOrder(TreeNode node) { |
| 124 | + if (node != null) { |
| 125 | + inOrder(node.left); |
| 126 | + System.out.print(root.data + " "); |
| 127 | + inOrder(node.right); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * @description 打印二叉搜索树 |
| 133 | + * @author rico |
| 134 | + * @created 2017年6月3日 下午6:08:42 |
| 135 | + * @param node |
| 136 | + */ |
| 137 | + public void printTree(TreeNode node) { |
| 138 | + if (node != null) { |
| 139 | + System.out.print(node.data); |
| 140 | + if (node.left != null || node.right != null) { |
| 141 | + System.out.print("("); |
| 142 | + printTree(node.left); |
| 143 | + System.out.print(","); |
| 144 | + printTree(node.right); |
| 145 | + System.out.print(")"); |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * @description 访问二叉搜索树的根结点 |
| 152 | + * @author rico |
| 153 | + * @created 2017年6月3日 下午3:54:49 |
| 154 | + * @return |
| 155 | + */ |
| 156 | + public TreeNode getRoot() { |
| 157 | + return root; |
| 158 | + } |
| 159 | +} |
0 commit comments