简体   繁体   English

在Turbo C中以树格式打印BST

[英]printing a BST in a tree format in turbo c

I want to print the contents of my BST in a tree format like: 我想以树格式打印BST的内容,例如:

my current print output: 我当前的打印输出:

10 -> 20 -> 30 -> 40 -> 60 -> 80 -> 90 -> 10-> 20-> 30-> 40-> 60-> 80-> 90->

what i want it to be like: 我想要它是什么样的:

        40
        /\
       /  \
     20    60
     / \    \
   10  30   80
              \
              90

I tried doing some gotoxy but for some reason i just cant get it to print like a tree, i think i need to do more than just gotoxy though. 我试着做些gotoxy,但是由于某种原因,我无法像树一样打印出来,我想我需要做的不只是gotoxy。 Also "\\" is not really necessary just an added feature to not confuse any of you. 同样,“ \\”并不是真正必需的,只是一个附加功能,不会使任何人感到困惑。

The Code are as follows: 守则如下:

Structure: 结构体:

struct btnode
{
    int value;
    struct btnode *l;
    struct btnode *r;
}*root = NULL, *temp = NULL, *t2, *t1;

print: 打印:

void inorder(struct btnode *t)
{
    if (root == NULL)
    {
        printf("No elements in a tree to display");
        return;
    }
    if (t->l != NULL)    
        inorder(t->l);
        printf("%d -> ", t->value);
    if (t->r != NULL)    
        inorder(t->r);
}

my attempt in tree printing: 我在树打印中的尝试:

void print(struct btnode *t, int x, int i, int y)
{
    i = i / 2 + 2;
    if (root == NULL)
    {
        printf("No elements in a tree to display");
        return;
    }

    if (t->l != NULL){
        print(t->l, (x + i), i, (y + 1));
    }

    if (t->r != NULL){
        print(t->r, (x + i), i, (y + 1));
    }
    gotoxy(x, y * 2);
    printf("%d -> ", t->value);
}

Any idea on how I can achieve the tree output base on my current output code, though I assume that i need to do some more if and else to turn it as a tree. 关于如何可以基于当前输出代码实现树输出的任何想法,尽管我认为如果要把树变成树,还需要做更多的工作。 anything would be of help, a guide or an idea would really be appreciated. 任何事情都会有所帮助,一个指南或一个想法将不胜感激。

thank you 谢谢

Tricky question. 棘手的问题。 Let's first look at a simpler problem: How to print the tree horizontally with the root to the left and the branches growing towards the right. 首先让我们看一个更简单的问题:如何水平打印树,使树的根向左,分支向右生长。 This can be done without cursor positioning in the console window by keeping track of the indentation level: 通过跟踪缩进级别,可以在不将光标定位在控制台窗口中的情况下完成此操作:

void horizontal(struct btnode *t, int level)
{
    int l = level;

    if (t == NULL) return;

    horizontal(t->l, level + 1);
    while (l--) printf("    ");
    printf("-> %d\n", t->value);
    horizontal(t->r, level + 1);
}

Printing the tree from top to bottom is similar. 从上到下打印树是相似的。 The indentation is now the position from the top. 缩进现在是从顶部开始的位置。 The tricky part is how to advance printing to the right. 棘手的部分是如何使打印前进到右边。 In the simple console example, that is done by printing a new line. 在简单的控制台示例中,这是通过打印新行来完成的。 Here, we must advance the x position. 在这里,我们必须前进x位置。 This can be done with a global variable x , but you can also keep the state in a variable pointed to in the print function: 这可以使用全局变量x来完成,但是您也可以将状态保留在print函数中指向的变量中:

void print(struct btnode *nd, int *x, int y)
{    
    if (nd == NULL) return;

    print(nd->l, x, y + 4);
    gotoxy(*x, y);
    printf("%d", nd->value);
    *x += 4;
    print(nd->r, x, y + 4);
}

Call the print function like this: 像这样调用print函数:

int x = 0;
print(root, &x, 0);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM