北工大咸鱼 2022-05-09 12:03 采纳率: 100%
浏览 26
已结题

二叉树的遍历出错三种遍历均出现错误

问题遇到的现象和发生背景
问题相关代码,请勿粘贴截图
运行结果及报错内容
我的解答思路和尝试过的方法
我想要达到的结果
前序中序后序的遍历不知道哪里错了
```java
import java.io.IOException;

public class dcshu {

    public static void main(String[] args) throws IOException{
    int i;
    int arr[]= {1,2,2,3,4,4,3};
    BinaryTree tree=new BinaryTree();
    System.out.print("root="+"[");
    for(i=0;i<7;i++) 
    {
        System.out.print(arr[i]+" ");
        
    }
    System.out.print("]");
    System.out.println();
    for(i=0;i<arr.length;i++)
    {
        tree.addto(arr[i]);
    }
    System.out.println("前序遍历的结果");
    tree.PreOrder(tree.rootNode);
 System.out.println();
    System.out.println("中序遍历的结果");
    tree.InOrder(tree.rootNode);
 System.out.println();
    System.out.println("后序遍历的结果");
    tree.PostOrder(tree.rootNode);
  
    
    
    }

}

public class dcneed {
       int value;
          dcneed leftchild;
          dcneed rightchild;
          public dcneed(int value) 
          {
              this.value=value;
              this.leftchild=null;
              this.rightchild=null;
          }
    }
    class BinaryTree{
        public  dcneed rootNode;
        public  void addto(int value) 
            {
                if(rootNode==null) 
                {
                    rootNode=new dcneed(value);
                    return;
                }
                dcneed currentNode=rootNode;
                while(true) 
                {
                    if(value<currentNode.value) {
                    if(currentNode.leftchild==null) {
                        currentNode.leftchild=new dcneed(value);
                        return;                }
                    else currentNode=currentNode.leftchild;
                    }
                    else {
                        if(currentNode.rightchild==null) 
                        {
                            currentNode.rightchild=new dcneed(value);
                            return;
                        }
                        else
                            currentNode=currentNode.rightchild;
                    }
                    
                }
            
                
            }
            public void InOrder(dcneed node) 
            {
                if(node!=null) {
                    InOrder(node.leftchild);
                    System.out.print(node.value);
                    InOrder(node.rightchild);
                }
            }
            public void PreOrder(dcneed node) 
            {
                if(node!=null) 
                {
                    System.out.print(node.value);
                    PreOrder(node.leftchild);
                    PreOrder(node.rightchild);
                }
            }
            public void PostOrder(dcneed node) 
            {
                if(node!=null) {
                    PostOrder(node.leftchild);
                    PostOrder(node.rightchild);
                    System.out.print(node.value);
                }
            }
        }
        正确的
![img](https://img-mid.csdnimg.cn/release/static/image/mid/ask/065729860256177.png "#left")
错误的(我的)
![img](https://img-mid.csdnimg.cn/release/static/image/mid/ask/659019860256146.png "#left")




```

  • 写回答

3条回答 默认 最新

  • CSDN专家-sinJack 2022-05-09 12:32
    关注

    问题原因:
    输出逻辑有问题,不是判断当前节点是否为空,而是判断当前节点的左右节点是否为空。
    (前中后序)输出代码修改如下:

    public void InOrder(dcneed node)
        {
            /*if(node!=null) {
                InOrder(node.leftchild);
                System.out.print(node.value);
                InOrder(node.rightchild);
            }*/
            //递归左节点
            if (node.leftchild!= null) {
                InOrder(node.leftchild);
            }
            //输出父节点
            System.out.print(node.value + " ");
            //递归右节点
            if (node.rightchild != null) {
                InOrder(node.rightchild);
            }
        }
        public void PreOrder(dcneed node)
        {
            //先输出父节点
            System.out.print(node.value + " ");
            //递归左子树
            if (node.leftchild != null) {
                PreOrder(node.leftchild);
            }
            //递归右子树
            if (node.rightchild != null) {
                PreOrder(node.rightchild);
            }
    
            /*if(node!=null)
            {
                System.out.print(node.value);
                PreOrder(node.leftchild);
                PreOrder(node.rightchild);
            }*/
        }
        public void PostOrder(dcneed node)
        {
            /*if(node!=null) {
                PostOrder(node.leftchild);
                PostOrder(node.rightchild);
                System.out.print(node.value);
            }*/
            //递归左节点
            if (node.leftchild != null) {
                PostOrder(node.leftchild);
            }
            //递归右节点
            if (node.rightchild != null) {
                PostOrder(node.rightchild);
            }
            //输出父节点
            System.out.print(node.value + " ");
        }
    

    参考看下:

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 5月17日
  • 已采纳回答 5月9日
  • 创建了问题 5月9日

悬赏问题

  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程