Skip to content

Commit

Permalink
Merge pull request #2 from 12378wzy/master
Browse files Browse the repository at this point in the history
^_^
  • Loading branch information
luoziyihao authored Feb 26, 2017
2 parents 71d6757 + 8872d60 commit 05fc6ef
Show file tree
Hide file tree
Showing 13 changed files with 606 additions and 0 deletions.
16 changes: 16 additions & 0 deletions group17/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
*.class

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

# Package Files #
*.jar
*.war
*.ear

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

#ide config
.metadata
.recommenders
6 changes: 6 additions & 0 deletions group17/1264835468/.classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="output" path="bin"/>
</classpath>
1 change: 1 addition & 0 deletions group17/1264835468/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/bin/
17 changes: 17 additions & 0 deletions group17/1264835468/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>1264835468</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
154 changes: 154 additions & 0 deletions group17/1264835468/src/assignment/BinaryTree.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
package assignment;

//
public class BinaryTree<T extends Comparable<? super T>> implements Iterable<BinaryTreeNode<T>> {
private BinaryTreeNode<T> root;

public BinaryTree(T data) {
root = new BinaryTreeNode<T>(data);
}

public BinaryTree(BinaryTreeNode<T> root) {
this.root = root;
}

public BinaryTreeNode<T> insert(T data) {
BinaryTreeNode<T> node = new BinaryTreeNode<T>(data);
if (root == null)
root = node;
else
insert(root, node);
return node;
}

public BinaryTreeNode<T> insert(BinaryTreeNode<T> node) {
return insert(node.getData());
}

private void insert(BinaryTreeNode<T> current, BinaryTreeNode<T> node) {

if (current.getData().compareTo(node.getData()) > 0) {
if (current.getLeft() == null)
current.setLeft(node);
else
insert(current.getLeft(), node);
}
else {
if (current.getRight() == null)
current.setRight(node);
else
insert(current.getRight(), node);
}
}

@Override
public String toString() {
return new BFSNodeQueue().toString();
}

/**
* 广度优先遍历节点队列
*
* @author Administrator
*
*/
private class BFSNodeQueue {
private MyQueue<BinaryTreeNode<T>> nodeQueue;

public BFSNodeQueue() {
nodeQueue = new MyQueue<>();
}

public boolean isEmpty() {
return nodeQueue.isEmpty();
}

public void enQueue(BinaryTreeNode<T> node) {
if (node != null) nodeQueue.enQueue(node);
}

// 出队同时把子节点入队
public BinaryTreeNode<T> deQueue() {
if (!isEmpty()) {
BinaryTreeNode<T> first = nodeQueue.deQueue();
enQueue(first.getLeft());
enQueue(first.getRight());
return first;
}
throw new QueueIsEmptyException();
}

// 把所有出队节点放进另一个队列中
public MyQueue<BinaryTreeNode<T>> getResult() {
prepare();
MyQueue<BinaryTreeNode<T>> result = new MyQueue<>();
while (!isEmpty()) {
result.enQueue(deQueue());
}
return result;
}

private void prepare() {
clearQueue();
enQueue(root);
}

public void clearQueue() {
while (!isEmpty()) {
deQueue();
}
}

@Override
public String toString() {
StringBuilder stringBuilder = new StringBuilder();

Iterator<BinaryTreeNode<T>> iterator = iterator();
while (iterator.hasNext()) {
stringBuilder.append(iterator.next() + "\n");
}
return stringBuilder.toString();
}
}

@Override
public Iterator<BinaryTreeNode<T>> iterator() {
return new BFSIterator();
}

private class BFSIterator implements Iterator<BinaryTreeNode<T>> {
MyArrayList<BinaryTreeNode<T>> list;
Iterator<BinaryTreeNode<T>> iterator;

public BFSIterator() {
MyQueue<BinaryTreeNode<T>> BFSQueue = new BFSNodeQueue().getResult();
list = new MyArrayList<>();
while (!BFSQueue.isEmpty()) {
list.add(BFSQueue.deQueue());
}
iterator = list.iterator();
}

@Override
public boolean hasNext() {
return iterator.hasNext();
}

@Override
public BinaryTreeNode<T> next() {
return iterator.next();
}
}

public static void main(String[] args) {
BinaryTree<Integer> binaryTree = new BinaryTree<>(5);
binaryTree.insert(6);
binaryTree.insert(7);
binaryTree.insert(4);
Iterator<BinaryTreeNode<Integer>> iterator = binaryTree.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
System.out.println(binaryTree);
}
}
58 changes: 58 additions & 0 deletions group17/1264835468/src/assignment/BinaryTreeNode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package assignment;

