简体   繁体   English

给定树中每个节点的结构,该结构如何在层上打印树,其中该结构包含节点的值及其父节点的值

[英]How to print a tree on levels, given a struct that holds the value of the node and the value of it's parent, for each node in the tree

I have an array, where the index is the data of the node, and the value at that index is the parent of that node. 我有一个数组,其中的索引是节点的数据,而该索引处的值是该节点的父节点。

array = {2, 7, 5, 2, 7, 7, -1, 5, 2} 数组= {2,7,5,2,7,7,-1,5,2}
data ---- 1, 2, 3, 4, 5, 6, 7, 8, 9 数据---- 1,2,3,4,5,6,7,8,9

I have to print this array as such: 我必须这样打印此数组:

7  
 2
  1
  4
  9
 5
  3
  8
 6

I have a struct that has the data from the array in two fields, value and parent, and than i created an array of t that holds those attributes for each node in the tree. 我有一个结构,该结构在两个字段(值和父级)中都有来自数组的数据,然后我创建了一个t数组,其中包含树中每个节点的那些属性。

struct t{
    int value;
    int parent;
};
t tree[n];

Here is what I came up with for now. 这是我现在想出的。 This prints the nodes in the correct order, but I have no idea how to add the spaces (" "), in order to highlight the levels of the tree. 这将以正确的顺序打印节点,但是我不知道如何添加空格(“”)以突出显示树的级别。 I hope you guys can give me some ideas. 我希望你们能给我一些想法。

void preetyPrint(t tree[], int n, int root)
{
    cout << root;
    cout << endl;

    for(int i = 0; i < n; i++)
    {
        if (tree[i].parent == root)
        {
            preetyPrint(tree, n, tree[i].value);
        }
    }
}

I'm sorry about the ambiguity of this question, I'm new here. 我很抱歉这个问题的模棱两可,我是新来的。 Still learning to post good questions. 仍在学习发布好问题。

There are two approaches to this: 有两种解决方法:

The int depth approach int depth方法

You need to pass one more parameter to your function, an int depth . 您需要再向函数传递一个参数int depth

When you call it from outside code, pass 0 for depth. 当您从外部代码调用它时,请传递0作为深度。

Then, in the recursive call inside prettyPrint() , invoke with depth + 1 . 然后,在prettyPrint()内的递归调用中,使用depth + 1调用。

Then, in your cout << root; 然后,在您的cout << root; replace it with cout << spaces << root; cout << spaces << root;替换它cout << spaces << root;

And add some code which computes a spaces string to consist of a number of space characters. 并添加一些代码,该代码计算由多个空格字符组成的spaces字符串。 The number of spaces should be depth multiplied by whatever you want your indentation to be. 空格数应乘以depth乘以您希望缩进的大小。 (Say, 2, or 4.) The calculation of the spaces string is left as an exercise to the reader. (说2或4。) spaces字符串的计算留给读者练习。

The string indentation approach string indentation方法

You need to pass one more parameter to your function, a string indentation . 您需要再向函数传递一个参数,即string indentation

When you call it from outside code, pass an empty string. 当您从外部代码调用它时,请传递一个空字符串。

Then, in the recursive call inside prettyPrint() , invoke with indentation + " " (assuming that you want each indentation level to be 2 characters wide.) 然后,在prettyPrint()内的递归调用中,使用indentation + " "调用(假设您希望每个缩进级别为2个字符宽。)

Then, in your cout << root; 然后,在您的cout << root; replace it with cout << indentation << root; cout << indentation << root;替换它cout << indentation << root;

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

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