forked from onlyliuxin/coding2017
-
Notifications
You must be signed in to change notification settings - Fork 15
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 #7 from liushurencx/master
...
- Loading branch information
Showing
10 changed files
with
989 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
package data_structure; | ||
|
||
|
||
import java.util.Arrays; | ||
import java.util.ConcurrentModificationException; | ||
import java.util.Iterator; | ||
import java.util.NoSuchElementException; | ||
|
||
|
||
public class MyArrayList { | ||
private int modCount; | ||
|
||
private static final Object[] EMPTY_ELEMENTDATA = {}; | ||
//容量 | ||
private int capacity; | ||
//实际添加元素长度 | ||
private int size=0; | ||
//定义一个私有的element数组 | ||
private Object[] elementData; | ||
|
||
public MyArrayList(){ | ||
elementData=EMPTY_ELEMENTDATA; | ||
capacity=0; | ||
} | ||
|
||
public MyArrayList(int initialCapacity) { | ||
|
||
if (initialCapacity < 0) | ||
throw new IllegalArgumentException("Illegal Capacity: "+ | ||
initialCapacity); | ||
this.elementData = new Object[initialCapacity]; | ||
capacity=initialCapacity; | ||
} | ||
|
||
public void add(Object o){ | ||
|
||
|
||
ensureCapacityInternal(size + 1); | ||
elementData[size++] = o; | ||
|
||
|
||
} | ||
|
||
public void add(int index ,Object o){ | ||
|
||
rangCheck(index); | ||
ensureCapacityInternal(size + 1); | ||
|
||
System.arraycopy(elementData, index, elementData, index + 1,size - index); | ||
elementData[index] =o; | ||
size++; | ||
|
||
|
||
|
||
|
||
} | ||
public Object get(int index){ | ||
rangCheck(index); | ||
return elementData[index]; | ||
} | ||
public Object remove(int index){ | ||
modCount++; | ||
rangCheck(index); | ||
Object o=null; | ||
o=elementData[index] ; | ||
int numMoved = size - index - 1; | ||
if (numMoved > 0) | ||
|
||
System.arraycopy(elementData, index+1, elementData, index ,size - index-1); | ||
elementData[--size] = null; | ||
|
||
|
||
return o; | ||
} | ||
public int size(){ | ||
return size; | ||
|
||
} | ||
public int Capacity(){ | ||
return capacity; | ||
} | ||
private void rangCheck(int index){ | ||
if(index > size || index < 0) | ||
throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); | ||
} | ||
|
||
private String outOfBoundsMsg(int index) { | ||
return "Index: "+index+", Size: "+size; | ||
} | ||
|
||
// private class MyIndexOutOfBoundsException extends RuntimeException{ | ||
// @SuppressWarnings("unused") | ||
// public MyIndexOutOfBoundsException(String e) { | ||
// super(); | ||
// } | ||
// } | ||
private void ensureCapacityInternal(int minCapacity) { | ||
|
||
modCount++; | ||
if (elementData == EMPTY_ELEMENTDATA) { | ||
minCapacity = Math.max(10, minCapacity); | ||
capacity=minCapacity; | ||
} | ||
if (minCapacity - elementData.length > 0) | ||
|
||
grow(minCapacity); | ||
} | ||
private void grow(int minCapacity) { | ||
// overflow-conscious code | ||
int oldCapacity = elementData.length; | ||
int newCapacity = oldCapacity + (oldCapacity >> 1); | ||
if (newCapacity - minCapacity < 0) | ||
newCapacity = minCapacity; | ||
capacity=newCapacity; | ||
|
||
elementData = Arrays.copyOf(elementData, newCapacity); | ||
} | ||
public Iterator<Object> iterator() { | ||
return new Itr(); | ||
} | ||
private class Itr implements Iterator<Object> { | ||
int cursor; | ||
int lastRet = -1; | ||
int expectedModCount = modCount; | ||
|
||
public boolean hasNext() { | ||
return cursor != size; | ||
} | ||
|
||
@Override | ||
public Object next() { | ||
|
||
int i = cursor; | ||
if (i >= size) | ||
throw new NoSuchElementException(); | ||
Object[] elementData = MyArrayList.this.elementData; | ||
if (i >= elementData.length) | ||
throw new ConcurrentModificationException(); | ||
cursor = i + 1; | ||
return elementData[lastRet = i]; | ||
|
||
} | ||
|
||
@Override | ||
public void remove() { | ||
if (lastRet < 0) | ||
throw new IllegalStateException(); | ||
checkForComodification(); | ||
|
||
try { | ||
MyArrayList.this.remove(lastRet); | ||
cursor = lastRet; | ||
lastRet = -1; | ||
expectedModCount = modCount; | ||
} catch (IndexOutOfBoundsException ex) { | ||
throw new ConcurrentModificationException(); | ||
} | ||
|
||
} | ||
final void checkForComodification() { | ||
if (modCount != expectedModCount) | ||
throw new ConcurrentModificationException(); | ||
} | ||
} | ||
|
||
|
||
} |
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,198 @@ | ||
package data_structure; | ||
|
||
|
||
|
||
public class MyBinaryTree { | ||
|
||
private BinaryTreeNode root; | ||
private int size; | ||
|
||
|
||
|
||
public void add(int key,Object o) { | ||
size++; | ||
BinaryTreeNode target=null; | ||
final BinaryTreeNode parent=root; | ||
final BinaryTreeNode newnode=new BinaryTreeNode(key,o,null,null,null); | ||
if(parent==null) | ||
root=newnode; | ||
else{ | ||
target=compareKey(key,parent); | ||
|
||
if (key < target.key) { | ||
target.left = newnode; | ||
newnode.top = target; | ||
} else if(key > target.key){ | ||
target.right = newnode; | ||
newnode.top = target; | ||
} | ||
else{ | ||
target.data=o; | ||
size--; | ||
} | ||
|
||
} | ||
|
||
} | ||
public Object get(int key){ | ||
BinaryTreeNode target=null; | ||
target=search( key); | ||
if(target==null) | ||
return null; | ||
else | ||
return target.data; | ||
} | ||
private BinaryTreeNode search(int key){ | ||
BinaryTreeNode target=null; | ||
final BinaryTreeNode parent=root; | ||
if(parent==null) | ||
return null; | ||
|
||
else | ||
target=compareKey(key,parent); | ||
if (key == target.key) { | ||
|
||
return target; | ||
} | ||
return null; | ||
} | ||
public Object remove(int key){ | ||
BinaryTreeNode replace=null; | ||
BinaryTreeNode target=null; | ||
BinaryTreeNode oldnode=null; | ||
|
||
target=search( key); | ||
if(target==null) | ||
return null; | ||
else | ||
{ | ||
oldnode=target; | ||
if(target.left==null&&target.right==null){ | ||
|
||
changeParent( target,null); | ||
target=null; | ||
} | ||
else if(target.left!=null&&target.right==null){ | ||
// replace=next(target.left); | ||
// target=replace; | ||
// changeParent(target,replace); | ||
// changeChild(target, replace); | ||
// changeParent(replace,null); | ||
// target=null; | ||
|
||
replace=target.left; | ||
changeParent(target,replace); | ||
replace.top=target.top; | ||
target=null; | ||
} | ||
else if(target.left==null&&target.right!=null){ | ||
// replace=prev(target.right); | ||
// target=replace; | ||
// changeParent(target,replace); | ||
// changeChild(target, replace); | ||
// changeParent(replace,null); | ||
// replace=null; | ||
|
||
replace=target.right; | ||
changeParent(target,replace); | ||
replace.top=target.top; | ||
target=null; | ||
} | ||
else if(target.left!=null&&target.right!=null){ | ||
int prev=prev(target.right).key; | ||
int next=next(target.left).key; | ||
if((next-key)>(key-prev)) | ||
replace=prev(target.right); | ||
else | ||
replace=next(target.left); | ||
target=replace; | ||
changeParent(target,replace); | ||
changeChild(target, replace); | ||
changeParent(replace,null); | ||
replace=null; | ||
} | ||
} | ||
size--; | ||
return oldnode.data; | ||
} | ||
private void changeParent(BinaryTreeNode target,BinaryTreeNode child){ | ||
BinaryTreeNode targetparent=null; | ||
targetparent=target.top; | ||
if(targetparent.key>target.key) | ||
targetparent.left=child; | ||
else | ||
targetparent.right=child; | ||
|
||
} | ||
private void changeChild(BinaryTreeNode target,BinaryTreeNode parent){ | ||
BinaryTreeNode targetleftchild=null; | ||
BinaryTreeNode targetrightchild=null; | ||
targetleftchild=target.left; | ||
targetrightchild=target.right; | ||
if(targetleftchild!=null) | ||
targetleftchild.top=parent; | ||
if(targetrightchild!=null) | ||
targetrightchild.top=parent; | ||
|
||
} | ||
//找到前驱节点 | ||
private BinaryTreeNode prev(BinaryTreeNode target){ | ||
// BinaryTreeNode prev=null; | ||
while(target.left!=null){ | ||
target=target.left; | ||
} | ||
return target; | ||
|
||
} | ||
//找到后驱节点 | ||
private BinaryTreeNode next(BinaryTreeNode target){ | ||
// BinaryTreeNode next=null; | ||
while(target.right!=null){ | ||
target=target.right; | ||
} | ||
return target; | ||
|
||
} | ||
public int size(){ | ||
|
||
return size; | ||
} | ||
private BinaryTreeNode compareKey(int key ,BinaryTreeNode node) { | ||
BinaryTreeNode parent=node; | ||
while (parent != null) { | ||
|
||
if (key < parent.key&&parent.left!=null) { | ||
parent = parent.left; | ||
} else if (key > parent.key&&parent.right!=null) { | ||
parent = parent.right; | ||
} else { | ||
return parent; | ||
|
||
} | ||
} | ||
return parent; | ||
|
||
|
||
} | ||
|
||
|
||
|
||
|
||
private static class BinaryTreeNode{ | ||
Object data; | ||
int key; | ||
BinaryTreeNode left; | ||
BinaryTreeNode right; | ||
BinaryTreeNode top; | ||
public BinaryTreeNode(int key,Object o, BinaryTreeNode top, BinaryTreeNode left,BinaryTreeNode right){ | ||
this.key=key; | ||
this.data=o; | ||
this.left=left; | ||
this.right=right; | ||
this.top=top; | ||
|
||
} | ||
|
||
|
||
} | ||
} |
Oops, something went wrong.