c语言 线索二叉树的中序遍历代码
能够运行的线索二叉树代码,保证看得懂,后期会专门出一篇文章讲线索二叉树是如何遍历的
这里注意:pre定义为全局变量
Node *pre=NULL; void inthread(Node *p) { if(p!=NULL) { inthread(p->left); if(p->left==NULL) { p->ltag=1; p->left=pre; } if(pre!=NULL&&pre->right==NULL) { pre->rtag=1; pre->right=p; } pre=p; inthread(p->right); } } void creatInthread(Node *node) { if(node!=NULL) { inthread(node); pre->right=NULL; pre->rtag=1; } } Node* next(Node *p) { Node *q=NULL; if(p->rtag==1) q=p->right; else { q=p->right; while(q->ltag==0) q=q->left; } return q; } void RunInthread(Node *node) { if(node==NULL) return; Node *p=node; if(p!=NULL) { while(p->ltag==0) { p=p->left; } printf("%d\t",p->data); while(p->right!=NULL) { p=next(p); printf("%d\t",p->data); } } }
相关推荐
boneix 2020-10-21
seanzed 2020-10-15
ifconfig 2020-10-14
学留痕 2020-09-20
往后余生 2020-09-17
kka 2020-09-14
redis 2020-09-07
lzccheng 2020-09-06
soyo 2020-08-31
stonerkuang 2020-08-18
LxyPython 2020-08-17
raksmart0 2020-08-17
Lzs 2020-08-14
MrHaoNan 2020-07-31
80530895 2020-07-05
lengyu0 2020-06-28
YarnSup 2020-06-28
huanglianhuabj00 2020-06-27