线索二叉树

来源:互联网 发布:淘宝店家开通蚂蚁花呗 编辑:程序博客网 时间:2024/06/07 00:59

头文件(线索二叉树结构体以及相关函数声明部分)

#ifndef TREAD#define THREADtypedef enum{link =0,thread =1} pointtag;typedef char elemtype;typedef struct bithrnode{bithrnode *leftchild;bithrnode *rightchild;pointtag ltag,rtag;elemtype data;}bithrnode,*bithetree;bithrnode *buynode();void freenode(bithrnode *ptr);bithrnode *createthr(char *&str);bithrnode *createthrtree(char *str);void inorder(bithrnode *ptr);void make(bithrnode *ptr,bithrnode *&p);void makethreadtree(bithrnode *ptr);bithrnode * first(bithrnode *p);bithrnode * next(bithrnode *p);void thrinorder(bithrnode *ptr);#endif

相关函数:

bithrnode *buynode(){bithrnode *s=(bithrnode *)malloc(sizeof(bithrnode));return s;}void freenode(bithrnode *ptr){free(ptr);}//ABC##DE##F##G#H##bithrnode *createthr(char *&str){bithrnode *s=NULL;if (*str!='#'){s=buynode();s->data=*str;s->ltag=s->rtag=link;s->leftchild=createthr(++str);s->rightchild=createthr(++str);}return s;}bithrnode *createthrtree(char *str){if (str==NULL){return NULL;}else{return createthr(str);}}void inorder(bithrnode *ptr){if (ptr!=NULL){inorder(ptr->leftchild);printf("%c ",ptr->data);inorder(ptr->rightchild);}}void makethreadtree(bithrnode *ptr){bithrnode *p=NULL;make(ptr,p);p->rightchild=NULL;p->rtag=thread;}void make(bithrnode *ptr,bithrnode *&p){if (ptr!=NULL){makethreadtree(ptr->leftchild);if (ptr->leftchild==NULL){ptr->leftchild=p;ptr->ltag=thread;}if (p!=NULL && p->rightchild==NULL){p->rightchild=ptr;p->rtag=thread;}p=ptr;makethreadtree(ptr->rightchild);}}bithrnode * first(bithrnode *p) {while(p!=NULL && p->ltag!=thread){p=p->leftchild;}return p;}bithrnode * next(bithrnode *p){if (p==NULL){return NULL;}if (p->rtag == thread){return p->rightchild;}else {return first(p->rightchild);}}void thrinorder(bithrnode *ptr){bithrnode *p=NULL;for (p=first(ptr);p!=NULL;p=next(p)){printf("%c",p->data);}printf("\n");}


原创粉丝点击