diff --git a/group24/626451284/data-structure/src/main/java/com/github/wdn/coding2017/basic/OutOfMemory.java b/group24/626451284/data-structure/src/main/java/com/github/wdn/coding2017/basic/OutOfMemory.java new file mode 100644 index 0000000000..295816aff1 --- /dev/null +++ b/group24/626451284/data-structure/src/main/java/com/github/wdn/coding2017/basic/OutOfMemory.java @@ -0,0 +1,33 @@ +package com.github.wdn.coding2017.basic; + +import java.util.ArrayList; +import java.util.List; + +/** + * Created by wdn on 2017/5/30 0030. + */ +public class OutOfMemory { + private List list = new ArrayList<>(); + private void stackFunction(){ + stackFunction(); + } + private void outOfMemory(){ + while (true) { + list.add(new int[1024 * 1000]); + } + } + private void outOfMemoryPermGenSpace(){ + // java8 设置-server -XX:MetaspaceSize=1M -XX:MaxMetaspaceSize=2m + // java8 之前设置-server -XX:PermSize=64M -XX:MaxPermSize=128M + + } + public static void main(String[] args) { + OutOfMemory out = new OutOfMemory(); + // StackOverflowError + //out.stackFunction(); + // OutOfMemoryError: Java heap space + //out.outOfMemory(); + // OutOfMemory :PermGen space + //out.outOfMemoryPermGenSpace(); + } +} diff --git a/group24/626451284/data-structure/src/main/java/com/github/wdn/coding2017/basic/tree/BinarySearchTree.java b/group24/626451284/data-structure/src/main/java/com/github/wdn/coding2017/basic/tree/BinarySearchTree.java new file mode 100644 index 0000000000..e7a5885f94 --- /dev/null +++ b/group24/626451284/data-structure/src/main/java/com/github/wdn/coding2017/basic/tree/BinarySearchTree.java @@ -0,0 +1,139 @@ +package com.github.wdn.coding2017.basic.tree; + +import java.util.ArrayList; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; + +/** + * Created by wangxin on 2017/5/26. + */ +public class BinarySearchTree { + BinaryTreeNode root; + public BinarySearchTree(BinaryTreeNode root){ + this.root = root; + } + public BinaryTreeNode getRoot(){ + return root; + } + public T findMin(){ + if (root == null) { + throw new RuntimeException("tree is null"); + } + BinaryTreeNode local = root; + while (local.getLeft()!=null){ + local = local.getLeft(); + } + return local.data; + } + public T findMax(){ + if (root == null) { + throw new RuntimeException("tree is null"); + } + BinaryTreeNode local = root; + while (local.getRight()!=null){ + local = local.getRight(); + } + return local.data; + } + public int height() { + if(root==null){ + return 0; + } + int leftDeep = findChild(root.getLeft(),2); + int rightDeep = findChild(root.getRight(),2); + return Math.max(leftDeep,rightDeep); + } + private int findChild(BinaryTreeNode node,int deepIndex){ + if(node.getLeft()!=null){ + findChild(node.getLeft(),deepIndex++); + } + if(node.getRight()!=null){ + findChild(node.getRight(),deepIndex++); + } + return deepIndex; + } + public int size() { + if(root==null){ + return 0; + } + int count = sumChild(root)+1; + return count; + } + private int sumChild(BinaryTreeNode node){ + int count = 0; + if(node.getLeft()!=null){ + count++; + count+=sumChild(node.getLeft()); + } + if(node.getRight()!=null){ + count++; + count+=sumChild(node.getRight()); + } + return count; + } + public void remove(T e){ + + } + public List levelVisit(){ + List values = new ArrayList<>(); + if(root == null){ + return values; + } + + Queue> queue = new LinkedList<>();//层序遍历时保存结点的队列 + queue.offer(root);//初始化 + while(!queue.isEmpty()){ + BinaryTreeNode node = queue.poll(); + //System.out.print(node.data + " ");//访问节点 + values.add(node.data); + if(node.left != null) + queue.offer(node.left); + if(node.right != null) + queue.offer(node.right); + } + return values; + } + public boolean isValid(){ + if (root == null) { + return true; + } + return dfs(root, Integer.MIN_VALUE, Integer.MAX_VALUE); + } + private boolean dfs(BinaryTreeNode root, Comparable low, Comparable up) { + if (root == null) { + return true; + } + if (root.data.compareTo(up) >= 0 || root.data.compareTo(low) <= 0) { + return false; + } + return dfs(root.left, low, root.data) && dfs(root.right, root.data, up); + } + public T getLowestCommonAncestor(BinaryTreeNode nodeData, T node1, T node2){ + if(nodeData == null){ + return null; + } + if (nodeData.data == node1 || nodeData.data == node2) { + return nodeData.data; + } + + // Divide + T left = getLowestCommonAncestor((BinaryTreeNode)nodeData.left, node1, node2); + T right = getLowestCommonAncestor((BinaryTreeNode)nodeData.right, node1, node2); + + // Conquer + if (left != null && right != null) { + return nodeData.data; + } + if (left != null) { + return left; + } + if (right != null) { + return right; + } + return null; + } + public List getNodesBetween(T n1, T n2){ + return null; + } +} diff --git a/group24/626451284/data-structure/src/main/java/com/github/wdn/coding2017/basic/tree/BinarySearchTreeTest.java b/group24/626451284/data-structure/src/main/java/com/github/wdn/coding2017/basic/tree/BinarySearchTreeTest.java new file mode 100644 index 0000000000..3920ff06f1 --- /dev/null +++ b/group24/626451284/data-structure/src/main/java/com/github/wdn/coding2017/basic/tree/BinarySearchTreeTest.java @@ -0,0 +1,113 @@ +package com.github.wdn.coding2017.basic.tree; + +import java.util.List; + +/** + * Created by wangxin on 2017/5/26. + */ + +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); + root.left.right.right = new BinaryTreeNode(5); + tree = new BinarySearchTree(root); + } + + @After + public void tearDown() throws Exception { + tree = null; + } + + @Test + public void testFindMin() { + Assert.assertEquals(1, tree.findMin().intValue()); + + } + + @Test + public void testFindMax() { + Assert.assertEquals(8, tree.findMax().intValue()); + } + + @Test + public void testHeight() { + Assert.assertEquals(4, tree.height()); + } + + @Test + public void testSize() { + Assert.assertEquals(7, tree.size()); + } + + @Test + public void testRemoveLeaf() { + tree.remove(3); + BinaryTreeNode root= tree.getRoot(); + Assert.assertEquals(4, root.left.right.data); + + } + @Test + public void testRemoveMiddleNode1() { + tree.remove(4); + BinaryTreeNode root= tree.getRoot(); + Assert.assertEquals(5, root.left.right.data); + Assert.assertEquals(3, root.left.right.left.data); + } + @Test + public void testRemoveMiddleNode2() { + tree.remove(2); + BinaryTreeNode root= tree.getRoot(); + Assert.assertEquals(3, root.left.data); + Assert.assertEquals(4, root.left.right.data); + } + + @Test + public void testLevelVisit() { + List values = tree.levelVisit(); + Assert.assertEquals("[6, 2, 8, 1, 4, 3, 5]", values.toString()); + + } + @Test + public void testLCA(){ + Assert.assertEquals(2,tree.getLowestCommonAncestor(tree.getRoot(),1, 5).intValue()); + Assert.assertEquals(2,tree.getLowestCommonAncestor(tree.getRoot(),1, 4).intValue()); + Assert.assertEquals(6,tree.getLowestCommonAncestor(tree.getRoot(),3, 8).intValue()); + } + @Test + public void testIsValid() { + + Assert.assertTrue(tree.isValid()); + + BinaryTreeNode root = new BinaryTreeNode(6); + root.left = new BinaryTreeNode(2); + root.right = new BinaryTreeNode(8); + root.left.left = new BinaryTreeNode(4); + root.left.right = new BinaryTreeNode(1); + root.left.right.left = new BinaryTreeNode(3); + tree = new BinarySearchTree(root); + + Assert.assertFalse(tree.isValid()); + } + @Test + public void testGetNodesBetween(){ + List numbers = this.tree.getNodesBetween(3, 8); + System.out.println(numbers.toString()); + + } +} \ No newline at end of file diff --git a/group24/626451284/data-structure/src/main/java/com/github/wdn/coding2017/basic/tree/BinaryTreeNode.java b/group24/626451284/data-structure/src/main/java/com/github/wdn/coding2017/basic/tree/BinaryTreeNode.java new file mode 100644 index 0000000000..458e539a34 --- /dev/null +++ b/group24/626451284/data-structure/src/main/java/com/github/wdn/coding2017/basic/tree/BinaryTreeNode.java @@ -0,0 +1,59 @@ +package com.github.wdn.coding2017.basic.tree; + +/** + * Created by wangxin on 2017/5/20. + */ +public class BinaryTreeNode { + + public T data; + public BinaryTreeNode left; + public BinaryTreeNode right; + public BinaryTreeNode() { + } + public BinaryTreeNode(T data) { + this.data = data; + } + + public BinaryTreeNode insert(T o){ + BinaryTreeNode result; + if(o.compareTo(data)<0){ + if(this.left==null){ + BinaryTreeNode newNode = new BinaryTreeNode(); + newNode.setData(o); + setLeft(newNode); + result = newNode; + }else{ + result = this.left.insert(o); + } + }else{ + if(this.right==null){ + BinaryTreeNode newNode = new BinaryTreeNode(); + newNode.setData(o); + setRight(newNode); + result = newNode; + }else{ + result = this.right.insert(o); + } + } + return result; + } + 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; + } + +} diff --git a/group24/626451284/data-structure/src/main/java/com/github/wdn/coding2017/basic/tree/BinaryTreeUtil.java b/group24/626451284/data-structure/src/main/java/com/github/wdn/coding2017/basic/tree/BinaryTreeUtil.java new file mode 100644 index 0000000000..6b3cfcb241 --- /dev/null +++ b/group24/626451284/data-structure/src/main/java/com/github/wdn/coding2017/basic/tree/BinaryTreeUtil.java @@ -0,0 +1,126 @@ +package com.github.wdn.coding2017.basic.tree; + +import java.util.ArrayList; +import java.util.List; +import java.util.Stack; + +/** + * Created by wangxin on 2017/5/20. + */ +public class BinaryTreeUtil { + /** + * 用递归的方式实现对二叉树的前序遍历, 需要通过BinaryTreeUtilTest测试 + * + * @param root + * @return + */ + public static List preOrderVisit(BinaryTreeNode root,List result) { + if(root!=null){ + result.add(root.getData()); + preOrderVisit(root.getLeft(),result); + preOrderVisit(root.getRight(),result); + } + return result; + } + + /** + * 用递归的方式实现对二叉树的中遍历 + * + * @param root + * @return + */ + public static List inOrderVisit(BinaryTreeNode root,List result) { + if(root!=null){ + inOrderVisit(root.getLeft(),result); + result.add(root.getData()); + inOrderVisit(root.getRight(),result); + } + return result; + } + + /** + * 用递归的方式实现对二叉树的后遍历 + * + * @param root + * @return + */ + public static List postOrderVisit(BinaryTreeNode root,List result) { + if(root!=null){ + postOrderVisit(root.getLeft(),result); + postOrderVisit(root.getRight(),result); + result.add(root.getData()); + } + + return result; + } + /** + * 用非递归的方式实现对二叉树的前序遍历 + * @param root + * @return + */ + public static List preOrderWithoutRecursion(BinaryTreeNode root) { + Stack> stack = new Stack<>(); + List result = new ArrayList<>(); + if(root==null){ + return result; + } + stack.push(root); + while (!stack.empty()){ + BinaryTreeNode currentNode = stack.pop(); + result.add(currentNode.getData()); + + if(currentNode.getRight()!=null){ + stack.push(currentNode.getRight()); + } + if(currentNode.getLeft()!=null){ + stack.push(currentNode.getLeft()); + } + } + return result; + } + /** + * 用非递归的方式实现对二叉树的中序遍历 + * @param root + * @return + */ + public static List inOrderWithoutRecursion(BinaryTreeNode root) { + Stack> stack = new Stack<>(); + ArrayList result = new ArrayList<>(); + BinaryTreeNode curt = root; + while (curt != null || !stack.empty()) { + while (curt != null) { + stack.push(curt); + curt = curt.getLeft(); + } + curt = stack.pop(); + result.add(curt.getData()); + curt = curt.getRight(); + } + return result; + } + public static List postOrderWithoutRecursion(BinaryTreeNode root) { + Stack> stack = new Stack<>(); + ArrayList result = new ArrayList<>(); + BinaryTreeNode curt = root; + BinaryTreeNode prev = null; + stack.push(curt); + while (!stack.empty()) { + curt = stack.peek(); + if ((curt.getLeft() == null && curt.getRight() == null) || (prev != null &&(prev == curt.getLeft() || prev == curt.getRight()))){ + result.add(curt.getData()); + stack.pop(); + prev = curt; + } + else{ + if(curt.getRight() != null){ + stack.push(curt.getRight()); + } + if(curt.getLeft() != null){ + stack.push(curt.getLeft()); + } + } + + } + return result; + } +} diff --git a/group24/626451284/data-structure/src/main/java/com/github/wdn/coding2017/basic/tree/BinaryTreeUtilTest.java b/group24/626451284/data-structure/src/main/java/com/github/wdn/coding2017/basic/tree/BinaryTreeUtilTest.java new file mode 100644 index 0000000000..d7a442fd30 --- /dev/null +++ b/group24/626451284/data-structure/src/main/java/com/github/wdn/coding2017/basic/tree/BinaryTreeUtilTest.java @@ -0,0 +1,81 @@ +package com.github.wdn.coding2017.basic.tree; + +import java.util.ArrayList; +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 = new ArrayList<>(); + BinaryTreeUtil.preOrderVisit(root,result); + Assert.assertEquals("[1, 2, 3, 4, 5]", result.toString()); + } + @Test + public void testInOrderVisit() { + List result = new ArrayList<>(); + BinaryTreeUtil.inOrderVisit(root,result); + Assert.assertEquals("[3, 2, 4, 1, 5]", result.toString()); + + } + + @Test + public void testPostOrderVisit() { + List result = new ArrayList<>(); + BinaryTreeUtil.postOrderVisit(root,result); + 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()); + + } + @Test + public void testPostOrderVisitWithoutRecursion() { + BinaryTreeNode node = root.getLeft().getRight(); + node.setLeft(new BinaryTreeNode(6)); + node.setRight(new BinaryTreeNode(7)); + + List result = BinaryTreeUtil.postOrderWithoutRecursion(root); + Assert.assertEquals("[3, 6, 7, 4, 2, 5, 1]", result.toString()); + + } +} \ No newline at end of file