Skip to content

Commit

Permalink
Merge pull request #9 from congcongcong250/master
Browse files Browse the repository at this point in the history
609990377_liren:DataStructure version 1
  • Loading branch information
Rong Huang authored Feb 26, 2017
2 parents e62e923 + 6d3967a commit 2e0d430
Show file tree
Hide file tree
Showing 8 changed files with 426 additions and 0 deletions.
3 changes: 3 additions & 0 deletions group02/609990377/DataStructure/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/bin/
.classpath
.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package com.github.congcongcong250.coding2017.basic;

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

public class ArrayList implements List {

private int size = 0;

private Object[] elementData = new Object[10];

public void add(Object o){

//check size limit
if(size + 1 > elementData.length){
int newlength = elementData.length * 3 / 2 + 1;
elementData = Arrays.copyOf(elementData, newlength);
}

elementData[size++] = o;
}

public void add(int index, Object o){
//Check Object class
if(size >= 1){
if(o.getClass() != elementData[0].getClass())
throw new InputMismatchException("Index:"+index+" Size:"+size);
}

//index Check
if(index >= size || index < 0){
throw new IndexOutOfBoundsException("Index:"+index+" Size:"+size);
}

//check size limit
if(size + 1 > elementData.length){
Object oldData[] = elementData;
int newlength = elementData.length * 3 / 2 + 1;
elementData = Arrays.copyOf(elementData, newlength);
}

for(int i = size-1; i >= index; i-- ){
elementData[i] = elementData[i-1];
}

elementData[index] = o;

}

public Object get(int index){
//index Check
if(index >= size || index < 0){
throw new IndexOutOfBoundsException("Index:"+index+" Size:"+size);
}

return elementData[index];
}

public Object remove(int index){
//index Check
if(index >= size || index < 0){
throw new IndexOutOfBoundsException("Index:"+index+" Size:"+size);
}

Object old = elementData[index];
for(int i = index; i < size-1 ; i++ ){
elementData[i] = elementData[i+1];
}
elementData[--size] = null;
return old;
}

public int size(){
return size;
}

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

private class Itr implements Iterator{
//index for next element to visit
private int cursor = 0;

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

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

@Override
public void remove() {
//Check bound
if(cursor == 0){
throw new NoSuchElementException();
}

ArrayList.this.remove(--cursor);

}


}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package com.github.congcongcong250.coding2017.basic;

public class BinaryTreeNode <Object extends Comparable<Object>>{

private Object data;
private BinaryTreeNode left;
private BinaryTreeNode right;

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

public BinaryTreeNode(Object obj){
data = obj;
left = null;
right = null;
}

public Object getData() {
return data;
}
public void setData(Object 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 insert(Object o){
//If is empty root
if(data == null){
data = o;
return this;
}

//If it is a normal root
BinaryTreeNode in;

if(o.compareTo(data) <= 0){
if(left == null){
in = new BinaryTreeNode(o);
left = in;
}else{
in = left.insert(o);
}
}else{
if(right == null){
in = new BinaryTreeNode(o);
right = in;
}else{
in = right.insert(o);
}
}

assert (in == null):"Insert error";
return in;
}



}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.github.congcongcong250.coding2017.basic;

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

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
package com.github.congcongcong250.coding2017.basic;

import java.util.NoSuchElementException;

public class LinkedList implements List {

private Node head;
private int size;

public LinkedList(){
head.next = head;
head.previous = head;
size = 0;
}

public void add(Object o){
addLast(o);
}

public void add(int index , Object o){
//Check bound
if(index >= size){
throw new IndexOutOfBoundsException("Index:"+index+" Size:"+size);
}

Node nx = this.find(index);
Node pr = nx.previous;
Node in = new Node(o,pr,nx);
nx.previous = in;
pr.next = in;
size++;
}

public Object get(int index){
//Check bound
if(index >= size){
throw new IndexOutOfBoundsException("Index:"+index+" Size:"+size);
}
return this.find(index);
}

public Object remove(int index){
//Check bound
if(index >= size){
throw new IndexOutOfBoundsException("Index:"+index+" Size:"+size);
}
Node rem = this.find(index);

Node pr = rem.previous;
Node nx = rem.next;
pr.next = nx;
nx.previous = pr;

Object ret = rem.data;
rem.previous = null;
rem.next = null;
rem.data = null;
size--;
return ret;
}

public int size(){
return size;
}

public void addFirst(Object o){
Node nx = head.next;
Node in = new Node(o,head, nx);
head.next = in;
nx.previous = in;
size++;
}

public void addLast(Object o){
Node last = head.previous;
Node in = new Node(o,last,head);
last.next = in;
head.previous = in;

size++;
}
public Object removeFirst(){
return remove(0);
}
public Object removeLast(){
return remove(size-1);
}
public Iterator iterator(){
return new ListItr();
}

private Node find(int index){
Node tra = head;

//If index < size/2
if( index < (size >> 1)){
for(int i = 0; i <= index; i++){
tra = tra.next;
}
}else{
for(int i = size; i >= index; i--){
tra = tra.previous;
}
}
return tra;
}

private static class Node{
Object data;
Node next;
Node previous;

public Node(Object obj,Node pre, Node nx){
data = obj;
next = nx;
previous = pre;
}


}

private class ListItr implements Iterator{
//Point to next node
Node cursor;
int nextIndex;

public ListItr(){
cursor = head.next;
nextIndex = 0;
}

@Override
public boolean hasNext() {
return nextIndex < size;
}

@Override
public Object next() {
Node re = cursor;
cursor = cursor.next;
nextIndex++;
return re;
}

public Object previous() {
Node re = cursor.previous.previous;
cursor = cursor.previous;
nextIndex--;
return re;
}

@Override
public void remove() {
//Check bound
if(nextIndex > size){
throw new NoSuchElementException("Iterates to the end");
}
LinkedList.this.remove(--nextIndex);

}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.github.congcongcong250.coding2017.basic;

public interface List {
public void add(Object o);
public void add(int index, Object o);
public Object get(int index);
public Object remove(int index);
public int size();
}
Loading

0 comments on commit 2e0d430

Please sign in to comment.