【问题标题】:Binary Tree Traversal not showing full list二叉树遍历未显示完整列表
【发布时间】:2020-12-16 10:55:36
【问题描述】:

所以目前我正在尝试遵循 FreeCodeCamp 中关于实现二叉树的教程,但在添加和遍历我的树时遇到了问题。

由于某种原因,我似乎可以将节点添加到我的树中,但是当我尝试通过迭代前序遍历来遍历我的树时,它只会拾取我的根节点。就好像我的节点没有相互指向一样。

我感觉问题出在我的 add 方法或遍历方法上,两者都在下面。任何帮助将不胜感激。

添加方法:

public boolean add(T thing) 
    {
        if(contains(thing)) 
        {
            return false;
        } else {
            root = add(root,thing); 
            count++;
            return true;
            
        }
        
    }
    
    
    private Node add(Node node,T thing) 
    {
        if(node == null) 
        {
            node = new Node(thing,null,null); 
        } else
        {
            if(thing.compareTo(node.value) <0) 
            {
                if(node.left == null)
                {
                    node.left = node = new Node(thing,null,null);
                } else{
                node.left =add(node.left,thing);
                }
            }
            else 
            {
                if(node.right == null)
                {
                    node.right = node = new Node(thing,null,null);
                }else {
                node.right = add(node.right,thing);
                }
            }
            
        }
        
        return node;
    }

遍历:

public void traverse()
   {
       preorder(root);
   }
   
   private void preorder(Node node)
   { int iteration=0;
       java.util.Stack<Node> stack = new java.util.Stack<Node>();
       System.out.println( "root is: " +node.value);
       stack.push(node);
       
       while(stack.empty() == false)
       {
            Node current = stack.pop();
            System.out.println("in preorder: "+current.value);
            
            if(current.right != null)
            {
                stack.push(current.right);
            }
            if(current.left != null)
            {
                stack.push(current.left);
            }
            iteration++;
            
       }
       System.out.println("iteration: "+iteration);
       
       
   }

【问题讨论】:

  • 如果您提供minimal reproducible example 会有所帮助,该minimal reproducible example 还包括使用演示问题的测试数据填充树的代码。
  • node.right = node = new Node(...) - 这对我来说似乎很奇怪。你确定你真的确定这会做你想做的事吗?请不要这样做,阅读起来非常混乱 - 将其分成两行。
  • 另外,add 似乎总是返回新的叶节点,但您随后将该返回值分配给 root = add(root,thing);

标签: java binary-tree nodes tree-traversal preorder


【解决方案1】:

在树中添加时您没有遍历树。检查我的树插入方法以获得想法:-

void insert(Node temp,int value) {
            if(temp==null){
                temp=new Node(value,null,null);
                this.root=temp;
            }
            else{
            Queue<Node> q = new LinkedList<>();
            q.add(temp);

            while (!q.isEmpty()) {
                temp = q.peek();
                q.remove();

                if (temp.left == null) {
                    temp.left = new Node(value, null, null);
                    break;
                } else
                    q.add(temp.left);

                if (temp.right == null) {
                    temp.right =new Node(value, null, null);
                    break;
                } else
                    q.add(temp.right);
            }
        }
    }

 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-01
    • 2022-11-11
    • 1970-01-01
    • 2021-11-20
    • 2017-09-16
    相关资源
    最近更新 更多