打印特定格式图二叉搜索树。在C [英] Printing a Binary search Tree in a certain format diagram. in c

查看:129
本文介绍了打印特定格式图二叉搜索树。在C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要打印二叉搜索树,它应该看起来像一棵树意味着,如果我有一棵树是5,6,7打印功能将打印像的例1

I need to print a Binary search tree and it should look like a tree means if I have a tree which is 5,6,7 the printing function will print it like Example 1:

insert
5
insert
6
insert
7
Tree is:
    5
        6
            7

现在让我们说一棵树是4,3,7的结果应该是像
例2

Now lets say a tree is 4,3,7 the result should be like Example 2:

insert
4
insert
3
insert
7
tree is:
    4
3       7

有1限制:应该完成递归

There is 1 restriction: it should be done recursively.

这是code我试图解决这个问题:

This is the code I tried to solve this problem with:

void PrintTabs(int n)
{
  if(n==0)
  {
      return;
  }
  else
  {
      printf("\t");
      PrintTabs(--n);
  }
}

void PrintTree(BST* root, int level)
{
     if (root==NULL)
     {
         return;
     }
     PrintTree(root->Right,++level);
     PrintTabs(level);
     printf("%d\n",*(int*)root->Value);
     PrintTree(root->Left,++level);
}

我的2个主要问题,它总是打印滑吧,所以我把它给了我一个糟糕的结果,但不知何故,有我看了

My 2 main issues are it always printed sliding right, so I moved the printing section between the two recursive calls it had given me a bad result but somehow it had a format of the tree I looked for

推荐答案

//这是用来寻找从根本上最左边的节点(即,多少左图)。这个计算使根在中心印刷

// This is used to find the leftmost node from the root (i.e, how much left). This is computed so that root is printed in the center.

findAlignment (BST * root, int *leftMost, int leftness) {

    if (root == NULL) {
        return;
    }

    if (leftness > *leftMost) {
        *leftMost = leftness;
    }

    findAlignment (root->left, leftMost, (leftness + 1));
    findAlignment (root->right, leftMost, (leftness - 1));

}

//这将打印使用最左边的节点信息的树。它调整的基础上水平和节点的leftness直接打印光标的位置。

// This prints the tree using the leftmost node info. It adjusts the cursor position based on level and the leftness of the node to directly print.

void PrintTree(BST* root, int leftAlignment, int level)
{
     if (root==NULL)
     {
         return;
     }

     // the first printf aligns the position of cursor on the screen.
     // This code may not be portable on all machines.
     // see http://c-faq.com/osdep/termcap.html for details.
     // This particular print moves the cursor to row: 'level' and col: 'leftAlignment * 4'. 
     // You can change the multiplication factor from 4 based on
     // how many chars root->value will have and your other requirements to make it properly align.
     // You can also multiply level by some factor, if you want to align better.
     printf("\033[%d;%dH", level, leftAlignment * 4);
     printf("%d",root->Value);

     PrintTree(root->Left, leftAlignment - 1, level + 1);
     PrintTree(root->Right, leftAlignment + 1, level + 1);
}

int leftMost = 0;
findAlignment (root, &leftMost, 0);
printf("\033[2J"); // clear screen
printTree (root, leftMost, 0);

这篇关于打印特定格式图二叉搜索树。在C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