当前位置: 首页 > news >正文

c++可视化打印树

#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;
}

http://www.lryc.cn/news/442633.html

相关文章:

  • ElementUI 快速入门:使用 Vue 脚手架搭建项目
  • 算法打卡:第十一章 图论part02
  • 广度优先搜索算法及其matlab程序详解
  • 力扣 438找到字符串中所有字母异位词
  • 图像滤波---各项异性扩散滤波使用笔记及代码
  • 用Go语言构建健壮的并发系统:深入理解错误传播与处理
  • 掌握C#中的动态规划技术
  • C语言进阶【5】---数据在内存中的存储【2】(小数存储很难吗?)
  • 如何更新至CDS-Beta下载ERA5数据
  • SQL编程题复习(24/9/20)
  • react crash course 2024 (1)理论概念
  • 有关JS下隐藏的敏感信息
  • Kafka 基于SASL/SCRAM动态认证部署,kafka加账号密码登录部署
  • 富格林:积攒经验阻挠欺诈套路
  • 51单片机-红外遥控器(NEC标准)-实验(红外遥控及调速电机)
  • 云手机的便捷性和安全性体现在哪?
  • 漫谈由标准输入\输出\错误输出引发的思考
  • 利用 IDEA 快速管理 k8s 集群
  • 【自然语言处理】实验三:新冠病毒的FAQ问答系统
  • 「C++系列」文件和流
  • 视频美颜SDK核心功能解析:打造高效直播美颜工具方案详解
  • 深入解析:高性能 SSE 服务器的设计与实现
  • C#为任意组件开发登录功能的记录
  • AI免费UI页面生成
  • 2024新动态:低代码开发占领新常态市场
  • 【SQL 用大白话描述事务并发 可能会遇到的问题】及解决策略
  • nginx安装及vue项目部署
  • 第十三周:机器学习笔记
  • HarmonyOS学习(十三)——数据管理(二) 关系型数据库
  • 【工具变量】科技金融试点城市DID数据集(2000-2023年)