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

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package basic.dataStructure.binaryTree;

/**
* Created by macvi on 2017/4/4.
*/
public class BinaryTreeNode<T> {
// private T data;
// private BinaryTreeNode left;
// private BinaryTreeNode right;
// private BinaryTreeNode before;
//
// private BinaryTreeNode(){}
//
// public BinaryTreeNode(T data){
// this.data = data;
// this.left = null;
// this.right = null;
// }
//
// public void setData(int data){
// BinaryTreeNode node = new BinaryTreeNode(data);
// if(compareTo(data)){
// if(this.left == null){
// this.left = node;
// }else{
// this.left.setData(data);
// }
// }else{
// if(this.right == null){
// this.right = node;
// }else{
// this.right.setData(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 T getData(){
// return data;
// }
//
// private boolean compareTo(int d) {
// System.out.println("data=" + this.data + ", d=" + d);
// return (Integer)this.data > d;
// }
//
// private StringBuffer dataStr = new StringBuffer();
// private int index = 0;
//// public String toString(BinaryTreeNode node) {
//// while (node.left != null || node.right != null){
//// dataStr.append(index + "层,数据=").append(node.data).append("|");
//// if(node.left != null){
//// dataStr.append(node.left.data)
//// }
//// index ++;
//// }
////
//// return dataStr.toString();
//// }
private T data;
private BinaryTreeNode<T> left;
private 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,120 @@
package basic.dataStructure.binaryTree;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

public class BinaryTreeUtil {
/**
* 用递归的方式实现对二叉树的前序遍历, 需要通过BinaryTreeUtilTest测试
*
* @param root
* @return
*/
public static <T> List<T> preOrderVisit(BinaryTreeNode<T> root) {
List<T> result = new ArrayList<T>();

preOrder(root, result);

return result;
}

private static <T> void preOrder(BinaryTreeNode<T> tree, List<T> result){
if(tree == null) return;
result.add(tree.getData());

preOrder(tree.getLeft(), result);
preOrder(tree.getRight(), result);
}

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

private static <T> void inOrder(BinaryTreeNode<T> tree, List<T> result){
if(tree == null) return;

inOrder(tree.getLeft(), result);
result.add(tree.getData());
inOrder(tree.getRight(), result);
}

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

private static <T> void postOrder(BinaryTreeNode<T> tree, List<T> result){
if(tree == null) return;

postOrder(tree.getLeft(), result);
postOrder(tree.getRight(), result);
result.add(tree.getData());
}

/**
* 用非递归的方式实现对二叉树的前序遍历
* @param root
* @return
*/
public static <T> List<T> preOrderWithoutRecursion(BinaryTreeNode<T> root) {
List<T> result = new ArrayList<T>();
Stack<BinaryTreeNode> buffer = new Stack();
BinaryTreeNode<T> tmp = root;
while(tmp != null){
//遍历这一排的左边节点,右边节点入栈缓存
BinaryTreeNode<T> node = tmp;
while(node != null){
result.add(node.getData());
if(node.getRight() != null) buffer.add(node.getRight());
node = node.getLeft();
}

//遍历右边
//栈中没有节点则表示遍历结束
if (buffer.isEmpty()) break;

tmp = buffer.pop();
}

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

BinaryTreeNode<T> tmp = root;
while (tmp != null || !buffer.isEmpty()){
while(tmp != null){
buffer.push(tmp);
tmp = tmp.getLeft();
}

tmp = buffer.pop();
result.add(tmp.getData());
tmp = tmp.getRight();
}
return result;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package basic.dataStructure.binaryTree;

import java.io.File;

public class FileList {
public void list(File f) {
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package data_structure.binaryTree;

import basic.dataStructure.binaryTree.BinaryTreeNode;
import basic.dataStructure.binaryTree.BinaryTreeUtil;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import java.util.List;



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());

}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package data_structure.list;

import org.junit.Test;
import basic.dataStructure.BinaryTreeNode;
import basic.dataStructure.binaryTree.BinaryTreeNode;

/**
* @author : 温友朝
Expand Down