数据结构 实验二 二叉树的遍历

1、用先序方法建立一棵二叉树;

2、实现输出二叉树先序、中序和后序遍历序列中第k个数据元素的操作;

3、实现判断二叉树是否是完全二叉树的操作。

BiTreeNode.java
package experiment2;
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;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public BiTreeNode getLchild() {
        return lchild;
    }

    public void setLchild(BiTreeNode lchild) {
        this.lchild = lchild;
    }

    public BiTreeNode getRchild() {
        return rchild;
    }

    public void setRchild(BiTreeNode rchild) {
        this.rchild = rchild;
    }
}

BiTree.java
package experiment2;

import java.util.Scanner;

public class BiTree {
    private  BiTreeNode root;
    private static int index=0;
    int count1=0,count2=0,count3=0;
    public BiTree() {
        this.root = null;
    }

    public BiTree(BiTreeNode root) {
        this.root = root;
    }
    public BiTree(String preStr){
        char c = preStr.charAt(index++);
        if (c != '#') {
            root = new BiTreeNode(c);
            root.lchild = new BiTree(preStr).root;
            root.rchild = new BiTree(preStr).root;
        } else
            root = null;
    }
    public void preRootTraverse(int k){
        if (root==null) {
            System.out.println(root);
        } else {
            preRootTraverse(root, k);
        }
    }
    private void preRootTraverse(BiTreeNode T,int k) {
        if (T != null) {
            count1++;
            if(count1==k) {
                System.out.println("先序的数据:"+T.data);
            }
            preRootTraverse(T.lchild,k);
            preRootTraverse(T.rchild,k);
        }
    }
    public void inRootTraverse(int k){
        if (root==null) {
            System.out.println(root);
        } else {
            inRootTraverse(root, k);
        }
    }
    private void inRootTraverse(BiTreeNode T,int k) {
        if (T != null) {
            inRootTraverse(T.lchild,k);
            count2++;
            if (count2==k) {
                System.out.println("中序的数据:"+T.data);
            }
            inRootTraverse(T.rchild,k);
        }
    }
    public void postRootTraverse(int k){
        if (root==null) {
            System.out.println(root);
        } else {
            postRootTraverse(root, k);
        }
    }
    private void postRootTraverse(BiTreeNode T,int k) {
        if (T != null) {
            postRootTraverse(T.lchild,k);
            postRootTraverse(T.rchild,k);
            count3++;
            if (count3==k) {
                System.out.println("后序的数据:"+T.data);
            }
        }
    }

    public BiTreeNode getRoot() {
        return root;
    }

    public void setRoot(BiTreeNode root) {
        this.root = root;
    }
    public int countNode() {
        return countNode(root);
    }
    private int countNode(BiTreeNode t) {
        if(t == null)
            return 0;
        return countNode(t.lchild) + countNode(t.rchild) + 1;
    }
    public void isCompleteTree(){
        isCompleteTree(root);
    }
    private  boolean isCompleteTree(BiTreeNode t)
    {
        if(t==null){
            System.out.println("该二叉树是完全二叉树");
            return true;
        }
        LinkQueue2<BiTreeNode> q=new LinkQueue2<>();
        q.offer(t);
        boolean isLeafOrLeft=false;
        while(!q.isEmpty()){
            BiTreeNode cur=q.poll();
            if (isLeafOrLeft){
                if (cur.lchild!=null||cur.rchild!=null){
                    System.out.println("该二叉树不是完全二叉树");
                    return false;
                }
            }
            else {
                if(cur.lchild!=null &&cur.rchild!=null){//左右孩子都有
                    q.offer(cur.lchild);
                    q.offer(cur.rchild);
                } else if(cur.lchild!=null){//只有左孩子
                    q.offer(cur.lchild);
                    isLeafOrLeft=true;
                } else if(cur.rchild!=null){//只有右孩子
                    System.out.println("该二叉树不是完全二叉树");
                    return false;
                } else{//叶子结点
                    isLeafOrLeft=true;
                }
            }
        }
        System.out.println("该二叉树是完全二叉树");
        return true;
    }
    public static void main(String[] args){
        BiTree t=new BiTree("AB##CDF##G##E##");//ABD##EG###CF#H###先序:ABDEGCFH,中序:DBGEAFHC后序:DGEBHFCA
        //ABDF###E##C##,AB##CDF##G##E##
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入k:");
        int k=sc.nextInt();
        t.preRootTraverse(k);
        t.inRootTraverse(k);
        t.postRootTraverse(k);
        t.isCompleteTree();
    }

}

代码不能直接运行,参考思想。
LinkQueue2链队列未给出
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

@8055@

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值