簡體   English   中英

在Java中使用適當的空間打印二叉樹

[英]Print Binary Tree With Proper Spaces in Java

我有以下代碼在每個級別上打印二叉樹。 但是我不希望當前輸出,我需要金字塔結構的額外空間:

import java.util.LinkedList;
import java.util.Queue;

public class LevelOrder 
{
static class Node
{
    int data;
    Node left;
    Node right;

    Node(int data){
        this.data = data;
        left = null;
        right =null;
    }
}

static void printLevelOrder(Node root)
{
    if(root == null)
        return;

    Queue<Node> q =new LinkedList<Node>();

    q.add(root);            
    while(true)
    {               
        int nodeCount = q.size();
        if(nodeCount == 0)
            break;

        while(nodeCount > 0)
        {    
            Node node = q.peek();
            System.out.print("("+node.data + ")");

            q.remove();

            if(node.left != null)
                q.add(node.left);
            if(node.right != null)
                q.add(node.right);

            if(nodeCount>1){
               System.out.print(", ");
            }
            nodeCount--;    
        }     
        System.out.println();
    }
}       
public static void main(String[] args) 
{           
    Node root = new Node(6);
    root.left = new Node(3);
    root.right = new Node(2);
    root.left.left = new Node(8);
    root.left.right = new Node(9);
    root.right.right = new Node(-2);
    root.right.left = new Node(3);
    printLevelOrder(root);    
   }

}

我當前的輸出為:

(6)
(3), (2)
(8), (9), (3), (-2)

但是我需要這樣的東西:

       (6)
    (3),  (2)
 (8), (9), (3), (-2)

如何有效打印上面的輸出並留有所需的空格。 請幫忙。

實際上您得到的是正確的輸出,但是您沒有以正確的格式獲得輸出。為此,您必須將相應的輸出顯示為三角形。我必須用我的代碼來完成。如果遇到任何問題,請放心發表評論。我一定會幫助您。

試試這個代碼:

import java.util.LinkedList;
import java.util.Queue; 
public class LevelOrder
 { 
 static class Node
 { int data; Node left; Node right;
Node(int data) 
{
this.data = data;
left = null;
right = null;
depth = 1;
}
}

static void printLevelOrder(Node root) 
{
if (root == null)
    return;

Queue < Node > q = new LinkedList < Node > ();

q.add(root);
while (true)
 {
    int nodeCount = q.size();
    if (nodeCount == 0)
        break;

    while (nodeCount > 0)
 {
        Node node = q.peek();
        System.out.print("(" + node.data + ")");

        q.remove();

        if (node.left != null)
            q.add(node.left);
        if (node.right != null)
            q.add(node.right);
        if (node.depth != null)
            System.out.print(" ");
        if (nodeCount > 1)
 {
            System.out.print(", ");
        }

        nodeCount--;
    }
    System.out.println();
}
}
public static void main(String[] args)
 {
Node root = new Node(6);
root.left = new Node(3);
root.right = new Node(2);
root.left.left = new Node(8);
root.left.right = new Node(9);
root.right.right = new Node(-2);
root.right.left = new Node(3);
printLevelOrder(root);
}
}

這就是您要尋找的東西,我介紹了一種計算樹的深度的方法,並根據您打印節點的級別,相應地介紹了空間。

import java.util.LinkedList;
import java.util.Queue;

public class Main 
{
static class Node
{
    int data;
    Node left;
    Node right;

    Node(int data){
        this.data = data;
        left = null;
        right =null;
    }
}

static int depthOfTree(Node root, int d) {
  if(root == null) {
    return d;
  }
  int left = d;
  int right = d;
  if(root.left != null) {
    left = depthOfTree(root.left, d+1);
  }
  if(root.right != null) {
    right = depthOfTree(root.right, d+1);
  }
  return Math.max(left, right);
}

static void printLevelOrder(Node root, int depth)
{
    if(root == null)
        return;

    Queue<Node> q =new LinkedList<Node>();

    q.add(root);            
    while(true)
    {               
        int nodeCount = q.size();
        if(nodeCount == 0)
            break;
        for(int i=0; i<depth; i++) {
          System.out.print("  ");
        }
        while(nodeCount > 0)
        {    
            Node node = q.peek();
            System.out.print("("+node.data + ")");

            q.remove();

            if(node.left != null)
                q.add(node.left);
            if(node.right != null)
                q.add(node.right);

            if(nodeCount>1){
               System.out.print(", ");
            }
            nodeCount--;    
        }
        depth--;
        System.out.println();
    }
}       
public static void main(String[] args) 
{           
    Node root = new Node(6);
    root.left = new Node(3);
    root.right = new Node(2);
    root.left.left = new Node(8);
    root.left.right = new Node(9);
    root.right.right = new Node(-2);
    root.right.left = new Node(3);
    int d = depthOfTree(root, 1);
    //System.out.println(d);
    printLevelOrder(root, d);    
   }

}

希望這可以幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM