C语言中如何利用递归实现线索二叉树
这篇“C语言中如何利用递归实现线索二叉树”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“C语言中如何利用递归实现线索二叉树”文章吧。
描述:将二叉树中结点的空左孩子指针域指向前驱结点,将空的右孩子指针域指向后继结点。
code:
#pragmawarning(disable:4996)#include<stdio.h>#include<stdlib.h>typedefstructTreeNode{chardata;structTreeNode*lchild,*rchild;intltag,rtag;}Tree,*BTree;BTreeBuild_Tree(void){BTreeT;charch;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();}returnT;}//先序线索化voidPre_Thread(BTreecur,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);}}//中序线索化voidIn_Thread(BTreecur,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);}}//后序线索化voidPost_Thread(BTreecur,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;}}intmain(void){BTreeT,p=NULL;T=Build_Tree();Pre_Thread(T,&p);//In_Thread(T,&p);//Post_Thread(T,&p);return0;}
以上就是关于“C语言中如何利用递归实现线索二叉树”这篇文章的内容,相信大家都有了一定的了解,希望小编分享的内容对大家有帮助,若想了解更多相关的知识内容,请关注恰卡编程网行业资讯频道。