Java数据结构--简单二叉树

BinaryTree类

/**
 * Created by root on 16-3-3.
 */
public class BinaryTree<E> {
    private E item;  //节点字段
    private BinaryTree left;//左分支
    private BinaryTree right;//右分支
    public E getItem(){//获得字段的方法
        return item;
    }
    public BinaryTree(){//constructor
        this.item=null;//无参constructor
        this.left=null;
        this.right=null;
    }
    public BinaryTree(E item){//有参constructor
        this.item=item;//给字段赋值
        this.left=null;//左分支初始化为null
        this.right=null;//右分支初始化为null
    }
    public BinaryTree setLeft(E item){//根据字段属性设置左分支
        this.left=new BinaryTree(item);
        return this.left;    //返回指向左分支的指针  便于构造树
    }
    public BinaryTree setRight(E item){//根据字段内容设置右分支
        this.right=new BinaryTree(item);
        return this.right;  //返回指向右分支的指针
    }
    public BinaryTree getLeft(){  //获得左分枝的指针
        return left;
    }
    public BinaryTree getRight(){ //获得右分支的指针
        return right;
    }
    public String toStringLevel(){ //广度优先遍历
        String result="";  //初始化字符串
        mQueue<BinaryTree> q=new mQueue<BinaryTree>(); //新建一个空队列
        q.add(this);   //将树的节点加入队列
        while(!q.isEmpty()){   //循环条件:队列不为空
            BinaryTree tree=q.remove();  //取得队列头部字段
               result+=tree.getItem();  //将位于队头的节点的字符加入字符串 第一个是A
            if(tree.getLeft()!=null){  //把节点的左分枝加入队列
                q.add(tree.getLeft());
            }
            if(tree.getRight()!=null){//把节点的右分支加入队列
                q.add(tree.getRight());
            }
        }
        return result; //返回结果
    }
}

ListNode类

/**
 * Created by root on 16-3-3.
 */
public class ListNode<E> {//链表节点
    E item;//链表节点字段 用于存放二叉树的节点
    ListNode next;
    public ListNode(){
        this.item=null;
        next=null;
    }
    public ListNode(E item){  //有参构造
        this.item=item;     //设置字段
        next=null;
    }
    public E getItem(){
        return this.item;
    }  //获取字段
    public void setItem(E item){
        this.item=item;
    } //重设字段
    public void setNext(ListNode next){
        this.next=next;
    }  //设置下一个节点
    public ListNode getNext(){//获得下一个节点
        return next;
    }
}

队列

/**
 * Created by root on 16-3-3.
 */
public class mQueue<E> {//利用链表节点实现的队列
    private ListNode front;//设置头指针
    private ListNode back;//设置尾指针
    public mQueue(){//无参构造 得到一个空队列
        this.front=null;
        this.back=null;
    }
    public boolean isEmpty(){
        return front==null;
    }//判断队列是否为空
    public void add(E item){   //向队列尾部增加节点
        if(isEmpty()){front=new ListNode(item);//如果队伍为空
        back=front;} //将头指针指向新节点 将尾指针指向新节点
        else{
            back.setNext(new ListNode(item));//如果队列不为空
            back=back.getNext();//增加一个新节点 并且将尾指针后移
        }

    }
    public E remove(){ //remove 方法
        ListNode node=front;  //返回头节点的字段
        front=front.getNext();//并且将头指针后移
        return (E)node.getItem();
    }

}

测试类

/**
 * Created by root on 16-3-3.
 */
public class Test {
    public static void main(String[] args) {
        BinaryTree A = new BinaryTree("A");
        BinaryTree H = A.setRight("H");
        BinaryTree G = A.setLeft("G");
        BinaryTree E = G.setRight("E");
        BinaryTree K = E.setLeft("K");
        BinaryTree L = E.setRight("L");
        BinaryTree C = H.setLeft("C");
        BinaryTree I = H.setRight("I");
        BinaryTree F = C.setLeft("F");
        BinaryTree M = C.setRight("M");
        BinaryTree J = F.setRight("J");
        BinaryTree N = I.setRight("N");
        System.out.println(A.toStringLevel());
    }
}
posted @ 2016-03-03 13:50  Salaku  阅读(144)  评论(0编辑  收藏  举报