Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,126 +1,155 @@
package com.johnChnia.coding2017.basic.tree;

import com.johnChnia.coding2017.basic.queue.Queue;
import java.util.List;
import java.util.Objects;

import static org.apache.commons.lang3.ObjectUtils.compare;
import static org.apache.commons.lang3.ObjectUtils.max;

/**
* Created by john on 2017/3/13.
* Created by john on 2017/5/27.
* <p>
* 二叉树:每个节点最多只有两个分支--https://zh.wikipedia.org/wiki/%E4%BA%8C%E5%8F%89%E6%A0%91
* 二叉搜索树:https://zh.wikipedia.org/wiki/%E4%BA%8C%E5%85%83%E6%90%9C%E5%B0%8B%E6%A8%B9
*/
public class BinarySearchTree {


/**
* The root node of tree.
*/
public BstNode root;
public class BinarySearchTree<T extends Comparable> {

private Queue q = new Queue();
BinaryTreeNode<T> root;

public BinarySearchTree(BinaryTreeNode<T> root) {
this.root = root;
}

private static class BstNode {
private int data;
private BstNode left;
private BstNode right;
public BinaryTreeNode<T> getRoot() {
return root;
}

@Override
public String toString() {
return String.valueOf(data);
public T findMin() {
if (Objects.nonNull(root)) {
BinaryTreeNode<T> node = root;
while (Objects.nonNull(node.getLeft())) {
node = node.getLeft();
}
return node.getData();
}
return null;
}

/**
* create an BinarySearchTree.
*
* @param root root node of tree
* @param data stored element
* @return binarySearchTree
*/
public BstNode insert(BstNode root, int data) {
if (root == null) {
root = getNewBstNode(data);
if (this.root == null) {
this.root = root;
public T findMax() {
if (Objects.nonNull(root)) {
BinaryTreeNode<T> node = root;
while (Objects.nonNull(node.getRight())) {
node = node.getRight();
}
return root;
}
if (data <= root.data) {
root.left = insert(root.left, data);
} else {
root.right = insert(root.right, data);
return node.getData();
}
return root;
return null;
}

private BstNode getNewBstNode(int data) {
BstNode node = new BstNode();
node.data = data;
node.left = null;
node.right = null;
return node;

public int height() {
if (Objects.nonNull(root)) {
return height(root);
}
return -1;
}

/**
* Returns the minimum value in the tree.
* Height of a node: Number of edges in longest path from the node to a leaf node
* Height of tree: Height of root
* Height of tree with 1 node: 0
* Depth of a node: Number of edges in path from root to that node
* 树的高度:max(leftTree,rightTree)+ 1
*
* @param root root node of the tree。
* @return the minimum value in the tree
* @throws IllegalArgumentException if root is null.
* @return 树的高度。
*/
public int findMin(BstNode root) {
int min;
if (root == null) {
throw new IllegalArgumentException("tree is empty");
} else if (root.left == null) {
min = root.data;
} else {
min = findMin(root.left);
private int height(BinaryTreeNode<T> root) {
if (Objects.isNull(root)) {
return 0;
}
int lheight = height(root.getLeft());
int rheight = height(root.getRight());

return max(lheight, rheight) + 1;
}

public int size() {
if (Objects.nonNull(root)) {
return size(root);
}
return min;
return -1;
}

/**
* Returns the maximum value in the tree.
* 参考:http://www.geeksforgeeks.org/write-a-c-program-to-calculate-size-of-a-tree/
* Size of a tree is the number of elements present in the tree.
* Size of a tree = Size of left subtree + 1 + Size of right subtree.
*
* @param root root node of the tree。
* @return the maximum value in the tree
* @throws IllegalArgumentException if root is null.
* @return 树的节点个数。
*/
public int findMax(BstNode root) {
int max;
if (root == null) {
throw new IllegalArgumentException("tree is empty");
} else if (root.right == null) {
max = root.data;
} else {
max = findMax(root.right);
private int size(BinaryTreeNode<T> root) {
if (Objects.isNull(root)) {
return 0;
}
return max;

return 1 + size(root.getLeft()) + size(root.getRight());
}


public void remove(T e) {
if (Objects.nonNull(e)) {
remove(root, e);
}
}

/**
* Traverse each node from left to right.
* http://www.geeksforgeeks.org/binary-search-tree-set-2-delete/
*
* @param root root node of the tree
* @param root 根节点
* @param e 删除节点
*/
public void LevelOrder(BstNode root) {
if (root == null) {
return;
}
q.add(root);
while (!q.empty()) {
BstNode current = (BstNode) q.peek();
if (current.left != null) {
q.add(current.left);
}
if (current.right != null) {
q.add(current.right);
private BinaryTreeNode<T> remove(BinaryTreeNode<T> root, T e) {
int flag = compare(e, root.data);
if (flag > 0) {
root.right = remove(root.getRight(), e);
} else if (flag < 0) {
root.left = remove(root.getLeft(), e);
} else {
// 叶子节点
if (Objects.isNull(root.getLeft()) && Objects.isNull(root.getRight())) {
return null;
} else if (Objects.isNull(root.getLeft())) { // 只有一个子节点
return root.getRight();
} else if (Objects.isNull(root.getRight())) {
return root.getLeft();
}
q.remove();
//两个子节点
BinarySearchTree<T> temp = new BinarySearchTree<>(root.getRight());
T data = temp.findMin();
remove(root, data);
root.data = data;
}
return root;
}

public BstNode getRoot() {
return root;
public List<T> levelVisit() {

return null;
}

public boolean isValid() {
return false;
}

public T getLowestCommonAncestor(T n1, T n2) {
return null;

}

public List<T> getNodesBetween(T n1, T n2) {
return null;
}

}


Original file line number Diff line number Diff line change
@@ -1,38 +1,66 @@
package com.johnChnia.coding2017.basic.tree;

import com.johnChnia.coding2017.basic.tree.BinarySearchTree;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.junit.MatcherAssert.assertThat;

/**
* Created by john on 2017/3/14.
* Created by john on 2017/5/27.
*/
public class BinarySearchTreeTest {

private BinarySearchTree bst;
BinarySearchTree<Integer> tree = null;

@Before
public void setUp() throws Exception {
bst = new BinarySearchTree();
bst.insert(bst.getRoot(), 10);
bst.insert(bst.getRoot(), 20);
bst.insert(bst.getRoot(), 9);
bst.insert(bst.getRoot(), 11);
bst.insert(bst.getRoot(), 12);
BinaryTreeNode<Integer> root = new BinaryTreeNode<Integer>(6);
root.left = new BinaryTreeNode<Integer>(2);
root.right = new BinaryTreeNode<Integer>(8);
root.left.left = new BinaryTreeNode<Integer>(1);
root.left.right = new BinaryTreeNode<Integer>(4);
root.left.right.left = new BinaryTreeNode<Integer>(3);
tree = new BinarySearchTree<Integer>(root);
}

@After
public void tearDown() throws Exception {
tree = null;
}

@Test
public void testFindMin() {
Assert.assertEquals(1, tree.findMin().intValue());

}

@Test
public void testFindMin() throws Exception {
assertThat(bst.findMin(bst.getRoot()), equalTo(9));
public void testFindMax() {
Assert.assertEquals(8, tree.findMax().intValue());
}

@Test
public void testHeight() {
Assert.assertEquals(4, tree.height());
}

@Test
public void testSize() {
Assert.assertEquals(6, tree.size());
}

@Test
public void testFindMax() throws Exception {
assertThat(bst.findMax(bst.getRoot()), equalTo(20));
public void testRemoveLeaf() {
tree.remove(4);
BinaryTreeNode<Integer> root = tree.getRoot();
Assert.assertEquals(3, root.left.right.data.intValue());

}

}
@Test
public void testRemoveMiddleNode() {
tree.remove(2);
BinaryTreeNode<Integer> root = tree.getRoot();
Assert.assertEquals(3, root.left.data.intValue());
Assert.assertEquals(4, root.left.right.data.intValue());
}
}