Skip to content
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package com.github.miniyk2012.coding2017.basic.tree;

import java.util.List;
import java.util.NoSuchElementException;

public class BinarySearchTree<T extends Comparable> {

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

public T findMin() {
if (root == null) {
throw new NoSuchElementException("根节点为空");
}

return findMin(root);
}

private T findMin(BinaryTreeNode<T> root) {
BinaryTreeNode<T> p = root;
while (p.left!=null) {
p = p.left;
}
return p.getData();
}

public T findMax(){
if (root == null) {
throw new NoSuchElementException("根节点为空");
}
return findMax(root);
}

private T findMax(BinaryTreeNode<T> root) {
BinaryTreeNode<T> p = root;
while (p.right!=null) {
p = p.right;
}
return p.getData();
}

public int height() {
if (root == null) {
return 0;
}
return getHeight(root);
}

private int getHeight(BinaryTreeNode<T> root) {
if (root == null) {
return 0;
}
int leftHeight = getHeight(root.left);
int rightHeight = getHeight(root.right);
return 1 + (leftHeight>rightHeight?leftHeight:rightHeight);
}

public int size() {
if (root == null) {
return 0;
}
return getSize(root);
}

private int getSize(BinaryTreeNode<T> root) {
if (root == null) {
return 0;
}
int leftSize = getSize(root.left);
int rightSize = getSize(root.right);
return 1 + leftSize + rightSize;
}
public void remove(T e){
if (root == null) {
throw new NoSuchElementException("根节点为空");
}
root = remove(e, root);
}

private BinaryTreeNode<T> remove(T e, BinaryTreeNode<T> root) {
if (root == null) {
throw new NoSuchElementException("不存在该元素,删除失败");
}
int cmp = root.data.compareTo(e);
if (cmp == 0) {
if (root.left == null) {
return root.right;
}
if (root.right == null) {
return root.left;
}
T min = findMin(root.right);
root.data = min;
root.right = deleteMin(root.right);
} else if (cmp < 0) {
root.right = remove(e, root.right);
} else {
root.left = remove(e, root.left);
}
return root;
}

private BinaryTreeNode<T> deleteMin(BinaryTreeNode<T> root) {
if (root.left == null) {
return root.right;
}
root.left = deleteMin(root.left);
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
@@ -0,0 +1,79 @@
package com.github.miniyk2012.coding2017.basic.tree;

import static org.junit.Assert.fail;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;



public class BinarySearchTreeTest {

BinarySearchTree<Integer> tree = null;

@Before
public void setUp() throws Exception {
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 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 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());
}

@Test
public void testRemoveRoot() {
tree.remove(6);
BinaryTreeNode<Integer> root= tree.getRoot();
Assert.assertEquals(8, root.data.intValue());
Assert.assertEquals(2, root.left.data.intValue());
Assert.assertNull(root.right);
Assert.assertEquals(3, root.left.right.left.data.intValue());

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.github.miniyk2012.coding2017.basic.tree;

public class BinaryTreeNode<T> {

protected T data;
protected BinaryTreeNode<T> left;
protected BinaryTreeNode<T> right;

public BinaryTreeNode(T data){
this.data=data;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
public BinaryTreeNode<T> getLeft() {
return left;
}
public void setLeft(BinaryTreeNode<T> left) {
this.left = left;
}
public BinaryTreeNode<T> getRight() {
return right;
}
public void setRight(BinaryTreeNode<T> right) {
this.right = right;
}

public BinaryTreeNode<T> insert(Object o){
return null;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package com.github.miniyk2012.coding2017.basic.tree;


import java.util.*;

public class BinaryTreeUtil {
/**
* 用递归的方式实现对二叉树的前序遍历, 需要通过BinaryTreeUtilTest测试
* 先访问root,再访问左子树,再访问右子树
* @param root
* @return
*/
public static <T> List<T> preOrderVisit(BinaryTreeNode<T> root) {
List<T> result = new ArrayList<>();
if (root == null) {
return result;
}
result.add(root.getData());
BinaryTreeNode<T> leftTreeNode = root.getLeft();
result.addAll(preOrderVisit(leftTreeNode));
BinaryTreeNode<T> rightTreeNode = root.getRight();
result.addAll(preOrderVisit(rightTreeNode));
return result;
}

/**
* 用递归的方式实现对二叉树的中遍历
* 先左后根再右
* @param root
* @return
*/
public static <T> List<T> inOrderVisit(BinaryTreeNode<T> root) {
List<T> result = new ArrayList<>();
if (root == null) {
return result;
}
BinaryTreeNode<T> leftTreeNode = root.getLeft();
result.addAll(inOrderVisit(leftTreeNode));
result.add(root.getData());
BinaryTreeNode<T> rightTreeNode = root.getRight();
result.addAll(inOrderVisit(rightTreeNode));
return result;
}

/**
* 用递归的方式实现对二叉树的后遍历
* 左右根
* @param root
* @return
*/
public static <T> List<T> postOrderVisit(BinaryTreeNode<T> root) {
List<T> result = new ArrayList<>();
if (root == null) {
return result;
}
BinaryTreeNode<T> leftTreeNode = root.getLeft();
result.addAll(postOrderVisit(leftTreeNode));
BinaryTreeNode<T> rightTreeNode = root.getRight();
result.addAll(postOrderVisit(rightTreeNode));
result.add(root.getData());
return result;
}
/**
* 用非递归的方式实现对二叉树的前序遍历
* @param root
* @return
*/
public static <T> List<T> preOrderWithoutRecursion(BinaryTreeNode<T> root) {

List<T> result = new ArrayList<T>();
Stack<BinaryTreeNode<T>> stack = new Stack<>(); // 存放接下来要访问的节点
if (root == null) {
return result;
}
stack.push(root);
while(!stack.isEmpty()) {
BinaryTreeNode<T> top = stack.pop();
result.add(top.getData());
if (top.getRight() != null) {
stack.add(top.getRight());
}
if (top.getLeft() != null) {
stack.add(top.getLeft());
}
}
return result;
}
/**
* 用非递归的方式实现对二叉树的中序遍历
* 先左后根再右
* @param root
* @return
*/
public static <T> List<T> inOrderWithoutRecursion(BinaryTreeNode<T> root) {

List<T> result = new ArrayList<T>();
if (root == null) {
return result;
}
Stack<BinaryTreeNode<T>> stack = new Stack<>();
stack.push(root);
BinaryTreeNode<T> top = stack.peek();
while (!stack.isEmpty()) {
if (top.getLeft() != null) {
stack.push(top.getLeft());
top = top.getLeft();
} else {
top = stack.pop();
result.add(top.getData());
if (top.getRight() != null) {
stack.push(top.getRight());
top = top.getRight();
}
}
}
return result;
}

}
Loading