佩奇inging 2020-10-19 10:07 采纳率: 0%
浏览 72

用java实现二叉树为什么遍历不出结果,只能得到一个A

package com.demo.demo;
class Node{

private E data;//节点数据

private Node<E> lChild;//定义节点的左孩子

private Node<E> rChild;//定义节点的右孩子

//设置构造函数
public Node(E date,Node<E>  lChild,Node<E>rChild){
    data=date;

    this.lChild=null;

    this.rChild=null;
}

//设置这个节点的节点信息
public void setData(E data) {
    this.data = data;
}
public E getData() {
    return data;
}
//获得这个节点的左子树
public Node getLChild(){
    return lChild;
}
//获得这个节点的右子树
public Node  getRChild(){
    return rChild;
}
//设置这个节点的左子树
public void setlChild(Node <E> lChild) {
    this.lChild = lChild;
}
//设置这个节点的右子树
public void setRChild(Node <E> rChild){
    this.rChild=rChild;
}

}

public class DemoTree {
/*
使用java语言实现一个二叉树

 */
//定义一个节点结构

public Node init() {
    //注意必须逆序建立,先建立子节点,再逆序往上建立,因为非叶子结点会使用到下面的节点,而初始化是按顺序初始化的,不逆序建立会报错

    Node H = new Node("A", null, null);


    Node  D = new Node("D", H, null);

    Node E = new Node("E", null, null);

    Node B = new Node("B", D, E);

    Node F = new Node("F", null, null);

    Node G = new Node("7", null, null);

    Node C = new Node("C", F, G);

    Node A = new Node("A", B, C);

    return A;   //返回根节点
}
//打印节点信息
public void printNode(Node <E>node){
    System.out.print(node.getData());
}

//使用递归实现遍历

//先序遍历
public void theFirstTraversal(Node root) {  //先序遍历
    printNode(root);
    if (root.getLChild() != null) {  //使用递归进行遍历左孩子
        theFirstTraversal(root.getLChild() );
    }
    if (root.getLChild()  != null) {  //递归遍历右孩子
        theFirstTraversal(root.getRChild());
    }
}
public void theInOrderTraversal(Node root) {  //中序遍历
    if (root.getLChild() != null) {
        theInOrderTraversal(root.getLChild());
    }
    printNode(root);
    if (root.getRChild() != null) {
        theInOrderTraversal(root.getRChild());
    }
}

public void thePostOrderTraversal(Node root) {  //后序遍历
    if (root.getLChild() != null) {
        thePostOrderTraversal(root.getLChild());
    }
    if(root.getRChild() != null) {
        thePostOrderTraversal(root.getRChild());
    }
    printNode(root);
}

public static void main(String[] args) {
    DemoTree <Character> dt = new DemoTree();

    Node <Character>dePro= dt.init();

    System.out.println("二叉树的先序遍历");

    dt.theFirstTraversal(dePro);




}

}

  • 写回答

