forked from onlyliuxin/coding2017
-
Notifications
You must be signed in to change notification settings - Fork 5
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 #2 from GloryChou/master
提交第一次作业
- Loading branch information
Showing
6 changed files
with
686 additions
and
0 deletions.
There are no files selected for viewing
174 changes: 174 additions & 0 deletions
174
group09/396077060/20170226/src/per/zyf/bds/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,174 @@ | ||
/** | ||
* @Title: ArrayList.java | ||
* @Description: TODO(用一句话描述该文件做什么) | ||
* @author glorychou | ||
* @date 2017年2月22日 下午10:41:58 | ||
*/ | ||
package per.zyf.bds; | ||
|
||
import java.util.Arrays; | ||
|
||
/** | ||
* ArrayList的存储结构其实就是一维数组 | ||
* 只不过在这个类中将对数组的一些操作(包括添加元素、删除元素等)封装了起来 | ||
* @author glorychou | ||
* | ||
* @param <E> | ||
* @see per.zyf.bds.List<E> | ||
*/ | ||
public class ArrayList<E> implements List<E> { | ||
// 默认数组大小 | ||
private static final int DEFAULT_CAPACITY = 10; | ||
|
||
// 数组实际大小 | ||
private int size; | ||
|
||
// 存储元素的数组 | ||
protected Object[] elementData; | ||
|
||
// 一个用来记录初始状态的空数组实例 | ||
private static final Object[] CAPACITY_EMPTY_ELEMENTDATA = {}; | ||
|
||
/*** | ||
* 构造初始元素数组 | ||
*/ | ||
public ArrayList() { | ||
this.elementData = CAPACITY_EMPTY_ELEMENTDATA; | ||
} | ||
|
||
/*** | ||
* | ||
* @Description: 在末尾添加元素 | ||
* @param e 元素 | ||
*/ | ||
@Override | ||
public boolean add(E e) { | ||
int minCapacity = size + 1; | ||
|
||
// 判断数组中是否有元素 | ||
if (elementData == CAPACITY_EMPTY_ELEMENTDATA) { | ||
minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity); | ||
} | ||
|
||
// 判断是否溢出 | ||
if (minCapacity - elementData.length > 0) | ||
grow(minCapacity); | ||
|
||
// 添加元素 | ||
elementData[size++] = e; | ||
|
||
return true; | ||
} | ||
|
||
/*** | ||
* | ||
* @Description: 在索引指定位置增加元素 | ||
* @param index 索引 | ||
* @param e 元素 | ||
*/ | ||
@Override | ||
public boolean add(int index, E e) { | ||
int minCapacity = size + 1; | ||
|
||
// 索引位置不合法抛出异常 | ||
rangeCheck(index); | ||
|
||
// 判断数组中是否有元素 | ||
if (elementData == CAPACITY_EMPTY_ELEMENTDATA) { | ||
minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity); | ||
} | ||
|
||
// 判断是否溢出 | ||
if (minCapacity - elementData.length > 0) | ||
grow(minCapacity); | ||
|
||
// 插入点后的元素后移 | ||
System.arraycopy(elementData, index, elementData, index + 1, size - index); | ||
|
||
// 在索引处加入数据 | ||
elementData[index] = e; | ||
|
||
return true; | ||
} | ||
|
||
/*** | ||
* | ||
* @Description: 得到索引指定位置的元素 | ||
* @param index 索引 | ||
* @return E 索引指定的元素 | ||
*/ | ||
@Override | ||
@SuppressWarnings("unchecked") | ||
public E get(int index) { | ||
// 索引位置不合法抛出异常 | ||
rangeCheck(index); | ||
|
||
return (E) elementData[index]; | ||
} | ||
|
||
/*** | ||
* | ||
* @Description: 删除索引指定位置的元素 | ||
* @param index 索引 | ||
* @return void | ||
*/ | ||
@Override | ||
@SuppressWarnings("unchecked") | ||
public E remove(int index) { | ||
// 索引位置不合法抛出异常 | ||
rangeCheck(index); | ||
|
||
E removeElement = (E) elementData[index]; | ||
|
||
// 将要移除元素后的元素前移 | ||
System.arraycopy(elementData, index + 1, elementData, index, size - index - 1); | ||
// 数组大小减一,并清除多余元素 | ||
elementData[--size] = null; | ||
|
||
return removeElement; | ||
} | ||
|
||
/* | ||
* @see per.zyf.bds.List#size() | ||
*/ | ||
@Override | ||
public int size() { | ||
return size; | ||
} | ||
|
||
|
||
/* | ||
* @see per.zyf.bds.List#isEmpty() | ||
*/ | ||
@Override | ||
public boolean isEmpty() { | ||
if (elementData == CAPACITY_EMPTY_ELEMENTDATA) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
/*** | ||
* | ||
* @Description: 溢出时增长空间 | ||
* @param minCapacity 最小容量 | ||
* @return void | ||
*/ | ||
private void grow(int minCapacity) { | ||
int oldCapacity = elementData.length; | ||
// 容量增大一半 | ||
int newCapacity = oldCapacity + (oldCapacity >> 1); | ||
elementData = Arrays.copyOf(elementData, newCapacity); | ||
} | ||
|
||
/*** | ||
* | ||
* @Description: 索引范围检查 | ||
* @param index 索引 | ||
* @return void | ||
*/ | ||
private void rangeCheck(int index) { | ||
if (index < 0 || index > size) | ||
throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size); | ||
} | ||
} |
81 changes: 81 additions & 0 deletions
81
group09/396077060/20170226/src/per/zyf/bds/BinaryTree.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,81 @@ | ||
/** | ||
* @Title: BinaryTree.java | ||
* @Description: TODO(用一句话描述该文件做什么) | ||
* @author glorychou | ||
* @date 2017年2月25日 下午10:22:03 | ||
*/ | ||
package per.zyf.bds; | ||
|
||
import java.util.Comparator; | ||
|
||
/** | ||
* @author glorychou | ||
* | ||
*/ | ||
public class BinaryTree<E extends Comparable<E>> { | ||
// 根节点 | ||
private Node<E> root; | ||
// 树大小 | ||
private int size; | ||
|
||
/** | ||
* @Description: 在树中插入元素 | ||
* @param e 节点数据 | ||
* @return boolean 处理情况 | ||
*/ | ||
public boolean add(E e) { | ||
|
||
// 创建新节点 | ||
final Node<E> newNode = new Node<>(null, e, null); | ||
|
||
// 按照二叉排序方式插入 | ||
if (root != null) { | ||
Node<E> parentNode = null; | ||
Node<E> compareNode = root; | ||
|
||
while(compareNode != null) { | ||
// 新节点大于比较节点则插入右子树中 | ||
if(e.compareTo(compareNode.item) > 0) { | ||
parentNode = compareNode; | ||
compareNode = compareNode.rightChild; | ||
|
||
if(compareNode == null) | ||
parentNode.rightChild = newNode; | ||
} else {// 新节点小于或等于比较节点则插入左子树中 | ||
parentNode = compareNode; | ||
compareNode = compareNode.leftChild; | ||
|
||
if(compareNode == null) | ||
parentNode.rightChild = newNode; | ||
} | ||
} | ||
} else | ||
root = newNode; | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* @Description: 中序遍历输出 | ||
* @return void 返回类型 | ||
*/ | ||
public void inorderPrint(Node<E> e) { | ||
if(e == null) return; | ||
inorderPrint(e.leftChild); | ||
System.out.print(e.item.toString() + " "); | ||
inorderPrint(e.rightChild); | ||
} | ||
|
||
// 树节点 | ||
private static class Node<E> { | ||
E item; | ||
Node<E> leftChild; | ||
Node<E> rightChild; | ||
|
||
Node(Node<E> l, E e, Node<E> r) { | ||
leftChild = l; | ||
item = e; | ||
rightChild = r; | ||
} | ||
} | ||
} |
Oops, something went wrong.