forked from onlyliuxin/coding2017
-
Notifications
You must be signed in to change notification settings - Fork 15
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 #16 from 592146505/master
update List and add BinaryTreeNode
- Loading branch information
Showing
9 changed files
with
303 additions
and
6 deletions.
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
group20/592146505/data _structure/src/org/wsc/exception/NullElementException.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,23 @@ | ||
package org.wsc.exception; | ||
|
||
/** | ||
* | ||
* 空元素异常 | ||
* @author Administrator | ||
* @date 2017年2月26日下午4:15:49 | ||
* @version v1.0 | ||
* | ||
*/ | ||
public class NullElementException extends RuntimeException { | ||
|
||
private static final long serialVersionUID = 4729177529481680909L; | ||
|
||
public NullElementException() { | ||
super(); | ||
} | ||
|
||
public NullElementException(String message) { | ||
super(message); | ||
} | ||
|
||
} |
23 changes: 23 additions & 0 deletions
23
group20/592146505/data _structure/src/org/wsc/exception/RepeatingElementException.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,23 @@ | ||
package org.wsc.exception; | ||
|
||
/** | ||
* | ||
* 重复元素异常 | ||
* @author Administrator | ||
* @date 2017年2月26日下午4:15:49 | ||
* @version v1.0 | ||
* | ||
*/ | ||
public class RepeatingElementException extends RuntimeException { | ||
|
||
private static final long serialVersionUID = 4729177529481680909L; | ||
|
||
public RepeatingElementException() { | ||
super(); | ||
} | ||
|
||
public RepeatingElementException(String message) { | ||
super(message); | ||
} | ||
|
||
} |
2 changes: 1 addition & 1 deletion
2
..._structure/src/cn/wsc/util/ArrayList.java → ...structure/src/org/wsc/list/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
21 changes: 21 additions & 0 deletions
21
group20/592146505/data _structure/src/org/wsc/list/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,21 @@ | ||
package org.wsc.list; | ||
|
||
public interface Iterator<E> { | ||
/** | ||
* 是否存在下一个元素 | ||
* @return | ||
*/ | ||
boolean hasNext(); | ||
|
||
/** | ||
* 获取下一个元素 | ||
* @return | ||
*/ | ||
E next(); | ||
|
||
/** | ||
* 删除当前元素 | ||
*/ | ||
void remove(); | ||
|
||
} |
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
2 changes: 1 addition & 1 deletion
2
...data _structure/src/cn/wsc/util/List.java → ...ata _structure/src/org/wsc/list/List.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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package cn.wsc.util; | ||
package org.wsc.list; | ||
|
||
/** | ||
* List接口 | ||
|
42 changes: 42 additions & 0 deletions
42
group20/592146505/data _structure/src/org/wsc/list/Queue.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,42 @@ | ||
package org.wsc.list; | ||
|
||
/** | ||
* | ||
* 队列 | ||
* | ||
* @author Administrator | ||
* @date 2017年2月25日下午6:08:01 | ||
* @version v1.0 | ||
* | ||
* @param <E> | ||
*/ | ||
public interface Queue<E> { | ||
|
||
/** | ||
* 入列 | ||
* | ||
* @param e | ||
*/ | ||
public void enQueue(E e); | ||
|
||
/** | ||
* 出列 | ||
* | ||
* @return | ||
*/ | ||
public E deQueue(); | ||
|
||
/** | ||
* 是否为空 | ||
* | ||
* @return | ||
*/ | ||
public boolean isEmpty(); | ||
|
||
/** | ||
* 元素长度 | ||
* | ||
* @return | ||
*/ | ||
public int size(); | ||
} |
4 changes: 3 additions & 1 deletion
4
...ta _structure/src/cn/wsc/utils/Stack.java → ...a _structure/src/org/wsc/stack/Stack.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
166 changes: 166 additions & 0 deletions
166
group20/592146505/data _structure/src/org/wsc/tree_node/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,166 @@ | ||
package org.wsc.tree_node; | ||
|
||
import org.wsc.exception.NullElementException; | ||
import org.wsc.exception.RepeatingElementException; | ||
|
||
/** | ||
* BinaryTreeNode 二叉树结构 | ||
* | ||
* | ||
* @author Administrator | ||
* @date 2017年2月26日下午5:47:32 | ||
* @version v1.0 | ||
* | ||
* @param <E> | ||
* 必须实现Comparable接口 | ||
*/ | ||
@SuppressWarnings("rawtypes") | ||
public class BinaryTreeNode<E extends Comparable> { | ||
|
||
/** 左节点 */ | ||
private BinaryTreeNode<E> left; | ||
/** 数据区 */ | ||
private E data; | ||
/** 右节点 */ | ||
private BinaryTreeNode<E> right; | ||
|
||
/** | ||
* 插入 | ||
* | ||
* @param data | ||
* @return | ||
*/ | ||
@SuppressWarnings("unchecked") | ||
public BinaryTreeNode<E> insert(E data) { | ||
if (data == null) | ||
throw new NullElementException("Do not insert a null"); | ||
// 当前数据区为空,则将data放入数据区 | ||
if (this.data == null) { | ||
this.data = data; | ||
return this; | ||
} | ||
// 对比当前数据区数据和data大小 | ||
int result = this.data.compareTo(data); | ||
// 如果相等,则抛出异常 | ||
if (result == 0) | ||
throw new RepeatingElementException("Do not insert duplicate element"); | ||
// 当前数据区数据大于data,将data递归放入左节点 | ||
if (result > 0) { | ||
// 左节点为空,则将数据置入左节点 | ||
if (left == null) | ||
left = new BinaryTreeNode<E>(data); | ||
else// 左节点不为空,则将数据递归置入左节点 | ||
left.insert(data); | ||
} else { | ||
// 右节点为空,则将数据置入右节点 | ||
if (right == null) | ||
right = new BinaryTreeNode<E>(data); | ||
else// 右节点不为空,则将数据递归置入右节点 | ||
right.insert(data); | ||
} | ||
return this; | ||
} | ||
|
||
/** | ||
* 查询 | ||
* | ||
* @param data | ||
* @return | ||
*/ | ||
@SuppressWarnings("unchecked") | ||
public BinaryTreeNode<E> seek(E data) { | ||
checkCurrElement(); | ||
if (data == null) | ||
return null; | ||
// 对比当前数据区数据和data大小 | ||
int result = this.data.compareTo(data); | ||
if (result == 0) { | ||
return this; | ||
} else if (result > 0) {// 当前数据区数据大于data,递归对比左节点 | ||
return left == null ? null : left.seek(data); | ||
} else {// 当前数据区数据小于data,递归对比右节点 | ||
return right == null ? null : right.seek(data); | ||
} | ||
|
||
} | ||
|
||
/** | ||
* 删除 | ||
* | ||
* @param data | ||
* @return | ||
*/ | ||
public BinaryTreeNode<E> remove(E data) { | ||
return removeChild(null, data); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
public BinaryTreeNode<E> removeChild(BinaryTreeNode<E> supNode, E data) { | ||
checkCurrElement(); | ||
if (data == null) | ||
return null; | ||
// 对比当前数据区数据和data大小 | ||
int result = this.data.compareTo(data); | ||
// 如果相同,将通过父节点将子节点引用置为null | ||
if (supNode != null && result == 0) { | ||
if (supNode.left == this) | ||
supNode.left = null; | ||
else | ||
supNode.right = null; | ||
} else if (result > 0) {// 当前数据区数据大于data,递归对比左节点 | ||
return left == null ? null : left.removeChild(this, data); | ||
} else {// 当前数据区数据小于data,递归对比右节点 | ||
return right == null ? null : right.removeChild(this, data); | ||
} | ||
return this; | ||
} | ||
|
||
/** | ||
* 检查当前节点元素是否有效 | ||
*/ | ||
private void checkCurrElement() { | ||
if (this.data == null) | ||
throw new NullElementException("The current node element is null"); | ||
} | ||
|
||
public BinaryTreeNode() { | ||
super(); | ||
} | ||
|
||
public BinaryTreeNode(E data) { | ||
super(); | ||
this.data = data; | ||
} | ||
|
||
public BinaryTreeNode(BinaryTreeNode<E> left, E data, BinaryTreeNode<E> right) { | ||
super(); | ||
this.left = left; | ||
this.data = data; | ||
this.right = right; | ||
} | ||
|
||
public E getData() { | ||
return data; | ||
} | ||
|
||
public void setData(E data) { | ||
this.data = data; | ||
} | ||
|
||
public BinaryTreeNode<E> getLeft() { | ||
return left; | ||
} | ||
|
||
public void setLeft(BinaryTreeNode<E> left) { | ||
this.left = left; | ||
} | ||
|
||
public BinaryTreeNode<E> getRight() { | ||
return right; | ||
} | ||
|
||
public void setRight(BinaryTreeNode<E> right) { | ||
this.right = right; | ||
} | ||
|
||
} |