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
Expand Up @@ -5,7 +5,9 @@
import org.junit.Before;
import org.junit.Test;

import com.github.ipk2015.coding2017.basic.BinaryTreeNode;
import com.github.ipk2015.coding2017.basic.tree.BinaryTreeNode1;



public class BinaryTreeNodeTest {

Expand All @@ -15,7 +17,7 @@ public void setUp() throws Exception {

@Test
public void testInsert() {
BinaryTreeNode node=new BinaryTreeNode();
BinaryTreeNode1 node=new BinaryTreeNode1();

node.setData(5);
node.insert(2);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package com.github.ipk2015.coding2017.basic.tree;



public class BinaryTreeNode<T> {

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

public BinaryTreeNode(T data){
this.data=data;
}
public BinaryTreeNode(){

}
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(T o){
BinaryTreeNode insertNode=new BinaryTreeNode(o);
BinaryTreeNode compareNode=this;

while(null!=compareNode){
if(null==compareNode.getData()){
compareNode.setData(o);
break;
}else{
Comparable com=(Comparable) compareNode.getData();

int result = com.compareTo(o);

if(result==0){
break;
}else if(result>0){
if(null==compareNode.getLeft()){
compareNode.setLeft(insertNode);
break;
}
compareNode=compareNode.getLeft();
}else if(result<0){
if(null==compareNode.getRight()){
compareNode.setRight(insertNode);
break;
}
compareNode=compareNode.getRight();
}
}
}

return insertNode;
}

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

public class BinaryTreeNode {
public class BinaryTreeNode1 {

private Comparable data;
private BinaryTreeNode left;
private BinaryTreeNode right;
private BinaryTreeNode1 left;
private BinaryTreeNode1 right;

public Object getData() {
return data;
}
public void setData(Comparable data) {
this.data = data;
}
public BinaryTreeNode getLeft() {
public BinaryTreeNode1 getLeft() {
return left;
}
public void setLeft(BinaryTreeNode left) {
public void setLeft(BinaryTreeNode1 left) {
this.left = left;
}
public BinaryTreeNode getRight() {
public BinaryTreeNode1 getRight() {
return right;
}
public void setRight(BinaryTreeNode right) {
public void setRight(BinaryTreeNode1 right) {
this.right = right;
}

public BinaryTreeNode insert(Comparable o){
BinaryTreeNode insertNode=new BinaryTreeNode();
BinaryTreeNode compareNode=this;
public BinaryTreeNode1 insert(Comparable o){
BinaryTreeNode1 insertNode=new BinaryTreeNode1();
BinaryTreeNode1 compareNode=this;
insertNode.setData(o);

while(null!=compareNode){
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
package com.github.ipk2015.coding2017.basic.tree;



import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
/*
* 二叉树的遍历
对于二叉树的遍历方式一般分为三种先序、中序、后序三种方式
先序遍历
若二叉树为空,则不进行任何操作:否则
1、访问根结点。
2、先序方式遍历左子树。
3、先序遍历右子树。
中序遍历
若二叉树为空,则不进行任何操作:否则
1、中序遍历左子树。
2、访问根结点。
3、中序遍历右子树。
后序遍历
若二叉树为空,则不进行任何操作:否则
1、后序遍历左子树。
2、后序遍历右子树。
3、访问根结点。
* */
public class BinaryTreeUtil {
/**
* 用递归的方式实现对二叉树的前序遍历, 需要通过BinaryTreeUtilTest测试
*
* @param root
* @return
*/
public static <T> List<T> preOrderVisit(BinaryTreeNode<T> root) {
List<T> result = new ArrayList<T>();
preOrderNode(root,result);
return result;
}
private static <T> void preOrderNode(BinaryTreeNode<T> node,List<T> list){
if(null == node){
return;
}

T data = node.getData();
if(null != data){
list.add(data);
}

BinaryTreeNode<T> leftNode = node.getLeft();
preOrderNode(leftNode,list);

BinaryTreeNode<T> rightNode = node.getRight();
preOrderNode(rightNode,list);
}
/**
* 用递归的方式实现对二叉树的中遍历
*
* @param root
* @return
*/
public static <T> List<T> inOrderVisit(BinaryTreeNode<T> root) {
List<T> result = new ArrayList<T>();
inOrderNode(root,result);
return result;
}

private static <T> void inOrderNode(BinaryTreeNode<T> node,List<T> list){
if(null == node){
return;
}

BinaryTreeNode<T> leftNode = node.getLeft();
inOrderNode(leftNode,list);

T data = node.getData();
if(null != data){
list.add(data);
}

BinaryTreeNode<T> rightNode = node.getRight();
inOrderNode(rightNode,list);
}
/**
* 用递归的方式实现对二叉树的后遍历
*
* @param root
* @return
*/
public static <T> List<T> postOrderVisit(BinaryTreeNode<T> root) {
List<T> result = new ArrayList<T>();
postOrderNode(root,result);
return result;
}

private static <T> void postOrderNode(BinaryTreeNode<T> node,List<T> list){
if(null == node){
return;
}

BinaryTreeNode<T> leftNode = node.getLeft();
postOrderNode(leftNode,list);

BinaryTreeNode<T> rightNode = node.getRight();
postOrderNode(rightNode,list);

T data = node.getData();
if(null != data){
list.add(data);
}

}
/**
* 用非递归的方式实现对二叉树的前序遍历
* @param root
* @return
*/
public static <T> List<T> preOrderWithoutRecursion(BinaryTreeNode<T> root) {

List<T> result = new ArrayList<T>();
Stack stack = new Stack();
stack.push(root);
while(!stack.isEmpty()){
BinaryTreeNode<T> node = (BinaryTreeNode<T>) stack.pop();
if(null == node){
break;
}
T data = node.getData();
if(null != data){
result.add(data);
}

BinaryTreeNode<T> rightNode = node.getRight();
if(null != rightNode){
stack.push(rightNode);
}
BinaryTreeNode<T> leftNode = node.getLeft();
if(null != leftNode){
stack.push(leftNode);
}
}

return result;
}
/**
* 用非递归的方式实现对二叉树的中序遍历
* @param root
* @return
*/
public static <T> List<T> inOrderWithoutRecursion(BinaryTreeNode<T> root) {

List<T> result = new ArrayList<T>();
Stack nodeStack = new Stack();
Stack midStack = new Stack();
nodeStack.push(root);
while(!nodeStack.isEmpty()){
BinaryTreeNode<T> node = (BinaryTreeNode<T>) nodeStack.pop();
if(null == node){
break;
}

BinaryTreeNode<T> rightNode = node.getRight();
if(null != rightNode){
nodeStack.push(rightNode);
}

BinaryTreeNode<T> leftNode = node.getLeft();
if(null != leftNode){
midStack.push(node.getData());
nodeStack.push(leftNode);
}else{
T data = node.getData();
if(null != data){
result.add(data);
}
if(!midStack.isEmpty()){
result.add((T) midStack.pop());
}
}
}
return result;
}

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

import java.util.List;

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



public class BinaryTreeUtilTest {

BinaryTreeNode<Integer> root = null;
@Before
public void setUp() throws Exception {
root = new BinaryTreeNode<Integer>(1);
root.setLeft(new BinaryTreeNode<Integer>(2));
root.setRight(new BinaryTreeNode<Integer>(5));
root.getLeft().setLeft(new BinaryTreeNode<Integer>(3));
root.getLeft().setRight(new BinaryTreeNode<Integer>(4));
}

@After
public void tearDown() throws Exception {
}

@Test
public void testPreOrderVisit() {

List<Integer> result = BinaryTreeUtil.preOrderVisit(root);
Assert.assertEquals("[1, 2, 3, 4, 5]", result.toString());


}
@Test
public void testInOrderVisit() {


List<Integer> result = BinaryTreeUtil.inOrderVisit(root);
Assert.assertEquals("[3, 2, 4, 1, 5]", result.toString());

}

@Test
public void testPostOrderVisit() {


List<Integer> result = BinaryTreeUtil.postOrderVisit(root);
Assert.assertEquals("[3, 4, 2, 5, 1]", result.toString());

}


@Test
public void testInOrderVisitWithoutRecursion() {
BinaryTreeNode<Integer> node = root.getLeft().getRight();
node.setLeft(new BinaryTreeNode<Integer>(6));
node.setRight(new BinaryTreeNode<Integer>(7));

List<Integer> result = BinaryTreeUtil.inOrderWithoutRecursion(root);
Assert.assertEquals("[3, 2, 6, 4, 7, 1, 5]", result.toString());

}
@Test
public void testPreOrderVisitWithoutRecursion() {
BinaryTreeNode<Integer> node = root.getLeft().getRight();
node.setLeft(new BinaryTreeNode<Integer>(6));
node.setRight(new BinaryTreeNode<Integer>(7));

List<Integer> result = BinaryTreeUtil.preOrderWithoutRecursion(root);
Assert.assertEquals("[1, 2, 3, 4, 6, 7, 5]", result.toString());

}
}
Loading