diff --git a/group24/75939388/learning2017/src/main/java/basic/dataStructure/binaryTree/BinarySearchTree.java b/group24/75939388/learning2017/src/main/java/basic/dataStructure/binaryTree/BinarySearchTree.java new file mode 100644 index 0000000000..acbb5c7083 --- /dev/null +++ b/group24/75939388/learning2017/src/main/java/basic/dataStructure/binaryTree/BinarySearchTree.java @@ -0,0 +1,92 @@ +package basic.dataStructure.binaryTree; + +import java.util.List; + +public class BinarySearchTree { + + BinaryTreeNode root; + + public BinarySearchTree(BinaryTreeNode root) { + this.root = root; + } + + public BinaryTreeNode getRoot() { + return root; + } + + public T findMin() { + return findMin(root); + } + + private T findMin(BinaryTreeNode root){ + List list = BinaryTreeUtil.preOrderVisit(root); + T min = list.get(0); + for (T t : list) { + min = min.compareTo(t) == -1 ? min : t; + } + return min; + } + + public T findMax() { + return findMax(root); + } + + private T findMax(BinaryTreeNode root){ + List list = BinaryTreeUtil.preOrderVisit(root); + T max = list.get(0); + for (T t : list) { + max = max.compareTo(t) == 1 ? max : t; + } + return max; + } + + public int height() { + return calHeight(root); + } + + private int calHeight(BinaryTreeNode root) { + if (root == null) return 0; + + int left = calHeight(root.left); + int right = calHeight(root.right); + + return (left > right ? left : right) + 1; + + } + + public int size() { + List list = BinaryTreeUtil.preOrderVisit(root); + return list.size(); + } + + public void remove(T e) { + remove(root, (Integer) e); + } + + private void remove(BinaryTreeNode node, int value) { + if (node == null) throw new RuntimeException("no such node has value = " + value); + + T data = node.getData(); + if (data.compareTo(value) == 0) { + if (node.left == null && node.right == null) { + node = null; + } else if (node.left != null && node.right == null) { + node.data = node.left.data; + node.left = null; + } else if (node.left == null && node.right != null) { + node.data = node.right.data; + node.right = null; + } else { + T replace = findMin(node.right); + node.data = replace; + remove(node.right, (Integer)replace); + } + } else if (data.compareTo(value) == -1) { + remove(node.right, value); + } else { + remove(node.left, value); + } + } + +} + diff --git a/group24/75939388/learning2017/src/main/java/basic/dataStructure/binaryTree/BinaryTreeNode.java b/group24/75939388/learning2017/src/main/java/basic/dataStructure/binaryTree/BinaryTreeNode.java index b2b96c0349..60783a166b 100644 --- a/group24/75939388/learning2017/src/main/java/basic/dataStructure/binaryTree/BinaryTreeNode.java +++ b/group24/75939388/learning2017/src/main/java/basic/dataStructure/binaryTree/BinaryTreeNode.java @@ -72,9 +72,9 @@ public class BinaryTreeNode { //// //// return dataStr.toString(); //// } - private T data; - private BinaryTreeNode left; - private BinaryTreeNode right; + public T data; + public BinaryTreeNode left; + public BinaryTreeNode right; public BinaryTreeNode(T data){ this.data=data; diff --git a/group24/75939388/learning2017/src/main/java/basic/dataStructure/binaryTree/FileList.java b/group24/75939388/learning2017/src/main/java/basic/dataStructure/binaryTree/FileList.java index 7d7af85490..8beb22f031 100644 --- a/group24/75939388/learning2017/src/main/java/basic/dataStructure/binaryTree/FileList.java +++ b/group24/75939388/learning2017/src/main/java/basic/dataStructure/binaryTree/FileList.java @@ -2,9 +2,56 @@ import java.io.File; +/** + * 给定一个目录,递归的列出下面所有的子目录和文件 + * + */ public class FileList { - public void list(File f) { + + public void list(File f, int level) { + if(!f.exists()){ + throw new RuntimeException("file " + f.getAbsolutePath() + " not existed"); + } + + StringBuilder head = new StringBuilder(); + head.append("|--").append(f.getName()); + System.out.println(head.toString()); + + File[] files = f.listFiles(); + for(File file : files){ + if(file.isDirectory()){ + printDirectory(file, level + 1); + }else{ + printFile(file, level); + } + } + } - + private void printDirectory(File f, int level){ + StringBuilder builder = new StringBuilder(); + for(int i = 0; i < level; i++){ + builder.append(" "); + } + builder.append("|--").append(f.getName()); + System.out.println(builder.toString()); + + File[] files = f.listFiles(); + for(File file : files){ + if(file.isDirectory()){ + printDirectory(file, level + 1); + }else{ + printFile(file, level); + } + } + } + + private void printFile(File f, int level){ + StringBuilder builder = new StringBuilder(); + for(int i = 0; i < level + 1; i++){ + builder.append(" "); + } + builder.append("|->").append(f.getName()); + System.out.println(builder.toString()); + } } diff --git a/group24/75939388/learning2017/src/main/java/basic/dataStructure/stack/expr/Calculator.java b/group24/75939388/learning2017/src/main/java/basic/dataStructure/stack/expr/Calculator.java index fe6510b532..236751360b 100644 --- a/group24/75939388/learning2017/src/main/java/basic/dataStructure/stack/expr/Calculator.java +++ b/group24/75939388/learning2017/src/main/java/basic/dataStructure/stack/expr/Calculator.java @@ -17,6 +17,7 @@ public static float getFloat(float val1, float val2, String oper) { if (val2 == 0) throw new RuntimeException("cannot divide 0, calculation canceled"); res = val1 / val2; } + // System.out.println("计算结果: " + val1 + oper + val2 + "=" + res); return res; } diff --git a/group24/75939388/learning2017/src/main/java/designPattern/decorator/Beverage.java b/group24/75939388/learning2017/src/main/java/designPattern/decorator/Beverage.java deleted file mode 100644 index e68e2e7b82..0000000000 --- a/group24/75939388/learning2017/src/main/java/designPattern/decorator/Beverage.java +++ /dev/null @@ -1,23 +0,0 @@ -package designPattern.decorator; - -/** - * @author : 温友朝 - * @date : 2017/5/5 - */ -public abstract class Beverage { - public static final int TALL = 0; - public static final int GRANDE = 1; - public static final int VENTI = 2; - - - public String description = ""; - public static int size = TALL; - - public String getDescription(){ - return description; - } - - public abstract double cost(); - - public abstract int size(); -} diff --git a/group24/75939388/learning2017/src/main/java/designPattern/decorator/CondimentDecorator.java b/group24/75939388/learning2017/src/main/java/designPattern/decorator/CondimentDecorator.java deleted file mode 100644 index dca6ade059..0000000000 --- a/group24/75939388/learning2017/src/main/java/designPattern/decorator/CondimentDecorator.java +++ /dev/null @@ -1,9 +0,0 @@ -package designPattern.decorator; - -/** - * @author : 温友朝 - * @date : 2017/5/5 - */ -public abstract class CondimentDecorator extends Beverage { - public abstract String getDescription(); -} diff --git a/group24/75939388/learning2017/src/main/java/designPattern/decorator/beverages/DarkRoast.java b/group24/75939388/learning2017/src/main/java/designPattern/decorator/beverages/DarkRoast.java deleted file mode 100644 index 2c39efecf9..0000000000 --- a/group24/75939388/learning2017/src/main/java/designPattern/decorator/beverages/DarkRoast.java +++ /dev/null @@ -1,24 +0,0 @@ -package designPattern.decorator.beverages; - -import designPattern.decorator.Beverage; - -/** - * @author : 温友朝 - * @date : 2017/5/5 - */ -public class DarkRoast extends Beverage { - - public DarkRoast(){ - description = "Dark Roast"; - } - - @Override - public double cost() { - return 0.99; - } - - @Override - public int size() { - return 0; - } -} diff --git a/group24/75939388/learning2017/src/main/java/designPattern/decorator/beverages/Decaf.java b/group24/75939388/learning2017/src/main/java/designPattern/decorator/beverages/Decaf.java deleted file mode 100644 index bb4f757ad4..0000000000 --- a/group24/75939388/learning2017/src/main/java/designPattern/decorator/beverages/Decaf.java +++ /dev/null @@ -1,24 +0,0 @@ -package designPattern.decorator.beverages; - -import designPattern.decorator.Beverage; - -/** - * @author : 温友朝 - * @date : 2017/5/5 - */ -public class Decaf extends Beverage { - - public Decaf(){ - description = "Decaf"; - } - - @Override - public double cost() { - return 1.99; - } - - @Override - public int size() { - return 0; - } -} diff --git a/group24/75939388/learning2017/src/main/java/designPattern/decorator/beverages/Espresso.java b/group24/75939388/learning2017/src/main/java/designPattern/decorator/beverages/Espresso.java deleted file mode 100644 index dca41cc66c..0000000000 --- a/group24/75939388/learning2017/src/main/java/designPattern/decorator/beverages/Espresso.java +++ /dev/null @@ -1,24 +0,0 @@ -package designPattern.decorator.beverages; - -import designPattern.decorator.Beverage; - -/** - * @author : 温友朝 - * @date : 2017/5/5 - */ -public class Espresso extends Beverage { - - public Espresso(){ - description = "Espresso"; - } - - public double cost() { - return 1.99d; - } - - - @Override - public int size() { - return 0; - } -} diff --git a/group24/75939388/learning2017/src/main/java/designPattern/decorator/beverages/HouseBlend.java b/group24/75939388/learning2017/src/main/java/designPattern/decorator/beverages/HouseBlend.java deleted file mode 100644 index 8b296aa3cf..0000000000 --- a/group24/75939388/learning2017/src/main/java/designPattern/decorator/beverages/HouseBlend.java +++ /dev/null @@ -1,23 +0,0 @@ -package designPattern.decorator.beverages; - -import designPattern.decorator.Beverage; - -/** - * @author : 温友朝 - * @date : 2017/5/5 - */ -public class HouseBlend extends Beverage { - - public HouseBlend(){ - description = "House Blend"; - } - - public double cost() { - return 0.89; - } - - @Override - public int size() { - return 0; - } -} diff --git a/group24/75939388/learning2017/src/main/java/designPattern/decorator/condiments/Mocha.java b/group24/75939388/learning2017/src/main/java/designPattern/decorator/condiments/Mocha.java deleted file mode 100644 index 799399332f..0000000000 --- a/group24/75939388/learning2017/src/main/java/designPattern/decorator/condiments/Mocha.java +++ /dev/null @@ -1,31 +0,0 @@ -package designPattern.decorator.condiments; - -import designPattern.decorator.Beverage; -import designPattern.decorator.CondimentDecorator; - -/** - * @author : 温友朝 - * @date : 2017/5/5 - */ -public class Mocha extends CondimentDecorator { - Beverage beverage; - - public Mocha(Beverage beverage){ - this.beverage = beverage; - - this.description += this.beverage.description + ", Mocha"; - } - - public String getDescription() { - return this.description; - } - - public double cost() { - return 0.2 + beverage.cost(); - } - - @Override - public int size() { - return 0; - } -} diff --git a/group24/75939388/learning2017/src/main/java/designPattern/decorator/condiments/Soy.java b/group24/75939388/learning2017/src/main/java/designPattern/decorator/condiments/Soy.java deleted file mode 100644 index ec4873c914..0000000000 --- a/group24/75939388/learning2017/src/main/java/designPattern/decorator/condiments/Soy.java +++ /dev/null @@ -1,31 +0,0 @@ -package designPattern.decorator.condiments; - -import designPattern.decorator.Beverage; -import designPattern.decorator.CondimentDecorator; - -/** - * @author : 温友朝 - * @date : 2017/5/5 - */ -public class Soy extends CondimentDecorator { - Beverage beverage; - - public Soy(Beverage beverage){ - this.beverage = beverage; - - this.description += this.beverage.description + ", Soy"; - } - - public String getDescription() { - return this.description; - } - - public double cost() { - return 0.15 + beverage.cost(); - } - - @Override - public int size() { - return 0; - } -} diff --git a/group24/75939388/learning2017/src/main/java/designPattern/decorator/condiments/Whip.java b/group24/75939388/learning2017/src/main/java/designPattern/decorator/condiments/Whip.java deleted file mode 100644 index 7b8cce87af..0000000000 --- a/group24/75939388/learning2017/src/main/java/designPattern/decorator/condiments/Whip.java +++ /dev/null @@ -1,31 +0,0 @@ -package designPattern.decorator.condiments; - -import designPattern.decorator.Beverage; -import designPattern.decorator.CondimentDecorator; - -/** - * @author : 温友朝 - * @date : 2017/5/5 - */ -public class Whip extends CondimentDecorator { - Beverage beverage; - - public Whip(Beverage beverage){ - this.beverage = beverage; - - this.description += beverage.description + ", "; - } - - public String getDescription() { - return this.description; - } - - public double cost() { - return 0.1 + beverage.cost(); - } - - @Override - public int size() { - return 0; - } -} diff --git a/group24/75939388/learning2017/src/main/java/miniJVM/print/ConstantPoolPrinter.java b/group24/75939388/learning2017/src/main/java/miniJVM/print/ConstantPoolPrinter.java index c776f49f01..c0f5d7d3de 100644 --- a/group24/75939388/learning2017/src/main/java/miniJVM/print/ConstantPoolPrinter.java +++ b/group24/75939388/learning2017/src/main/java/miniJVM/print/ConstantPoolPrinter.java @@ -73,42 +73,6 @@ public void visitUTF8(UTF8Info info) { System.out.print(i + "# = "); ConstantInfo cnst = pool.getConstantInfo(i); cnst.accept(visitor); -// if(cnst instanceof ClassInfo){ -// sb.append("Class "); -// sb.append("#" + ((ClassInfo) cnst).getUtf8Index()); -// sb.append(" //" + ((ClassInfo) cnst).getClassName()); -// }else if(cnst instanceof UTF8Info){ -// sb.append("Utf8 "); -// sb.append(((UTF8Info) cnst).getValue()); -// }else if(cnst instanceof MethodRefInfo){ -// sb.append("MethodRef "); -// sb.append("#" + ((MethodRefInfo) cnst).getClassInfoIndex()); -// sb.append(".").append("#" + ((MethodRefInfo) cnst).getNameAndTypeIndex()); -// sb.append(" //" + ((MethodRefInfo) cnst).getClassName()); -// sb.append("." + ((MethodRefInfo) cnst).getMethodName()); -// sb.append(":" + ((MethodRefInfo) cnst).getParamAndReturnType()); -// }else if(cnst instanceof NameAndTypeInfo){ -// sb.append("NameAndType "); -// sb.append("#" + ((NameAndTypeInfo) cnst).getIndex1()); -// sb.append(":#" + ((NameAndTypeInfo) cnst).getIndex2()); -// sb.append(" //" + ((NameAndTypeInfo) cnst).getName()); -// sb.append(":" + ((NameAndTypeInfo) cnst).getTypeInfo()); -// }else if(cnst instanceof FieldRefInfo){ -// sb.append("Fieldref "); -// sb.append("#" + ((FieldRefInfo) cnst).getClassInfoIndex()); -// sb.append("." + ((FieldRefInfo) cnst).getNameAndTypeIndex()); -// sb.append(" //" + ((FieldRefInfo) cnst).getClassName()); -// sb.append("." + ((FieldRefInfo) cnst).getFieldName()); -// sb.append(":" + ((FieldRefInfo) cnst).getFieldType()); -// }else if(cnst instanceof StringInfo){ -// sb.append("String "); -// sb.append("#" + ((StringInfo) cnst).getIndex()); -// sb.append(" //" + cnst.toString()); -// }else{ -// throw new RuntimeException(cnst.getType() + "not processed"); -// } - -// System.out.println(sb.toString()); } } } diff --git a/group24/75939388/learning2017/src/test/java/data_structure/tree/BinarySearchTreeTest.java b/group24/75939388/learning2017/src/test/java/data_structure/tree/BinarySearchTreeTest.java new file mode 100644 index 0000000000..6dbef3a2bc --- /dev/null +++ b/group24/75939388/learning2017/src/test/java/data_structure/tree/BinarySearchTreeTest.java @@ -0,0 +1,62 @@ +package data_structure.tree; + +import basic.dataStructure.binaryTree.BinarySearchTree; +import basic.dataStructure.binaryTree.BinaryTreeNode; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + + + +public class BinarySearchTreeTest { + + BinarySearchTree tree = null; + + @Before + public void setUp() throws Exception { + BinaryTreeNode root = new BinaryTreeNode(6); + root.left = new BinaryTreeNode(2); + root.right = new BinaryTreeNode(8); + root.left.left = new BinaryTreeNode(1); + root.left.right = new BinaryTreeNode(4); + root.left.right.left = new BinaryTreeNode(3); + tree = new BinarySearchTree(root); + } + + @After + public void tearDown() throws Exception { + tree = null; + } + + @Test + public void testFind() { + Assert.assertEquals(1, tree.findMin().intValue()); + Assert.assertEquals(8, tree.findMax().intValue()); + } + + @Test + public void testHeight() { + Assert.assertEquals(4, tree.height()); + } + + @Test + public void testSize() { + Assert.assertEquals(6, tree.size()); + } + + @Test + public void testRemoveLeaf() { + tree.remove(4); + BinaryTreeNode root= tree.getRoot(); + Assert.assertEquals(3, root.left.right.data.intValue()); + + } + @Test + public void testRemoveMiddleNode() { + tree.remove(2); + BinaryTreeNode root= tree.getRoot(); + Assert.assertEquals(3, root.left.data.intValue()); + Assert.assertEquals(4, root.left.right.data.intValue()); + } +} diff --git a/group24/75939388/learning2017/src/test/java/data_structure/binaryTree/BinaryTreeUtilTest.java b/group24/75939388/learning2017/src/test/java/data_structure/tree/BinaryTreeUtilTest.java similarity index 98% rename from group24/75939388/learning2017/src/test/java/data_structure/binaryTree/BinaryTreeUtilTest.java rename to group24/75939388/learning2017/src/test/java/data_structure/tree/BinaryTreeUtilTest.java index 09b50a1148..b3657aece4 100644 --- a/group24/75939388/learning2017/src/test/java/data_structure/binaryTree/BinaryTreeUtilTest.java +++ b/group24/75939388/learning2017/src/test/java/data_structure/tree/BinaryTreeUtilTest.java @@ -1,4 +1,4 @@ -package data_structure.binaryTree; +package data_structure.tree; import basic.dataStructure.binaryTree.BinaryTreeNode; import basic.dataStructure.binaryTree.BinaryTreeUtil; diff --git a/group24/75939388/learning2017/src/test/java/data_structure/tree/FileListTest.java b/group24/75939388/learning2017/src/test/java/data_structure/tree/FileListTest.java new file mode 100644 index 0000000000..18f3bddc0a --- /dev/null +++ b/group24/75939388/learning2017/src/test/java/data_structure/tree/FileListTest.java @@ -0,0 +1,28 @@ +package data_structure.tree; + +import basic.dataStructure.binaryTree.FileList; +import org.junit.Before; +import org.junit.Test; + +import java.io.File; + +/** + * Created by macvi on 2017/5/16. + */ +public class FileListTest { + + FileList fl = null; + + @Before + public void init(){ + fl = new FileList(); + } + + @Test + public void test(){ + String path1 = "E:\\Downloads"; + String path2 = "E:\\系统ISO"; + + fl.list(new File(path2), 0); + } +} diff --git a/group24/75939388/learning2017/src/test/java/designPattern/StarBuzzCoffeeTest.java b/group24/75939388/learning2017/src/test/java/designPattern/StarBuzzCoffeeTest.java deleted file mode 100644 index 8f04ce4ff1..0000000000 --- a/group24/75939388/learning2017/src/test/java/designPattern/StarBuzzCoffeeTest.java +++ /dev/null @@ -1,35 +0,0 @@ -package designPattern; - -import designPattern.decorator.Beverage; -import designPattern.decorator.beverages.DarkRoast; -import designPattern.decorator.beverages.Espresso; -import designPattern.decorator.beverages.HouseBlend; -import designPattern.decorator.condiments.Mocha; -import designPattern.decorator.condiments.Soy; -import designPattern.decorator.condiments.Whip; -import org.junit.Test; - -/** - * @author : 温友朝 - * @date : 2017/5/5 - */ -public class StarBuzzCoffeeTest { - - @Test - public void test1(){ - Beverage espresso = new Espresso(); - System.out.println(espresso.getDescription() + " cost $" + espresso.cost()); - - Beverage darkRoast = new DarkRoast(); - darkRoast = new Mocha(darkRoast); - darkRoast = new Mocha(darkRoast); - darkRoast = new Whip(darkRoast); - System.out.println(darkRoast.getDescription() + " cost $" + darkRoast.cost()); - - Beverage houseBlend = new HouseBlend(); - houseBlend = new Soy(houseBlend); - houseBlend = new Mocha(houseBlend); - houseBlend = new Whip(houseBlend); - System.out.println(houseBlend.getDescription() + " cost $" + houseBlend.cost()); - } -} diff --git a/group24/75939388/learning2017/src/test/java/gabageCollection/MemoryLeakSimulation.java b/group24/75939388/learning2017/src/test/java/gabageCollection/MemoryLeakSimulation.java new file mode 100644 index 0000000000..483efec77d --- /dev/null +++ b/group24/75939388/learning2017/src/test/java/gabageCollection/MemoryLeakSimulation.java @@ -0,0 +1,33 @@ +package gabageCollection; + +import java.util.ArrayList; +import java.util.List; + +/** + * @author : 温友朝 + * @date : 2017/5/17 + */ +public class MemoryLeakSimulation { + + public static void testOutOfMemory(){ +// List list = new ArrayList(); + List list = new ArrayList(); + for(;;){ + list.add(new byte[10*1024*1024]); + } + } + + public static void testStackOverFlowError(){ + testStackOverFlowError(); + } + + public static void testOutOfMemoryPermGenSpace(){ + //出现在热部署时最多 + } + + public static void main(String[] args){ +// testOutOfMemory(); +// testStackOverFlowError(); + testOutOfMemoryPermGenSpace(); + } +} diff --git a/group24/75939388/learning2017/src/test/java/thread/ThreadTest.java b/group24/75939388/learning2017/src/test/java/thread/ThreadTest.java new file mode 100644 index 0000000000..8463d6e4ab --- /dev/null +++ b/group24/75939388/learning2017/src/test/java/thread/ThreadTest.java @@ -0,0 +1,27 @@ +package thread; + +/** + * Created by macvi on 2017/5/3. + */ +public class ThreadTest extends Thread { + boolean stop = false; + int value = 0; + public void run() { + while (!stop) { + value++; + } + } + public static void main(String[] args) + throws Exception { + + ThreadTest t = new ThreadTest(); + t.start(); + Thread.sleep(2000); + t.stop = true; + System.out.println("value = " + t.value); + Thread.sleep(2000); + System.out.println("value = " + t.value); + + } +} +