如何在控制台中正确显示二进制搜索树

How to display binary search tree in console properly?

本文关键字:显示 二进制 搜索树 控制台      更新时间:2023-10-16

我正试图在控制台中显示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时都会短路,这实际上是一个问题:除非level1,否则-不是正确的输出。

root为0和root不为0之间的唯一区别是,您无法从中读取值(因此可以用-替换它(;然而,只有当level1时,您才能真正读取该值(注意,您可能也会尝试读取leftright(,因此没有理由测试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