Skip to content

Commit

Permalink
Merge pull request #2 from fenq/master
Browse files Browse the repository at this point in the history
merge 395443277 assignment
  • Loading branch information
gqipan authored Feb 25, 2017
2 parents ff0ad0e + beaf0d3 commit 3eb1db4
Show file tree
Hide file tree
Showing 13 changed files with 809 additions and 0 deletions.
111 changes: 111 additions & 0 deletions group11/395443277/data_structure/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@


.metadata

bin/

tmp/

*.tmp

*.bak

*.swp

*~.nib

local.properties

.settings/

.loadpath

.recommenders



# Eclipse Core

.project



# External tool builders

.externalToolBuilders/



# Locally stored "Eclipse launch configurations"

*.launch



# PyDev specific (Python IDE for Eclipse)

*.pydevproject



# CDT-specific (C/C++ Development Tooling)

.cproject



# JDT-specific (Eclipse Java Development Tools)

.classpath



# Java annotation processor (APT)

.factorypath



# PDT-specific (PHP Development Tools)

.buildpath



# sbteclipse plugin

.target



# Tern plugin

.tern-project



# TeXlipse plugin

.texlipse



# STS (Spring Tool Suite)

.springBeans



# Code Recommenders

.recommenders/



# Scala IDE specific (Scala & Java development for Eclipse)

.cache-main

.scala_dependencies

.worksheet
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package com.coding.basic;

import java.util.Arrays;

public class ArrayList implements List {

private int size = 0;

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

public void add(Object o){
if (size == elementData.length) {
// double size
doubleSize();
}

elementData[size] = o;
size++;
}

private void doubleSize() {
elementData = Arrays.copyOf(elementData, elementData.length * 2);
}

public void add(int index, Object o){
// check size
if (size == elementData.length) {
doubleSize();
}

//shift and add element
System.arraycopy(elementData, index, elementData, index+1, size - index);
elementData[index] = o;
size++;
}

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

public Object remove(int index){
if (size == 0) {
return null;
}

// remove element and shift
Object target = elementData[index];
System.arraycopy(elementData, index+1, elementData, index, size - index - 1);

// reset last element
elementData[size-1] = null;
size--;
return target;
}

public int size(){
return size;
}

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

private class SeqIterator implements Iterator {
int i = 0;

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

@Override
public Object next() {
if (!hasNext()) {
return null;
}
return elementData[i++];
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package com.coding.basic;

import static org.junit.Assert.*;

import org.junit.Test;

public class ArrayListTest {

@Test
public void testAddObject() {
ArrayList list = new ArrayList();
list.add(5);
assertEquals(5, list.get(0));

list.add(4);
list.add(3);
list.add(2);
list.add(1);
assertEquals(1, list.get(4));

// size equals to 5
assertEquals(5, list.size());
}

@Test
public void testAddIntObject() {
ArrayList list = new ArrayList();
list.add(5);
list.add(4);
list.add(3);
list.add(2);
list.add(1);

// change position 2 element
list.add(2, 10);

// pos 2 has 10
assertEquals(10, list.get(2));

// last element is 1
assertEquals(1, list.get(5));

// size is 6
assertEquals(6, list.size());
}

@Test
public void testRemove() {
ArrayList list = new ArrayList();
list.add(5);
list.add(4);
list.add(3);
list.add(2);
list.add(1);

Object removed = list.remove(2);
assertEquals(removed, 3);

assertEquals(2, list.get(2));
assertEquals(4, list.size());
assertEquals(null, list.get(4));

list.add(6);
assertEquals(6, list.get(4));
}

@Test
public void testIterator() {
ArrayList list = new ArrayList();
list.add(5);
list.add(4);
list.add(3);
list.add(2);
list.add(1);

Iterator it = list.iterator();
if(it.hasNext()) {
assertEquals(5, it.next());
assertEquals(4, it.next());
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.coding.basic;

public class BinaryTreeNode implements Comparable<Object>{

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

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 (this.compareTo(o)==0) {
return null;
} else {
// current value less than inserted value
// go right
if (this.compareTo(o)<0) {
if (this.right == null) {
BinaryTreeNode nd = new BinaryTreeNode();
nd.setData(o);
this.setRight(nd);
} else {
this.getRight().insert(o);
}
}
// greater than
// go left
else if(this.compareTo(o)>0) {
if (this.left == null) {
BinaryTreeNode nd = new BinaryTreeNode();
nd.setData(o);
this.setLeft(nd);
} else {
this.getLeft().insert(o);
}
}
}

return null;
}

/**
* oversimplified implementation: only allows int and string
*/
@Override
public int compareTo(Object nd) throws ClassCastException{
if (!(nd instanceof Object)) {
throw new ClassCastException("An object expected.");
}

if (nd instanceof String) {
return ((String)this.data).compareTo((String) nd);
} else {
return ((int) this.data) -((int) nd);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.coding.basic;

import static org.junit.Assert.*;

import org.junit.Test;

public class BinaryTreeNodeTest {

@Test
public void testInsert() {
BinaryTreeNode root = new BinaryTreeNode();
root.setData(3);

root.insert(2);
BinaryTreeNode left = root.getLeft();
assertEquals(2,left.getData());

root.insert(5);
BinaryTreeNode right = root.getRight();
assertEquals(5, right.getData());

root.insert(7);
BinaryTreeNode rr = right.getRight();
assertEquals(7, rr.getData());
}

@Test
public void testCompareTo() {
BinaryTreeNode n1 = new BinaryTreeNode();
n1.setData("abc");

assertEquals(true, n1.compareTo("cde")<0);

BinaryTreeNode n3 = new BinaryTreeNode();
n3.setData(1);

assertEquals(true, n3.compareTo(2)<0);
}

}
Loading

0 comments on commit 3eb1db4

Please sign in to comment.