如何在控制台中正确显示二进制搜索树
How to display binary search tree in console properly?
我正试图在控制台中显示BST。这是我的代码(这是在这里找到的代码的修改版本:打印级别顺序二进制搜索树格式(:
string printLevel(node *root, int level, string gap) {
if (!root) {
return gap + "-" + gap;
}
if (level==1) {
stringstream out;
out<<root->value;
return gap + out.str() + gap;
} else if (level>1) {
string leftStr = printLevel(root->left, level-1, gap);
string rightStr = printLevel(root->right, level-1, gap);
return leftStr + " " + rightStr;
} else return "";
}
void printLevelOrder (node* root, int depth) {
for (int i=1; i<=depth; i++) {
string gap="";
for (int j=0; j<pow(2,depth-i)-1; j++) {
gap+=" ";
}
string levelNodes = printLevel(root, i, gap);
cout<<levelNodes<<endl;
}
}
例如,结果应该是这样的:
4
1 6
- 2 5 -
- - - 3 - - - -
但事实却是:
4
1 6
- 2 5 -
- - 3 - - -
如果我没有正确理解,当程序到达空叶时,递归就会停止,因此在结果的较低级别上缺少"-"。但是,我怎么知道我应该在较低级别上画多少呢?如何做到这一点?
我插入代码以查看它在哪里出错(因为在浏览器中运行调试器…(,您可以在这里看到它。再现功能为:
string printLevel(node *root, int level, string gap) {
if (root == 0) {
cout << ".. printLevel - " << root << ": " << gap << "-" << gap << "n";
return gap + "-" + gap;
}
if (level==1) {
stringstream out;
out<<root->value;
cout << ".. printLevel - " << root << ": " << gap << root->value << gap << "n";
return gap + out.str() + gap;
} else if (level>1) {
string leftStr = printLevel(root->left, level-1, gap);
string rightStr = printLevel(root->right, level-1, gap);
cout << ".. printLevel - " << root << ": '" << leftStr << "', '" << rightStr << "'n";
return leftStr + " " + rightStr;
} else return "";
}
这里有一个有趣的输出:
.. printLevel - <none>: -
.. printLevel - <none>: -
.. printLevel - { 3, <none>, <none> }: 3
.. printLevel - { 2, <none>, { 3, <none>, <none> } }: '-', '3'
.. printLevel - { 1, <none>, { 2, <none>, { 3, <none>, <none> } } }: '-', '- 3'
因此,问题是每当root
为0时都会短路,这实际上是一个问题:除非level
为1
,否则-
不是正确的输出。
root
为0和root
不为0之间的唯一区别是,您无法从中读取值(因此可以用-
替换它(;然而,只有当level
是1
时,您才能真正读取该值(注意,您可能也会尝试读取left
或right
(,因此没有理由测试root == 0
,除非您在level == 1
分支中。
让我们稍微重新排序一下:
string printLevel(node *root, int level, string gap) {
if (level==1) {
// if (root == 0) {
// cout << ".. printLevel - " << root << ": " << gap << "-" << gap << "n";
// return gap + "-" + gap;
// }
stringstream out;
out<<root->value;
cout << ".. printLevel - " << root << ": " << gap << root->value << gap << "n";
return gap + out.str() + gap;
} else if (level>1) {
// string leftStr = printLevel(root ? root->left : 0, level-1, gap);
// string rightStr = printLevel(root ? root->right : 0, level-1, gap);
cout << ".. printLevel - " << root << ": '" << leftStr << "', '" << rightStr << "'n";
return leftStr + " " + rightStr;
} else return "";
}
注:我用"注释"突出显示"了修改后的行。
现在,树打印正确。
void BinaryTree::Display(TreeNode *current, int indent)
{
if (current != nullptr)
{
Display(current->left, indent + 4);
if (indent > 0)
cout << setw(indent) << " ";
cout << current->value << endl;
Display(current->right, indent + 4);
}
}
从左到右打印树,而不是自上而下。
1
2
3
4
5
6
7
8
12
18
这是我的代码。它印得很好,可能不是完全对称的。小描述:
- 第一个功能-逐层打印(根lv->离开lv(
- 第二个函数-距新线起点的距离
- 第三个功能-打印节点并计算两个打印之间的距离
void Tree::TREEPRINT()
{
int i = 0;
while (i <= treeHeight(getroot())){
printlv(i);
i++;
cout << endl;
}
}
void Tree::printlv(int n){
Node* temp = getroot();
int val = pow(2, treeHeight(root) -n+2);
cout << setw(val) << "";
prinlv(temp, n, val);
}
void Tree::dispLV(Node*p, int lv, int d)
{
int disp = 2 * d;
if (lv == 0){
if (p == NULL){
cout << " x ";
cout << setw(disp -3) << "";
return;
}
else{
int result = ((p->key <= 1) ? 1 : log10(p->key) + 1);
cout << " " << p->key << " ";
cout << setw(disp - result-2) << "";
}
}
else
{
if (p == NULL&& lv >= 1){
dispLV(NULL, lv - 1, d);
dispLV(NULL, lv - 1, d);
}
else{
dispLV(p->left, lv - 1, d);
dispLV(p->right, lv - 1, d);
}
}
}
输入:
50-28-19-30-29-17-42-200-160-170-180-240-44-26-27
输出:https://i.stack.imgur.com/TtPXY.png
相关文章:
- 我写的 ASCII 到二进制转换器C++向后显示二进制,如何使其正确显示?
- 以 10 c++ 为基数显示 1024 位二进制数
- 二进制文件中的某些数据如图所示,并以奇怪的方式显示OS
- 为什么动态链接的二进制显示硬编码为so name
- 无法使用 fstream 从二进制文件中读取字符串,而是显示奇怪的符号
- 以C++显示浮点类型的二进制表示形式
- 二进制搜索 - 代码编译并运行后不显示输出
- 关于以二进制存储并显示其小数
- 二进制搜索程序。在应用测试用例时给出正确的输出仍然显示" "超出时间限制" "错误
- 在二进制搜索树中显示到节点的路径
- 二进制搜索树顺序树显示
- PDB无法显示发布版本崩溃的确切行号,但在二进制调试版本的情况下显示精确行号
- 数据C++的二进制表示显示的数据不正确
- C++.将二进制文件读入缓冲区后,如何以十六进制显示缓冲区
- OpenGL代码显示人体图形,从二进制文件中读取顶点
- ASCII 和二进制 - 按原样显示,但读取返回垃圾
- 如何让 CMake 在 IDE 中显示不属于任何二进制目标的标头
- 如何在数组中显示二进制搜索树结果
- 我如何停止我的Windows控制台应用程序触发哔哔声时显示二进制数据
- 如何在控制台中正确显示二进制搜索树