c++可视化打印树

cpp 复制代码
#include <iostream>
#include <string>
 
// 定义节点结构体
struct Node {
    std::string data;
    Node* left;
    Node* right;
 
    Node(const std::string& data) : data(data), left(nullptr), right(nullptr) {}
};
 
// 递归打印树
void printTree(Node* root, std::string indent = "") {
    if (root == nullptr) {
        return;
    }
    std::cout << indent << "|-- " << root->data << std::endl;
    if (root->left != nullptr) {
        std::string newIndent = indent + "   ";
        printTree(root->left, newIndent);
    }
    if (root->right != nullptr) {
        std::string newIndent = indent + "   ";
        printTree(root->right, newIndent);
    }
}
 
int main() {
    // 创建树
    Node* root = new Node("A");
    root->left = new Node("B");
    root->right = new Node("C");
    root->left->left = new Node("D");
    root->left->right = new Node("E");
    root->right->left = new Node("F");
    root->right->right = new Node("G");
 
    // 打印树
    printTree(root);
 
    // 清理内存
    delete root;
 
    return 0;
}

这段代码定义了一个简单的二叉树结构体

Node和一个递归函数

printTree来打印树。它演示了如何使用C++创建和操作树形结构,并以可视化的方式打印出来。在

main函数中,我们创建了一棵树,并调用

printTree函数来打印它。最后,我们释放了为树节点分配的内存。

#include <iostream>

#include <string>

// 定义节点结构体

struct Node {

std::string data;

Node* left;

Node* right;

Node(const std::string& data) : data(data), left(nullptr), right(nullptr) {}

};

// 递归打印树

void printTree(Node* root, std::string indent = "") {

if (root == nullptr) {

return;

}

std::cout << indent << "|-- " << root->data << std::endl;

if (root->left != nullptr) {

std::string newIndent = indent + " ";

printTree(root->left, newIndent);

}

if (root->right != nullptr) {

std::string newIndent = indent + " ";

printTree(root->right, newIndent);

}

}

int main() {

// 创建树

Node* root = new Node("A");

root->left = new Node("B");

root->right = new Node("C");

root->left->left = new Node("D");

root->left->right = new Node("E");

root->right->left = new Node("F");

root->right->right = new Node("G");

// 打印树

printTree(root);

// 清理内存

delete root;

return 0;

}

#include <cstring>

#include <cstdio>

#include <cstdlib>

#include <queue>

#include <stack>

#define MAX 200

using namespace std;

typedef struct TreeNode {

char c;

int depth;

struct TreeNode * FirstChild;

struct TreeNode * NextSibling;

} *TREE, tree;

/* 传入参数为:

* 层序输入的字符串

* 根节点T(实际上没有存数据,深度已设为零) */

TREE BuildTree (char s\[\], TREE root) {

queue <TREE> Q;

stack <TREE> S;

S.push(root);

int depth = 0; //用于更新当前遍历到的深度

/* 将字母改成树的节点

* 并依次将每个节点入队 */

for(int i = 0; si != '\0'; i++) {

switch (si) {

case ',':

break;

case '(':

depth++;

break;

case ')':

depth--;

break;

default: //只有可能为节点字母了

TREE temp = (TREE)malloc(sizeof(tree)); //申请一个节点

temp->c = si;

temp->depth = depth; //记录节点的深度

temp->FirstChild = temp->NextSibling =NULL;

Q.push(temp); //节点入队

}

}

/* 利用栈建树

* 注意:最开始栈内已经加入一个元素root,其深度为0

* 其他节点已经在队列中,深度 >= 1 */

while(!Q.empty()) {

//依次将队头节点与栈顶结点比较:

if(Q.front()->depth > S.top()->depth) {

S.top()->FirstChild = Q.front(); //队头挂在栈顶的左儿子

S.push(Q.front()); //队头入栈

Q.pop(); //出队

}

else if(Q.front()->depth == S.top()->depth) {

S.top()->NextSibling = Q.front(); //队头挂在栈顶的右兄弟

S.push(Q.front()); //队头入栈

Q.pop(); //出队

}

else {

//出栈,一直到栈顶节点深度不小于队头节点

while (Q.front()->depth < S.top()->depth) {

S.pop();

}

}

}

return root;

/* root是传入的参数

* 通过此函数 已经将待建立的树

* 完整地挂在了root的左儿子上 */

}

/* 传入树的某个节点 */

void printLine(TREE T) {

for (int i = 0; i < T->depth - 1; i++) {

printf(" "); //多一层则多四个空格凹进

}

printf("%c\n", T->c);

}

/* 传入树的根节点 */

void printTree(TREE root) {

//递归的终点,空树不打印

if (!root) {

return;

}

//打印根节点

printLine(root);

/* 依次打印每个子树 */

TREE p = root->FirstChild;

while(p) {

printTree(p);

p = p->NextSibling;

}

}

int main () {

char sMAX;

scanf("%\^\\n",s);

//gets(s);

TREE T = (TREE) malloc(sizeof(tree));

T->depth = 0;

T->FirstChild = T->NextSibling = NULL;

//建树

TREE root = BuildTree(s, T)->FirstChild;

//输出树

printTree(root);

return 0;

}

相关推荐
http阿拉丁神猫4 小时前
Go语言入门笔记:从基础到开发环境搭建(二)
开发语言·笔记·golang
不会就选b5 小时前
C++补充部分(一)--lambda 表达式
开发语言·c++
Pniubi8 小时前
HashMap的扩容和树化边界
java·开发语言
gCode Teacher 格码致知9 小时前
Pandas教学-17:Series与Dataframe的关系
开发语言·python·pandas
发量惊人的中年网工10 小时前
分公司访问总部ERP慢怎么办?SD-WAN组网的排查与验收要点
服务器·开发语言·php
下页、再停留10 小时前
【C#桌面客户端系列学习-15】生成可视化图表之折线图、柱状图、饼状图
开发语言·c#
ShineWinsu10 小时前
对于JavaScript:操作元素的解析
开发语言·前端·javascript·面试·web·dom·元素
益莱储中国11 小时前
深度解析:RF 实验室从 5G迈向 6G 的过程中保持技术领先与设备升级的路径
开发语言·php
丰色木夕12 小时前
Ubuntu22.04下源码编译安装USD-Brep
c++·ubuntu·nvidia·usd-brep
benchmark_cc12 小时前
Python 量化系统如何设计统一的数据访问接口:从策略解耦到数据层架构
开发语言·python·数据分析·pandas·量化交易·股票数据·quantdash