打印一颗满二叉树
效果如图:
先看main()函数
int main() {
node* T;//定义一个T作为树根节点
createTree(T);//生成一棵树
cout << "树的深度:" << depth(T) << endl;
print(T);//将这棵树打印出来
return 0;
}
- node节点定义
struct node {
int date;
char info;//节点信息
node* left;
node* right;
};
- 构建树的方式,以前序序列输入一颗树的节点,对应的null用“#”表示
//以递归的方式构建一棵树,
void createTree(node*& t) {
char str;
cin >> str;
if (str == '#') {
t = NULL;
}
else {
t = new node;//为t开辟空间
t->info = str;
createTree(t->left);
createTree(t->right);
}
}
-
求树的深度
int depth(node* root) { if (root == nullptr) { return 0; } int left = depth(root->left); int right = depth(root->right); return max(left, right) + 1; }
-
打印树文章来源:https://www.toymoban.com/news/detail-757371.html
感觉太繁琐了,直接拿去用就行,不需要知道细节。文章来源地址https://www.toymoban.com/news/detail-757371.html
//打印一棵满二叉树,只能打印满二叉树,节点数目最好不要超过10 void print(node*& root) { //存放打印的二叉树 char str[10][100] = {}; queue<node*> q; int h = depth(root); q.push(root); int index = 0; while (!q.empty()) { int size = q.size(); //存放每一层的节点 vector<char> list; for (int i = 0; i < size; i++) { node* temp = q.front(); q.pop(); list.push_back(temp->info); //cout << temp->info; if (temp->left != nullptr) { q.push(temp->left); } if (temp->right != nullptr) { q.push(temp->right); } } bool flag = true; int j = 0; //打印前面部分空白 while (j <= 2*h-1-index) { str[index][j] = ' '; j++; } //保持第一行居中 if (index == 0) { for (int m = 0; m < h - 2; m++) { str[index][j++] = ' '; } } for (int k = 0; k < list.size(); k++) { //如果是一层最后一个节点 if (k == list.size() - 1) { str[index][j++] = list[k]; } else { //相邻左右子节点 if (k % 2 == 0) { str[index][j++] = list[k]; for (int l = 0; l < 3 + 2 * (h - index/2-1); l++) { str[index][j++] = ' '; } } else { str[index][j++] = list[k]; str[index][j++] = ' '; } } } index+=2; //cout << endl; } for (int i = 0; i < 10; i++) { if (i % 2 == 1) { for (int j = 0; j < 100; j++) { str[i][j] = ' '; } } } for (int i = 0; i < 10; i++) { if (i % 2 == 0) { for (int j = 0; j < 100; j++) { if (str[i][j] - '0' >= 0 && str[i][j] - '0' <= 9 && i < 2*h -2) { str[i + 1][j - 1] = '/'; str[i + 1][j + 1] = '\\'; } } } } for (int i = 0; i < 10; i++) { for (int j = 0; j < 100; j++) { cout << str[i][j]; } cout << endl; } }
完整代码
#include <iostream>
#include<algorithm>
#include<cmath>
#include <queue>
using namespace std;
struct node {
int date;
char info;
node* left;
node* right;
};
//以递归的方式构建一棵树
void createTree(node*& t) {
char str;
cin >> str;
if (str == '#') {
t = NULL;
}
else {
t = new node;//为t开辟空间
t->info = str;
createTree(t->left);
createTree(t->right);
}
}
//树的深度
int depth(node* root) {
if (root == nullptr) {
return 0;
}
int left = depth(root->left);
int right = depth(root->right);
return max(left, right) + 1;
}
//打印一棵树满二叉树,只能打印满二叉树,节点数目最好不要超过10
void print(node*& root) {
//存放打印的二叉树
char str[10][100] = {};
queue<node*> q;
int h = depth(root);
q.push(root);
int index = 0;
while (!q.empty()) {
int size = q.size();
//存放每一层的节点
vector<char> list;
for (int i = 0; i < size; i++) {
node* temp = q.front();
q.pop();
list.push_back(temp->info);
//cout << temp->info;
if (temp->left != nullptr) {
q.push(temp->left);
}
if (temp->right != nullptr) {
q.push(temp->right);
}
}
bool flag = true;
int j = 0;
//打印前面部分空白
while (j <= 2*h-1-index) {
str[index][j] = ' ';
j++;
}
//保持第一行居中
if (index == 0) {
for (int m = 0; m < h - 2; m++) {
str[index][j++] = ' ';
}
}
for (int k = 0; k < list.size(); k++) {
//如果是一层最后一个节点
if (k == list.size() - 1) {
str[index][j++] = list[k];
}
else {
//相邻左右子节点
if (k % 2 == 0) {
str[index][j++] = list[k];
for (int l = 0; l < 3 + 2 * (h - index/2-1); l++) {
str[index][j++] = ' ';
}
}
else {
str[index][j++] = list[k];
str[index][j++] = ' ';
}
}
}
index+=2;
//cout << endl;
}
for (int i = 0; i < 10; i++) {
if (i % 2 == 1) {
for (int j = 0; j < 100; j++) {
str[i][j] = ' ';
}
}
}
for (int i = 0; i < 10; i++) {
if (i % 2 == 0) {
for (int j = 0; j < 100; j++) {
if (str[i][j] - '0' >= 0 && str[i][j] - '0' <= 9 && i < 2*h -2) {
str[i + 1][j - 1] = '/';
str[i + 1][j + 1] = '\\';
}
}
}
}
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 100; j++) {
cout << str[i][j];
}
cout << endl;
}
}
int main() {
node* T;
createTree(T);
cout << "树的深度:" << depth(T) << endl;
print(T);
return 0;
}
到了这里,关于构建一颗二叉树,并将其打印出来(c++实现)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!