From 55cd2d45d17c43c1dc9a3b86d64ac602a1801c7f Mon Sep 17 00:00:00 2001 From: johnChnia Date: Tue, 14 Mar 2017 17:42:14 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E6=90=9C=E7=B4=A2=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=A0=91=E5=AE=9E=E7=8E=B0=E5=8F=8A=E5=8D=95=E5=85=83?= =?UTF-8?q?=E6=B5=8B=E8=AF=95=EF=BC=8C=E4=BF=AE=E6=94=B9ArrayList=E3=80=81?= =?UTF-8?q?LinkList=20=E5=AE=9E=E7=8E=B0=20List=20=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../johnChnia/coding2017/basic/ArrayList.java | 19 +-- .../coding2017/basic/BinarySearchTree.java | 124 ++++++++++++++++++ .../coding2017/basic/LinkedList.java | 29 ++-- .../com/johnChnia/coding2017/basic/List.java | 16 +++ .../com/johnChnia/coding2017/basic/Queue.java | 10 +- .../com/johnChnia/coding2017/basic/Stack.java | 4 +- .../basic/{test => }/ArrayListTest.java | 6 +- .../basic/BinarySearchTreeTest.java | 37 ++++++ .../basic/{test => }/LinkedListTest.java | 3 +- .../basic/{test => }/QueueTest.java | 3 +- .../basic/{test => }/StackTest.java | 2 +- 11 files changed, 221 insertions(+), 32 deletions(-) create mode 100644 group24/315863321/src/main/java/com/johnChnia/coding2017/basic/BinarySearchTree.java create mode 100644 group24/315863321/src/main/java/com/johnChnia/coding2017/basic/List.java rename group24/315863321/src/test/java/com/johnChnia/coding2017/basic/{test => }/ArrayListTest.java (92%) create mode 100644 group24/315863321/src/test/java/com/johnChnia/coding2017/basic/BinarySearchTreeTest.java rename group24/315863321/src/test/java/com/johnChnia/coding2017/basic/{test => }/LinkedListTest.java (95%) rename group24/315863321/src/test/java/com/johnChnia/coding2017/basic/{test => }/QueueTest.java (93%) rename group24/315863321/src/test/java/com/johnChnia/coding2017/basic/{test => }/StackTest.java (97%) diff --git a/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/ArrayList.java b/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/ArrayList.java index 4881c6518c..800f89d6ab 100644 --- a/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/ArrayList.java +++ b/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/ArrayList.java @@ -7,8 +7,8 @@ * @// TODO: 2017/3/15 支持泛型 */ -public class ArrayList { - private int[] elementData; +public class ArrayList implements List{ + private Object[] elementData; private int size = 0; /** @@ -20,7 +20,7 @@ public class ArrayList { */ public ArrayList(int initialCapacity) { if (initialCapacity > 0) { - elementData = new int[initialCapacity]; + elementData = new Object[initialCapacity]; } else { throw new IllegalArgumentException("Illegal Capacity: " + initialCapacity); @@ -34,7 +34,7 @@ public ArrayList(int initialCapacity) { * @return the element at the specified position in this list * @throws IndexOutOfBoundsException {@inheritDoc} */ - public int get(int index) { + public Object get(int index) { rangeCheck(index); rangeCheckForAdd(index); return elementData[index]; @@ -46,7 +46,7 @@ public int get(int index) { * * @param element element to be appended to this list */ - public void add(int element) { + public void add(Object element) { ensureCapacityInternal(size + 1); elementData[size++] = element; } @@ -61,7 +61,7 @@ public void add(int element) { * @param index index at which the specified element is to be inserted * @throws IndexOutOfBoundsException {@inheritDoc} */ - public void add(int element, int index) { + public void add(int index, Object element) { rangeCheckForAdd(index); ensureCapacityInternal(size + 1); System.arraycopy(elementData, index, elementData, index + 1, @@ -79,9 +79,9 @@ public void add(int element, int index) { * @return the element that was removed from the list * @throws IndexOutOfBoundsException {@inheritDoc} */ - public int remove(int index) { + public Object remove(int index) { rangeCheckForAdd(index); - int oldValue = elementData[index]; + Object oldValue = elementData[index]; int numMoved = size() - index - 1; if (numMoved > 0) { System.arraycopy(elementData, index + 1, elementData, index, @@ -115,7 +115,8 @@ public int size() { * number of elements specified by the double length of list. */ private void grow() { - elementData = Arrays.copyOf(elementData, 2 * elementData.length); + elementData = Arrays.copyOf(elementData, + 2 * elementData.length); } public String toString() { diff --git a/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/BinarySearchTree.java b/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/BinarySearchTree.java new file mode 100644 index 0000000000..48d3e41b12 --- /dev/null +++ b/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/BinarySearchTree.java @@ -0,0 +1,124 @@ +package com.johnChnia.coding2017.basic; + +/** + * Created by john on 2017/3/13. + */ +public class BinarySearchTree { + + + /** + * The root node of tree. + */ + public BstNode root; + + private Queue q = new Queue(); + + + private static class BstNode { + private int data; + private BstNode left; + private BstNode right; + + @Override + public String toString() { + return String.valueOf(data); + } + } + + /** + * create an BinarySearchTree. + * + * @param root root node of tree + * @param data stored element + * @return binarySearchTree + */ + public BstNode insert(BstNode root, int data) { + if (root == null) { + root = getNewBstNode(data); + if (this.root == null) { + this.root = root; + } + return root; + } + if (data <= root.data) { + root.left = insert(root.left, data); + } else { + root.right = insert(root.right, data); + } + return root; + } + + private BstNode getNewBstNode(int data) { + BstNode node = new BstNode(); + node.data = data; + node.left = null; + node.right = null; + return node; + } + + /** + * Returns the minimum value in the tree. + * + * @param root root node of the tree。 + * @return the minimum value in the tree + * @throws IllegalArgumentException if root is null. + */ + public int findMin(BstNode root) { + int min; + if (root == null) { + throw new IllegalArgumentException("tree is empty"); + } else if (root.left == null) { + min = root.data; + } else { + min = findMin(root.left); + } + return min; + } + + /** + * Returns the maximum value in the tree. + * + * @param root root node of the tree。 + * @return the maximum value in the tree + * @throws IllegalArgumentException if root is null. + */ + public int findMax(BstNode root) { + int max; + if (root == null) { + throw new IllegalArgumentException("tree is empty"); + } else if (root.right == null) { + max = root.data; + } else { + max = findMax(root.right); + } + return max; + } + + + /** + * Traverse each node from left to right. + * + * @param root root node of the tree + */ + public void LevelOrder(BstNode root) { + if (root == null) { + return; + } + q.add(root); + while (!q.empty()) { + BstNode current = (BstNode) q.peek(); + if (current.left != null) { + q.add(current.left); + } + if (current.right != null) { + q.add(current.right); + } + q.remove(); + } + } + + public BstNode getRoot() { + return root; + } + +} diff --git a/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/LinkedList.java b/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/LinkedList.java index f2825659b9..79109cc5c8 100644 --- a/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/LinkedList.java +++ b/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/LinkedList.java @@ -8,7 +8,7 @@ * @// TODO: 2017/3/15 支持泛型 */ -public class LinkedList { +public class LinkedList implements List { private Node first = null; private int size = 0; @@ -21,7 +21,7 @@ public LinkedList() { } private static class Node { - int element; + Object element; Node next; Node prev; } @@ -31,7 +31,7 @@ private static class Node { * * @param element element to be appended to this list */ - public void add(int element) { + public void add(Object element) { Node newNode = new Node(); if (first == null) { addWhenListIsEmpty(newNode, element); @@ -47,7 +47,7 @@ public void add(int element) { size++; } - private void addWhenListIsEmpty(Node newNode, int element) { + private void addWhenListIsEmpty(Node newNode, Object element) { first = newNode; first.element = element; first.next = null; @@ -60,7 +60,7 @@ private void addWhenListIsEmpty(Node newNode, int element) { * * @param element the element to add */ - public void addFirst(int element) { + public void addFirst(Object element) { Node newNode = new Node(); if (first == null) { addWhenListIsEmpty(newNode, element); @@ -85,7 +85,7 @@ public void addFirst(int element) { * @param element element to be inserted. * @throws RuntimeException if list size less than 2. */ - public void add(int index, int element) { + public void add(int index, Object element) { if (size() < 2) throw new RuntimeException("list size should greater than or equal to 2"); isElementIndex(index); @@ -132,16 +132,25 @@ public void remove() { } + /** + * @param index + * @return + * @// TODO: 2018/3/14 if i am happy, i will implement it right now! + */ + public Object remove(int index) { + return null; + } + /** * Removes and returns the first element from this list. * * @return the first element from this list */ - public int removeFirst() { + public Object removeFirst() { Node f = first; if (f == null) throw new NoSuchElementException(); - int element = f.element; + Object element = f.element; Node next = first.next; first.element = 0; first.next = null; // help GC @@ -160,7 +169,7 @@ public int removeFirst() { * @param index index of the element to return * @return the element at the specified position in this list */ - public int get(int index) { + public Object get(int index) { checkElementIndex(index); Node node = first; if (index == 0) { @@ -178,7 +187,7 @@ public int get(int index) { * @return the first element in this list * @throws NoSuchElementException if this list is empty */ - public int getFirst() { + public Object getFirst() { final Node f = first; if (f == null) throw new NoSuchElementException(); diff --git a/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/List.java b/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/List.java new file mode 100644 index 0000000000..5616a17e70 --- /dev/null +++ b/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/List.java @@ -0,0 +1,16 @@ +package com.johnChnia.coding2017.basic; + +/** + * Created by john on 2017/3/12. + */ +public interface List { + public void add(Object o); + + public void add(int index, Object o); + + public Object get(int index); + + public Object remove(int index); + + public int size(); +} diff --git a/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/Queue.java b/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/Queue.java index 4fd2558ea3..2551b7a243 100644 --- a/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/Queue.java +++ b/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/Queue.java @@ -26,7 +26,7 @@ public Queue() { * @param element the element to add * @return {@code true} */ - public boolean add(int element) { + public boolean add(Object element) { arrayList.add(element); return true; } @@ -38,7 +38,7 @@ public boolean add(int element) { * @return the head of this queue * @throws NoSuchElementException if this queue is empty */ - public int remove() { + public Object remove() { if (arrayList.empty()) throw new NoSuchElementException(emptyMsg()); return arrayList.remove(0); @@ -52,7 +52,7 @@ public int remove() { * * @return the head of this queue, or {@code 0} if this queue is empty */ - public int peek() { + public Object peek() { if (arrayList.empty()) return 0; return arrayList.get(0); @@ -80,4 +80,8 @@ public int size() { private String emptyMsg() { return "Size: " + size(); } + + public boolean empty() { + return arrayList.size() == 0; + } } diff --git a/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/Stack.java b/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/Stack.java index e11be853f9..d6a496217d 100644 --- a/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/Stack.java +++ b/group24/315863321/src/main/java/com/johnChnia/coding2017/basic/Stack.java @@ -33,7 +33,7 @@ public void push(int element) { * @return The object at the top of this stack. * @throws EmptyStackException if this stack is empty. */ - public int pop() { + public Object pop() { if (empty()) { throw new EmptyStackException(); } @@ -47,7 +47,7 @@ public int pop() { * @return the object at the top of this stack. * @throws EmptyStackException if this stack is empty. */ - public int peek() { + public Object peek() { if (empty()) { throw new EmptyStackException(); } diff --git a/group24/315863321/src/test/java/com/johnChnia/coding2017/basic/test/ArrayListTest.java b/group24/315863321/src/test/java/com/johnChnia/coding2017/basic/ArrayListTest.java similarity index 92% rename from group24/315863321/src/test/java/com/johnChnia/coding2017/basic/test/ArrayListTest.java rename to group24/315863321/src/test/java/com/johnChnia/coding2017/basic/ArrayListTest.java index 13c81724ad..a80003b85e 100644 --- a/group24/315863321/src/test/java/com/johnChnia/coding2017/basic/test/ArrayListTest.java +++ b/group24/315863321/src/test/java/com/johnChnia/coding2017/basic/ArrayListTest.java @@ -1,4 +1,4 @@ -package com.johnChnia.coding2017.basic.test; +package com.johnChnia.coding2017.basic; import com.johnChnia.coding2017.basic.ArrayList; import org.junit.Before; @@ -44,7 +44,7 @@ public void testAddElementByIndex() { for (int i = 0; i < 3; i++) { arrayList3.add(10); } - arrayList3.add(1000, 1); + arrayList3.add(1, 1000); assertThat(arrayList3.get(1), equalTo(1000)); } @@ -53,7 +53,7 @@ public void testRemoveElementByIndex() { for (int i = 0; i < 6; i++) { arrayList4.add(i); } - int removed = arrayList4.remove(4); + Object removed = arrayList4.remove(4); System.out.println(arrayList4); assertThat(removed, equalTo(4)); assertThat(arrayList4.size(), equalTo(5)); diff --git a/group24/315863321/src/test/java/com/johnChnia/coding2017/basic/BinarySearchTreeTest.java b/group24/315863321/src/test/java/com/johnChnia/coding2017/basic/BinarySearchTreeTest.java new file mode 100644 index 0000000000..d756c31198 --- /dev/null +++ b/group24/315863321/src/test/java/com/johnChnia/coding2017/basic/BinarySearchTreeTest.java @@ -0,0 +1,37 @@ +package com.johnChnia.coding2017.basic; + +import org.junit.Before; +import org.junit.Test; + +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.junit.MatcherAssert.assertThat; + +/** + * Created by john on 2017/3/14. + */ +public class BinarySearchTreeTest { + + private BinarySearchTree bst; + + @Before + public void setUp() throws Exception { + bst = new BinarySearchTree(); + bst.insert(bst.getRoot(), 10); + bst.insert(bst.getRoot(), 20); + bst.insert(bst.getRoot(), 9); + bst.insert(bst.getRoot(), 11); + bst.insert(bst.getRoot(), 12); + } + + @Test + public void testFindMin() throws Exception { + assertThat(bst.findMin(bst.getRoot()), equalTo(9)); + + } + + @Test + public void testFindMax() throws Exception { + assertThat(bst.findMax(bst.getRoot()), equalTo(20)); + } + +} \ No newline at end of file diff --git a/group24/315863321/src/test/java/com/johnChnia/coding2017/basic/test/LinkedListTest.java b/group24/315863321/src/test/java/com/johnChnia/coding2017/basic/LinkedListTest.java similarity index 95% rename from group24/315863321/src/test/java/com/johnChnia/coding2017/basic/test/LinkedListTest.java rename to group24/315863321/src/test/java/com/johnChnia/coding2017/basic/LinkedListTest.java index bd8ad5ac4c..b92defffd8 100644 --- a/group24/315863321/src/test/java/com/johnChnia/coding2017/basic/test/LinkedListTest.java +++ b/group24/315863321/src/test/java/com/johnChnia/coding2017/basic/LinkedListTest.java @@ -1,6 +1,5 @@ -package com.johnChnia.coding2017.basic.test; +package com.johnChnia.coding2017.basic; -import com.johnChnia.coding2017.basic.LinkedList; import org.junit.Before; import org.junit.Test; diff --git a/group24/315863321/src/test/java/com/johnChnia/coding2017/basic/test/QueueTest.java b/group24/315863321/src/test/java/com/johnChnia/coding2017/basic/QueueTest.java similarity index 93% rename from group24/315863321/src/test/java/com/johnChnia/coding2017/basic/test/QueueTest.java rename to group24/315863321/src/test/java/com/johnChnia/coding2017/basic/QueueTest.java index 54cd7f9385..ebe017b2d6 100644 --- a/group24/315863321/src/test/java/com/johnChnia/coding2017/basic/test/QueueTest.java +++ b/group24/315863321/src/test/java/com/johnChnia/coding2017/basic/QueueTest.java @@ -1,6 +1,5 @@ -package com.johnChnia.coding2017.basic.test; +package com.johnChnia.coding2017.basic; -import com.johnChnia.coding2017.basic.Queue; import org.junit.Before; import org.junit.Test; diff --git a/group24/315863321/src/test/java/com/johnChnia/coding2017/basic/test/StackTest.java b/group24/315863321/src/test/java/com/johnChnia/coding2017/basic/StackTest.java similarity index 97% rename from group24/315863321/src/test/java/com/johnChnia/coding2017/basic/test/StackTest.java rename to group24/315863321/src/test/java/com/johnChnia/coding2017/basic/StackTest.java index 7fb4a35757..43a7d858d9 100644 --- a/group24/315863321/src/test/java/com/johnChnia/coding2017/basic/test/StackTest.java +++ b/group24/315863321/src/test/java/com/johnChnia/coding2017/basic/StackTest.java @@ -1,4 +1,4 @@ -package com.johnChnia.coding2017.basic.test; +package com.johnChnia.coding2017.basic; import com.johnChnia.coding2017.basic.Stack; import org.junit.Before;