2条回答 默认 最新

  • qq_21059191 2020-10-19 10:54
    关注

    package com.flcz.enjoy.demo;

    public class DemoTree {
    //定义一个节点结构

    public Node init() {
        //注意必须逆序建立,先建立子节点,再逆序往上建立,因为非叶子结点会使用到下面的节点,而初始化是按顺序初始化的,不逆序建立会报错
    
        Node H = new Node("A", null, null);
    
    
        Node  D = new Node("D", H, null);
    
        Node E = new Node("E", null, null);
    
        Node B = new Node("B", D, E);
    
        Node F = new Node("F", null, null);
    
        Node G = new Node("7", null, null);
    
        Node C = new Node("C", F, G);
    
        Node A = new Node("A", B, C);
    
        return A;   //返回根节点
    }
    //打印节点信息
    public void printNode(Node node){
        System.out.print(node.getData());
    }
    

    //使用递归实现遍历

    //先序遍历
    public void theFirstTraversal(Node root) {  //先序遍历
        printNode(root);
        if (root.getLChild() != null) {  //使用递归进行遍历左孩子
            theFirstTraversal(root.getLChild() );
        }
        if (root.getLChild()  != null) {  //递归遍历右孩子
            theFirstTraversal(root.getRChild());
        }
    }
    public void theInOrderTraversal(Node root) {  //中序遍历
        if (root.getLChild() != null) {
            theInOrderTraversal(root.getLChild());
        }
        printNode(root);
        if (root.getRChild() != null) {
            theInOrderTraversal(root.getRChild());
        }
    }
    
    public void thePostOrderTraversal(Node root) {  //后序遍历
        if (root.getLChild() != null) {
            thePostOrderTraversal(root.getLChild());
        }
        if(root.getRChild() != null) {
            thePostOrderTraversal(root.getRChild());
        }
        printNode(root);
    }
    
    public static void main(String[] args) {
        DemoTree dt = new DemoTree();
    
        Node <Character>dePro= dt.init();
    
        System.out.println("二叉树的先序遍历");
    
        dt.theFirstTraversal(dePro);
    
    
    
    
    }
    

    }

    package com.flcz.enjoy.demo;

    public class Node {

    private E data;//节点数据
    
    private Node<E> lChild;//定义节点的左孩子
    
    private Node<E> rChild;//定义节点的右孩子
    
    //设置构造函数
    public Node(E date,Node<E>  lChild,Node<E>rChild){
        data=date;
    
        this.lChild=null;
    
        this.rChild=null;
    }
    
    //设置这个节点的节点信息
    public void setData(E data) {
        this.data = data;
    }
    public E getData() {
        return data;
    }
    //获得这个节点的左子树
    public Node getLChild(){
        return lChild;
    }
    //获得这个节点的右子树
    public Node  getRChild(){
        return rChild;
    }
    //设置这个节点的左子树
    public void setlChild(Node <E> lChild) {
        this.lChild = lChild;
    }
    //设置这个节点的右子树
    public void setRChild(Node <E> rChild){
        this.rChild=rChild;
    }
    

    }图片说明这个位置打印出A

    左孩子和右孩子都为空,因此遍历了得不到结果 。
    //设置构造函数
    public Node(E date,Node lChild,NoderChild){
    data=date;

        this.lChild=null;
    
        this.rChild=null;
    }
    

    再此处赋值出错了
    正确的是
    public Node(E date,Node lChild,NoderChild){
    data=date;

        this.lChild=lChild;
    
        this.rChild=rChild;
    }
        此处判断有误
         //先序遍历
    public void theFirstTraversal(Node root) {  //先序遍历
        printNode(root);
        if (root.getLChild() != null) {  //使用递归进行遍历左孩子
            theFirstTraversal(root.getLChild() );
        }
        if (root.getLChild()  != null) {  //递归遍历右孩子
            theFirstTraversal(root.getRChild());
        }
    }
        正确的是
         //先序遍历
    public void theFirstTraversal(Node root) {  //先序遍历
        printNode(root);
        if (root.getLChild() != null) {  //使用递归进行遍历左孩子
            theFirstTraversal(root.getLChild() );
        }
        if (root.getRChild()  != null) {  //递归遍历右孩子
            theFirstTraversal(root.getRChild());
        }
    }
        得出的结果是
        二叉树的先序遍历
    

    ABDAECF7

    如果有用帮忙点个赞。多谢了!

    评论

报告相同问题?

悬赏问题

  • ¥30 电脑误删了手机的照片怎么恢复?
  • ¥15 (标签-python|关键词-char)
  • ¥15 python+selenium,在新增时弹出了一个输入框
  • ¥15 苹果验机结果的api接口哪里有??单次调用1毛钱及以下。
  • ¥20 学生成绩管理系统设计
  • ¥15 来一个cc穿盾脚本开发者
  • ¥15 CST2023安装报错
  • ¥15 使用diffusionbert生成文字 结果是PAD和UNK怎么办
  • ¥15 有人懂怎么做大模型的客服系统吗?卡住了卡住了
  • ¥20 firefly-rk3399上启动卡住了