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;

}

相关推荐
liangbm310 分钟前
MATLAB系列02:MATLAB基础
开发语言·数据结构·笔记·matlab·教程·工程基础·高级绘图
change95131 小时前
PHP纯离线搭建(php 8.1.7)
开发语言·php
福鸦2 小时前
详解c++:new和delete
开发语言·c++
qq_172805592 小时前
Go Testify学习与使用
开发语言·golang·go
兩尛2 小时前
java--面向对象编程(中级部分)
java·开发语言
createcrystal2 小时前
《算法笔记》例题解析 第3章入门模拟--3图形输出(9题)2021-03-03
c++·笔记·算法
卡戎-caryon2 小时前
【Linux】09.Linux 下的调试器——gdb/cgdb
linux·运维·服务器·开发语言·笔记
Xxxx. .Xxxx3 小时前
C语言程序设计实验与习题指导 (第4版 )课后题-第二章+第三章
java·c语言·开发语言
逸狼3 小时前
【JavaEE初阶】多线程6(线程池\定时器)
java·开发语言·算法
tan77º4 小时前
【C++】异常
c++·算法