forked from onlyliuxin/coding2017
-
Notifications
You must be signed in to change notification settings - Fork 11
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 #26 from sargeles/master
第二周提交
- Loading branch information
Showing
18 changed files
with
693 additions
and
177 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
This file was deleted.
Oops, something went wrong.
32 changes: 0 additions & 32 deletions
32
group24/330657387/src/com/coding/basic/BinaryTreeNode.java
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
88 changes: 88 additions & 0 deletions
88
group24/330657387/src/main/week01/data_structure/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,88 @@ | ||
package main.week01.data_structure; | ||
|
||
import java.util.Arrays; | ||
|
||
public class ArrayList implements List { | ||
|
||
private int size = 0; | ||
|
||
private Object[] elementData = new Object[10]; | ||
|
||
private void ensureCapacity(int input) { | ||
if (input > elementData.length) { | ||
growCapacity(); | ||
} | ||
} | ||
|
||
private void growCapacity() { | ||
elementData = Arrays.copyOf(elementData, size * 2); | ||
} | ||
|
||
private void rangeCheck(int index) { | ||
if (index > size || index < 0) { | ||
throw new IndexOutOfBoundsException(); | ||
} | ||
} | ||
|
||
public void add(Object o) { | ||
ensureCapacity(size + 1); | ||
elementData[size++] = o; | ||
} | ||
|
||
public void add(int index, Object o) { | ||
rangeCheck(index); | ||
ensureCapacity(size + 1); | ||
System.arraycopy(elementData, index, elementData, index + 1, size | ||
- index); | ||
elementData[index] = o; | ||
size++; | ||
} | ||
|
||
public Object get(int index) { | ||
rangeCheck(index); | ||
return elementData[index]; | ||
} | ||
|
||
public Object remove(int index) { | ||
rangeCheck(index); | ||
Object dest = elementData[index]; | ||
System.arraycopy(elementData, index + 1, elementData, index, size | ||
- index - 1); | ||
elementData[size---1]=null;//·ÀÖ¹ÄÚ´æй© | ||
return dest; | ||
} | ||
|
||
public int size() { | ||
return size; | ||
} | ||
|
||
public class ArrayListIterator implements Iterator { | ||
|
||
private ArrayList list; | ||
|
||
private int position = 0; | ||
|
||
private ArrayListIterator() { | ||
} | ||
|
||
private ArrayListIterator(ArrayList list) { | ||
this.list = list; | ||
} | ||
|
||
@Override | ||
public boolean hasNext() { | ||
return position + 1 <= list.size; | ||
} | ||
|
||
@Override | ||
public Object next() { | ||
return list.get(position++); | ||
} | ||
|
||
} | ||
|
||
public ArrayListIterator iterator() { | ||
return new ArrayListIterator(this); | ||
} | ||
|
||
} |
121 changes: 121 additions & 0 deletions
121
group24/330657387/src/main/week01/data_structure/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,121 @@ | ||
package main.week01.data_structure; | ||
|
||
import com.sun.org.apache.regexp.internal.recompile; | ||
|
||
public class BinaryTreeNode<T extends Comparable> { | ||
|
||
private T data; | ||
private BinaryTreeNode left; | ||
private BinaryTreeNode right; | ||
private int size; | ||
|
||
public BinaryTreeNode(){} | ||
|
||
public BinaryTreeNode(T data) | ||
{ | ||
this.data=data; | ||
this.left=null; | ||
this.right=null; | ||
} | ||
|
||
public T getData() { | ||
return data; | ||
} | ||
|
||
public void setData(T 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; | ||
} | ||
|
||
/** | ||
* data不允许重复 | ||
* @param data | ||
* @return | ||
*/ | ||
public BinaryTreeNode insert(T data){ | ||
int compareResult = this.data.compareTo(data); | ||
if(compareResult == 0){ | ||
return this; | ||
} | ||
if(compareResult > 0){ | ||
if(this.left == null){ | ||
this.left = new BinaryTreeNode(data); | ||
return this.left; | ||
}else{ | ||
return this.left.insert(data); | ||
} | ||
}else{ | ||
if(this.right == null){ | ||
this.right = new BinaryTreeNode(data); | ||
return this.right; | ||
}else{ | ||
return this.right.insert(data); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* 允许节点为空 | ||
* @param data | ||
* @return | ||
*/ | ||
@SuppressWarnings("unchecked") | ||
public BinaryTreeNode search(T data){ | ||
if(this.data == null){ | ||
return null; | ||
} | ||
int compareResult = this.data.compareTo(data); | ||
if (compareResult > 0) { | ||
if (this.left == null) { | ||
return null; | ||
} else { | ||
return this.left.search(data); | ||
} | ||
} else if (compareResult < 0) { | ||
if (this.right == null) { | ||
return null; | ||
} else { | ||
return this.right.search(data); | ||
} | ||
} else { | ||
return this; | ||
} | ||
|
||
} | ||
|
||
private BinaryTreeNode findMin() { | ||
if (this.data == null) { | ||
return null; | ||
} | ||
if (this.left == null) { | ||
return this; | ||
} | ||
return this.left.findMin(); | ||
} | ||
|
||
private BinaryTreeNode findMax() { | ||
if (this.data == null) { | ||
return null; | ||
} | ||
if (this.right == null) { | ||
return this; | ||
} | ||
return this.right.findMin(); | ||
} | ||
|
||
} |
2 changes: 1 addition & 1 deletion
2
...657387/src/com/coding/basic/Iterator.java → .../main/week01/data_structure/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
Oops, something went wrong.