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
35 changes: 35 additions & 0 deletions group24/798277403/src/basic/tree/BinaryTreeNode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package 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 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;
}

}
119 changes: 119 additions & 0 deletions group24/798277403/src/basic/tree/BinaryTreeUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package basic.tree;

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>();
prevAdd(root,result);
return result;
}
private static <T> void prevAdd(BinaryTreeNode<T> node,List<T> result){
if(node!=null){
result.add(node.getData());
if(node.getLeft()!=null){
prevAdd(node.getLeft(),result);
}
if(node.getRight()!=null){
prevAdd(node.getRight(),result);
}
}
}

/**
* 用递归的方式实现对二叉树的中遍历
* @param root
* @return
*/
public static <T> List<T> inOrderVisit(BinaryTreeNode<T> root) {
List<T> result = new ArrayList<T>();
inOrderAdd(root,result);
return result;
}
private static <T> void inOrderAdd(BinaryTreeNode<T> node,List<T> result){
if(node!=null){
if(node.getLeft()!=null){
inOrderAdd(node.getLeft(),result);
}
result.add(node.getData());
if(node.getRight()!=null){
inOrderAdd(node.getRight(),result);
}
}
}

/**
* 用递归的方式实现对二叉树的后遍历
*
* @param root
* @return
*/
public static <T> List<T> postOrderVisit(BinaryTreeNode<T> root) {
List<T> result = new ArrayList<T>();
postOrderAdd(root,result);
return result;
}
private static <T> void postOrderAdd(BinaryTreeNode<T> node,List<T> result){
if(node!=null){
if(node.getLeft()!=null){
postOrderAdd(node.getLeft(),result);
}
if(node.getRight()!=null){
postOrderAdd(node.getRight(),result);
}
result.add(node.getData());
}
}
/**
* 用非递归的方式实现对二叉树的前序遍历
* @param root
* @return
*/
public static <T> List<T> preOrderWithoutRecursion(BinaryTreeNode<T> root) {
List<T> result = new ArrayList<T>();
Stack<BinaryTreeNode<T>> stack = new Stack<>();
while(!stack.isEmpty() || root!=null){
//先压入所有左节点,在压入前访问
while(root!=null){
result.add(root.getData());
stack.push(root);
root = root.getLeft();
}
//左节点压入完后压入右节点
if(!stack.isEmpty()){
root = stack.pop();
root = root.getRight();
}
}
return result;
}
/**
* 用非递归的方式实现对二叉树的中序遍历
* @param root
* @return
*/
public static <T> List<T> inOrderWithoutRecursion(BinaryTreeNode<T> root) {
List<T> result = new ArrayList<T>();
Stack<BinaryTreeNode<T>> stack = new Stack<>();
while(!stack.isEmpty() || root!=null){
while(root!=null){
stack.push(root);
root = root.getLeft();
}
if(!stack.isEmpty()){
root = stack.pop();
result.add(root.getData());
root = root.getRight();
}
}
return result;
}

}
73 changes: 73 additions & 0 deletions group24/798277403/src/basic/tree/BinaryTreeUtilTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package basic.tree;

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

}
}
27 changes: 27 additions & 0 deletions group24/798277403/src/basic/tree/FileList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package basic.tree;

import java.io.File;

/**
* 给定一个目录,递归的列出下面所有的子目录和文件
*/
public class FileList {
public static void list(File f) {
if(f.isDirectory()){
File[] files = f.listFiles();
for(File file : files){
if(file.isDirectory()){
System.out.println(file.getName());
list(file);
}else{
System.out.println(file.getName());
}
}
}
}

public static void main(String[] args) {
File f = new File("C:\\Users\\zhouliang\\Desktop\\其他");
list(f);
}
}