Skip to content

Commit

Permalink
Merge pull request #26 from sargeles/master
Browse files Browse the repository at this point in the history
第二周提交
  • Loading branch information
DonaldY authored Mar 20, 2017
2 parents ec41868 + 6212acf commit 54b641a
Show file tree
Hide file tree
Showing 18 changed files with 693 additions and 177 deletions.
2 changes: 1 addition & 1 deletion group24/330657387/.project
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>2017Learning</name>
<name>2017Learning_330657387</name>
<comment></comment>
<projects>
</projects>
Expand Down
62 changes: 0 additions & 62 deletions group24/330657387/src/com/coding/basic/ArrayList.java

This file was deleted.

32 changes: 0 additions & 32 deletions group24/330657387/src/com/coding/basic/BinaryTreeNode.java

This file was deleted.

19 changes: 0 additions & 19 deletions group24/330657387/src/com/coding/basic/Queue.java

This file was deleted.

22 changes: 0 additions & 22 deletions group24/330657387/src/com/coding/basic/Stack.java

This file was deleted.

88 changes: 88 additions & 0 deletions group24/330657387/src/main/week01/data_structure/ArrayList.java
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 group24/330657387/src/main/week01/data_structure/BinaryTreeNode.java
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();
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.coding.basic;
package main.week01.data_structure;

public interface Iterator {
public boolean hasNext();
Expand Down
Loading

0 comments on commit 54b641a

Please sign in to comment.