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; s[i] != '\0'; i++) {

switch (s[i]) {

case ',':

break;

case '(':

depth++;

break;

case ')':

depth--;

break;

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

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

temp->c = s[i];

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 s[MAX];

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;

}

相关推荐
крон1 小时前
【Auto.js例程】华为备忘录导出到其他手机
开发语言·javascript·智能手机
zh_xuan2 小时前
c++ 单例模式
开发语言·c++·单例模式
老胖闲聊2 小时前
Python Copilot【代码辅助工具】 简介
开发语言·python·copilot
Blossom.1182 小时前
使用Python和Scikit-Learn实现机器学习模型调优
开发语言·人工智能·python·深度学习·目标检测·机器学习·scikit-learn
曹勖之3 小时前
基于ROS2,撰写python脚本,根据给定的舵-桨动力学模型实现动力学更新
开发语言·python·机器人·ros2
豆沙沙包?3 小时前
2025年- H77-Lc185--45.跳跃游戏II(贪心)--Java版
java·开发语言·游戏
军训猫猫头4 小时前
96.如何使用C#实现串口发送? C#例子
开发语言·c#
liuyang-neu4 小时前
java内存模型JMM
java·开发语言
利刃大大4 小时前
【在线五子棋对战】二、websocket && 服务器搭建
服务器·c++·websocket·网络协议·项目
喜欢吃燃面4 小时前
C++刷题:日期模拟(1)
c++·学习·算法