Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JVM6 #65

Merged
merged 1 commit into from
May 21, 2017
Merged

JVM6 #65

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
9 changes: 9 additions & 0 deletions group11/1178243325/week12/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
apply plugin: 'java'

repositories {
mavenCentral()
}

dependencies {
testCompile("junit:junit:4.12")
}
11 changes: 11 additions & 0 deletions group11/1178243325/week12/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
## 讲课内容:

## 第12周作业(JVM7)(05-08 至 05-14)
- 数据结构,实现FileList.java(给定一个目录, 递归的列出下面所以的子目录和文件)
- 实现BinaryTreeUtil.java, 包括用递归方式来实现对二叉树的前序,中序,后序遍历,用非递归方式实现对二叉树的前序和中序遍历,需要通过测试用例BinaryTreeUtilTest.java
- 写一篇文章

## 完成情况:

## 我的收获:
其实最难是坚持
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.sprint.datastructure.tree;

public class BinaryTreeNode<T extends Comparable> {
private T data;
private BinaryTreeNode<T> left;
private BinaryTreeNode<T> right;

public BinaryTreeNode(T data) {
this.data = data;
}
//setter && getter
public void setData(T data) {
this.data = data;
}
public T getData() {
return data;
}

public void setLeft(BinaryTreeNode<T> left) {
this.left = left;
}
public BinaryTreeNode<T> getLeft() {
return left;
}

public void setRight(BinaryTreeNode<T> right) {
this.right = right;
}
public BinaryTreeNode<T> getRight() {
return right;
}

public BinaryTreeNode<T> insert(T data) {
//二叉树的基本原则:
// left.data < this.data < right.data
if (this.data == null) {
this.data = data;
return this;
}

int compareResult = this.data.compareTo(data);
if (compareResult > 0) {
//插入的data 小于 this.data
if (this.left == null) {
this.left = new BinaryTreeNode<T>(data);
return this.left;
} else {
return this.left.insert(data);
}
} else if (compareResult < 0) {
//插入的data 大于 this.data
if (this.right == null) {
this.right = new BinaryTreeNode<T>(data);
this.right.data = data;
return this.right;
} else {
return this.right.insert(data);
}
} else {
//二叉树中不存在重复的元素
return this;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package com.sprint.datastructure.tree;

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

private static <T extends Comparable> void preOrderVisit(BinaryTreeNode<T> root, List<T> values) {
if (root != null) {
values.add(root.getData());
preOrderVisit(root.getLeft(), values);
preOrderVisit(root.getRight(), values);
}
}

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

private static <T extends Comparable> void inOrderVisit(BinaryTreeNode<T> root, List<T> values) {
if (root != null) {
inOrderVisit(root.getLeft(), values);
values.add(root.getData());
inOrderVisit(root.getRight(), values);
}
}
/**
* 用递归的方式实现对二叉树的后序遍历,需要通过BinaryTreeUtil测试用例
* @param root
* @return
*/
public static <T extends Comparable> List<T> postOrderVisit(BinaryTreeNode<T> root) {
List<T> result = new ArrayList<T>();
postOrderVisit(root, result);
return result;
}

private static <T extends Comparable> void postOrderVisit(BinaryTreeNode<T> root, List<T> values) {
if (root != null) {
postOrderVisit(root.getLeft(), values);
postOrderVisit(root.getRight(), values);
values.add(root.getData());
}
}

/**
* 用非递归的方式实现二叉树的前序遍历
* @param root
* @return
*/
public static <T extends Comparable> List<T> preOrderWithoutRecursion(BinaryTreeNode<T> root) {
List<T> result = new ArrayList<T>();
Stack<BinaryTreeNode<T>> stack = new Stack<BinaryTreeNode<T>>();
if (root != null) {
stack.push(root);
while (!stack.isEmpty()) {
BinaryTreeNode<T> node = (BinaryTreeNode<T>)stack.peek();
while (node != null) {
result.add(node.getData());
stack.push(node.getLeft());
node = stack.peek();
}
node = stack.pop();
if (!stack.isEmpty()) {
node = stack.pop();
stack.push(node.getRight());
}
}

}
return result;
}

/**
* 用非递归的方式实现二叉树的中序遍历
* @param root
* @return
*/
public static <T extends Comparable> List<T> inOrderWithoutRecursion(BinaryTreeNode<T> root) {
List<T> result = new ArrayList<T>();
Stack<BinaryTreeNode<T>> stack = new Stack<BinaryTreeNode<T>>();
BinaryTreeNode<T> node = root;
while (node != null || !stack.isEmpty()) {
if (node != null) {
stack.push(node);
node = node.getLeft();
} else {
node = stack.pop();
result.add(node.getData());
node = node.getRight();
}
}
return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.sprint.datastructure.tree;

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,66 @@
package com.sprint.datastructure.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));
}

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

}
}