Skip to content

Commit

Permalink
Merge pull request #8 from yyglider/master
Browse files Browse the repository at this point in the history
code01
  • Loading branch information
Mr-ChangK authored Mar 13, 2017
2 parents 4e36aec + 210e651 commit 22d07aa
Show file tree
Hide file tree
Showing 12 changed files with 863 additions and 0 deletions.
139 changes: 139 additions & 0 deletions group23/769232552/coding/src/main/java/code01/ArrayList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
package code01;

/**
* Created by yaoyuan on 2017/3/6.
*/
public class ArrayList implements List {

private int max_size = 0;//总长度
private int current_size = 0; //当前长度
private float extendPercent = 2; //扩展系数

private Object[] elementData;

/**
* 默认构造函数,初始化数组长度为100
*/
public ArrayList(){
this.elementData = new Object[100];
this.max_size = 100;
}
/**
* 构造函数
* @param size,初始化数组长度
*/
public ArrayList(int size){
this.elementData = new Object[size];
this.max_size = size;
}

/**
* 顺序添加元素,超出原始界限时,数组自动扩展
*/
public void add(Object o) {
//如果越界了,需要复制原有的数组到扩充后的数组中
if(this.current_size + 1 > this.max_size) {
this.elementData = copyToNew(this.elementData,this.max_size * 2);
this.max_size = (int) Math.floor(this.max_size * this.extendPercent);
}
this.elementData[this.current_size] = o;
this.current_size ++;

}

/**
* 指定位置添加元素
* 一种是在中间,一种是当前插入的位置尾部(如果超过尾部则默认添加到尾部)
*/
public void add(int index, Object o){
assert(index>=0);
//如果越界了,需要复制原有的数组到扩充后的数组中
if(this.current_size + 1 > this.max_size) {
//如果越界了,需要复制原有的数组到扩充后的数组中
this.elementData = copyToNew(this.elementData,this.max_size * 2);
this.max_size = (int) Math.floor(this.max_size * this.extendPercent);
}
//数组中间插入
if(index < this.current_size){
//需要把当前位置的元素往后移动
for (int i = this.current_size - 1; i >= index; i--) {
this.elementData[i+1] = this.elementData[i];
}
this.elementData[index] = o;
}else {
//后面加入
this.elementData[this.current_size] = o;
}
this.current_size ++;
}

public Object get(int index){
if(index >= 0 && index <= this.current_size-1){
return this.elementData[index];
}else {
throw new ArrayIndexOutOfBoundsException(index);
}
}

/**
* 删除指定位置的元素
* @param index
* @return
*/
public Object remove(int index){
Object result = null;
if(index >= 0 && index <= current_size-1){
result = elementData[index];
//删除操作
if(index == current_size - 1){
elementData[index] = null;
}else {
//需要把当前位置后面的元素往前移动
for (int i = index; i < this.current_size-1 ; i++) {
this.elementData[i] = this.elementData[i+1];
}
this.elementData[this.current_size-1] = null;
}
this.current_size --;
}else {
throw new ArrayIndexOutOfBoundsException(index);
}
return result;
}

public int size(){
return this.current_size;
}

public Iterator iterator(){
return new Iterator() {
int cursor = 0;
int before_cursor = -1;
@Override
public boolean hasNext() {
return cursor != ArrayList.this.size();
}

@Override
public Object next() {
Object next = ArrayList.this.get(cursor);
before_cursor = cursor++;
return next;
}

@Override
public void remove(){
ArrayList.this.remove(before_cursor);
}
};
}

private Object[] copyToNew(Object[] oldArray, int extendSize){
Object[] newArray = new Object[extendSize];
for (int i = 0; i < size(); i++) {
newArray[i] = oldArray[i];
}
return newArray;
}

}
77 changes: 77 additions & 0 deletions group23/769232552/coding/src/main/java/code01/BinaryTree.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package code01;

/**
* Created by yaoyuan on 2017/3/10.
*/
public class BinaryTree<T extends Comparable<T>>{

private BinaryTreeNode root = null;
private int size = 0;

public BinaryTreeNode createBinaryTree(T[] array){
for(T data : array){
this.insert(data);
}
return this.root;
}

public void insert(T data){
if(this.root == null){
BinaryTreeNode node = new BinaryTreeNode(data);
this.root = node;
this.size ++;
return;
}
BinaryTreeNode cursor = this.root;
while (cursor != null){
if(data.compareTo((T) cursor.data) <= 0){
if(cursor.left == null) {
cursor.left = new BinaryTreeNode(data);
return;
}
cursor = cursor.left;
}
if(data.compareTo((T) cursor.data) > 0){
if(cursor.right == null) {
cursor.right = new BinaryTreeNode(data);
return;
}
cursor = cursor.right;
}
}
this.size ++;
}

public void leftOrderScan(BinaryTreeNode cursor){
if(cursor == null){
return;
}
leftOrderScan(cursor.left);
System.out.println(cursor.data.toString() + " ");
leftOrderScan(cursor.right);
}

public BinaryTreeNode getRoot(){
return this.root;
}

class BinaryTreeNode<T> {

private T data;
private BinaryTreeNode left;
private BinaryTreeNode right;

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

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

}
}
7 changes: 7 additions & 0 deletions group23/769232552/coding/src/main/java/code01/Iterator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package code01;

public interface Iterator {
public boolean hasNext();
public Object next();
public void remove();
}
Loading

0 comments on commit 22d07aa

Please sign in to comment.