public class BinaryTreeNode<T extends Comparable<? super T>> {
private T data;
private BinaryTreeNode<T> left;
private BinaryTreeNode<T> right;

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

public T getData() {
return data;
}

public void setData(T data) {
this.data = data;
}

public BinaryTreeNode<T> getLeft() {
return left;
}

public void setLeft(BinaryTreeNode<T> left) {
this.left = left;
}

public BinaryTreeNode<T> getRight() {
return right;
}

public void setRight(BinaryTreeNode<T> right) {
this.right = right;
}

@Override
public String toString() {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("node:" + data);
// 非叶节点则加上左右子节点data
if (left != null || right != null) {
if (left != null)
stringBuilder.append(",left:" + left.data);
else
stringBuilder.append(",left:null");
if (right != null)
stringBuilder.append(",right:" + right.data);
else
stringBuilder.append(",right:null");
}
return stringBuilder.toString();
}

public static void main(String[] args) {
// BinaryTreeNode<Integer> binaryTreeNode = new BinaryTreeNode<>(1);
}

}
6 changes: 6 additions & 0 deletions group17/1264835468/src/assignment/Iterable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package assignment;

//
public interface Iterable<T> {
Iterator<T> iterator();
}
7 changes: 7 additions & 0 deletions group17/1264835468/src/assignment/Iterator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package assignment;

public interface Iterator<E> {
public boolean hasNext();

public E next();
}
14 changes: 14 additions & 0 deletions group17/1264835468/src/assignment/List.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package assignment;

//
public interface List<E> {
public void add(E o);

public void add(int index, E o);

public E get(int index);

public E remove(int index);

public int size();
}
110 changes: 110 additions & 0 deletions group17/1264835468/src/assignment/MyArrayList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package assignment;

import java.util.Arrays;

public class MyArrayList<E> implements List<E>, Iterable<E> {
private Object[] elementData;
private static final int DEFAULT_SIZE = 10;
private int size;

public MyArrayList() {
this(DEFAULT_SIZE);
}

public MyArrayList(int initSize) {
if (initSize < 0) {
throw new IllegalArgumentException(initSize + " < 0");
}
if (initSize == 0) {
elementData = new Object[DEFAULT_SIZE];
}
else {
elementData = new Object[initSize];
}
size = 0;
}

public void add(E o) {
growIfNeed();
elementData[size++] = o;
}

public void add(int index, E o) {
if (index < 0 || index > size) {
throw new IllegalArgumentException("index:" + index);
}
growIfNeed();
System.arraycopy(elementData, index, elementData, index + 1, size - index);
elementData[index] = o;
size++;
}

@SuppressWarnings("unchecked")
public E get(int index) {
rangeCheck(index);
return (E) elementData[index];
}

public E remove(int index) {
rangeCheck(index);
E target = get(index);
System.arraycopy(elementData, index + 1, elementData, index, size - index - 1);
size--;
return target;
}

public int size() {
return size;
}

private void rangeCheck(int index) {
if (index >= size) {
throw new NoSuchElementException("index:" + index);
}
}

private void growIfNeed() {
if (size == elementData.length)
grow();
}

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

@Override
public Iterator<E> iterator() {
return new ArrayIterator<>();
}

private class ArrayIterator<E> implements Iterator<E> {
private int currentPos = 0;

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

@SuppressWarnings("unchecked")
@Override
public E next() {
rangeCheck(currentPos);
return (E) elementData[currentPos++];
}

}

@Override
public String toString() {
return Arrays.toString(Arrays.copyOf(elementData, size));
}

}

class NoSuchElementException extends RuntimeException {

public NoSuchElementException(String string) {
super(string);
}

}
Loading

0 comments on commit 05fc6ef

Please sign in to comment.