代码改进,用printf代替cout。#include <iostream> #include <queue> #include <stack> #include <string> using namespace std; struct Student { string name; int number; int score; }; struct TreeNode { Student data; TreeNode* left; TreeNode* right; TreeNode(Student s) : data(s), left(nullptr), right(nullptr) {} }; void postOrder(TreeNode* root) { if (!root) return; postOrder(root->left); postOrder(root->right); cout << root->data.name << " " << root->data.number << " " << root->data.score << endl; } void preOrder(TreeNode* root) { stack<TreeNode*> s; s.push(root); while (!s.empty()) { TreeNode* node = s.top(); s.pop(); if (node) { cout << node->data.name << " " << node->data.number << " " << node->data.score << endl; s.push(node->right); s.push(node->left); } } } void levelOrder(TreeNode* root) { queue<TreeNode*> q; q.push(root); while (!q.empty()) { TreeNode* node = q.front(); q.pop(); if (node) { cout << node->data.name << " " << node->data.number << " " << node->data.score << endl; q.push(node->left); q.push(node->right); } } } int main() { Student students[] = { {"Lei Zhenzi", 101401, 82}, {"Jiang Ziya", 100032, 90}, {"Ne Zha", 101674, 70}, {"Shen Gongbao", 101982, 87}, {"Jiu Weihu", 107431, 75}, {"Tian Zun", 100001, 98}, {"Tai Yi", 101009, 81}, {"Yang Jian", 101321, 63}, {"Huang Feihu", 101567, 72}, {"Zhou Wang", 108160, 55}, {"Li Jing", 102456, 84}, {"Tu Xingsun", 102245, 65}, }; int n = sizeof(students) / sizeof(Student); TreeNode* root = new TreeNode(students[0]); queue<TreeNode*> q; q.push(root); for (int i = 1; i < n; i += 2) { TreeNode* parent = q.front(); q.pop(); parent->left = new TreeNode(students[i]); q.push(parent->left); if (i + 1 < n) { parent->right = new TreeNode(students[i + 1]); q.push(parent->right); } } cout << "Post-order traversal: " << endl; postOrder(root); cout << "Pre-order traversal: " << endl; preOrder(root); cout << "Level-order traversal: " << endl; levelOrder(root); return 0; }
时间: 2023-07-14 17:14:03 浏览: 86
好的,这里是使用 printf 替换 cout 后的代码:
#include <cstdio>
#include <queue>
#include <stack>
#include <string>
using namespace std;
struct Student {
string name;
int number;
int score;
};
struct TreeNode {
Student data;
TreeNode* left;
TreeNode* right;
TreeNode(Student s) : data(s), left(nullptr), right(nullptr) {}
};
void postOrder(TreeNode* root) {
if (!root) return;
postOrder(root->left);
postOrder(root->right);
printf("%s %d %d\n", root->data.name.c_str(), root->data.number, root->data.score);
}
void preOrder(TreeNode* root) {
stack<TreeNode*> s;
s.push(root);
while (!s.empty()) {
TreeNode* node = s.top();
s.pop();
if (node) {
printf("%s %d %d\n", node->data.name.c_str(), node->data.number, node->data.score);
s.push(node->right);
s.push(node->left);
}
}
}
void levelOrder(TreeNode* root) {
queue<TreeNode*> q;
q.push(root);
while (!q.empty()) {
TreeNode* node = q.front();
q.pop();
if (node) {
printf("%s %d %d\n", node->data.name.c_str(), node->data.number, node->data.score);
q.push(node->left);
q.push(node->right);
}
}
}
int main() {
Student students[] = {
{"Lei Zhenzi", 101401, 82},
{"Jiang Ziya", 100032, 90},
{"Ne Zha", 101674, 70},
{"Shen Gongbao", 101982, 87},
{"Jiu Weihu", 107431, 75},
{"Tian Zun", 100001, 98},
{"Tai Yi", 101009, 81},
{"Yang Jian", 101321, 63},
{"Huang Feihu", 101567, 72},
{"Zhou Wang", 108160, 55},
{"Li Jing", 102456, 84},
{"Tu Xingsun", 102245, 65},
};
int n = sizeof(students) / sizeof(Student);
TreeNode* root = new TreeNode(students[0]);
queue<TreeNode*> q;
q.push(root);
for (int i = 1; i < n; i += 2) {
TreeNode* parent = q.front();
q.pop();
parent->left = new TreeNode(students[i]);
q.push(parent->left);
if (i + 1 < n) {
parent->right = new TreeNode(students[i + 1]);
q.push(parent->right);
}
}
printf("Post-order traversal: \n");
postOrder(root);
printf("Pre-order traversal: \n");
preOrder(root);
printf("Level-order traversal: \n");
levelOrder(root);
return 0;
}
阅读全文