【发布时间】:2013-07-30 05:55:09
【问题描述】:
也许是快速/简单的问题。我已经实现了一棵二叉树,然后我希望将二叉搜索树转换为数组,或者至少像在数组中一样打印出来。我遇到的问题是如何在 '\0' 中获取 NULL/标志。
例如,假设我有一棵树:
10
/ \
6 12
/ \ \
1 8 15
\
4
我希望它打印出它应该如何打印。喜欢:
[10,6,12,1,8,\0,15,\0,4,\0,\0,\0,\0,\0,\0]
^Something Like this^ I don't know if I counted the NULL correctly.
或者关于我想如何以可视方式显示我的树的另一个选项是如何正确输出间距,例如“/”和“\”指向父母的键:
10
/ \
6 12
/ \ \
1 8 15
\
4
这是我尝试在代码方面详细阐述的内容,但我卡住了:
void BreadthFirstTravseral(struct node* root)
{
queue<node*> q;
if (!root) {
return;
}
for (q.push(root); !q.empty(); q.pop()) {
const node * const temp_node = q.front();
cout<<temp_node->data << " ";
if (temp_node->left) {
q.push(temp_node->left);
}
if (temp_node->right) {
q.push(temp_node->right);
}
}
}
非常感谢任何类型的帮助或链接和/或建议和/或示例代码。
【问题讨论】:
标签: c++ binary-search-tree breadth-first-search