新人第一次发博客,请大家多多关照啦
一、前言
对于Java程序员来说,大家都应该知道JDK里面提供了Array,List,HashMap三种最常见的数据结构API接口,以及具体的实现方法。其实java JDK1.6里面就有树的概念了,大家可以去 看看com.sun.source.tree 包下面一大推树的为实现的接口。附上一小段JDK提供的二叉树的API接口代码,我就不深究了,毕竟只是接口,具体方法要我们自己实现的。。。
@jdk.Exported
public interface BinaryTree extends ExpressionTree {
ExpressionTree getLeftOperand();
ExpressionTree getRightOperand();
}
二、正文
自己定义二叉树接口,先附上代码
/**
* Created by minghao_zl on 14-9-16.
*/
public interface Btree<T> {
/**
* 获得根节点的数据 get data of root
* @return null if is a empty tree,the data of the root node otherwise
*/
public abstract T root();
/**
* 插入一个元素 insert
* @param element
* 插入成功返回true,失败返回false
* @return true if insert success, false otherwise
*/
public abstract boolean insert( T element );
/**
* 删除一个元素 delete
* @param element
* 插入成功返回true,失败返回false
* @return true if delete success, false otherwise
*/
public abstract boolean delete( T element );
/**
* 查找一个元素 insert
* @param element
* 元素存在返回true,失败返回false
* @return true if exist, false otherwise
*/
public abstract boolean query( T element );
/**
* 修改一个元素 modify
* @param oldElement,newElement
* 修改成功返回true,失败返回false
* @return true if modify success, false otherwise
*/
public abstract boolean modify( T oldElement, T newElement );
/**
* 二叉树的节点个数 size
* 如果是一颗空树返回0,否则返回树的节点个数
* @return 0 if is a empty tree, positive number otherwise
*/
public abstract int size();
}
一共提供了六种接口,增删改查以及获取根节点root()
和获取节点个数size();
接下来自己写了一个binary search tree简称BStree二叉搜索树,来具体实现Btree的方法。代码如下
import java.util.Comparator;
/**
* Created by minghao_zl on 14-9-16.
*/
public class BStree<T> implements Btree<T>{
//根节点
private Node<T> rootNode;
//size
private int size;
/**
* 比较器,二叉搜索树当然要比较两个节点之间的大小啦。。。。
*/
private Comparator<T> comparator;
/**
* 构造函数传进来一个自定义的比较器
* @param comparator
*/
public BStree(Comparator<T> comparator){
this.size = 0;
this.comparator = comparator;
}
/**
* 如果根节点不为空,返回根节点的数据
* @return null if is a empty tree,data of the root node otherwise
*/
@Override
public T root() {
if( null == rootNode ) {
return null;
}//end if
return rootNode.getData();
}
/**
* 插入到某个节点下面
* @param element
* @return true if insert success, false otherwise
*/
@Override
public boolean insert(T element) {
Node<T> tempNode = new Node<T>(element,null,null);
//如果没有根节点就创建一个根节点
if( rootNode == null ) {
rootNode = tempNode;
size++;
return true;
}else{
boolean flag = insert(rootNode,tempNode);
//如果成功了size++
if(flag){
size++;
}
return flag;
}
}
/**
* 插入到某个节点下面
* @param node
* @param data
* @return true if insert success, false otherwise
*/
private boolean insert( Node<T> node , Node<T> data ){
if( null == node ){
// do nothing
return false;
}//end if
//没有比较器是不行滴...嘿嘿...
if( null == comparator ){
System.out.println("A comparator is needed while inserting a element into BStree");
return false;
}//end if
/**
* 提醒一下 comparator.compare(A,B)
* 如果A>B 返回positive number
* 如果A<B 返回negative number
* 如果A=B 返回0
*/
int flag = comparator.compare(node.getData(),data.getData());
if( flag == 0 ){
//不能插入重复的。。。嘿嘿。。如果大家觉得可以插入相同的节点,往左插还是往右大家确定啦,嘎嘎。。。
System.out.println("Duplicate element:[ "+data.getData().toString()+" ]!");
return false;
}else if( flag > 0 ){
//flag > 0 说明要插入的节点比当前节点大,要插到右边
if( null == node.getLeft() ){
node.setLeft(data);
return true;
}else {
return insert(node.getLeft(), data);
}
}else{
//flag < 0 说明要插入的节点比当前节点小,要插到左边
if( null == node.getRight() ){
node.setRight(data);
return true;
}else {
return insert(node.getRight(), data);
}
}
}
@Override
public boolean delete(T element) {
return false;
}
@Override
public boolean query(T element) {
return false;
}
@Override
public boolean modify(T oldElement, T newElement) {
return false;
}
@Override
public int size() {
return size;
}
/**
* 打印全部节点的数据
*/
public void printfAll(){
if( rootNode == null ){
System.out.println("Empty tree!");
}else{
printf(rootNode);
}
}
/**
* 打印以某个节点为根节点的全部节点的数据
*/
public void printf(Node<T> node){
if( node == null ){
return;
}else{
System.out.println(node.getData().toString());
printf(node.getLeft());
printf(node.getRight());
}
}
/**
* 判断一个节点是否为叶子节点
* @param node
* @return true if the node is a leaf,false otherwise
*/
private boolean isLeaf( Node<T> node ){
return null != node && null == node.getLeft() && null == node.getRight();
}
/**
* 判断一个树是否为空树
* @return true if is a empty tree,false otherwise
*/
public boolean isEmpty( ){
return size == 0;
}
}
代码里面有注释,我就不详细说明了......
相关的类还有Node.java和自定义了一个整数的比较器MyComparator.java。
Node.java
/**
* Created by minghao_zl on 14-9-16.
*/
public class Node<T> {
private T data;
private Node<T> left,right;
public Node(T data){
this.data = data;
this.left = null;
this.right = null;
}
public Node(T data,Node left, Node right){
this.data = data;
this.left = left;
this.right = right;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
public Node<T> getLeft() {
return left;
}
public void setLeft(Node<T> left) {
this.left = left;
}
public Node<T> getRight() {
return right;
}
public void setRight(Node<T> right) {
this.right = right;
}
}
MyComparator.java 简单的比较两个整数,如果
import java.util.Comparator;
/**
* Created by minghao_zl on 14-9-16.
*/
/**
* 提醒一下 comparator.compare(A,B)
* 如果A>B 返回positive number
* 如果A<B 返回negative number
* 如果A=B 返回0
*/
public class MyComparator implements Comparator{
@Override
public int compare(Object o1, Object o2) {
int a1 = (Integer) o1;
int a2 = (Integer) o2;
int flag;
if( a1 == a2 ){
flag = 0;
}else if( a1 < a2 ){
flag = -1;
}else {
flag = 1;
}
return flag;
}
}
在Main里面是这样写的
public static void main( String []args ) {
Btree<Integer> btree = new BStree<Integer>(new MyComparator());
btree.insert(5);
btree.insert(1);
btree.insert(2);
btree.insert(3);
btree.insert(4);
btree.insert(6);
btree.insert(7);
((BStree)btree).printfAll();
}
结果.printAll打印函数是先根遍历的,所以
哈哈,插入方法大致就完成了。。。。