以C++打印BST的功能
Function to print BST in C++
Helo, 我正在寻找帮助,以尽可能轻松地编写我理解以漂亮的方式打印出BST的功能。喜欢
50
/
30 70
这是我将元素添加到树的代码:
#include <iostream>
using namespace std;
struct node
{
int key;
struct node *left;
struct node *right;
};
struct node *root;
struct node *make_leaf(int new_data);
struct node *add_node(struct node* root, int key);
int main()
{
node *root = NULL;
int new_element, how;
cout<<"How many elements?"<<endl;
cin>>how;
for(int i=0; i<how; ++i){
cout<<"Enter element value"<<endl;
cin>>new_element;
root = add_node(root, new_element);
}
return 0;
}
struct node* make_leaf(int new_data){
node *nd=new node;
nd->key=new_data;
nd->left=NULL;
nd->right=NULL;
return nd;
}
struct node *add_node(struct node* root, int key){
if (root==NULL)
{
return make_leaf(key);
}
else
{
if (root->key > key)
{
root->left = add_node(root->left, key);
}
else
{
root->right = add_node(root->right, key);
}
}
return root;
}
我正在寻求帮助,但我正在开始我的编程冒险,所以请不要生我的气:)谢谢!
编辑
我已经尝试了下面的函数,但它没有在顶部写入节点根,因为它应该是:/
void postorder(struct node * root, int indent)
{
if(root != NULL) {
if(root->right) {
postorder(root->right, indent+4);
}
if (indent) {
cout << setw(indent) << ' ';
}
if (root->right) cout<<" /n" << setw(indent) << ' ';
if(root->left) {
cout << setw(indent) << ' ' <<" \n";
postorder(root->left, indent+4);
}
}
}
new是保留关键字。 请将变量"new"的名称更改为其他名称。
将"新建"更改为"新建1"编译程序。
相关文章:
- 在执行其他功能的同时播放动画(LED矩阵和Arduino/ESP8266)
- 多态性和功能结合
- 带内存和隔离功能的SQLite
- 在CMakeLists.txt的安装功能中使用.cmake文件有什么用
- 类模板的成员功能的定义在单独的TU中完全专业化
- 有没有一种方法可以创建一个带有哈希表的数据库,该哈希表具有恒定时间查找功能
- 如何在BST的这个简单递归实现中消除警告
- 如何在C++中获得"静态纯虚拟"功能?
- 两个文件使用彼此的功能-如何解决
- 我应该实现右值推送功能吗?我应该使用std::move吗
- QML按钮点击功能执行顺序
- 无法理解此 return 语句的功能,没有它就会发生运行时错误
- 有没有可能有一个只有ADL才能找到的非好友功能
- 功能样式转换从 'int' 到 'ItemType' 的匹配转换
- 文件系统:复制功能的速度秘诀是什么
- 在用于格式4的arm模拟器中实现功能时的一个问题
- 如何在Directwrite中获得给定字体的可用OpenType功能
- 以C++打印BST的功能
- 我在BST中的删除功能甚至不起作用
- BST的预购功能