Skip to content

Commit

Permalink
Merge pull request #2 from nusubmarine01/master
Browse files Browse the repository at this point in the history
update
  • Loading branch information
ztc-dev authored Feb 26, 2017
2 parents 81c40c9 + bd10c5c commit e8b1b8a
Show file tree
Hide file tree
Showing 59 changed files with 2,669 additions and 0 deletions.
34 changes: 34 additions & 0 deletions group03/1360464792/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Created by .ignore support plugin (hsz.mobi)
### Java template
# Compiled class file
*.class

# Log file
*.log

# BlueJ files
*.ctxt

# Mobile Tools for Java (J2ME)
.mtj.tmp/

# Package Files #
*.jar
*.war
*.ear
*.zip
*.tar.gz
*.rar

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*


### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr

### maven
target
42 changes: 42 additions & 0 deletions group03/1360464792/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>rui.study</groupId>
<artifactId>coding2017</artifactId>
<version>0.0.1-SNAPSHOT</version>

<name>学习数据结构</name>

<properties>
<java.version>1.6</java.version>
<encoding>UTF-8</encoding>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>

</dependencies>

<build>
<finalName>coding2017</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<encoding>${encoding}</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
142 changes: 142 additions & 0 deletions group03/1360464792/src/main/java/rui/study/coding2017/ArrayList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
package rui.study.coding2017;


import java.util.Arrays;
import java.util.NoSuchElementException;

public class ArrayList implements List {

private int size;

private Object[] elementData;

private static Object[] emptyObjects={};

private static int defaultCapacity=10;

public void add(Object o){
ensureCapacity(this.size+1);
elementData[size++]=o;
}

public void add(int index, Object o){
rangeCheckForAdd(index);
if(elementData[index]!=null){
ensureCapacity(this.size+1);
//执行数组拷贝
System.arraycopy(elementData,index,elementData,index+1,size-index);
size++;
}
elementData[index]=o;
}

public Object get(int index){
rangeCheck(index);
return elementData[index];
}

public Object remove(int index){
rangeCheck(index);
Object object=elementData[index];

int numMoved=size-index-1;
//如果是最后一位remove ,无需进行数组拷贝
if(numMoved>0){
System.arraycopy(elementData, index+1, elementData, index,numMoved);
}
elementData[--size]=null;
return object;
}

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

public Iterator iterator(){
return new ArrayListIterator();
}

public ArrayList(){
this.size=0;
this.elementData=emptyObjects;
}

public ArrayList(int size){
this.size=size;
if(size>0){
this.elementData=new Object[size];
}else if(size==0){
this.elementData=emptyObjects;
}else{
throw new IllegalArgumentException("非法容器大小 "+size);
}

}

/**
* 判断索引是否合法
* @param index 索引
*/
private void rangeCheckForAdd(int index) {
if(index>size||index<0)throw new IndexOutOfBoundsException("索引为"+index+",但是当前数组长度为:"+size);
}

/**
* 判断索引是否合法 ,
* @param index 索引
*/
private void rangeCheck(int index) {
if(index>=size||index<0)throw new IndexOutOfBoundsException("索引为"+index+",但是当前数组长度为:"+size);
}


/**
* 确保当前数组能够长度能够容纳新的对象,如果不够,就自行增长
* @param needLength 需要的数组长度
*/
private void ensureCapacity(int needLength) {
if(elementData==emptyObjects){
needLength = Math.max(defaultCapacity, needLength);
}

if(needLength-elementData.length>0){
this.grow(needLength);
}
}

/**
* 数组扩容
* @param needLength 需要的长度
*/
private void grow(int needLength) {
int elementLength=elementData.length;
//扩容1.5倍
int newLength=elementLength+(elementLength>>1);

if(needLength-newLength>0){
newLength=needLength;
}
this.elementData= Arrays.copyOf(this.elementData,newLength);
}

private class ArrayListIterator implements Iterator{
//游标,当前迭代器执行到何处了
private int cursor=0;

@Override
public boolean hasNext() {
return cursor!=size;
}

@Override
public Object next() {
if (cursor >= size)throw new NoSuchElementException();
Object[] elementData = ArrayList.this.elementData;
return elementData[cursor++];
}
}




}
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package rui.study.coding2017;

/**
* 二叉树
* Created by 赵睿 on 2017/2/25.
*/
public class BinaryTree {
private BinaryTreeNode root;

private int size;

public void insert(Comparable comparable){
BinaryTreeNode binaryTreeNode=new BinaryTreeNode(comparable);

if(this.root==null){
this.root=binaryTreeNode;
}else {
boolean flag=false;
BinaryTreeNode cursorNode=root;
while(!flag){
if(comparable.compareTo(cursorNode.getData())<0){
if(cursorNode.getLeft()==null){
cursorNode.setLeft(binaryTreeNode);
flag=true;
}else{
cursorNode=cursorNode.getLeft();
}
}else {
if(cursorNode.getRight()==null){
cursorNode.setRight(binaryTreeNode);
flag=true;
}else{
cursorNode=cursorNode.getRight();
}
}

}
}
size++;
}

public LinkedList inorder(){
LinkedList linkedList=new LinkedList();
sortLeft(linkedList,root);
sortRight(linkedList,root);
return linkedList;
}

private void sortRight(LinkedList linkedList,BinaryTreeNode binaryTreeNode){
Queue queue=getRightList(binaryTreeNode);
while(!queue.isEmpty()){
BinaryTreeNode queueNode = (BinaryTreeNode) queue.deQueue();
sortLeft(linkedList,queueNode);
}

}

private void sortLeft(LinkedList linkedList,BinaryTreeNode binaryTreeNode){
Stack stack=getLeftList(binaryTreeNode);
while(!stack.isEmpty()) {
BinaryTreeNode stackNode = (BinaryTreeNode) stack.pop();
linkedList.add(stackNode.getData());
Queue queue = getRightList(stackNode);
while (!queue.isEmpty()) {
BinaryTreeNode queueNode = (BinaryTreeNode) queue.deQueue();
sortLeft(linkedList,queueNode);
}
}
linkedList.add(binaryTreeNode.getData());
}


private Stack getLeftList(BinaryTreeNode binaryTreeNode){
Stack stack=new Stack();
while(binaryTreeNode.getLeft()!=null){
binaryTreeNode=binaryTreeNode.getLeft();
stack.push(binaryTreeNode);
}
return stack;
}

private Queue getRightList(BinaryTreeNode binaryTreeNode){
Queue queue=new Queue();
while(binaryTreeNode.getRight()!=null){
binaryTreeNode=binaryTreeNode.getRight();
queue.enQueue(binaryTreeNode);
}
return queue;
}



}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package rui.study.coding2017;

public class BinaryTreeNode {

private Comparable data;
private BinaryTreeNode left;
private BinaryTreeNode right;

public Comparable getData() {
return data;
}
public void setData(Comparable 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() {
}

public BinaryTreeNode(Comparable data) {
this.data = data;
}

public BinaryTreeNode(Comparable data, BinaryTreeNode left, BinaryTreeNode right) {
this.data = data;
this.left = left;
this.right = right;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package rui.study.coding2017;


public interface Iterator {
public boolean hasNext();
public Object next();

}
Loading

0 comments on commit e8b1b8a

Please sign in to comment.