Skip to content

Commit

Permalink
Merge pull request #9 from onlyLYJ/master
Browse files Browse the repository at this point in the history
hope no surprise this time
  • Loading branch information
guodongym authored Feb 26, 2017
2 parents 80f24b9 + 3ea180f commit 1595986
Show file tree
Hide file tree
Showing 20 changed files with 1,535 additions and 0 deletions.
7 changes: 7 additions & 0 deletions group12/382266293/.classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
<classpathentry kind="output" path="bin"/>
</classpath>
1 change: 1 addition & 0 deletions group12/382266293/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/bin/
17 changes: 17 additions & 0 deletions group12/382266293/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>382266293Learning</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
43 changes: 43 additions & 0 deletions group12/382266293/src/Collection/AbstractList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package Collection;

public abstract class AbstractList<E> implements List<E> {

protected static final String PREFIX = "[";
protected static final String SUFFIX = "]";
protected static final String SEPERATOR = ", ";
protected static final int MAX_SIZE = Integer.MAX_VALUE/3;

protected void checkIndex(int i) {
if( i < 0 || i > Math.min(size(), MAX_SIZE))
throw new IndexOutOfBoundsException("Size :" + size()+", Index: " + i);
}

public boolean isEmpty() {
return size() == 0;
}


public int indexOf(E e) {
for (int i = 0; i < size()-1; i++) {
if (get(i).equals(e))
return i;
}
return -1;
}

protected abstract Iterator<E> iterator();

@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(PREFIX);
for (int i = 0; i < size(); i++) {
sb.append(get(i));
if (i < size()-1)
sb.append(SEPERATOR);
}
sb.append(SUFFIX);
return sb.toString();
}

}
148 changes: 148 additions & 0 deletions group12/382266293/src/Collection/Concrete/ArrayList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
package Collection.Concrete;

import java.util.Arrays;
import java.util.NoSuchElementException;

import Collection.AbstractList;
import Collection.Iterator;

public class ArrayList<E> extends AbstractList<E> {

private E[] elements;
private int size;
private static final int INITIAL_SIZE = 16;


public ArrayList() {
elements = (E[]) new Object[INITIAL_SIZE];
size = 0;
}

@Override
public void add(E e) {
checkCapacity();
elements[size++] = e;
}

private void checkCapacity() {
if (size >= MAX_SIZE)
throw new IndexOutOfBoundsException("Reached max size");

if (elements.length - size < INITIAL_SIZE)
grow();
}

synchronized private void grow() {

int newCapacity = size * 2;
newCapacity = (newCapacity < 0 || newCapacity - MAX_SIZE > 0) ? MAX_SIZE : newCapacity;
E[] target = (E[]) new Object[newCapacity];
System.arraycopy(elements, 0, target, 0, size);
elements = target;

}

public void add(int index, E e) {
checkCapacity();
if (index == size) {
add(e);
return;
}
checkIndex(index);
synchronized (this) {
System.arraycopy(elements, index, elements, index+1, size-index+1);
elements[index] = e;
size++;
}
}

@Override
public E get(int index) {
checkIndex(index);
return elements[index];
}


public E getLast() {
return get(size-1);
}

public void addLast(E e) {
add(e);
}

public E removeLast() {
return elements[--size];
}

public E remove(int index) {
checkIndex(index);
E result = elements[index];
synchronized (this) {
System.arraycopy(elements, index+1, elements, index, size-index-1);
elements[--size] = null;
}
return result;
}

@Override
public int size() {
return size;
}


@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + Arrays.hashCode(elements);
result = prime * result + size;
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
ArrayList other = (ArrayList) obj;
if (!Arrays.equals(elements, other.elements))
return false;
if (size != other.size)
return false;
return true;
}

public Iterator<E> iterator(){
return new ArrayListIterator<E>(this);
}

private class ArrayListIterator<E> implements Iterator<E> {

private ArrayList<E> myArrayList;
private int pos;

public ArrayListIterator(ArrayList<E> arrayList) {
myArrayList = arrayList;
pos = 0;
}

@Override
public boolean hasNext() {
return pos < size;
}

@Override
public E next() {
if (hasNext())
return (E) elements[pos++];
throw new NoSuchElementException();
}
}



}
126 changes: 126 additions & 0 deletions group12/382266293/src/Collection/Concrete/BinaryTreeNode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
package Collection.Concrete;

public class BinaryTreeNode<E extends Comparable<E>> {

private E data;
private BinaryTreeNode left;
private BinaryTreeNode right;
private int size;
private ArrayList<E> myList;



public BinaryTreeNode() {
this.data = null;
this.left = null;
this.right = null;
}

public BinaryTreeNode(E data) {
this.data = data;
this.left = null;
this.right = null;
}

public boolean isEmpty() {
return data == null;
}

public int size() {
return size;
}

public BinaryTreeNode<E> insert(E o) {
BinaryTreeNode<E> res;
if (isEmpty()) {
data = o;
size++;
return this;
} else {
BinaryTreeNode<E> p = this;
res = new BinaryTreeNode<E>(o);
while (true) {
if (res.getData().compareTo(p.getData()) < 0) {
if (p.left == null) {
p.setLeft(res);
break;
}
p = p.left;
} else if (res.getData().compareTo(p.getData()) > 0) {
if (p.right == null) {
p.setRight(res);
break;
}
p = p.right;
} else {
return null;
}
}
size++;
}
return res;
}



public ArrayList<E> preOrderTraversal(BinaryTreeNode<E> node) {

if (node != null) {
preOrderTraversal(node.left);
myList.add(node.data);
preOrderTraversal(node.right);
}
return myList;
}

@Override
public String toString() {
myList = new ArrayList<E>();
return preOrderTraversal(this).toString();
}

public E getData() {
return data;
}
public void setData(E 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;
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((data == null) ? 0 : data.hashCode());
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
BinaryTreeNode other = (BinaryTreeNode) obj;
if (data == null) {
if (other.data != null)
return false;
} else if (!data.equals(other.data))
return false;
return true;
}

}
Loading

0 comments on commit 1595986

Please sign in to comment.