forked from onlyliuxin/coding2017
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from RishyPa/master
第一次数据结构作业
- Loading branch information
Showing
8 changed files
with
733 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?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>com.rishy</groupId> | ||
<artifactId>struct</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
|
||
|
||
</project> |
162 changes: 162 additions & 0 deletions
162
group10/569420966/struct/src/main/java/com/myutil/ArrayList.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
package com.myutil; | ||
|
||
|
||
import java.text.MessageFormat; | ||
import java.util.NoSuchElementException; | ||
|
||
/** | ||
* 数组列表 | ||
*/ | ||
public class ArrayList<T> implements List<T> { | ||
private Object[] elementData; | ||
private int size = 0; | ||
private static final int DEFAULT_SIZE = 10; | ||
|
||
/** | ||
* 判断边界 | ||
* <p> | ||
* <pre> | ||
* 若 index < 0 或者 index > size 则抛出非法参数异常 | ||
* </pre> | ||
* | ||
* @param index 当前索引 | ||
*/ | ||
private void judgeRange(int index) { | ||
if (index < 0) { | ||
throw new IllegalArgumentException( | ||
MessageFormat.format("Index is must be great or equal then 0. index:{0}", index)); | ||
} | ||
if (index >= this.size) { | ||
throw new IllegalArgumentException( | ||
MessageFormat.format("Index is must be less then size(). index:{0}", index)); | ||
} | ||
if (this.size == Integer.MAX_VALUE) { | ||
throw new IllegalArgumentException("Array already can not Expansion."); | ||
} | ||
} | ||
|
||
/** | ||
* 扩充数组容量 | ||
* <p> | ||
* <pre> | ||
* 若 size >= elementData.length 则对数组进行扩容 | ||
* 扩容至原(elementData.length+1) * 2 | ||
* </pre> | ||
*/ | ||
private void capacityExpansion() { | ||
if (this.size >= elementData.length) { | ||
Object[] tmpData = new Object[(elementData.length + 1) * 2]; | ||
System.arraycopy(elementData, 0, tmpData, 0, elementData.length); | ||
elementData = tmpData; | ||
} | ||
} | ||
|
||
public ArrayList() { | ||
elementData = new Object[DEFAULT_SIZE]; | ||
} | ||
|
||
public ArrayList(int capacity) { | ||
if (capacity < 0) { | ||
throw new IllegalArgumentException( | ||
MessageFormat.format("Capacity is must be great or equal 0. capacity:{0}", capacity)); | ||
} | ||
this.elementData = new Object[capacity]; | ||
} | ||
|
||
public void add(T element) { | ||
capacityExpansion(); | ||
elementData[this.size] = element; | ||
this.size++; | ||
} | ||
|
||
public void add(T element, int index) { | ||
judgeRange(index); | ||
capacityExpansion(); | ||
if (this.size - index > 0) { | ||
System.arraycopy(elementData, index, elementData, index + 1, this.size - index); | ||
} | ||
elementData[index] = element; | ||
this.size++; | ||
} | ||
|
||
public T remove(int index) { | ||
judgeRange(index); | ||
T tmpObject = (T) elementData[index]; | ||
if (this.size - index > 0) { | ||
System.arraycopy(elementData, index + 1, elementData, index, this.size - index - 1); | ||
} | ||
this.size--; | ||
return tmpObject; | ||
} | ||
|
||
public T get(int index) { | ||
judgeRange(index); | ||
return (T) elementData[index]; | ||
} | ||
|
||
public int size() { | ||
return this.size; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
StringBuilder sb = new StringBuilder(); | ||
sb.append("["); | ||
for (int i = 0; i < this.size; i++) { | ||
sb.append((T) elementData[i]); | ||
if (i < this.size - 1) { | ||
sb.append(","); | ||
} | ||
} | ||
sb.append("]"); | ||
|
||
return sb.toString(); | ||
} | ||
|
||
/** | ||
* 获取迭代器 | ||
* | ||
* @return 迭代器 | ||
*/ | ||
public Iterator<T> iterator() { | ||
return new ArrayListIterator(); | ||
} | ||
|
||
private class ArrayListIterator implements Iterator<T> { | ||
int position = 0; | ||
int lastRet = -1; | ||
|
||
public boolean hasNext() { | ||
return position < ArrayList.this.size(); | ||
} | ||
|
||
public T next() { | ||
if (position >= size) { | ||
throw new NoSuchElementException(); | ||
} | ||
int i = position; | ||
T element = ArrayList.this.get(position++); | ||
lastRet = i; | ||
return element; | ||
} | ||
|
||
public T remove() { | ||
if (lastRet < 0) { | ||
throw new IllegalStateException(); | ||
} | ||
T removeElement = ArrayList.this.remove(lastRet); | ||
position = lastRet; | ||
lastRet = -1; | ||
return removeElement; | ||
} | ||
} | ||
|
||
public static void main(String[] args) { | ||
ArrayList<Integer> ids = new ArrayList<>(); | ||
for (int i = 0; i < 11; i++) { | ||
ids.add(i); | ||
} | ||
Iterator iterator = ids.iterator(); | ||
System.out.println(ids); | ||
} | ||
} |
167 changes: 167 additions & 0 deletions
167
group10/569420966/struct/src/main/java/com/myutil/BinaryTreeNode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
package com.myutil; | ||
|
||
import java.util.Random; | ||
|
||
/** | ||
* 二叉树 | ||
*/ | ||
public class BinaryTreeNode<T extends Comparable<T>> { | ||
private T element; | ||
private BinaryTreeNode<T> left; | ||
private BinaryTreeNode<T> right; | ||
|
||
public T getElement() { | ||
return element; | ||
} | ||
|
||
public void setElement(T element) { | ||
this.element = element; | ||
} | ||
|
||
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; | ||
} | ||
|
||
/** | ||
* 将元素插入二叉树 | ||
* | ||
* @param element 元素 | ||
* @return 插入后的节点 | ||
*/ | ||
public BinaryTreeNode<T> insert(T element) { | ||
if (element == null) { | ||
throw new IllegalArgumentException("Element must be not null."); | ||
} | ||
|
||
BinaryTreeNode<T> currentNode = null; | ||
if (this.element == null) { | ||
currentNode = this; | ||
currentNode.element = element; | ||
} else { | ||
currentNode = compareToElement(element, this); | ||
} | ||
|
||
return currentNode; | ||
} | ||
|
||
private BinaryTreeNode<T> compareToElement(T element, BinaryTreeNode<T> curr) { | ||
if (element.compareTo(curr.element) == -1) { | ||
if (curr.left == null) { | ||
BinaryTreeNode<T> node = new BinaryTreeNode<>(); | ||
node.element = element; | ||
curr.left = node; | ||
return node; | ||
} else { | ||
return compareToElement(element, curr.left); | ||
} | ||
} else { | ||
if (curr.right == null) { | ||
BinaryTreeNode<T> node = new BinaryTreeNode<>(); | ||
node.element = element; | ||
curr.right = node; | ||
return node; | ||
} else { | ||
return compareToElement(element, curr.right); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* 先序遍历 | ||
* | ||
* @return 按先序遍历顺序展示节点值 | ||
*/ | ||
public String preOrderTraversal() { | ||
return concatPreOrder(this); | ||
} | ||
|
||
private String concatPreOrder(BinaryTreeNode<T> node) { | ||
StringBuilder ret = new StringBuilder(); | ||
if (node.left != null) { | ||
ret.append(concatPreOrder(node.left)); | ||
} | ||
|
||
ret.append(node.element).append(" "); | ||
|
||
if (node.right != null) { | ||
ret.append(concatPreOrder(node.right)); | ||
} | ||
|
||
return ret.toString(); | ||
} | ||
|
||
/** | ||
* 中序遍历 | ||
* | ||
* @return 按中序遍历顺序展示节点值 | ||
*/ | ||
public String inOrderTraversal() { | ||
return concatInOrder(this); | ||
} | ||
|
||
private String concatInOrder(BinaryTreeNode<T> node) { | ||
StringBuilder ret = new StringBuilder(); | ||
|
||
ret.append(node.element).append(" "); | ||
|
||
if (node.left != null) { | ||
ret.append(concatInOrder(node.left)); | ||
} | ||
|
||
if (node.right != null) { | ||
ret.append(concatInOrder(node.right)); | ||
} | ||
|
||
return ret.toString(); | ||
} | ||
|
||
/** | ||
* 后序遍历 | ||
* | ||
* @return 按后序遍历顺序展示节点值 | ||
*/ | ||
public String postOrderTraversal() { | ||
return concatPostOrder(this); | ||
} | ||
|
||
private String concatPostOrder(BinaryTreeNode<T> node) { | ||
StringBuilder ret = new StringBuilder(); | ||
|
||
if (node.right != null) { | ||
ret.append(concatPostOrder(node.right)); | ||
} | ||
|
||
ret.append(node.element).append(" "); | ||
|
||
if (node.left != null) { | ||
ret.append(concatPostOrder(node.left)); | ||
} | ||
|
||
return ret.toString(); | ||
} | ||
|
||
public static void main(String[] args) { | ||
BinaryTreeNode<Integer> binaryTree = new BinaryTreeNode<>(); | ||
Random random = new Random(); | ||
for (int i = 0; i < 5; i++) { | ||
binaryTree.insert(random.nextInt(100)); | ||
} | ||
|
||
|
||
System.out.println(binaryTree.preOrderTraversal()); | ||
System.out.println(binaryTree.inOrderTraversal()); | ||
System.out.println(binaryTree.postOrderTraversal()); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
group10/569420966/struct/src/main/java/com/myutil/Iterator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.myutil; | ||
|
||
/** | ||
* 迭代器 | ||
*/ | ||
public interface Iterator<T> { | ||
|
||
/** | ||
* 是否有下一个元素 | ||
* | ||
* @return true-有 false-无 | ||
*/ | ||
boolean hasNext(); | ||
|
||
/** | ||
* 获取下一个元素 | ||
* | ||
* @return 下一个元素 | ||
*/ | ||
T next(); | ||
|
||
/** | ||
* 删除当前迭代的元素 | ||
* | ||
* @return 被删除的元素 | ||
*/ | ||
T remove(); | ||
} |
Oops, something went wrong.