java数据结构实现_几种常见的数据结构的JAVA实现

依旧没有文字说明,只有少量的注释,二叉查找树有很多参考资料,这里就不多说了。下面奉上JAVA代码

package utility.structure;

import java.io.Serializable;

import java.security.InvalidParameterException;

import java.util.Comparator;

import java.util.ConcurrentModificationException;

/**

*

* @author odie.tang

* @version 1.0 11/09/09

*/

public class BinarySearchTree implements Comparable,Serializable{

private static final long serialVersionUID = -8154673525487170187L;

/**

* The comparator, or null if priority queue uses elements'

* natural ordering.

*/

private Comparator super E> comparator = null;

private E data;

private BinarySearchTree parent = null;

private BinarySearchTree leftChild = null;

private BinarySearchTree rightChild = null;

/**

* level indicates node's level in the whole tree, eg: root's level is 1, and its children's level is 2...

*/

private int level;

/**

* to identify the whether the child is a left child or a right one

*/

enum Child{left,right};

public BinarySearchTree() {

this.level = 0;

}

public BinarySearchTree(Comparator super E> comparator){

this.comparator = comparator;

this.level = 0;

}

public BinarySearchTree(E root) {

this(root,null);

}

public BinarySearchTree(E root,Comparator super E> comparator){

this.data = root;

this.comparator = comparator;

this.level = 1;

}

private BinarySearchTree(BinarySearchTree parent,Child childType,E child){

this.data = child;

this.parent = parent;

this.level = parent.level + 1;

if (childType == Child.left)

parent.leftChild = this;

else

parent.rightChild = this;

this.comparator = parent.comparator;

}

public void add(E e){

if (this.level == 0){

this.level = 1;

this.data = e;

}

else{

BinarySearchTree<

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值