forked from onlyliuxin/coding2017
-
Notifications
You must be signed in to change notification settings - Fork 7
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 #6 from Viscaria233/master
数据结构单元测试
- Loading branch information
Showing
16 changed files
with
1,006 additions
and
0 deletions.
There are no files selected for viewing
136 changes: 136 additions & 0 deletions
136
group01/895457260/code/src/datastructure/basic/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,136 @@ | ||
package datastructure.basic; | ||
|
||
public class ArrayList implements List { | ||
|
||
private int size = 0; | ||
|
||
private Object[] elementData; | ||
|
||
public ArrayList() { | ||
elementData = new Object[100]; | ||
} | ||
|
||
public ArrayList(int initCapacity) { | ||
elementData = new Object[initCapacity]; | ||
} | ||
|
||
public void add(Object o) { | ||
autoGrow(); | ||
elementData[size()] = o; | ||
size++; | ||
} | ||
|
||
public void add(int index, Object o) { | ||
autoGrow(); | ||
System.arraycopy(elementData, index, elementData, index + 1, size() - index); | ||
elementData[index] = o; | ||
size++; | ||
} | ||
|
||
public Object get(int index) { | ||
checkIndex(index); | ||
return elementData[index]; | ||
} | ||
|
||
public Object remove(int index) { | ||
checkIndex(index); | ||
Object removed = elementData[index]; | ||
System.arraycopy(elementData, index + 1, elementData, index, size() - index - 1); | ||
size--; | ||
return removed; | ||
} | ||
|
||
public int size() { | ||
return size; | ||
} | ||
|
||
public Iterator iterator() { | ||
return new Iterator() { | ||
int index = -1; | ||
@Override | ||
public boolean hasNext() { | ||
return index + 1 < size(); | ||
} | ||
|
||
@Override | ||
public Object next() { | ||
index++; | ||
return elementData[index]; | ||
} | ||
}; | ||
} | ||
|
||
private void autoGrow() { | ||
if (size >= elementData.length) { | ||
Object[] newArray = new Object[nextCapacity()]; | ||
System.arraycopy(elementData, 0, newArray, 0, elementData.length); | ||
elementData = newArray; | ||
} | ||
} | ||
|
||
private int nextCapacity() { | ||
return elementData.length * 2; | ||
} | ||
|
||
private void checkIndex(int index) { | ||
if (index >= size() || index < 0) { | ||
throw new IndexOutOfBoundsException(indexOutOfBoundMessage(index)); | ||
} | ||
} | ||
|
||
private String indexOutOfBoundMessage(int index) { | ||
return "index: " + index + ", size: " + size(); | ||
} | ||
|
||
public static void main(String[] args) { | ||
ArrayList list = new ArrayList(); | ||
for (int i = 0; i < 10; ++i) { | ||
list.add(i); | ||
list.add(10 - i); | ||
} | ||
System.out.println("------------------size"); | ||
System.out.println("size: " + list.size()); | ||
|
||
System.out.println("------------------for(int i)"); | ||
for (int i = 0; i < list.size(); ++i) { | ||
System.out.print(list.get(i) + " "); | ||
} | ||
|
||
System.out.println("\n-----------------iterator"); | ||
Iterator iterator = list.iterator(); | ||
while (iterator.hasNext()) { | ||
System.out.print(iterator.next() + " "); | ||
} | ||
|
||
System.out.println("\n-----------------add at index 0 100~104"); | ||
for (int i = 100; i < 105; ++i) { | ||
list.add(0, i); | ||
} | ||
list.print(); | ||
System.out.println("-----------------add at last 200~204"); | ||
for (int i = 200; i < 205; ++i) { | ||
list.add(list.size(), i); | ||
} | ||
list.print(); | ||
|
||
System.out.println("-----------------removeFirst x4"); | ||
for (int i = 0; i < 4; ++i) { | ||
list.remove(0); | ||
} | ||
list.print(); | ||
|
||
System.out.println("\n-----------------removeLast x4"); | ||
for (int i = 0; i < 4; ++i) { | ||
list.remove(list.size() - 1); | ||
} | ||
list.print(); | ||
} | ||
|
||
public void print() { | ||
Iterator iterator = iterator(); | ||
while (iterator.hasNext()) { | ||
System.out.print(iterator.next() + " "); | ||
} | ||
System.out.println("\nsize: " + size()); | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
group01/895457260/code/src/datastructure/basic/BinarySortedTree.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,61 @@ | ||
package datastructure.basic; | ||
|
||
/** | ||
* Created by Haochen on 2017/2/24. | ||
* TODO: | ||
*/ | ||
public class BinarySortedTree<T extends Comparable> { | ||
|
||
private BinaryTreeNode root = null; | ||
|
||
public void traverse(Visitor visitor) { | ||
traverse(root, visitor); | ||
} | ||
|
||
private void traverse(BinaryTreeNode node, Visitor visitor) { | ||
if (node == null) { | ||
return; | ||
} | ||
traverse(node.getLeft(), visitor); | ||
visitor.visit(node); | ||
traverse(node.getRight(), visitor); | ||
} | ||
|
||
public interface Visitor { | ||
void visit(BinaryTreeNode node); | ||
} | ||
|
||
//不递归的写法 | ||
public void add(T o) { | ||
//根节点空,直接加入 | ||
if (root == null) { | ||
root = new BinaryTreeNode(); | ||
root.setData(o); | ||
} else { | ||
BinaryTreeNode target = root; | ||
//从根结点不断向下比较target和o,o小则往左,o大则往右,相等不加入 | ||
while (true) { | ||
int compare = o.compareTo(target.getData()); | ||
if (compare == 0) {//相等不加入 | ||
return; | ||
} else if (compare < 0) {//o小往左 | ||
if (target.getLeft() == null) {//左空则加入 | ||
target.setLeft(new BinaryTreeNode()); | ||
target.getLeft().setData(o); | ||
return; | ||
} else {//不空继续比较 | ||
target = target.getLeft(); | ||
} | ||
} else {//o大往右 | ||
if (target.getRight() == null) { | ||
target.setRight(new BinaryTreeNode()); | ||
target.getRight().setData(o); | ||
return; | ||
} else { | ||
target = target.getRight(); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
group01/895457260/code/src/datastructure/basic/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,32 @@ | ||
package datastructure.basic; | ||
|
||
public class BinaryTreeNode { | ||
|
||
private Object data; | ||
private BinaryTreeNode left; | ||
private BinaryTreeNode right; | ||
|
||
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){ | ||
return null; | ||
} | ||
|
||
} |
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,6 @@ | ||
package datastructure.basic; | ||
|
||
public interface Iterator { | ||
boolean hasNext(); | ||
Object next(); | ||
} |
132 changes: 132 additions & 0 deletions
132
group01/895457260/code/src/datastructure/basic/LinkedList.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,132 @@ | ||
package datastructure.basic; | ||
|
||
import datastructure.exception.EmptyListException; | ||
|
||
public class LinkedList implements List { | ||
|
||
private Node head; | ||
private int size; | ||
|
||
public LinkedList() { | ||
head = new Node(); | ||
} | ||
|
||
@Override | ||
public void add(Object o) { | ||
addLast(o); | ||
} | ||
|
||
@Override | ||
public void add(int index , Object o) { | ||
Node pre = findNode(index - 1); | ||
Node node = new Node(); | ||
node.data = o; | ||
addNode(node, pre); | ||
} | ||
|
||
@Override | ||
public Object get(int index) { | ||
checkIndex(index); | ||
return findNode(index).data; | ||
} | ||
|
||
@Override | ||
public Object remove(int index) { | ||
checkIndex(index); | ||
Node pre = findNode(index - 1); | ||
Node removed = pre.next; | ||
removeNode(removed, pre); | ||
return removed.data; | ||
} | ||
|
||
@Override | ||
public int size() { | ||
return size; | ||
} | ||
|
||
public void addFirst(Object o) { | ||
Node node = new Node(); | ||
node.data = o; | ||
addNode(node, head); | ||
} | ||
|
||
public void addLast(Object o) { | ||
Node node = new Node(); | ||
node.data = o; | ||
Node pre = findNode(size() - 1); | ||
addNode(node, pre); | ||
} | ||
|
||
public Object removeFirst() { | ||
if (size() == 0) { | ||
throw new EmptyListException(); | ||
} | ||
Node removed = head.next; | ||
removeNode(head.next, head); | ||
return removed.data; | ||
} | ||
|
||
public Object removeLast() { | ||
if (size() == 0) { | ||
throw new EmptyListException(); | ||
} | ||
return remove(size() - 1); | ||
} | ||
|
||
@Override | ||
public Iterator iterator() { | ||
return new Iterator() { | ||
Node node = head; | ||
@Override | ||
public boolean hasNext() { | ||
return node.next != null; | ||
} | ||
|
||
@Override | ||
public Object next() { | ||
node = node.next; | ||
return node.data; | ||
} | ||
}; | ||
} | ||
|
||
private static class Node{ | ||
Object data; | ||
Node next; | ||
} | ||
|
||
private Node findNode(int index) { | ||
if (index == -1) { | ||
return head; | ||
} else { | ||
checkIndex(index); | ||
} | ||
Node node = head.next; | ||
for (int i = 0; i < index; ++i) { | ||
node = node.next; | ||
} | ||
return node; | ||
} | ||
|
||
private void checkIndex(int index) { | ||
if (index >= size() || index < 0) { | ||
throw new IndexOutOfBoundsException(indexOutOfBoundMessage(index)); | ||
} | ||
} | ||
|
||
private String indexOutOfBoundMessage(int index) { | ||
return "index: " + index + ", size: " + size(); | ||
} | ||
|
||
private void addNode(Node node, Node pre) { | ||
node.next = pre.next; | ||
pre.next = node; | ||
size++; | ||
} | ||
|
||
private void removeNode(Node node, Node pre) { | ||
pre.next = node.next; | ||
node.next = null; | ||
size--; | ||
} | ||
} |
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 datastructure.basic; | ||
|
||
public interface List { | ||
void add(Object o); | ||
void add(int index, Object o); | ||
Object get(int index); | ||
Object remove(int index); | ||
int size(); | ||
Iterator iterator(); | ||
} |
Oops, something went wrong.