diff --git a/group24/798277403/src/basic/tree/BinaryTreeNode.java b/group24/798277403/src/basic/tree/BinaryTreeNode.java new file mode 100644 index 0000000000..af3f3c817f --- /dev/null +++ b/group24/798277403/src/basic/tree/BinaryTreeNode.java @@ -0,0 +1,35 @@ +package basic.tree; + +public class BinaryTreeNode { + + private T data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public BinaryTreeNode(T data){ + this.data=data; + } + 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(Object o){ + return null; + } + +} diff --git a/group24/798277403/src/basic/tree/BinaryTreeUtil.java b/group24/798277403/src/basic/tree/BinaryTreeUtil.java new file mode 100644 index 0000000000..f4d666c335 --- /dev/null +++ b/group24/798277403/src/basic/tree/BinaryTreeUtil.java @@ -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 List preOrderVisit(BinaryTreeNode root) { + List result = new ArrayList(); + prevAdd(root,result); + return result; + } + private static void prevAdd(BinaryTreeNode node,List 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 List inOrderVisit(BinaryTreeNode root) { + List result = new ArrayList(); + inOrderAdd(root,result); + return result; + } + private static void inOrderAdd(BinaryTreeNode node,List 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 List postOrderVisit(BinaryTreeNode root) { + List result = new ArrayList(); + postOrderAdd(root,result); + return result; + } + private static void postOrderAdd(BinaryTreeNode node,List 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 List preOrderWithoutRecursion(BinaryTreeNode root) { + List result = new ArrayList(); + Stack> 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 List inOrderWithoutRecursion(BinaryTreeNode root) { + List result = new ArrayList(); + Stack> 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; + } + +} diff --git a/group24/798277403/src/basic/tree/BinaryTreeUtilTest.java b/group24/798277403/src/basic/tree/BinaryTreeUtilTest.java new file mode 100644 index 0000000000..daa057329c --- /dev/null +++ b/group24/798277403/src/basic/tree/BinaryTreeUtilTest.java @@ -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 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/798277403/src/basic/tree/FileList.java b/group24/798277403/src/basic/tree/FileList.java new file mode 100644 index 0000000000..bd0f8e1740 --- /dev/null +++ b/group24/798277403/src/basic/tree/FileList.java @@ -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); + } +}