实验五 二叉树的定义与遍历

实验目的:
1.熟悉二叉树的二叉链表存储结构。
2.掌握二叉树的遍历运算。
实验内容:
1.二叉树的链式存储结构(二叉链表)。
2.二叉树的遍历运算(4 种算法)。
3.其它基本运算(选做)。
二叉树调用栈处理先根、中根、后根的非递归算法,使用队列处理层次遍历的非递归算法。
以下是使用先根、中根、后根的递归算法和层次遍历的非递归算法。

Node结点类:
package ch03;
public class Node 
{
    Node next;
    Object data;

    public Node()
    {
        this(null,null);
    }

    public Node(Object data)
    {
        this(data,null);
    }

    public Node(Object data,Node next)
    {
        this.data = data;
        this.next = next;
    }
}
Iqueue接口:
package ch03;
public interface Iqueue
{
    public void clear();
    public boolean isEmpty();
    public int length();
    public Object peer();
    public void offer(Object x)throws Exception;
    public Object poll();

}
链队列LinkQueue类:
package ch03;
import ch03.Node;
public class LinkQueue implements Iqueue
{
    private Node  front;
    private Node rear;
    public LinkQueue()
    {
        front = rear = null;
    }
    public void clear()
    {
        front = rear = null;
    }
    public boolean isEmpty()
    {
        return front == null;       
    }
    public int length()
    {
        Node p = front ;
        int length = 0;
        while(p!=null)
        {
            p = p.next;
            ++length;
        }
        return length;
    }
    public Object peer()
    {
        if(front != null)
        return front.data ;
        else
        return null;
    }
    public void offer(Object x)throws Exception
    {
        Node p = new Node(x);
        if(front!=null)
        {
            rear.next = p;
            rear = p;
        }
        else
        front = rear = p;

    }
    public Object poll()
    {
        if(front!= null)
        {
            Node p = front;
            front = front.next;
            if(p == rear)
                        rear = null;
            return p.data;
        }
        else
        return null;
    }

}
BiTreeNode类,二叉树类,创建二叉树中的结点
package ch03;
public class BiTreeNode 
{
    public Object data;
    public BiTreeNode Lchild,Rchild;
    public BiTreeNode()
    {
        this(null);
    }

    public BiTreeNode(Object data)
    {
        this(data,null,null);   
    }

    public BiTreeNode(Object data,BiTreeNode Lchild,BiTreeNode Rchild)
    {

        this.data = data;
        this.Lchild = Lchild;
        this.Rchild = Rchild;
    }
}
二叉树建立并遍历方法类:
package ch03;
import ch03.LinkQueue;
public class BiTree
{
    public BiTreeNode root;
    public BiTree()
    {
        this.root = null;
    }

    public BiTree(BiTreeNode root)
    {
        this.root = root;   
    }

    //使用先根、后根建立一个二叉树的算法
    public BiTree(String preOrder,String inOrder,int preIndex,int inIndex,int count)
    {
        if(count>0)
        {
            char r = preOrder.charAt(preIndex);
            int i = 0;
            for(;i<count;i++)
            {
                if(r == inOrder.charAt(i + inIndex))
                break;
                root = new BiTreeNode(r);
                root.Lchild = new BiTree(preOrder,inOrder,preIndex+1,inIndex,i).root;
                root.Rchild = new BiTree(preOrder,inOrder,preIndex+i+1,inIndex+i+1,count-i-1).root;
            }
        }
    }

    //先根遍历二叉树的递归算法
    public void preRootTraverse(BiTreeNode T)
    {
        if(T!=null)
        {   
            System.out.println(T.data);
            preRootTraverse(T.Lchild);
            preRootTraverse(T.Rchild);
        }
    }

    //中根遍历二叉树的递归算法
    public void inRootTraverse(BiTreeNode T)
    {
        if(T!=null)
        {
            inRootTraverse(T.Lchild);
            System.out.println(T.data);
            inRootTraverse(T.Rchild);
        }
    }

//后根遍历二叉树的算法
    public void postRootTraverse(BiTreeNode T)
    {
        if(T!=null)
        {
            postRootTraverse(T.Lchild);
            postRootTraverse(T.Rchild);
            System.out.println(T.data);
        }
    }

    public BiTreeNode getRoot()
    {
        return root;    
    }

    public void setRoot(BiTreeNode root)
    {
        this.root = root;
    }

    public void levelTraverse()throws Exception
    {
        BiTreeNode T = root;
        LinkQueue L  = new LinkQueue();
        L.offer(T);
        while(!L.isEmpty())
        {
            T = (BiTreeNode)L.poll();
            System.out.print(T.data);
            if(T.Lchild!=null)
            L.offer(T.Lchild);
            if(T.Rchild!=null)
            L.offer(T.Rchild);
        }
    }


}
主函数:
package ch03;
public class DebugBiTree
{
    public BiTree createBiTree()
    {
        BiTreeNode d = new BiTreeNode("D");
        BiTreeNode  g = new BiTreeNode("G");
        BiTreeNode  h = new BiTreeNode("H");
        BiTreeNode  e = new BiTreeNode('E',g,null);
        BiTreeNode  b = new BiTreeNode('B',d,e);
        BiTreeNode  f = new BiTreeNode('F',null,h);
        BiTreeNode  c = new BiTreeNode('C',f,null); 
        BiTreeNode  a = new BiTreeNode('A',b,c);
        return new BiTree(a);
    }
    public  static void main(String args[])throws Exception
    {
        DebugBiTree debugBiTree = new DebugBiTree();
        BiTree biTree = debugBiTree.createBiTree();
        BiTreeNode root = biTree.root;
        System.out.println("(递归)先根遍历序列为:");
        biTree.preRootTraverse(root);
        //调试中根遍历
        System.out.println("递归中根遍历的序列为:");
        biTree.inRootTraverse(root);
        //调试后根遍历
        System.out.println("递归后根遍历序列为:");
        biTree.postRootTraverse(root);
        //层次遍历
        System.out.println("层次遍历序列为:");
        biTree.levelTraverse();
        System.out.println();
    }
}

这里写图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值