一个漂亮的打印二叉树的程序

40 篇文章 0 订阅

转自

// copyright @ L.J.SHOU Jan.16, 2014  
// a fancy binary tree printer  
#ifndef BINARY_TREE_PRINTER_H_  
#define BINARY_TREE_PRINTER_H_  

#include <cmath>  
#include <iostream>  
#include <vector>  
using std::vector;  
using std::cout;  
using std::endl;  
using std::max;  

void PrintBinaryTree(TreeNode *root);  

struct TreeNode  
{  
  TreeNode *left;  
  TreeNode *right;  
  int val;  
  TreeNode(int x=0)  
   : val(x), left(NULL), right(NULL){}  
};  

static int MaxLevel(TreeNode *root)  
{  
  if(root == NULL) return 0;  
  return max(MaxLevel(root->left), MaxLevel(root->right)) + 1;  
}  

// test whether all elements in vector are NULL  
static bool IsAllElementsNULL(const vector<TreeNode*> &nodes)  
{  
  vector<TreeNode*>::const_iterator it = nodes.begin();  

  while(it != nodes.end()){  
    if(*it) return false;   
    ++it;  
  }  
  return true;  
}  

static void PrintWhiteSpaces(int num)  
{  
  for(int i=0; i<num; ++i)  
    cout << " ";  
}  

void PrintNode(vector<TreeNode*> &nodes, int level, int max_level)  
{  
  if(nodes.empty() || IsAllElementsNULL(nodes)) return; // exit  

  int floor = max_level - level;  
  int endge_lines = 1 << (max(floor-1, 0));  
  int first_spaces = (1 << floor) - 1;  
  int between_spaces = (1 << (floor+1)) - 1;  

  PrintWhiteSpaces(first_spaces);  

  // print the 'level' level   
  vector<TreeNode*> new_nodes;  
  vector<TreeNode*>::const_iterator it = nodes.begin();  
  for(; it != nodes.end(); ++it){  
    if(*it != NULL){  
      cout << (*it)->val;  
      new_nodes.push_back((*it)->left);  
      new_nodes.push_back((*it)->right);  
    }  
    else{  
      new_nodes.push_back(NULL);  
      new_nodes.push_back(NULL);  
      cout << " ";  
    }  
    PrintWhiteSpaces(between_spaces);  
  }  
  cout << endl;  

  // print the following /s and \s  
  for(int i=1; i<= endge_lines; ++i){  
    for(int j=0; j<nodes.size(); ++j){  
      PrintWhiteSpaces(first_spaces - i);  
      if(nodes[j] == NULL){  
        PrintWhiteSpaces(endge_lines + endge_lines + i + 1);  
        continue;  
      }  
      if(nodes[j]->left != NULL)  
        cout << "/";  
      else  
        PrintWhiteSpaces(1);  

      PrintWhiteSpaces(i+i-1);  

      if(nodes[j]->right != NULL)  
        cout << "\\";  
      else  
        PrintWhiteSpaces(1);  

      PrintWhiteSpaces(endge_lines + endge_lines - i);  
    }  
    cout << endl;  
  }  

  PrintNode(new_nodes, level+1, max_level);  

}  

// wrapper function  
void PrintBinaryTree(TreeNode *root)  
{  
  int max_level = MaxLevel(root);  
  vector<TreeNode*> nodes;  

  nodes.push_back(root);  

  PrintNode(nodes, 1, max_level);  
}  

#endif  

#if 0  
int main(void)  
{  
  TreeNode *root(NULL);  

  root = new TreeNode(1);  
  root->left = new TreeNode(2);  
  root->right = new TreeNode(3);  

  root->left->left = new TreeNode(4);  
  root->right->right = new TreeNode(7);  

  root->left->left->left = new TreeNode(8);  
  root->left->left->right = new TreeNode(9);  
  PrintBinaryTree(root);  

  root = Destroy(root);  
  return 0;  
}  
#endif  
### 回答1: 要在 Java 中打印二叉树,你需要先实现二叉树的数据结构,然后使用递归遍历二叉树,并在遍历的过程中打印节点的值。 具体实现可以参考以下代码: ``` class Node { int value; Node left; Node right; public Node(int value) { this.value = value; } } class BinaryTree { Node root; public void printTree(Node node) { if (node == null) { return; } System.out.print(node.value + " "); printTree(node.left); printTree(node.right); } } public class Main { public static void main(String[] args) { BinaryTree tree = new BinaryTree(); tree.root = new Node(1); tree.root.left = new Node(2); tree.root.right = new Node(3); tree.root.left.left = new Node(4); tree.root.left.right = new Node(5); tree.printTree(tree.root); } } ``` 执行上面的代码,你将得到如下的输出: ``` 1 2 4 5 3 ``` ### 回答2: 要运行一个打印二叉树的Java程序,首先需要定义一个二叉树的数据结构。可以通过创建一个二叉树节点类来实现,该节点类应该包含数值和左右子节点的引用。然后,可以使用递归方法来遍历二叉树打印节点的值。 以下是一个简单的Java程序示例,用于打印二叉树: ```java // 定义二叉树节点类 class TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int val) { this.val = val; } } // 打印二叉树的方法 public class PrintBinaryTree { public static void printTree(TreeNode root) { if (root == null) { return; } // 打印当前节点的值 System.out.print(root.val + " "); // 递归打印左子树 printTree(root.left); // 递归打印右子树 printTree(root.right); } public static void main(String[] args) { // 创建一个二叉树 TreeNode root = new TreeNode(1); root.left = new TreeNode(2); root.right = new TreeNode(3); root.left.left = new TreeNode(4); root.left.right = new TreeNode(5); // 打印二叉树 printTree(root); } } ``` 这个程序首先定义了一个二叉树节点类,并在主程序中创建了一个简单的二叉树。然后,通过调用 `printTree` 方法,程序会按顺序打印二叉树上每个节点的值。在这个例子中,打印的结果应为 `1 2 4 5 3`。 ### 回答3: 运行 Java 程序打印二叉树是一种通过代码实现将二叉树的结构和内容输出到控制台的方式。下面是一个简单的 Java 程序示例: ```java // 定义二叉树节点类 class TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int x) { val = x; } } // 定义打印二叉树的方法 class PrintBinaryTree { public static void printTree(TreeNode root) { if (root == null) { return; } // 打印根节点的值 System.out.print(root.val + " "); // 递归打印左子树 printTree(root.left); // 递归打印右子树 printTree(root.right); } public static void main(String[] args) { // 创建一个二叉树 TreeNode root = new TreeNode(1); root.left = new TreeNode(2); root.right = new TreeNode(3); root.left.left = new TreeNode(4); root.left.right = new TreeNode(5); // 打印二叉树 printTree(root); } } ``` 在上面的代码中,我们定义了一个二叉树节点类 `TreeNode`,包含节点值和左右子节点的引用。然后我们定义了一个 `PrintBinaryTree` 类,其中的 `printTree` 方法使用前序遍历的方式打印二叉树。在 `main` 方法中,我们创建了一个二叉树并调用 `printTree` 方法来打印这个二叉树。运行该程序将输出二叉树的节点值序列:1 2 4 5 3。 注意:这只是一个简单的示例,实际情况中打印二叉树可能需要根据具体需求进行修改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值