diff --git a/group24/121111914/src/com/github/ipk2015/coding2017/basic/test/BinaryTreeNodeTest.java b/group24/121111914/src/com/github/ipk2015/coding2017/basic/test/BinaryTreeNodeTest.java index 4b2fe88ee0..585bbd7e6a 100644 --- a/group24/121111914/src/com/github/ipk2015/coding2017/basic/test/BinaryTreeNodeTest.java +++ b/group24/121111914/src/com/github/ipk2015/coding2017/basic/test/BinaryTreeNodeTest.java @@ -5,7 +5,9 @@ import org.junit.Before; import org.junit.Test; -import com.github.ipk2015.coding2017.basic.BinaryTreeNode; +import com.github.ipk2015.coding2017.basic.tree.BinaryTreeNode1; + + public class BinaryTreeNodeTest { @@ -15,7 +17,7 @@ public void setUp() throws Exception { @Test public void testInsert() { - BinaryTreeNode node=new BinaryTreeNode(); + BinaryTreeNode1 node=new BinaryTreeNode1(); node.setData(5); node.insert(2); diff --git a/group24/121111914/src/com/github/ipk2015/coding2017/basic/tree/BinaryTreeNode.java b/group24/121111914/src/com/github/ipk2015/coding2017/basic/tree/BinaryTreeNode.java new file mode 100644 index 0000000000..7da18fa204 --- /dev/null +++ b/group24/121111914/src/com/github/ipk2015/coding2017/basic/tree/BinaryTreeNode.java @@ -0,0 +1,70 @@ +package com.github.ipk2015.coding2017.basic.tree; + + + +public class BinaryTreeNode { + + private T data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public BinaryTreeNode(T data){ + this.data=data; + } + public BinaryTreeNode(){ + + } + public T getData() { + return data; + } + public void setData(T data) { + this.data = data; + } + public BinaryTreeNode getLeft() { + return left; + } + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + public BinaryTreeNode getRight() { + return right; + } + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public BinaryTreeNode insert(T o){ + BinaryTreeNode insertNode=new BinaryTreeNode(o); + BinaryTreeNode compareNode=this; + + while(null!=compareNode){ + if(null==compareNode.getData()){ + compareNode.setData(o); + break; + }else{ + Comparable com=(Comparable) compareNode.getData(); + + int result = com.compareTo(o); + + if(result==0){ + break; + }else if(result>0){ + if(null==compareNode.getLeft()){ + compareNode.setLeft(insertNode); + break; + } + compareNode=compareNode.getLeft(); + }else if(result<0){ + if(null==compareNode.getRight()){ + compareNode.setRight(insertNode); + break; + } + compareNode=compareNode.getRight(); + } + } + } + + return insertNode; + } + +} diff --git a/group24/121111914/src/com/github/ipk2015/coding2017/basic/BinaryTreeNode.java b/group24/121111914/src/com/github/ipk2015/coding2017/basic/tree/BinaryTreeNode1.java similarity index 65% rename from group24/121111914/src/com/github/ipk2015/coding2017/basic/BinaryTreeNode.java rename to group24/121111914/src/com/github/ipk2015/coding2017/basic/tree/BinaryTreeNode1.java index 9fbcb7f585..a6f0c96a12 100644 --- a/group24/121111914/src/com/github/ipk2015/coding2017/basic/BinaryTreeNode.java +++ b/group24/121111914/src/com/github/ipk2015/coding2017/basic/tree/BinaryTreeNode1.java @@ -1,10 +1,10 @@ -package com.github.ipk2015.coding2017.basic; +package com.github.ipk2015.coding2017.basic.tree; -public class BinaryTreeNode { +public class BinaryTreeNode1 { private Comparable data; - private BinaryTreeNode left; - private BinaryTreeNode right; + private BinaryTreeNode1 left; + private BinaryTreeNode1 right; public Object getData() { return data; @@ -12,22 +12,22 @@ public Object getData() { public void setData(Comparable data) { this.data = data; } - public BinaryTreeNode getLeft() { + public BinaryTreeNode1 getLeft() { return left; } - public void setLeft(BinaryTreeNode left) { + public void setLeft(BinaryTreeNode1 left) { this.left = left; } - public BinaryTreeNode getRight() { + public BinaryTreeNode1 getRight() { return right; } - public void setRight(BinaryTreeNode right) { + public void setRight(BinaryTreeNode1 right) { this.right = right; } - public BinaryTreeNode insert(Comparable o){ - BinaryTreeNode insertNode=new BinaryTreeNode(); - BinaryTreeNode compareNode=this; + public BinaryTreeNode1 insert(Comparable o){ + BinaryTreeNode1 insertNode=new BinaryTreeNode1(); + BinaryTreeNode1 compareNode=this; insertNode.setData(o); while(null!=compareNode){ diff --git a/group24/121111914/src/com/github/ipk2015/coding2017/basic/tree/BinaryTreeUtil.java b/group24/121111914/src/com/github/ipk2015/coding2017/basic/tree/BinaryTreeUtil.java new file mode 100644 index 0000000000..268e00ddd8 --- /dev/null +++ b/group24/121111914/src/com/github/ipk2015/coding2017/basic/tree/BinaryTreeUtil.java @@ -0,0 +1,183 @@ +package com.github.ipk2015.coding2017.basic.tree; + + + +import java.util.ArrayList; +import java.util.List; +import java.util.Stack; +/* + * 二叉树的遍历 + 对于二叉树的遍历方式一般分为三种先序、中序、后序三种方式 + 先序遍历 + 若二叉树为空,则不进行任何操作:否则 + 1、访问根结点。 + 2、先序方式遍历左子树。 + 3、先序遍历右子树。 + 中序遍历 + 若二叉树为空,则不进行任何操作:否则 + 1、中序遍历左子树。 + 2、访问根结点。 + 3、中序遍历右子树。 + 后序遍历 + 若二叉树为空,则不进行任何操作:否则 + 1、后序遍历左子树。 + 2、后序遍历右子树。 + 3、访问根结点。 + * */ +public class BinaryTreeUtil { + /** + * 用递归的方式实现对二叉树的前序遍历, 需要通过BinaryTreeUtilTest测试 + * + * @param root + * @return + */ + public static List preOrderVisit(BinaryTreeNode root) { + List result = new ArrayList(); + preOrderNode(root,result); + return result; + } + private static void preOrderNode(BinaryTreeNode node,List list){ + if(null == node){ + return; + } + + T data = node.getData(); + if(null != data){ + list.add(data); + } + + BinaryTreeNode leftNode = node.getLeft(); + preOrderNode(leftNode,list); + + BinaryTreeNode rightNode = node.getRight(); + preOrderNode(rightNode,list); + } + /** + * 用递归的方式实现对二叉树的中遍历 + * + * @param root + * @return + */ + public static List inOrderVisit(BinaryTreeNode root) { + List result = new ArrayList(); + inOrderNode(root,result); + return result; + } + + private static void inOrderNode(BinaryTreeNode node,List list){ + if(null == node){ + return; + } + + BinaryTreeNode leftNode = node.getLeft(); + inOrderNode(leftNode,list); + + T data = node.getData(); + if(null != data){ + list.add(data); + } + + BinaryTreeNode rightNode = node.getRight(); + inOrderNode(rightNode,list); + } + /** + * 用递归的方式实现对二叉树的后遍历 + * + * @param root + * @return + */ + public static List postOrderVisit(BinaryTreeNode root) { + List result = new ArrayList(); + postOrderNode(root,result); + return result; + } + + private static void postOrderNode(BinaryTreeNode node,List list){ + if(null == node){ + return; + } + + BinaryTreeNode leftNode = node.getLeft(); + postOrderNode(leftNode,list); + + BinaryTreeNode rightNode = node.getRight(); + postOrderNode(rightNode,list); + + T data = node.getData(); + if(null != data){ + list.add(data); + } + + } + /** + * 用非递归的方式实现对二叉树的前序遍历 + * @param root + * @return + */ + public static List preOrderWithoutRecursion(BinaryTreeNode root) { + + List result = new ArrayList(); + Stack stack = new Stack(); + stack.push(root); + while(!stack.isEmpty()){ + BinaryTreeNode node = (BinaryTreeNode) stack.pop(); + if(null == node){ + break; + } + T data = node.getData(); + if(null != data){ + result.add(data); + } + + BinaryTreeNode rightNode = node.getRight(); + if(null != rightNode){ + stack.push(rightNode); + } + BinaryTreeNode leftNode = node.getLeft(); + if(null != leftNode){ + stack.push(leftNode); + } + } + + return result; + } + /** + * 用非递归的方式实现对二叉树的中序遍历 + * @param root + * @return + */ + public static List inOrderWithoutRecursion(BinaryTreeNode root) { + + List result = new ArrayList(); + Stack nodeStack = new Stack(); + Stack midStack = new Stack(); + nodeStack.push(root); + while(!nodeStack.isEmpty()){ + BinaryTreeNode node = (BinaryTreeNode) nodeStack.pop(); + if(null == node){ + break; + } + + BinaryTreeNode rightNode = node.getRight(); + if(null != rightNode){ + nodeStack.push(rightNode); + } + + BinaryTreeNode leftNode = node.getLeft(); + if(null != leftNode){ + midStack.push(node.getData()); + nodeStack.push(leftNode); + }else{ + T data = node.getData(); + if(null != data){ + result.add(data); + } + if(!midStack.isEmpty()){ + result.add((T) midStack.pop()); + } + } + } + return result; + } + +} diff --git a/group24/121111914/src/com/github/ipk2015/coding2017/basic/tree/BinaryTreeUtilTest.java b/group24/121111914/src/com/github/ipk2015/coding2017/basic/tree/BinaryTreeUtilTest.java new file mode 100644 index 0000000000..bee37c03a9 --- /dev/null +++ b/group24/121111914/src/com/github/ipk2015/coding2017/basic/tree/BinaryTreeUtilTest.java @@ -0,0 +1,75 @@ +package com.github.ipk2015.coding2017.basic.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)); + } + + @After + public void tearDown() throws Exception { + } + + @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()); + + } +} diff --git a/group24/121111914/src/com/github/ipk2015/coding2017/basic/tree/FileList.java b/group24/121111914/src/com/github/ipk2015/coding2017/basic/tree/FileList.java new file mode 100644 index 0000000000..93c80da541 --- /dev/null +++ b/group24/121111914/src/com/github/ipk2015/coding2017/basic/tree/FileList.java @@ -0,0 +1,30 @@ +package com.github.ipk2015.coding2017.basic.tree; + + + +import java.io.File; +/* + * 给定一个目录,递归的列出下面所有的子目录和文件 + * */ +public class FileList { + private static String stringSpace = " "; + public static void list(File f) { + listOneFile(f,0); + } + + private static void listOneFile(File f,int space){ + if(f.exists()){ + File[] listFiles = f.listFiles(); + for(int i = 0;i