Skip to content

Commit

Permalink
Merge pull request #15 from cmhello88/master
Browse files Browse the repository at this point in the history
basic data structure
  • Loading branch information
nusubmarine01 authored Feb 26, 2017
2 parents bf45542 + 70235df commit 7dfc9d3
Show file tree
Hide file tree
Showing 12 changed files with 625 additions and 0 deletions.
91 changes: 91 additions & 0 deletions group03/345943980/2017Learning/src/com/coding/basic/ArrayList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package com.coding.basic;

//import java.util.Arrays;

public class ArrayList implements List {

private int size = 0; // 记录数组当前长度

private Object[] elementData = new Object[10]; // 初始长度

/*
* (non-Javadoc)
*
* @see com.coding.basic.List#add(java.lang.Object)
*/
public void add(Object o) {
if (size > elementData.length) { // size大于数组初始长度,需要对原数组进行扩容
grow(elementData, 10);
}
this.elementData[size++] = o;
}

/*
* 在指定下标位置插入元素 (non-Javadoc)
*
* @see com.coding.basic.List#add(int, java.lang.Object)
*/
public void add(int index, Object o) {
if (size > elementData.length) { // size大于数组初始长度,需要对原数组进行扩容
grow(elementData, 10);
}
if (index > size || index < 0)
throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
System.arraycopy(elementData, index, elementData, index + 1, size - index);
elementData[index] = o;
size++;
}

public Object get(int index) {
// 1、先要判断index所在处有无值,没有则返回null
Object o = elementData[index];
if (null == o)
throw new IndexOutOfBoundsException();
return o;
}

public Object remove(int index) {
Object oldVal = elementData[index]; // 保留要删除的元素
int numMoved = size - index - 1;
if (numMoved > 0) {
System.arraycopy(elementData, index + 1, elementData, index, numMoved);// 讲移除位置之后的元素向前 挪动
}
elementData[--size] = null; // 将数组末尾元素置为空
return oldVal;
}

/**
* 获取数组元素个数
*/
public int size() {
return this.size;
}

public Object[] grow(Object[] src, int size) {
// Arrays.copyOf(src, src.length+size);
Object[] target = new Object[src.length + size];
System.arraycopy(src, 0, target, 0, src.length);
return target;
}

public Iterator iterator() {

return new ArrayListIterator();
}

private class ArrayListIterator implements Iterator {

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

@Override
public Object next() {
return elementData[cursor++];
}

}

}
132 changes: 132 additions & 0 deletions group03/345943980/2017Learning/src/com/coding/basic/BinaryTree.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
package com.coding.basic;

/**
* 二叉树
*
* @author cm
*/
public class BinaryTree {

private BinaryTreeNode root;

public BinaryTreeNode insert(Object o) {
BinaryTreeNode binaryTreeNode = new BinaryTreeNode(o);
if (null == root) {
root = new BinaryTreeNode(o);
} else {
boolean flag = false;
BinaryTreeNode cursorNode = root;
while (!flag) {
if (binaryTreeNode.compareTo(cursorNode) < 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();
}
}
}
}
return binaryTreeNode;
}

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;
}

private class BinaryTreeNode implements Comparable<BinaryTreeNode> {
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(Object o) {
setData(o);
}

@Override
public int compareTo(BinaryTreeNode binaryTreeNode) {
Integer currVal = (Integer) root.getData();
Integer compVal = (Integer) binaryTreeNode.getData();
if (currVal < compVal)
return -1;
else if (currVal == compVal)
return 0;
else
return 1;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.coding.basic;

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

}
129 changes: 129 additions & 0 deletions group03/345943980/2017Learning/src/com/coding/basic/LinkedList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package com.coding.basic;

/**
* 单向链表
*
* @author Administrator
*
*/
public class LinkedList implements List {

private Node head = new Node(null, null);
private int size = 0;

public LinkedList() {
head.next = head;
}

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

public void add(int index, Object o) {
// 1、检查是否在合理范围内
if (index > size || index < 0)
throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
Node currNode = findNodeByIndex(index);
Node newNode = new Node(o, currNode);
if (index == 0) { // 直接插入到第一个位置
head = newNode;
} else {
Node preNode = findNodeByIndex(index - 1);
preNode.next = newNode;
}
size++;
}

public Object get(int index) {
return findNodeByIndex(index).data;
}

public Object remove(int index) {
if (index > size || index < 0)
throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
Node targetNode = this.findNodeByIndex(index);
Object obj = targetNode.data;
if (index == 0) {
targetNode.data = null;
head = targetNode.next;
} else {
Node preNode = findNodeByIndex(index - 1);
preNode.next = targetNode.next;
}
// targetNode.data = null;
size--;
return obj;
}

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

public void addFirst(Object o) {
Node nextNode = head;
Node newNode = new Node(o, nextNode);
head = newNode;
size++;
}

public void addLast(Object o) {
Node subNode = new Node(o, null);
if (size == 0) {
head = subNode;
} else {
Node lastNode = findNodeByIndex(size - 1);
lastNode.next = subNode;
}
size++;
}

public Object removeFirst() {
return this.remove(0);
}

public Object removeLast() {
return this.remove(size - 1);
}

private Node findNodeByIndex(int index) {
Node lastNode = head;
for (int i = 0; i < index; i++) {
lastNode = lastNode.next;
}
return lastNode;
}

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

private static class Node {
Object data;
Node next;

Node(Object data, Node next) {
this.data = data;
this.next = next;
}
}

private class LinkedListIterator implements Iterator {

private int cursor = 0;
private Node cursorNode = head;

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

@Override
public Object next() {
Object obj = cursorNode.data;
cursorNode = cursorNode.next;
cursor++;
return obj;
}

}
}
9 changes: 9 additions & 0 deletions group03/345943980/2017Learning/src/com/coding/basic/List.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.coding.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 7dfc9d3

Please sign in to comment.