From 80e77d02a986f8f6c23ae9de429b0a2e9acdf990 Mon Sep 17 00:00:00 2001 From: x_zhaohu Date: Sat, 13 May 2017 20:52:36 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90BinaryTreeNodeUtil.java?= =?UTF-8?q?=E5=B9=B6=E9=80=9A=E8=BF=87=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- group11/1178243325/week12/build.gradle | 9 ++ group11/1178243325/week12/readme.md | 11 ++ .../datastructure/tree/BinaryTreeNode.java | 64 ++++++++++ .../datastructure/tree/BinaryTreeUtil.java | 112 ++++++++++++++++++ .../sprint/datastructure/tree/FileList.java | 8 ++ .../tree/BinaryTreeUtilTest.java | 66 +++++++++++ 6 files changed, 270 insertions(+) create mode 100644 group11/1178243325/week12/build.gradle create mode 100644 group11/1178243325/week12/readme.md create mode 100644 group11/1178243325/week12/src/main/java/com/sprint/datastructure/tree/BinaryTreeNode.java create mode 100644 group11/1178243325/week12/src/main/java/com/sprint/datastructure/tree/BinaryTreeUtil.java create mode 100644 group11/1178243325/week12/src/main/java/com/sprint/datastructure/tree/FileList.java create mode 100644 group11/1178243325/week12/src/test/java/com/sprint/datastructure/tree/BinaryTreeUtilTest.java diff --git a/group11/1178243325/week12/build.gradle b/group11/1178243325/week12/build.gradle new file mode 100644 index 0000000000..c4da624f95 --- /dev/null +++ b/group11/1178243325/week12/build.gradle @@ -0,0 +1,9 @@ +apply plugin: 'java' + +repositories { + mavenCentral() +} + +dependencies { + testCompile("junit:junit:4.12") +} diff --git a/group11/1178243325/week12/readme.md b/group11/1178243325/week12/readme.md new file mode 100644 index 0000000000..5ede9f45ab --- /dev/null +++ b/group11/1178243325/week12/readme.md @@ -0,0 +1,11 @@ +## 讲课内容: + +## 第12周作业(JVM7)(05-08 至 05-14) +- 数据结构,实现FileList.java(给定一个目录, 递归的列出下面所以的子目录和文件) +- 实现BinaryTreeUtil.java, 包括用递归方式来实现对二叉树的前序,中序,后序遍历,用非递归方式实现对二叉树的前序和中序遍历,需要通过测试用例BinaryTreeUtilTest.java +- 写一篇文章 + +## 完成情况: + +## 我的收获: +其实最难是坚持 diff --git a/group11/1178243325/week12/src/main/java/com/sprint/datastructure/tree/BinaryTreeNode.java b/group11/1178243325/week12/src/main/java/com/sprint/datastructure/tree/BinaryTreeNode.java new file mode 100644 index 0000000000..ce8f1cda6e --- /dev/null +++ b/group11/1178243325/week12/src/main/java/com/sprint/datastructure/tree/BinaryTreeNode.java @@ -0,0 +1,64 @@ +package com.sprint.datastructure.tree; + +public class BinaryTreeNode { + private T data; + private BinaryTreeNode left; + private BinaryTreeNode 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 left) { + this.left = left; + } + public BinaryTreeNode getLeft() { + return left; + } + + public void setRight(BinaryTreeNode right) { + this.right = right; + } + public BinaryTreeNode getRight() { + return right; + } + + public BinaryTreeNode 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(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(data); + this.right.data = data; + return this.right; + } else { + return this.right.insert(data); + } + } else { + //二叉树中不存在重复的元素 + return this; + } + } +} diff --git a/group11/1178243325/week12/src/main/java/com/sprint/datastructure/tree/BinaryTreeUtil.java b/group11/1178243325/week12/src/main/java/com/sprint/datastructure/tree/BinaryTreeUtil.java new file mode 100644 index 0000000000..a87457cff2 --- /dev/null +++ b/group11/1178243325/week12/src/main/java/com/sprint/datastructure/tree/BinaryTreeUtil.java @@ -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 List preOrderVisit(BinaryTreeNode root) { + List result = new ArrayList(); + preOrderVisit(root, result); + return result; + } + + private static void preOrderVisit(BinaryTreeNode root, List values) { + if (root != null) { + values.add(root.getData()); + preOrderVisit(root.getLeft(), values); + preOrderVisit(root.getRight(), values); + } + } + + /** + * 用递归的方式实现对二叉树的中序遍历,需要通过BinaryTreeUtil测试用例 + * @param root + * @return + */ + public static List inOrderVisit(BinaryTreeNode root) { + List result = new ArrayList(); + inOrderVisit(root, result); + return result; + } + + private static void inOrderVisit(BinaryTreeNode root, List values) { + if (root != null) { + inOrderVisit(root.getLeft(), values); + values.add(root.getData()); + inOrderVisit(root.getRight(), values); + } + } + /** + * 用递归的方式实现对二叉树的后序遍历,需要通过BinaryTreeUtil测试用例 + * @param root + * @return + */ + public static List postOrderVisit(BinaryTreeNode root) { + List result = new ArrayList(); + postOrderVisit(root, result); + return result; + } + + private static void postOrderVisit(BinaryTreeNode root, List values) { + if (root != null) { + postOrderVisit(root.getLeft(), values); + postOrderVisit(root.getRight(), values); + values.add(root.getData()); + } + } + + /** + * 用非递归的方式实现二叉树的前序遍历 + * @param root + * @return + */ + public static List preOrderWithoutRecursion(BinaryTreeNode root) { + List result = new ArrayList(); + Stack> stack = new Stack>(); + if (root != null) { + stack.push(root); + while (!stack.isEmpty()) { + BinaryTreeNode node = (BinaryTreeNode)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 List inOrderWithoutRecursion(BinaryTreeNode root) { + List result = new ArrayList(); + Stack> stack = new Stack>(); + BinaryTreeNode 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; + } +} diff --git a/group11/1178243325/week12/src/main/java/com/sprint/datastructure/tree/FileList.java b/group11/1178243325/week12/src/main/java/com/sprint/datastructure/tree/FileList.java new file mode 100644 index 0000000000..9cfecb9420 --- /dev/null +++ b/group11/1178243325/week12/src/main/java/com/sprint/datastructure/tree/FileList.java @@ -0,0 +1,8 @@ +package com.sprint.datastructure.tree; + +import java.io.File; +public class FileList { + public void list(File f) { + + } +} diff --git a/group11/1178243325/week12/src/test/java/com/sprint/datastructure/tree/BinaryTreeUtilTest.java b/group11/1178243325/week12/src/test/java/com/sprint/datastructure/tree/BinaryTreeUtilTest.java new file mode 100644 index 0000000000..ce501d3e0e --- /dev/null +++ b/group11/1178243325/week12/src/test/java/com/sprint/datastructure/tree/BinaryTreeUtilTest.java @@ -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 root = null; + @Before + public void setUp() throws Exception { + root = new BinaryTreeNode(1); + root.setLeft(new BinaryTreeNode(2)); + root.setRight(new BinaryTreeNode(5)); + root.getLeft().setLeft(new BinaryTreeNode(3)); + root.getLeft().setRight(new BinaryTreeNode(4)); + } + + @Test + public void testPreOrderVisit() { + + List result = BinaryTreeUtil.preOrderVisit(root); + Assert.assertEquals("[1, 2, 3, 4, 5]", result.toString()); + + + } + @Test + public void testInOrderVisit() { + + List result = BinaryTreeUtil.inOrderVisit(root); + Assert.assertEquals("[3, 2, 4, 1, 5]", result.toString()); + + } + + @Test + public void testPostOrderVisit() { + + List result = BinaryTreeUtil.postOrderVisit(root); + Assert.assertEquals("[3, 4, 2, 5, 1]", result.toString()); + + } + + + @Test + public void testInOrderVisitWithoutRecursion() { + BinaryTreeNode node = root.getLeft().getRight(); + node.setLeft(new BinaryTreeNode(6)); + node.setRight(new BinaryTreeNode(7)); + + List result = BinaryTreeUtil.inOrderWithoutRecursion(root); + Assert.assertEquals("[3, 2, 6, 4, 7, 1, 5]", result.toString()); + + } + @Test + public void testPreOrderVisitWithoutRecursion() { + BinaryTreeNode node = root.getLeft().getRight(); + node.setLeft(new BinaryTreeNode(6)); + node.setRight(new BinaryTreeNode(7)); + + List result = BinaryTreeUtil.preOrderWithoutRecursion(root); + Assert.assertEquals("[1, 2, 3, 4, 6, 7, 5]", result.toString()); + + } +}