From 40a7e2f5eee694b2e1665cb91271c4be8a9d9169 Mon Sep 17 00:00:00 2001 From: liangduoduo666666 <798277403@qq.com> Date: Mon, 29 May 2017 20:21:20 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9C=80=E5=90=8E=E4=B8=A4=E6=AC=A1=E4=BD=9C?= =?UTF-8?q?=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/basic/tree/BinarySearchTree.java | 359 ++++++++++++++++++ .../src/basic/tree/BinarySearchTreeTest.java | 113 ++++++ .../src/basic/tree/BinaryTreeNode.java | 6 +- .../src/memory_exception/OomHeap.java | 25 ++ .../src/memory_exception/OomPermGen.java | 46 +++ .../memory_exception/StackOverflowError.java | 15 + .../798277403/src/memory_exception/Test.java | 13 + .../org.eclipse.core.resources.prefs | 2 - 8 files changed, 574 insertions(+), 5 deletions(-) create mode 100644 group24/798277403/src/basic/tree/BinarySearchTree.java create mode 100644 group24/798277403/src/basic/tree/BinarySearchTreeTest.java create mode 100644 group24/798277403/src/memory_exception/OomHeap.java create mode 100644 group24/798277403/src/memory_exception/OomPermGen.java create mode 100644 group24/798277403/src/memory_exception/StackOverflowError.java create mode 100644 group24/798277403/src/memory_exception/Test.java delete mode 100644 liuxin/.settings/org.eclipse.core.resources.prefs diff --git a/group24/798277403/src/basic/tree/BinarySearchTree.java b/group24/798277403/src/basic/tree/BinarySearchTree.java new file mode 100644 index 0000000000..0dff39c51f --- /dev/null +++ b/group24/798277403/src/basic/tree/BinarySearchTree.java @@ -0,0 +1,359 @@ +package basic.tree; + + +import basic.queue.Queue; + +import java.util.ArrayList; +import java.util.List; + +/** + * 二叉查找树,左子树小于右子树 + */ +public class BinarySearchTree { + + BinaryTreeNode root; + + public BinarySearchTree(BinaryTreeNode root) { + this.root = root; + } + + + public BinaryTreeNode getRoot() { + return root; + } + + public T findMin() { + BinaryTreeNode head = root; + T min = null; + while (head != null) { + min = (T) head.getData(); + head = head.left; + } + return min; + } + + public T findMax() { + BinaryTreeNode head = root; + T max = null; + while (head != null) { + max = (T) head.getData(); + head = head.right; + } + return max; + } + + public int height() { + return getHeight(root); + } + + private int getHeight(BinaryTreeNode node) { + if (node == null) { + return 0; + } + int leftH = getHeight(node.left); + int rightH = getHeight(node.right); + return leftH > rightH ? leftH + 1 : rightH + 1; + } + + public int size() { + BinaryTreeNode temp = root; + int size = 0; + return BinaryTreeUtil.inOrderVisit(root).size(); + } + + /** + * 删除中间的结点,树要做调整 + * + * @param e 要删除的结点值 + */ + public void remove(T e) { + BinaryTreeNode node = root; + BinaryTreeNode father = null; + //找到要删除的结点 + while (node != null) { + if (node.getData().equals(e)) { + break; + } + if (node.getData().compareTo(e) > 0) { + father = node; + node = node.getLeft(); + } else { + father = node; + node = node.getRight(); + } + } + + //删除的结点是root结点 + if (father == null) { + //TODO + } + + //待删除结点为叶子结点 + if (node.getLeft() == null && node.getRight() == null) { + if (father.left == node) { + father.left = null; + } else { + father.right = null; + } + } + + //待删除结点左结点为null,右结点不为null + if (node.getLeft() == null && node.getRight() != null) { + BinaryTreeNode right = node.getRight(); + if (father.left == node) { + father.left = right; + } else { + father.right = right; + } + } + + + //待删除结点右结点为null,左结点不为null + if (node.getLeft() != null && node.getRight() == null) { + BinaryTreeNode left = node.getLeft(); + if (father.left == node) { + father.left = left; + } else { + father.right = left; + } + } + + //待删除结点左右结点都不为null + if (node.getLeft() != null && node.getRight() != null) { + //从右子树中选择最小的一个结点替换当前结点 + BinaryTreeNode min = node.getRight(); + BinaryTreeNode temp = node.getRight(); + BinaryTreeNode minFather = node.getRight(); + while (temp.left != null) { + min = temp.getLeft(); + temp = temp.left; + if (temp.left != null) { + minFather = temp; + } + } + minFather.left = null; + min.left = node.getLeft(); + min.right = node.getRight(); + if (father.left == node) { + father.left = min; + } else { + father.right = min; + } + } + + } + + + public void remove1(T e) { + removeNode(e, this.root); + } + + private BinaryTreeNode removeNode(T e, BinaryTreeNode node) { + if (this.root == null) { + throw new NullPointerException(); + } + + //找子树 + if (e.compareTo(node.getData()) < 0) { + node.setLeft(removeNode(e, node.getLeft())); + } else if (e.compareTo(node.getData()) > 0) { + node.setRight(removeNode(e, node.getRight())); + } else { + //删除 + if (node.getLeft() != null && node.getRight() != null) { + BinaryTreeNode n = findMinNode(node.getRight()); + node.setData(n.getData()); + node.setRight(removeNode((T) node.getData(), node.getRight())); + } else { + BinaryTreeNode n = node; + if (node.getLeft() == null) { + node = node.getRight(); + } else if (node.getRight() == null) { + node = node.getLeft(); + } + } + } + return node; + + } + + private BinaryTreeNode findMinNode(BinaryTreeNode node) { + + if (node == null) { + return null; + } + + if (node.getLeft() == null) { + return node; + } + + return findMinNode(node.getLeft()); + } + + /** + * 按层遍历 + * @return + */ + public List levelVisit() { + + List list = new ArrayList(); + + Queue queue = new Queue(); + + queue.enQueue(this.root); + + while (!queue.isEmpty()) { + + BinaryTreeNode node = queue.deQueue(); + + list.add((T) node.getData()); + + if (node.getLeft() != null) { + queue.enQueue(node.getLeft()); + } + + if (node.getRight() != null) { + queue.enQueue(node.getRight()); + } + + } + + return list; + } + + /** + * 是否为二叉树 + * + * @return + */ + public boolean isValid() { + + Queue queue = new Queue(); + + queue.enQueue(this.root); + + while (!queue.isEmpty()) { + + BinaryTreeNode node = queue.deQueue(); + + T t = (T) node.getData(); + + if (node.getLeft() != null) { + + if (t.compareTo(node.getLeft().getData()) <= 0) { + return false; + } + + queue.enQueue(node.getLeft()); + } + + if (node.getRight() != null) { + + if (t.compareTo(node.getRight().getData()) >= 0) { + return false; + } + + queue.enQueue(node.getRight()); + } + + } + + return true; + + } + + /** + * 获取两个节点的最小公共祖先 + * 同层算同一级别? + * + * @param n1 + * @param n2 + * @return + */ + public T getLowestCommonAncestor(T n1, T n2) { + + if (n1.compareTo(n2) > 0) { + T t = n1; + n1 = n2; + n2 = t; + } + + BinaryTreeNode ancestor = null; + + Queue queue = new Queue(); + + queue.enQueue(this.root); + + while (!queue.isEmpty()) { + + BinaryTreeNode node = queue.deQueue(); + + T t = (T) node.getData(); + + if (t.compareTo(n1) > 0 && t.compareTo(n2) < 0) { + ancestor = node; + } + + if (node.getLeft() != null) { + + queue.enQueue(node.getLeft()); + } + + if (node.getRight() != null) { + + queue.enQueue(node.getRight()); + } + + } + + + return (T) ancestor.getData(); + + } + + /** + * 给定两个值, 获得处于这两个值中间的节点 + * + * @param n1 + * @param n2 + * @return + */ + public List getNodesBetween(T n1, T n2) { + + if (n1.compareTo(n2) > 0) { + T t = n1; + n1 = n2; + n2 = t; + } + + List list = new ArrayList(); + + Queue queue = new Queue(); + + queue.enQueue(this.root); + + while (!queue.isEmpty()) { + + BinaryTreeNode node = queue.deQueue(); + + T t = (T) node.getData(); + + if (t.compareTo(n1) > 0 && t.compareTo(n2) < 0) { + list.add(t); + } + + if (node.getLeft() != null) { + queue.enQueue(node.getLeft()); + } + + if (node.getRight() != null) { + queue.enQueue(node.getRight()); + } + + } + + return list; + + } +} + diff --git a/group24/798277403/src/basic/tree/BinarySearchTreeTest.java b/group24/798277403/src/basic/tree/BinarySearchTreeTest.java new file mode 100644 index 0000000000..6c4071fc7e --- /dev/null +++ b/group24/798277403/src/basic/tree/BinarySearchTreeTest.java @@ -0,0 +1,113 @@ +package basic.tree; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + + + +public class BinarySearchTreeTest { + + BinarySearchTree tree = null; + + /** 6 + * 2 8 + * 1 4 + * 3 + * @throws Exception + */ + @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 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(6, tree.size()); + } + + @Test + public void testRemoveLeaf() { + tree.remove1(4); + BinaryTreeNode root= tree.getRoot(); + Assert.assertEquals(3, root.left.right.data.intValue()); + + } + @Test + public void testRemoveMiddleNode() { + tree.remove1(2); + BinaryTreeNode root= tree.getRoot(); + Assert.assertEquals(3, root.left.data.intValue()); + Assert.assertEquals(4, root.left.right.data.intValue()); + } + + @Test + public void testRemove() { + tree.remove1(6); + BinaryTreeNode root= tree.getRoot(); + Assert.assertEquals(2, root.left.data.intValue()); + Assert.assertEquals(8, root.data.intValue()); + //Assert.assertEquals(null, root.left); + } + + @Test + public void testLevelVisit() { + String str = tree.levelVisit().toString(); + + Assert.assertEquals("[6, 2, 8, 1, 4, 3]", str); + + } + + @Test + public void testIsValid() { + + Assert.assertEquals(true, tree.isValid()); + + } + + @Test + public void testGetLowestCommonAncestor() { + + Assert.assertEquals(3, (int)tree.getLowestCommonAncestor(2, 8)); + + } + + @Test + public void testGetNodesBetween() { + String str = tree.getNodesBetween(1, 4).toString(); + Assert.assertEquals("[2, 3]", str); + + str = tree.getNodesBetween(0, 9).toString(); + Assert.assertEquals("[6, 2, 8, 1, 4, 3]", str); + + + } +} diff --git a/group24/798277403/src/basic/tree/BinaryTreeNode.java b/group24/798277403/src/basic/tree/BinaryTreeNode.java index af3f3c817f..f7a8e2db95 100644 --- a/group24/798277403/src/basic/tree/BinaryTreeNode.java +++ b/group24/798277403/src/basic/tree/BinaryTreeNode.java @@ -2,9 +2,9 @@ public class BinaryTreeNode { - 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/798277403/src/memory_exception/OomHeap.java b/group24/798277403/src/memory_exception/OomHeap.java new file mode 100644 index 0000000000..835348786e --- /dev/null +++ b/group24/798277403/src/memory_exception/OomHeap.java @@ -0,0 +1,25 @@ +package memory_exception; + +import java.util.ArrayList; +import java.util.List; + +/** + * Created by zhouliang on 2017-05-27. + */ +public class OomHeap { + public static void main(String[] args) { + List list = new ArrayList(); + int i = 0; + boolean flag = true; + while (flag){ + try { + i++; + list.add(new byte[1024 * 1024]);//每次增加一个1M大小的数组对象 + }catch (Throwable e){ + e.printStackTrace(); + flag = false; + System.out.println("count="+i);//记录运行的次数 + } + } + } +} diff --git a/group24/798277403/src/memory_exception/OomPermGen.java b/group24/798277403/src/memory_exception/OomPermGen.java new file mode 100644 index 0000000000..cfbc82c480 --- /dev/null +++ b/group24/798277403/src/memory_exception/OomPermGen.java @@ -0,0 +1,46 @@ +package memory_exception; + + +import java.io.File; +import java.net.URL; +import java.net.URLClassLoader; +import java.util.ArrayList; +import java.util.List; + +/** + * JDK1.6才能模拟出这个异常 + * Created by zhouliang on 2017-05-27. + */ +public class OomPermGen { +/* public static void main(String[] args) { + // 使用List保持着常量池引用,避免Full GC 回收常量池行为 + List list = new ArrayList(); + //10MB的PermSize在int范围内足够产生OOM了 + int i = 0; + while (true) { + //调用intern方法,将字符串全部放在常量池中 + list.add(String.valueOf(i++).intern()); + } + }*/ + static String path3 = "C:\\Users\\zhouliang\\Desktop\\mycoding\\coding2017\\group24\\798277403\\out\\production\\zhouliang"; + static String path2 = "C:\\Users\\zhouliang\\Desktop\\mycoding\\"; + + public static void main(String[] args) { + URL url = null; + List classLoaderList = new ArrayList(); + int count = 0; + try { + url = new File(path2).toURI().toURL(); + System.out.println(url); + URL[] urls = {url}; + while (true){ + ClassLoader loader = new URLClassLoader(urls); + classLoaderList.add(loader); + loader.loadClass("EmployeeV1"); + System.out.println(++count); + } + } catch (Exception e) { + e.printStackTrace(); + } + } +} diff --git a/group24/798277403/src/memory_exception/StackOverflowError.java b/group24/798277403/src/memory_exception/StackOverflowError.java new file mode 100644 index 0000000000..03bf52d925 --- /dev/null +++ b/group24/798277403/src/memory_exception/StackOverflowError.java @@ -0,0 +1,15 @@ +package memory_exception; + +/** + * Created by zhouliang on 2017-05-27. + */ +public class StackOverflowError { + public static int count = 0; + public static void StackOverflowErrorTest(){ + System.out.println(++count); + StackOverflowErrorTest(); + } + public static void main(String[] args) { + StackOverflowErrorTest(); + } +} diff --git a/group24/798277403/src/memory_exception/Test.java b/group24/798277403/src/memory_exception/Test.java new file mode 100644 index 0000000000..189ce3ac7d --- /dev/null +++ b/group24/798277403/src/memory_exception/Test.java @@ -0,0 +1,13 @@ +package memory_exception; + +/** + * Created by zhouliang on 2017-05-27. + */ +public class Test { + public static final String CODE = "Code"; //Java代码编译成的字节码指令 + public static final String CONST_VALUE = "ConstantValue"; //final关键字定义的常量值 + public static final String EXCEPTIONS = "Exceptions"; //方法抛出的异常 + public static final String LINE_NUM_TABLE = "LineNumberTable"; //Java源码的行号和字节码指令的对应关系 + public static final String LOCAL_VAR_TABLE = "LocalVariableTable"; //方法的局部变量描述 + public static final String STACK_MAP_TABLE = "StackMapTable"; //给类型检查验证器使用 +} diff --git a/liuxin/.settings/org.eclipse.core.resources.prefs b/liuxin/.settings/org.eclipse.core.resources.prefs deleted file mode 100644 index 99f26c0203..0000000000 --- a/liuxin/.settings/org.eclipse.core.resources.prefs +++ /dev/null @@ -1,2 +0,0 @@ -eclipse.preferences.version=1 -encoding/=UTF-8