Status CreateBiThrTree(BiThrTree *T) { /* 按先序输入二叉线索树中结点的值,构造二叉线索树T */ /* 0(整型)/空格(字符型)表示空结点 */ TElemType h; #if CHAR scanf("%c",&h); #else scanf("%d",&h); #endif if(h==Nil) *T=NULL; else { *T=(BiThrTree)malloc(sizeof(BiThrNode)); if(!*T) exit(OVERFLOW); (*T)->data=h; /* 生成根结点(先序) */ CreateBiThrTree(&(*T)->lchild); /* 递归构造左子树 */ if((*T)->lchild) /* 有左孩子 */ (*T)->LTag=Link; CreateBiThrTree(&(*T)->rchild); /* 递归构造右子树 */ if((*T)->rchild) /* 有右孩子 */ (*T)->RTag=Link; } return OK; }
/* bo6-3.c 二叉树的二叉线索存储(存储结构由c6-3.h定义)的基本操作,包括算法6.5~6.7 */ void CreateBiThrTree(BiThrTree *T) { /* 按先序输入线索二叉树中结点的值,构造线索二叉树T。0(整型)/空格(字符型)表示空结点 */ TElemType ch; scanf(form,&ch); if(ch==Nil) *T=NULL; else { *T=(BiThrTree)malloc(sizeof(BiThrNode)); /* 生成根结点(先序) */ if(!*T) exit(OVERFLOW); (*T)->data=ch; /* 给根结点赋植 */ CreateBiThrTree(&(*T)->lchild); /* 递归构造左子树 */ if((*T)->lchild) /* 有左孩子 */ (*T)->LTag=Link; /* 给左标志赋值(指针) */ CreateBiThrTree(&(*T)->rchild); /* 递归构造右子树 */ if((*T)->rchild) /* 有右孩子 */ (*T)->RTag=Link; /* 给右标志赋值(指针) */ } }
int main() { BiThrTree H,T; printf("请按前序输入二叉树(如:'ABDH##I##EJ###CF##G##')\n"); CreateBiThrTree(&T); /* 按前序产生二叉树 */ InOrderThreading(&H,T); /* 中序遍历,并中序线索化二叉树 */ printf("中序遍历(输出)二叉线索树:\n"); InOrderTraverse_Thr(H); /* 中序遍历(输出)二叉线索树 */ printf("\n"); return 0; }
/* 0(整型)/空格(字符型)表示空结点 */ Status CreateBiThrTree(BiThrTree *T) { TElemType h; scanf("%c",&h); if(h==Nil) *T=NULL; else { *T=(BiThrTree)malloc(sizeof(BiThrNode)); if(!*T) exit(OVERFLOW); (*T)->data=h; /* 生成根结点(前序) */ CreateBiThrTree(&(*T)->lchild); /* 递归构造左子树 */ if((*T)->lchild) /* 有左孩子 */ (*T)->LTag=Link; CreateBiThrTree(&(*T)->rchild); /* 递归构造右子树 */ if((*T)->rchild) /* 有右孩子 */ (*T)->RTag=Link; } return OK; }
int main() { BiThrTree Thrt, T, p, q; CreateBiThrTree(T); InOrderThreading(Thrt, T); // InOrderTraverse_Thr(Thrt); p = Thrt->lchild; q = InPostPre(Thrt, p); if(q != NULL) { printf("%c\n", q->data); }else { printf("该节点为后序的第一个节点,没有前驱\n"); }//注意这里不可能发生q==NULL,因为本程序在生成ThreadTree时,将其做成双向的,头结点也是尾节点,都有前驱 }
void main() { BiThrTree H,T; #if CHAR printf("请按先序输入二叉树(如:ab三个空格表示a为根结点,b为左子树的二叉树)\n"); #else printf("请按先序输入二叉树(如:1 2 0 0 0表示1为根结点,2为左子树的二叉树)\n"); #endif CreateBiThrTree(&T); /* 按先序产生二叉树 */ InOrderThreading(&H,T); /* 中序遍历,并中序线索化二叉树 */ printf("中序遍历(输出)二叉线索树:\n"); InOrderTraverse_Thr(H,vi); /* 中序遍历(输出)二叉线索树 */ printf("\n"); }
int main() { BiThrTree H, T; #if CHAR printf("请按先序输入二叉树(如:ab三个空格表示a为根结点,b为左子树的二叉树)\n"); #else printf("请按先序输入二叉树(如:1 2 0 0 0表示1为根结点,2为左子树的二叉树)\n"); #endif CreateBiThrTree(&T); InOrderThreading(&H, T); printf("中序遍历(输出)二叉线索树:\n"); InOrderTraverse_Thr(H, vi); printf("\n"); return 0; }
Status CreateBiThrTree(BiThrTree *T) { TElemType h; #if CHAR scanf("%c", &h); #else scanf("%d",&h); #endif if (h == Nil) *T = NULL; else { *T = (BiThrTree) malloc(sizeof(BiThrNode)); if (!*T) exit(OVERFLOW); (*T)->data = h; CreateBiThrTree(&(*T)->lchild); if ((*T)->lchild) (*T)->LTag = Link; CreateBiThrTree(&(*T)->rchild); if ((*T)->rchild) (*T)->RTag = Link; } return OK; }