C语言中怎么利用递归实现线索二叉树
本篇文章为大家展示了C语言中怎么利用递归实现线索二叉树,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
创新互联-云计算及IDC服务提供商,涵盖公有云、IDC机房租用、简阳服务器托管、等保安全、私有云建设等企业级互联网基础服务,咨询电话:18980820575
描述:将二叉树中结点的空左孩子指针域指向前驱结点,将空的右孩子指针域指向后继结点。
code:
#pragma warning(disable:4996) #include#include typedef struct TreeNode { char data; struct TreeNode *lchild, *rchild; int ltag, rtag; }Tree,*BTree; BTree Build_Tree(void) { BTree T; char ch; scanf("%c", &ch); if (ch == '#') { T = NULL; } else { T = (BTree)malloc(sizeof(Tree)); T->data = ch; T->ltag = 0; T->rtag = 0; T->lchild = Build_Tree(); T->rchild = Build_Tree(); } return T; } //先序线索化 void Pre_Thread(BTree cur, BTree *pre) { if (cur && cur->ltag==0) { printf("%c ", cur->data); if (cur->lchild == NULL) { cur->lchild = *pre; (*pre)->ltag = 1; cur->ltag = 1; } if (cur->rchild == NULL) { cur->rtag = 1; } if (*pre && (*pre)->rtag == 1) { (*pre)->rchild = cur; } *pre = cur; Pre_Thread(cur->lchild, pre); Pre_Thread(cur->rchild, pre); } } //中序线索化 void In_Thread(BTree cur, BTree *pre) { if (cur) { In_Thread(cur->lchild, pre); printf("%c ", cur->data); if (cur->lchild==NULL) { cur->lchild = *pre; cur->ltag = 1; } if (cur->rtag == NULL) { cur->rtag = 1; } if (*pre && (*pre)->rtag == 1) { (*pre)->rchild = cur; } *pre = cur; In_Thread(cur->rchild, pre); } } //后序线索化 void Post_Thread(BTree cur, BTree *pre) { if (cur) { Post_Thread(cur->lchild, pre); Post_Thread(cur->rchild, pre); printf("%c ", cur->data); if (cur->lchild == NULL) { cur->lchild = *pre; cur->ltag = 1; } if (cur->rchild == NULL) { cur->rtag = 1; } if (*pre && (*pre)->rtag == 1) { (*pre)->rchild = cur; } *pre = cur; } } int main(void) { BTree T,p=NULL; T = Build_Tree(); Pre_Thread(T, &p); //In_Thread(T, &p); //Post_Thread(T, &p); return 0; }
上述内容就是C语言中怎么利用递归实现线索二叉树,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注创新互联行业资讯频道。
网页名称:C语言中怎么利用递归实现线索二叉树
标题路径:http://ybzwz.com/article/ppidgo.html