文章目录
- 1 参考博客:
- 2 线索二叉树的节点结构
- 3、分析他人博客
1 参考博客:
1、【图解】数据结构代码领背-中序建立线索二叉树
 2、【数据结构】中序线索二叉树的实现(源代码以及讲解)
2 线索二叉树的节点结构

 
3、分析他人博客
具体博客地址请参考:【数据结构】中序线索二叉树的实现(源代码以及讲解)
首先需要进行二叉树的创建。
#define _CRT_SECURE_NO_WARNINGS
//参考博客:
//https://zhuanlan.zhihu.com/p/590566514
//https://blog.csdn.net/Zhangsama1/article/details/121362652
#include <iostream>  //引入头文件
#include <cstring>  
using namespace std;
typedef char Elemtype;
#define Maxsize 5
#define STR_SIZE 1024
#define ERROR 0
#define OK    1
enum Tag { Link, Thead };
/*建立线索二叉树的结构体*/
typedef struct ThreadNode
{
	Elemtype data; //数据域
	struct ThreadNode* lchild;//左孩子指针域
	struct ThreadNode* rchild;//右孩子指针域
	int ltag;  //左标志位 0 1
	int rtag;  //右标志位 0 1
}ThreadNode, * ThreadTree;
void CreateBinaryTree(ThreadTree& T);
void draw_level(ThreadNode* node, bool left, char* str);
void draw(ThreadNode* root);
int main()
{
	ThreadTree T=NULL;
	CreateBinaryTree(T);
	draw(T);
	return 0;
}
//新建二叉树  ABD##E##C##
void CreateBinaryTree(ThreadTree& T)
{
	Elemtype ch;
	cin >> ch;
	if (ch == '#')
		T = NULL;
	else
	{
		T = (ThreadTree)malloc(sizeof(ThreadNode));
		if (T == NULL)
			exit(false);
		T->data = ch;
		T->ltag = Link;   //初始化设定左右子树为指针类型
		T->rtag = Link;
		CreateBinaryTree(T->lchild);  //构建左子树
		CreateBinaryTree(T->rchild);   //构建右子树
	}
}
/*****************************************************************************
* @date   2020/4/19
* @brief  水平画树
* @param  node	二叉树节点
* @param  left	判断左右
* @param  str 	可变字符串
*****************************************************************************/
void draw_level(ThreadNode* node, bool left, char* str) {
    if (node->rchild) {
        draw_level(node->rchild, false, strcat(str, (left ? "|     " : "      ")));
    }
    printf("%s", str);
    printf("%c", (left ? '\\' : '/'));
    printf("-----");
    printf("%c\n", node->data);
    if (node->lchild) {
        draw_level(node->lchild, true, strcat(str, (left ? "      " : "|     ")));
    }
    //  "      " : "|     " 长度为 6
    str[strlen(str) - 6] = '\0';
}
/*****************************************************************************
* @date   2020/4/19
* @brief  根节点画树
* @param  root	二叉树根节点
*****************************************************************************/
void draw(ThreadNode* root) {
    char str[STR_SIZE];
    memset(str, '\0', STR_SIZE);
    /**
     * 1. 在 windows 下,下面是可执行的
     * 2. 在 Linux   下,执行会报 Segmentation fault
     *      需要使用中间变量
     */
    if (root->rchild) {
        draw_level(root->rchild, false, str);
    }
    printf("%c\n", root->data);
    if (root->lchild) {
        draw_level(root->lchild, true, str);
    }
}

 



















