Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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);
Expand All @@ -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];
Expand All @@ -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;
}
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* @// TODO: 2017/3/15 支持泛型
*/

public class LinkedList {
public class LinkedList implements List {

private Node first = null;
private int size = 0;
Expand All @@ -21,7 +21,7 @@ public LinkedList() {
}

private static class Node {
int element;
Object element;
Node next;
Node prev;
}
Expand All @@ -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);
Expand All @@ -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;
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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
Expand All @@ -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) {
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -80,4 +80,8 @@ public int size() {
private String emptyMsg() {
return "Size: " + size();
}

public boolean empty() {
return arrayList.size() == 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand All @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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));
}

Expand All @@ -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));
Expand Down
Loading