Skip to content

Commit

Permalink
Merge pull request #16 from 592146505/master
Browse files Browse the repository at this point in the history
update List and add BinaryTreeNode
  • Loading branch information
honokaBiu authored Feb 26, 2017
2 parents dd5393b + 5245959 commit 554fec7
Show file tree
Hide file tree
Showing 9 changed files with 303 additions and 6 deletions.
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);
}

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

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cn.wsc.util;
package org.wsc.list;

import java.util.Arrays;
import java.util.ConcurrentModificationException;
Expand Down
21 changes: 21 additions & 0 deletions group20/592146505/data _structure/src/org/wsc/list/Iterator.java
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();

}
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
package cn.wsc.util;
package org.wsc.list;

import java.util.ConcurrentModificationException;
import java.util.NoSuchElementException;

/**
* LinkedList类
*
* 实现List接口和Queue接口
* 基于链表的集合
* @author Administrator
* @date 2017年2月25日上午10:52:41
* @version v1.0
*
* @param <E>
*/
public class LinkedList<E> implements List<E> {
public class LinkedList<E> implements List<E>,Queue<E> {

private int size;
Node<E> first; // 链表的头节点
Expand Down Expand Up @@ -224,6 +225,14 @@ public E remove(int index) {
return unlink(node(index));
}

public E removeFirst() {
//获取头节点
final Node<E> f = first;
if (f == null)
throw new NoSuchElementException();
return remove(0);
}

/**
* 删除节点
*
Expand Down Expand Up @@ -252,6 +261,16 @@ E unlink(Node<E> x) {
return element;
}

@Override
public void enQueue(E e) {
linkLast(e);
}

@Override
public E deQueue() {
return removeFirst();
}

/**
* 位置范围检查 >0 && <=size
*
Expand Down Expand Up @@ -281,4 +300,5 @@ private void checkElementIndex(int index) {
private String outOfBoundsMsg(int index) {
return "Index: " + index + ", Size: " + this.size;
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cn.wsc.util;
package org.wsc.list;

/**
* List接口
Expand Down
42 changes: 42 additions & 0 deletions group20/592146505/data _structure/src/org/wsc/list/Queue.java
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();
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
package cn.wsc.utils;
package org.wsc.stack;

import org.wsc.list.ArrayList;

public class Stack {
private ArrayList elementData = new ArrayList();
Expand Down
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;
}

}

0 comments on commit 554fec7

Please sign in to comment.