打印二叉树- c++
Print Binary Tree - C++
我正试图以更可读的方式打印二叉树,而不仅仅是一行。我使用这个问题的答案作为开始,但它从左到右打印数据,如下所示:
25
15
10
20
30
35
我需要它看起来像这样:
25
15 30
10 20 35
这是我的代码:
void printTree(AVLNode* root, int indent)
{
if (root != nullptr) {
if (indent) {
cout << setw(indent) << ' ';
}
cout << root->data << endl;
if (root->left) printTree(root->left, indent + 4);
if (root->right) printTree(root->right, indent + 4);
}
}
有什么办法可以让它按我想要的方式打印吗?
考虑以下示例
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
// .... your code ....
void buildTree(AVLNode* root, int scrWidth, int itemWidth)
// breadth-first traversal with depth limit based on screen width and output field width for one elemet
{
bool notFinished = false;
// check the root
if (root)
{
notFinished = true;
}
// calculate maximum possible depth
int depth = 1;
int field = scrWidth;
while (field > itemWidth)
{
depth++;
field /= 2;
}
// check result
if (depth < 1)
{
cout << " -= erroneous output options =-" << endl;
return;
}
AVLNode** pItems = new AVLNode*[1];
*pItems = root; // pointer to item on the first level
int itemCnt = 1;
int divWidth = 1;
// loop for output not more than depth levels until the data is not finished
// where level is current depth of tree, and root is on the first level
for (int level = 1; level <= depth && notFinished; level++)
{
itemCnt = (level == 1) ? 1 : (itemCnt * 2);
divWidth *= 2;
// make list of pointers to refer items on next level
AVLNode** list = new AVLNode*[itemCnt * 2];
// output all utems of that level
int nextCnt = 0;
notFinished = false;
for (int i = 0; i < itemCnt; i++, nextCnt += 2)
{
int curWidth = (scrWidth / divWidth) * ((i > 0) ? 2 : 1);
cout << setw((curWidth>=itemWidth) ? curWidth:(itemWidth/(1+(i==0))));
if (pItems[i])
{
cout << pItems[i]->data;
list[nextCnt] = pItems[i]->left;
list[nextCnt + 1] = pItems[i]->right;
if (list[nextCnt] || list[nextCnt + 1])
notFinished = true;
}
else
{
cout << ".";
list[nextCnt] = NULL;
list[nextCnt + 1] = NULL;
}
}
cout << endl;
// free the memory allocated for list of pointers
if (pItems)
delete[] pItems;
pItems = list; // and shift to new one (for next level)
}
delete[] pItems;
}
int main(int argc, char* argv[])
{
// create some structure
AVLNode * root = NULL;
// code for making tree
// ....
buildTree(root, 80, 5);
// some other code
// ....
return 0;
}
Call buildTree(root, 80, 5);
这样打印树(其中.
表示NULL而不是item):
64
58 .
24 62 . .
0 78 . . . . . .
41 69 . . . . . . . . . . . . . .
但是对于相同的数据buildTree(root, 40, 10);
将输出
64
58 .
24 62 . .
。只有3层,因为第4个关卡有8个道具,如果每个道具需要10个角色,那么总权重40是不够的。
注意:我没有足够的时间来调试代码并使其完美,但我希望它能帮助你找到你自己的解决方案
相关文章:
- 从父数组测试用例构造二叉树失败
- 打印时有二叉树问题.用户输入不打印任何内容
- 试图找到二叉树的深度
- 二叉树结构平衡,使用递归时EXC_BAD_ACCESS
- 指向二叉树中新节点的指针
- 我试图用这段代码找到二叉树的高度,但它一直返回 0,有人可以告诉我为什么吗?
- C++:如何计算二叉树中其值模块高度小于 2 的节点数?
- 二叉树级别顺序遍历在leetcode中
- 运行无限循环的最小二叉树问题
- 打印二叉树中的常见元素
- 二叉树基准测试结果
- 带有矢量重复值的二叉树打印出来
- 签入二叉树的函数是平衡C++
- 我有两棵二叉树.我想在不更改输入树的情况下深度复制两个二叉树的结果
- 如何计算给定数字在二叉树中出现的次数?
- 为什么我的二叉树会覆盖其根的叶子?
- 将二叉树的节点插入链表 (C++)
- 二叉树级别顺序插入 c++
- 在 C++ 中创建两个不同的二叉树时出现分段错误
- 如何在使用 ItemType 的模板类时将新节点插入二叉树 c++.(我是 c++ 的新手)