线索化二叉树的建立,遍历

来源:互联网 发布:无锡java招聘 编辑:程序博客网 时间:2024/05/21 07:52
#include<stdio.h>#define maxsize 50typedef struct threadnode{int data;threadnode *lchild, *rchild;int ltag, rtag;};//线索化递归void inthread(threadnode *p, threadnode *pre){if (p!= NULL){inthread(p->lchild, pre);if (p->lchild == NULL){p->lchild = pre;p->ltag = 1;}if (pre != NULL&&pre->rchild == NULL){pre->rchild=p;pre->rtag = 1;}pre = p;inthread(p->rchild, pre);}}//中序线索二叉树的建立void creatinthread(threadnode *t){threadnode *pre = NULL;if (t != NULL){inthread(t, pre);pre->rchild = NULL;pre->rtag = 1;}}//遍历threadnode * firstnode(threadnode *p){while (p->ltag == 0){p = p->lchild;}return p;}threadnode *nextnode(threadnode *p){if (p->rtag == 0){return firstnode(p->rchild);}else{return p->rchild;}}

0 0
原创粉丝点击