forked from onlyliuxin/coding2017
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from MrWenGQ/master
Master
- Loading branch information
Showing
13 changed files
with
1,093 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
group22/1193590951/githubitem/src/com/github/mrwengq/first/ArrayList.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package com.github.mrwengq.first; | ||
|
||
public class ArrayList implements List { | ||
|
||
private int size = 0; | ||
private Object elementData[] = new Object[5]; | ||
|
||
public void add(Object o) { | ||
if (size >= elementData.length) | ||
elementData = copyAddArray(elementData); | ||
elementData[size] = o; | ||
size++; | ||
} | ||
|
||
public void add(int index, Object o) { | ||
if (index > size - 1 || index < 0) { | ||
throw new ArrayIndexOutOfBoundsException(); | ||
} else { | ||
elementData = addUpdateArray(elementData, index); | ||
elementData[index] = o; | ||
size++; | ||
return; | ||
} | ||
} | ||
|
||
public Object get(int index) { | ||
if (index > size - 1 || index < 0) | ||
throw new ArrayIndexOutOfBoundsException(); | ||
else | ||
return elementData[index]; | ||
} | ||
|
||
public Object remove(int index) { | ||
if (index > size - 1 || index < 0) | ||
throw new ArrayIndexOutOfBoundsException(); | ||
if (index == size - 1) { | ||
elementData[index] = null; | ||
size--; | ||
return null; | ||
} else { | ||
delUpdateArray(elementData, index); | ||
size--; | ||
return null; | ||
} | ||
} | ||
|
||
public int size() { | ||
return size; | ||
} | ||
|
||
public Iterator iterator() { | ||
return new Iterator() { | ||
|
||
int index=-1; | ||
|
||
public boolean hasNext() { | ||
index++; | ||
if(index<size){ | ||
|
||
Object ob = elementData[index]; | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
public Object next() { | ||
return elementData[index]; | ||
} | ||
|
||
}; | ||
} | ||
|
||
private Object[] copyAddArray(Object elementData[]) { //对数组扩容 增加量为原长度3/4 | ||
Object ob[] = new Object[elementData.length+(elementData.length * 3) / 4]; | ||
System.arraycopy(((Object) (elementData)), 0, ((Object) (ob)), 0, | ||
elementData.length); | ||
return ob; | ||
} | ||
|
||
private Object[] addUpdateArray(Object elementData[], int index) {//添加时修改数组索引 | ||
Object temp = null; //中间变量 | ||
for (int i = 0; i < size; i++) | ||
if (i > index) {//判断受影响索引 | ||
temp = elementData[index]; | ||
elementData[index] = elementData[i]; | ||
elementData[i] = temp; | ||
if (i == size - 1) { //判断为最后一位 | ||
if (size > elementData.length) | ||
elementData = copyAddArray(elementData); | ||
elementData[size] = elementData[index]; | ||
} | ||
} | ||
|
||
return elementData; | ||
} | ||
|
||
private void delUpdateArray(Object elementData[], int index) {//删除时修改索引 | ||
for (int i = 0; i < size; i++){ | ||
|
||
if (i > index && i < size ){//判断受影响索引 | ||
elementData[i - 1] = elementData[i]; | ||
if (i == size - 1){ | ||
elementData[i] = null; | ||
} | ||
} | ||
|
||
} | ||
} | ||
|
||
} |
112 changes: 112 additions & 0 deletions
112
group22/1193590951/githubitem/src/com/github/mrwengq/first/ArrayListTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
package com.github.mrwengq.first; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
public class ArrayListTest { | ||
ArrayList list = new ArrayList(); | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
} | ||
|
||
@After | ||
public void tearDown() throws Exception { | ||
} | ||
|
||
@Test | ||
public void testAddObject() { | ||
ArrayList list = new ArrayList(); | ||
list.add(1); | ||
list.add(2); | ||
list.add(3); | ||
list.add(4); | ||
list.add(5); | ||
list.add(6); | ||
list.add(7); | ||
int[] o = new int[] { 1, 2, 3, 4, 5, 6, 7 }; | ||
for (int i = 0; i < list.size(); i++) { | ||
assertEquals(o[i], o[i]); | ||
} | ||
|
||
} | ||
|
||
@Test | ||
public void testAddIntObject() { | ||
ArrayList list = new ArrayList(); | ||
list.add(1); | ||
list.add(2); | ||
list.add(3); | ||
list.add(4); | ||
list.add(5); | ||
list.add(6); | ||
list.add(7); | ||
list.add(5, 9); | ||
int[] o = new int[] { 1, 2, 3, 4, 5, 9, 6, 7 }; | ||
for (int i = 0; i < list.size(); i++) { | ||
assertEquals(o[i], o[i]); | ||
} | ||
} | ||
|
||
@Test | ||
public void testGet() { | ||
ArrayList list = new ArrayList(); | ||
list.add(1); | ||
list.add(2); | ||
list.add(3); | ||
list.add(4); | ||
list.add(5); | ||
list.add(6); | ||
list.add(7); | ||
assertEquals(list.get(5), 6); | ||
assertEquals(list.get(2), 3); | ||
assertEquals(list.get(4), 5); | ||
assertEquals(list.get(6), 7); | ||
} | ||
|
||
@Test | ||
public void testRemove() { | ||
ArrayList list = new ArrayList(); | ||
list.add(1); | ||
list.add(2); | ||
list.add(3); | ||
list.add(4); | ||
list.add(5); | ||
list.remove(3); | ||
assertEquals(list.get(3), 5); | ||
} | ||
|
||
@Test | ||
public void testSize() { | ||
ArrayList list = new ArrayList(); | ||
list.add(1); | ||
list.add(2); | ||
list.add(3); | ||
list.add(4); | ||
list.add(5); | ||
assertEquals(list.size(), 5); | ||
} | ||
|
||
@Test | ||
public void testIterator() { | ||
ArrayList list = new ArrayList(); | ||
list.add(1); | ||
list.add(2); | ||
list.add(3); | ||
list.add(4); | ||
list.add(5); | ||
Iterator iter = list.iterator(); | ||
int i = 0; | ||
int[] o = new int[] { 1, 2, 3, 4, 5}; | ||
|
||
while(iter.hasNext()){ | ||
|
||
assertEquals(iter.next(),o[i]); | ||
i++; | ||
} | ||
} | ||
|
||
} |
98 changes: 98 additions & 0 deletions
98
group22/1193590951/githubitem/src/com/github/mrwengq/first/BinaryTreeNode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package com.github.mrwengq.first; | ||
|
||
import java.util.Comparator; | ||
|
||
public class BinaryTreeNode { | ||
|
||
private Object data; | ||
private BinaryTreeNode left; | ||
private BinaryTreeNode right; | ||
private Comparator cpt; | ||
public BinaryTreeNode() { | ||
cpt = new Comparator() { | ||
|
||
public int compare(Object o1, Object o2) { | ||
int ob1 = ((Integer) o1).intValue(); | ||
int ob2 = ((Integer) o2).intValue(); | ||
if (ob1 > ob2){ | ||
return 1; | ||
} | ||
if(ob1<ob2){ | ||
return -1; | ||
} | ||
return 0; | ||
} | ||
|
||
}; | ||
} | ||
|
||
public Object getData() { | ||
return data; | ||
} | ||
|
||
public void setData(Object 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; | ||
} | ||
|
||
public BinaryTreeNode insert(Object o) { | ||
if (data == null) { | ||
data = o; | ||
return null; | ||
} | ||
BinaryTreeNode tree = new BinaryTreeNode(); | ||
tree.setData(o); | ||
int comValue = cpt.compare(data, tree.data); | ||
if (comValue > 0) { | ||
if (getLeft() == null) { | ||
this.setLeft(tree); | ||
return null; | ||
} | ||
left.insert(o); | ||
} else if (comValue < 0) { | ||
if (getRight() == null) { | ||
this.setRight(tree); | ||
return null; | ||
} | ||
right.insert(o); | ||
} | ||
return null; | ||
} | ||
//新加入的方法用于获取节点,帮助测试insert | ||
public BinaryTreeNode findNode(Object o){ | ||
|
||
|
||
if((int)this.data == (int)o){ | ||
return this; | ||
|
||
}else if((int)this.data > (int)o){ | ||
if(this.left!=null){ | ||
return this.left.findNode(o); | ||
} | ||
return null; | ||
}else if((int)this.data <(int)o){ | ||
if(this.right!=null){ | ||
return this.right.findNode(o); | ||
} | ||
return null; | ||
} | ||
return null; | ||
|
||
} | ||
|
||
} |
37 changes: 37 additions & 0 deletions
37
group22/1193590951/githubitem/src/com/github/mrwengq/first/BinaryTreeNodeTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.github.mrwengq.first; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
public class BinaryTreeNodeTest { | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
} | ||
|
||
@After | ||
public void tearDown() throws Exception { | ||
} | ||
|
||
@Test | ||
public void testInsert() { | ||
BinaryTreeNode btnt = new BinaryTreeNode(); | ||
btnt.insert(15); | ||
btnt.insert(16); | ||
btnt.insert(23); | ||
btnt.insert(10); | ||
btnt.insert(18); | ||
btnt.insert(9); | ||
|
||
assertEquals(btnt.getData(), 15); | ||
assertEquals(btnt.getLeft().getData(),10); | ||
assertEquals(btnt.getRight().getData(),16); | ||
assertEquals(btnt.findNode(16).getRight().getData(),23); | ||
assertEquals(btnt.findNode(23).getLeft().getData(),18); | ||
assertEquals(btnt.findNode(10).getLeft().getData(),9); | ||
} | ||
|
||
} |
10 changes: 10 additions & 0 deletions
10
group22/1193590951/githubitem/src/com/github/mrwengq/first/Iterator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.github.mrwengq.first; | ||
|
||
|
||
public interface Iterator | ||
{ | ||
|
||
public abstract boolean hasNext(); | ||
|
||
public abstract Object next(); | ||
} |
Oops, something went wrong.