forked from onlyliuxin/coding2017
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #65 from zhaohuXing/master
JVM6
- Loading branch information
Showing
6 changed files
with
270 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
- 写一篇文章 | ||
|
||
## 完成情况: | ||
|
||
## 我的收获: | ||
其实最难是坚持 |
64 changes: 64 additions & 0 deletions
64
group11/1178243325/week12/src/main/java/com/sprint/datastructure/tree/BinaryTreeNode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
112 changes: 112 additions & 0 deletions
112
group11/1178243325/week12/src/main/java/com/sprint/datastructure/tree/BinaryTreeUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
group11/1178243325/week12/src/main/java/com/sprint/datastructure/tree/FileList.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
|
||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
...p11/1178243325/week12/src/test/java/com/sprint/datastructure/tree/BinaryTreeUtilTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
|
||
} | ||
} |