<legend id='MwA1R'><style id='MwA1R'><dir id='MwA1R'><q id='MwA1R'></q></dir></style></legend>
    • <bdo id='MwA1R'></bdo><ul id='MwA1R'></ul>

  1. <small id='MwA1R'></small><noframes id='MwA1R'>

    <i id='MwA1R'><tr id='MwA1R'><dt id='MwA1R'><q id='MwA1R'><span id='MwA1R'><b id='MwA1R'><form id='MwA1R'><ins id='MwA1R'></ins><ul id='MwA1R'></ul><sub id='MwA1R'></sub></form><legend id='MwA1R'></legend><bdo id='MwA1R'><pre id='MwA1R'><center id='MwA1R'></center></pre></bdo></b><th id='MwA1R'></th></span></q></dt></tr></i><div id='MwA1R'><tfoot id='MwA1R'></tfoot><dl id='MwA1R'><fieldset id='MwA1R'></fieldset></dl></div>

    1. <tfoot id='MwA1R'></tfoot>

      Java 使用特定格式的级别顺序打印二叉树

      Java Printing a Binary Tree using Level-Order in a Specific Format(Java 使用特定格式的级别顺序打印二叉树)
    2. <i id='ge6h2'><tr id='ge6h2'><dt id='ge6h2'><q id='ge6h2'><span id='ge6h2'><b id='ge6h2'><form id='ge6h2'><ins id='ge6h2'></ins><ul id='ge6h2'></ul><sub id='ge6h2'></sub></form><legend id='ge6h2'></legend><bdo id='ge6h2'><pre id='ge6h2'><center id='ge6h2'></center></pre></bdo></b><th id='ge6h2'></th></span></q></dt></tr></i><div id='ge6h2'><tfoot id='ge6h2'></tfoot><dl id='ge6h2'><fieldset id='ge6h2'></fieldset></dl></div>

        <bdo id='ge6h2'></bdo><ul id='ge6h2'></ul>

        • <tfoot id='ge6h2'></tfoot>

          <legend id='ge6h2'><style id='ge6h2'><dir id='ge6h2'><q id='ge6h2'></q></dir></style></legend>

              <tbody id='ge6h2'></tbody>

              <small id='ge6h2'></small><noframes id='ge6h2'>

                本文介绍了Java 使用特定格式的级别顺序打印二叉树的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                好的,我已经阅读了所有其他相关问题,但找不到对 java 有帮助的问题.我从解读其他语言的内容中得到了大致的想法;但我还没有弄清楚.

                Okay, I have read through all the other related questions and cannot find one that helps with java. I get the general idea from deciphering what i can in other languages; but i am yet to figure it out.

                问题:我想对排序进行级别排序(我使用递归)并以树的一般形状将其打印出来.

                Problem: I would like to level sort (which i have working using recursion) and print it out in the general shape of a tree.

                所以说我有这个:

                    1 
                   / 
                  2   3
                 /   / 
                4   5   6
                

                我的代码打印出这样的级别顺序:

                My code prints out the level order like this:

                1 2 3 4 5 6
                

                我想这样打印出来:

                1
                2 3
                4 5 6
                

                现在,在你给我做一个关于我的工作的道德演讲之前......我已经完成了我的 AP Comp Sci 项目,当我的老师提到广度优先搜索的事情时,我对此感到好奇.

                Now before you give me a moral speech about doing my work... I have already finished my AP Comp Sci project and got curious about this when my teacher mentioned the Breadth First Search thing.

                我不知道它是否会有所帮助,但这是我目前的代码:

                I don't know if it will help, but here is my code so far:

                /**
                  * Calls the levelOrder helper method and prints out in levelOrder.
                  */
                 public void levelOrder()
                 {
                  q = new QueueList();
                  treeHeight = height();
                  levelOrder(myRoot, q, myLevel);
                 }
                
                 /**
                  * Helper method that uses recursion to print out the tree in 
                  * levelOrder
                  */
                 private void levelOrder(TreeNode root, QueueList q, int curLev)
                 {
                  System.out.print(curLev);
                  if(root == null)
                  {
                   return;
                  }
                
                  if(q.isEmpty())
                  {
                   System.out.println(root.getValue());
                  }
                  else
                  {
                   System.out.print((String)q.dequeue()+", ");
                  }
                
                  if(root.getLeft() != null)
                  {
                   q.enqueue(root.getLeft().getValue());
                   System.out.println();
                  }
                  if(root.getRight() != null)
                  {
                   q.enqueue(root.getRight().getValue());
                   System.out.println();
                   curLev++;
                  }
                
                  levelOrder(root.getLeft(),q, curLev);
                  levelOrder(root.getRight(),q, curLev);
                 }
                

                据我所知,我需要使用树的总高度,并使用一个级别计数器...唯一的问题是我的级别计数器在我的 levelOrder 使用递归返回通过树时不断计数.

                From what i can figure out, i will need to use the total height of the tree, and use a level counter... Only problem is my level counter keeps counting when my levelOrder uses recursion to go back through the tree.

                对不起,如果这太多了,但一些提示会很好.:)

                Sorry if this is to much, but some tips would be nice. :)

                推荐答案

                这是代码,这个问题是在一次采访中问我的...

                Here is the code, this question was asked to me in one of the interviews...

                public void printTree(TreeNode tmpRoot) {
                
                        Queue<TreeNode> currentLevel = new LinkedList<TreeNode>();
                        Queue<TreeNode> nextLevel = new LinkedList<TreeNode>();
                
                        currentLevel.add(tmpRoot);
                
                        while (!currentLevel.isEmpty()) {
                            Iterator<TreeNode> iter = currentLevel.iterator();
                            while (iter.hasNext()) {
                                TreeNode currentNode = iter.next();
                                if (currentNode.left != null) {
                                    nextLevel.add(currentNode.left);
                                }
                                if (currentNode.right != null) {
                                    nextLevel.add(currentNode.right);
                                }
                                System.out.print(currentNode.value + " ");
                            }
                            System.out.println();
                            currentLevel = nextLevel;
                            nextLevel = new LinkedList<TreeNode>();
                
                        }
                
                    }
                

                这篇关于Java 使用特定格式的级别顺序打印二叉树的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)
                How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)
                Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)
                Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)
                How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)
                How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)
                    <tbody id='LO0vQ'></tbody>
                  <legend id='LO0vQ'><style id='LO0vQ'><dir id='LO0vQ'><q id='LO0vQ'></q></dir></style></legend>

                • <small id='LO0vQ'></small><noframes id='LO0vQ'>

                  <tfoot id='LO0vQ'></tfoot>

                  • <bdo id='LO0vQ'></bdo><ul id='LO0vQ'></ul>
                          <i id='LO0vQ'><tr id='LO0vQ'><dt id='LO0vQ'><q id='LO0vQ'><span id='LO0vQ'><b id='LO0vQ'><form id='LO0vQ'><ins id='LO0vQ'></ins><ul id='LO0vQ'></ul><sub id='LO0vQ'></sub></form><legend id='LO0vQ'></legend><bdo id='LO0vQ'><pre id='LO0vQ'><center id='LO0vQ'></center></pre></bdo></b><th id='LO0vQ'></th></span></q></dt></tr></i><div id='LO0vQ'><tfoot id='LO0vQ'></tfoot><dl id='LO0vQ'><fieldset id='LO0vQ'></fieldset></dl></div>