From 65e8eb2d93cb438f0c32c97061d1cb13ea1fed01 Mon Sep 17 00:00:00 2001 From: orajavac Date: Fri, 12 May 2017 14:44:41 +0800 Subject: [PATCH 1/3] =?UTF-8?q?20170512=5F1444=20=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../coding2017/basic/BinaryTreeNode.java | 6 +- .../coding2017/basic/tree/BinaryTreeUtil.java | 120 ++++++++++++++++++ .../basic/tree/BinaryTreeUtilTest.java | 74 +++++++++++ .../coding2017/basic/tree/FileList.java | 23 ++++ 4 files changed, 222 insertions(+), 1 deletion(-) create mode 100644 group02/562768642/src/com/github/orajavac/coding2017/basic/tree/BinaryTreeUtil.java create mode 100644 group02/562768642/src/com/github/orajavac/coding2017/basic/tree/BinaryTreeUtilTest.java create mode 100644 group02/562768642/src/com/github/orajavac/coding2017/basic/tree/FileList.java diff --git a/group02/562768642/src/com/github/orajavac/coding2017/basic/BinaryTreeNode.java b/group02/562768642/src/com/github/orajavac/coding2017/basic/BinaryTreeNode.java index 65ef21a344..00664bac32 100644 --- a/group02/562768642/src/com/github/orajavac/coding2017/basic/BinaryTreeNode.java +++ b/group02/562768642/src/com/github/orajavac/coding2017/basic/BinaryTreeNode.java @@ -1,10 +1,14 @@ package com.github.orajavac.coding2017.basic; -public class BinaryTreeNode { +public class BinaryTreeNode { private Object data; private BinaryTreeNode left; private BinaryTreeNode right; + + public BinaryTreeNode(Integer i){ + data = i; + } public Object getData() { return data; diff --git a/group02/562768642/src/com/github/orajavac/coding2017/basic/tree/BinaryTreeUtil.java b/group02/562768642/src/com/github/orajavac/coding2017/basic/tree/BinaryTreeUtil.java new file mode 100644 index 0000000000..10f86313d0 --- /dev/null +++ b/group02/562768642/src/com/github/orajavac/coding2017/basic/tree/BinaryTreeUtil.java @@ -0,0 +1,120 @@ +package com.github.orajavac.coding2017.basic.tree; + +import java.util.ArrayList; +import java.util.List; +import com.github.orajavac.coding2017.basic.BinaryTreeNode; +import com.github.orajavac.coding2017.basic.stack.Stack; + +public class BinaryTreeUtil { + + private static Stack s = new Stack(); + + /** + * 用递归的方式实现对二叉树的前序遍历, 需要通过BinaryTreeUtilTest测试 + * + * @param root + * @return + */ + @SuppressWarnings("unchecked") + public static List preOrderVisit(BinaryTreeNode root) { + return null; + } + + /** + * 用递归的方式实现对二叉树的中遍历 + * + * @param root + * @return + */ + public static List inOrderVisit(BinaryTreeNode root) { + List result = new ArrayList(); + + return result; + } + + /** + * 用递归的方式实现对二叉树的后遍历 + * + * @param root + * @return + */ + public static List postOrderVisit(BinaryTreeNode root) { + List result = new ArrayList(); + + return result; + } + /** + * 用非递归的方式实现对二叉树的前序遍历 + * @param root + * @return + */ + @SuppressWarnings("unchecked") + public static List preOrderWithoutRecursion(BinaryTreeNode root) { + + List result = new ArrayList(); + result.add((T) root.getData()); + BinaryTreeNode node = root; + preOrderWithoutRecursionProcess(node,result); + node.setLeft(root.getRight()); + preOrderWithoutRecursionProcess(node,result); + return result; + } + + @SuppressWarnings("unchecked") + public static List preOrderWithoutRecursionProcess(BinaryTreeNode node,List result) { + while(true){ + if (node.getLeft()!=null){ + result.add((T) node.getLeft().getData()); + s.push(node.getLeft()); + node = node.getLeft(); + }else{ + node = (BinaryTreeNode)s.pop(); + node.setLeft(node.getRight()); + } + if (node.getLeft()==null&&s.length()==0) + break; + } + return result; + } + + + /** + * 用非递归的方式实现对二叉树的中序遍历 + * @param root + * @return + */ + @SuppressWarnings("unchecked") + public static List inOrderWithoutRecursion(BinaryTreeNode root) { + + List result = new ArrayList(); + BinaryTreeNode node = root; + while (node.getLeft()!=null){ + s.push(node.getLeft()); + node = node.getLeft(); + } + + inOrderVisitProcess(node,s,result); + return result; + } + + @SuppressWarnings("unchecked") + public static List inOrderVisitProcess(BinaryTreeNode node,Stack s,List result) { + if (node.getRight()!=null){ + while (true){ + if (node.getLeft()!=null){ + s.push(node.getLeft()); + node = node.getLeft(); + }else{ + node = (BinaryTreeNode)s.pop(); + result.add((T) node.getData()); + break; + } + } + }else{ + node = (BinaryTreeNode)s.pop(); + result.add((T) node.getData()); + } + return result; + } + +} diff --git a/group02/562768642/src/com/github/orajavac/coding2017/basic/tree/BinaryTreeUtilTest.java b/group02/562768642/src/com/github/orajavac/coding2017/basic/tree/BinaryTreeUtilTest.java new file mode 100644 index 0000000000..dec8596352 --- /dev/null +++ b/group02/562768642/src/com/github/orajavac/coding2017/basic/tree/BinaryTreeUtilTest.java @@ -0,0 +1,74 @@ +package com.github.orajavac.coding2017.basic.tree; + +import java.util.List; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import com.github.orajavac.coding2017.basic.BinaryTreeNode; + +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/group02/562768642/src/com/github/orajavac/coding2017/basic/tree/FileList.java b/group02/562768642/src/com/github/orajavac/coding2017/basic/tree/FileList.java new file mode 100644 index 0000000000..fb7b1244e7 --- /dev/null +++ b/group02/562768642/src/com/github/orajavac/coding2017/basic/tree/FileList.java @@ -0,0 +1,23 @@ +package com.github.orajavac.coding2017.basic.tree; + +import java.io.File; +public class FileList { + + public void list(File f) { + if(f!=null){ + if(f.isDirectory()){ + File[] fileArray=f.listFiles(); + if(fileArray!=null){ + for (int i = 0; i < fileArray.length; i++) { + //递归调用 + list(fileArray[i]); + } + } + } + else{ + System.out.println(f); + } + } + } + +} From 7859cebbb1da9793b5a01e54803ef3f125ba2802 Mon Sep 17 00:00:00 2001 From: orajavac Date: Fri, 19 May 2017 09:24:17 +0800 Subject: [PATCH 2/3] =?UTF-8?q?20170519=5F0924=20=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../coding2017/basic/BinaryTreeNode.java | 27 ++++---- .../basic/tree/BinarySearchTree.java | 63 ++++++++++++++++++ .../basic/tree/BinarySearchTreeTest.java | 65 +++++++++++++++++++ .../coding2017/jvm/OutOfMemoryErrorDemo.java | 38 +++++++++++ .../coding2017/jvm/PermGenSpaceDemo.java | 37 +++++++++++ .../jvm/StackOverflowErrorDemo.java | 26 ++++++++ 6 files changed, 242 insertions(+), 14 deletions(-) create mode 100644 group02/562768642/src/com/github/orajavac/coding2017/basic/tree/BinarySearchTree.java create mode 100644 group02/562768642/src/com/github/orajavac/coding2017/basic/tree/BinarySearchTreeTest.java create mode 100644 group02/562768642/src/com/github/orajavac/coding2017/jvm/OutOfMemoryErrorDemo.java create mode 100644 group02/562768642/src/com/github/orajavac/coding2017/jvm/PermGenSpaceDemo.java create mode 100644 group02/562768642/src/com/github/orajavac/coding2017/jvm/StackOverflowErrorDemo.java diff --git a/group02/562768642/src/com/github/orajavac/coding2017/basic/BinaryTreeNode.java b/group02/562768642/src/com/github/orajavac/coding2017/basic/BinaryTreeNode.java index 00664bac32..7e06bc634a 100644 --- a/group02/562768642/src/com/github/orajavac/coding2017/basic/BinaryTreeNode.java +++ b/group02/562768642/src/com/github/orajavac/coding2017/basic/BinaryTreeNode.java @@ -2,34 +2,33 @@ public class BinaryTreeNode { - private Object data; - private BinaryTreeNode left; - private BinaryTreeNode right; - - public BinaryTreeNode(Integer i){ - data = i; - } + public T data; + public BinaryTreeNode left; + public BinaryTreeNode right; - public Object getData() { + public BinaryTreeNode(T data){ + this.data=data; + } + public T getData() { return data; } - public void setData(Object data) { + public void setData(T data) { this.data = data; } - public BinaryTreeNode getLeft() { + public BinaryTreeNode getLeft() { return left; } - public void setLeft(BinaryTreeNode left) { + public void setLeft(BinaryTreeNode left) { this.left = left; } - public BinaryTreeNode getRight() { + public BinaryTreeNode getRight() { return right; } - public void setRight(BinaryTreeNode right) { + public void setRight(BinaryTreeNode right) { this.right = right; } - public BinaryTreeNode insert(Object o){ + public BinaryTreeNode insert(Object o){ return null; } diff --git a/group02/562768642/src/com/github/orajavac/coding2017/basic/tree/BinarySearchTree.java b/group02/562768642/src/com/github/orajavac/coding2017/basic/tree/BinarySearchTree.java new file mode 100644 index 0000000000..ad14ebb302 --- /dev/null +++ b/group02/562768642/src/com/github/orajavac/coding2017/basic/tree/BinarySearchTree.java @@ -0,0 +1,63 @@ +package com.github.orajavac.coding2017.basic.tree; + +import com.github.orajavac.coding2017.basic.BinaryTreeNode; +import com.github.orajavac.coding2017.basic.stack.Stack; + +@SuppressWarnings("rawtypes") +public class BinarySearchTree { + + private static Stack s = new Stack(); + BinaryTreeNode root; + public BinarySearchTree(BinaryTreeNode root){ + this.root = root; + } + public BinaryTreeNode getRoot(){ + return root; + } + public T findMin(){ + T result = findMinOrMax(Integer.parseInt(getRoot().getData().toString()),getRoot(),false); + root.setLeft(root.getRight()); + result = findMinOrMax((Integer) result,getRoot(),false); + return result; + } + public T findMax(){ + T result = findMinOrMax(Integer.parseInt(getRoot().getData().toString()),getRoot(),true); + root.setLeft(root.getRight()); + result = findMinOrMax((Integer) result,getRoot(),true); + return result; + } + public int height() { + return -1; + } + public int size() { + return -1; + } + public void remove(T e){ + + } + + public T findMinOrMax(Integer num,BinaryTreeNode node,boolean bol){ + while(true){ + if (node.getLeft()!=null){ + if (bol){ + if (Integer.parseInt(node.getLeft().getData().toString()) > num){ //max + num = Integer.parseInt(node.getLeft().getData().toString()); + } + }else{ + if (Integer.parseInt(node.getLeft().getData().toString()) < num){ //min + num = Integer.parseInt(node.getLeft().getData().toString()); + } + } + s.push(node.getLeft()); + node = node.getLeft(); + }else{ + node = (BinaryTreeNode)s.pop(); + node.setLeft(node.getRight()); + } + if (node.getLeft()==null&&s.length()==0) + break; + } + return (T)num; + } + +} \ No newline at end of file diff --git a/group02/562768642/src/com/github/orajavac/coding2017/basic/tree/BinarySearchTreeTest.java b/group02/562768642/src/com/github/orajavac/coding2017/basic/tree/BinarySearchTreeTest.java new file mode 100644 index 0000000000..0891147226 --- /dev/null +++ b/group02/562768642/src/com/github/orajavac/coding2017/basic/tree/BinarySearchTreeTest.java @@ -0,0 +1,65 @@ +package com.github.orajavac.coding2017.basic.tree; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import com.github.orajavac.coding2017.basic.BinaryTreeNode; + +public class BinarySearchTreeTest { +BinarySearchTree tree = null; + + @SuppressWarnings("unchecked") + @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.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/group02/562768642/src/com/github/orajavac/coding2017/jvm/OutOfMemoryErrorDemo.java b/group02/562768642/src/com/github/orajavac/coding2017/jvm/OutOfMemoryErrorDemo.java new file mode 100644 index 0000000000..bb87d1f02b --- /dev/null +++ b/group02/562768642/src/com/github/orajavac/coding2017/jvm/OutOfMemoryErrorDemo.java @@ -0,0 +1,38 @@ +package com.github.orajavac.coding2017.jvm; + +import java.util.Stack; + +/** + * Exception in thread "main" java.lang.OutOfMemoryError: Java heap space + at java.util.Arrays.copyOf(Arrays.java:2245) + at java.util.Arrays.copyOf(Arrays.java:2219) + at java.util.Vector.grow(Vector.java:262) + at java.util.Vector.ensureCapacityHelper(Vector.java:242) + at java.util.Vector.addElement(Vector.java:616) + at java.util.Stack.push(Stack.java:67) + at com.github.orajavac.coding2017.jvm.OutOfMemoryErrorDemo.f(OutOfMemoryErrorDemo.java:12) + at com.github.orajavac.coding2017.jvm.OutOfMemoryErrorDemo.main(OutOfMemoryErrorDemo.java:18) + + * @author ora + * + */ + +public class OutOfMemoryErrorDemo { + + + public static Stack s = new Stack(); + + public static void f(){ + for (int i=0;i<99999;i++){ + s.push("a"); + } + } + public static void main(String[] args) { + + while(true){ + f(); + } + + } + +} diff --git a/group02/562768642/src/com/github/orajavac/coding2017/jvm/PermGenSpaceDemo.java b/group02/562768642/src/com/github/orajavac/coding2017/jvm/PermGenSpaceDemo.java new file mode 100644 index 0000000000..3f4128848f --- /dev/null +++ b/group02/562768642/src/com/github/orajavac/coding2017/jvm/PermGenSpaceDemo.java @@ -0,0 +1,37 @@ +package com.github.orajavac.coding2017.jvm; + +import java.io.File; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLClassLoader; +import java.util.ArrayList; +import java.util.List; + +import com.github.orajavac.coding2017.basic.tree.FileList; + +class demo{ + public static List l = new ArrayList(); +} + +public class PermGenSpaceDemo { + + public static void main(String[] args) throws ClassNotFoundException, MalformedURLException, NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { + URL url = null; + List classLoaderList = new ArrayList(); + try { + url = new File("D:/coding2017/group02/562768642/bin/com/github/orajavac/coding2017").toURI().toURL(); + URL[] urls = { url }; + while (true) { + ClassLoader loader = new URLClassLoader(urls); + classLoaderList.add(loader); + loader.loadClass("com.github.orajavac.coding2017.basic.ArrayList"); + } + } catch (Exception e) { + e.printStackTrace(); + } + } +} + + diff --git a/group02/562768642/src/com/github/orajavac/coding2017/jvm/StackOverflowErrorDemo.java b/group02/562768642/src/com/github/orajavac/coding2017/jvm/StackOverflowErrorDemo.java new file mode 100644 index 0000000000..8690cbc545 --- /dev/null +++ b/group02/562768642/src/com/github/orajavac/coding2017/jvm/StackOverflowErrorDemo.java @@ -0,0 +1,26 @@ +package com.github.orajavac.coding2017.jvm; + +import java.util.ArrayList; +import java.util.List; +import java.util.Stack; + +/** + * Exception in thread "main" java.lang.StackOverflowError + at com.github.orajavac.coding2017.jvm.StackOverflowErrorDemo.f(StackOverflowErrorDemo.java:26) + at com.github.orajavac.coding2017.jvm.StackOverflowErrorDemo.f(StackOverflowErrorDemo.java:26) + * + */ + +public class StackOverflowErrorDemo { + + public static void f(){ + f(); + } + + public static void main(String[] args) { + while(true){ + f(); + } + } + +} From 27986f4e8c07a5a619e335345cc31bd045f33fa4 Mon Sep 17 00:00:00 2001 From: orajavac Date: Wed, 24 May 2017 13:59:28 +0800 Subject: [PATCH 3/3] =?UTF-8?q?20170524=5F1359=20=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../basic/tree/BinarySearchTree.java | 141 +++++++++++++++++- .../coding2017/basic/tree/BinaryTreeUtil.java | 40 ++++- 2 files changed, 174 insertions(+), 7 deletions(-) diff --git a/group02/562768642/src/com/github/orajavac/coding2017/basic/tree/BinarySearchTree.java b/group02/562768642/src/com/github/orajavac/coding2017/basic/tree/BinarySearchTree.java index ad14ebb302..b3b7eb5217 100644 --- a/group02/562768642/src/com/github/orajavac/coding2017/basic/tree/BinarySearchTree.java +++ b/group02/562768642/src/com/github/orajavac/coding2017/basic/tree/BinarySearchTree.java @@ -1,5 +1,9 @@ package com.github.orajavac.coding2017.basic.tree; +import java.util.ArrayList; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; import com.github.orajavac.coding2017.basic.BinaryTreeNode; import com.github.orajavac.coding2017.basic.stack.Stack; @@ -26,16 +30,42 @@ public T findMax(){ result = findMinOrMax((Integer) result,getRoot(),true); return result; } - public int height() { - return -1; + + public int height(){ + return height(root); + } + + public int height(BinaryTreeNode root) { + if(root==null){ + return 0; + }else{ + int l = height(root.left); + int r = height(root.right); + if (l>r){ + return l+1; + }else{ + return r+1; + } + } } - public int size() { - return -1; + + public int size(){ + return size(root); + } + + public int size(BinaryTreeNode root) { + if(root==null){ + return 0; + } + return size(root.left)+1+size(root.right); } + + public void remove(T e){ } + @SuppressWarnings("unchecked") public T findMinOrMax(Integer num,BinaryTreeNode node,boolean bol){ while(true){ if (node.getLeft()!=null){ @@ -60,4 +90,107 @@ public T findMinOrMax(Integer num,BinaryTreeNode node,boolean bol){ return (T)num; } + @SuppressWarnings("unchecked") + public List levelVisit(){ + Queue q = new LinkedList(); + List result = new ArrayList(); + result.add(root.data); + q.add(root.getLeft()); + q.add(root.getRight()); + while(q.size()!=0){ + BinaryTreeNode node = (BinaryTreeNode)q.poll(); + result.add(node.data); + if (node!=null&&node.left!=null){ + q.add(node.getLeft()); + } + if (node!=null&&node.right!=null){ + q.add(node.getRight()); + } + } + return result; + } + + public boolean isValid(){ + return isValid(root,Integer.parseInt(root.data.toString())); + } + + public boolean isValid(BinaryTreeNode node,int d){ + boolean f = false; + if (node == null){ + return false; + } + if (Integer.parseInt(node.data.toString()) <= d) { + f = true; + d = Integer.parseInt(node.data.toString()); + isValid(node.left,d); + }else{ + f = false; + } + + if (Integer.parseInt(node.data.toString()) >= d) { + f = true; + d = Integer.parseInt(node.data.toString()); + isValid(node.right,d); + }else{ + f = false; + } + + return f; + } + + public T getLowestCommonAncestor(T n1, T n2){ + return getLowestCommonAncestor(root,n1,n2); + + } + + public T getLowestCommonAncestor(BinaryTreeNode n,T n1, T n2){ + if (root == null) + return null; + if ((Integer.parseInt(root.data.toString()) >= (Integer) n1 && Integer + .parseInt(root.data.toString()) <= (Integer) n2) + || (Integer.parseInt(root.data.toString()) >= (Integer) n1 && Integer + .parseInt(root.data.toString()) <= (Integer) n2)) + return (T) root.data.toString(); + else if (Integer.parseInt(root.data.toString()) > (Integer) n1 + && Integer.parseInt(root.data.toString()) > (Integer) n2) + return getLowestCommonAncestor(root.left, n1, n2); + else if (Integer.parseInt(root.data.toString()) < (Integer) n1 + && Integer.parseInt(root.data.toString()) < (Integer) n2) + return getLowestCommonAncestor(root.right, n1, n2); + return null; + } + + public List getNodesBetween(T n1, T n2){ + List result = new ArrayList(); + List seq = new ArrayList(); + result.add((T) root.getData()); + BinaryTreeNode node = root; + getNodesBetweenProcess(node,result,seq,n1,n2); + node.setLeft(root.getRight()); + getNodesBetweenProcess(node,result,seq,n1,n2); + return seq; + } + + public List getNodesBetweenProcess(BinaryTreeNode node,List result,List seq, + T n1, T n2){ + while(true){ + if (node.getLeft()!=null){ + if (Integer.parseInt(node.getLeft().getData().toString()) >= Integer + .parseInt(n1.toString()) + && Integer + .parseInt(node.getLeft().getData().toString()) <= Integer + .parseInt(n2.toString())) { + seq.add((T) node.getLeft().getData()); + } + s.push(node.getLeft()); + node = node.getLeft(); + }else{ + node = (BinaryTreeNode)s.pop(); + node.setLeft(node.getRight()); + } + if (node.getLeft()==null&&s.length()==0) + break; + } + return seq; + } } \ No newline at end of file diff --git a/group02/562768642/src/com/github/orajavac/coding2017/basic/tree/BinaryTreeUtil.java b/group02/562768642/src/com/github/orajavac/coding2017/basic/tree/BinaryTreeUtil.java index 10f86313d0..852b0eea8e 100644 --- a/group02/562768642/src/com/github/orajavac/coding2017/basic/tree/BinaryTreeUtil.java +++ b/group02/562768642/src/com/github/orajavac/coding2017/basic/tree/BinaryTreeUtil.java @@ -17,7 +17,19 @@ public class BinaryTreeUtil { */ @SuppressWarnings("unchecked") public static List preOrderVisit(BinaryTreeNode root) { - return null; + List result = new ArrayList(); + preOrderVisit(root,result); + return result; + } + + public static List preOrderVisit(BinaryTreeNode node,List result) { + if(node==null){ + return null; + } + result.add(node.getData()); + preOrderVisit(node.getLeft(),result); + preOrderVisit(node.getRight(),result); + return result; } /** @@ -28,7 +40,17 @@ public static List preOrderVisit(BinaryTreeNode root) { */ public static List inOrderVisit(BinaryTreeNode root) { List result = new ArrayList(); - + inOrderVisit(root,result); + return result; + } + + public static List inOrderVisit(BinaryTreeNode node,List result) { + if(node==null){ + return null; + } + inOrderVisit(node.getLeft(),result); + result.add(node.getData()); + inOrderVisit(node.getRight(),result); return result; } @@ -40,9 +62,21 @@ public static List inOrderVisit(BinaryTreeNode root) { */ public static List postOrderVisit(BinaryTreeNode root) { List result = new ArrayList(); - + postOrderVisit(root,result); return result; } + + public static List postOrderVisit(BinaryTreeNode node,List result) { + if(node==null){ + return null; + } + postOrderVisit(node.getLeft(),result); + inOrderVisit(node.getRight(),result); + result.add(node.getData()); + return result; + } + + /** * 用非递归的方式实现对二叉树的前序遍历 * @param root