标签:
1 typedef enum 2 { 3 SubTree, //子树 4 Thread //线索 5 }NodeFlag; 6 7 typedef struct ThreadTree 8 { 9 DATA data; 10 NodeFlag lflag; 11 NodeFlag rflag; 12 struct ThreadTree *left; 13 struct ThreadTree *right; 14 }ThreadBinTree; 15 16 ThreadBinTree *Previous=NULL; 17 18 void BinTreeThreading_LDR(ThreadBinTree *bt) //二叉树按中序线索化 19 { 20 if(bt) 21 { 22 BinTreeThreading_LDR(bt->left); 23 bt->lflag=(bt->left)?SubTree:Thread; //判断左子树有没有孩子 24 bt->rflag=(bt->right)?SubTree:Thread; 25 if(Previous) 26 { 27 if(Previous->rflag==Thread) //判断前驱的右子树有没有孩子 28 Previous->right=bt; //把这个节点的地址赋给前驱的右子树 29 if(bt->lflag==Thread) //判断这个节点的左子树有没有孩子 30 bt->left=Previous; //把前驱保存在这个节点的左子树中 31 32 } 33 Previous=bt; 34 BinTreeThreading_LDR(bt->right); 35 } 36 } 37 ThreadBinTree *BinTreeNext_LDR(ThreadBinTree *bt) //求指定节点的后继 38 { 39 ThreadBinTree *nextnode; 40 if(!bt) 41 return NULL; 42 if(bt->rflag==Thread) 43 return bt->right; 44 else 45 { 46 nextnode=bt->right; 47 while(nextnode->lflag==SubTree) 48 { 49 nextnode=nextnode->left; 50 } 51 return nextnode; 52 } 53 } 54 ThreadBinTree *BinTreePrevious_LDR(ThreadBinTree *bt) 55 { 56 ThreadBinTree *prenode; 57 if(!bt) return NULL; 58 if(bt->lflag==Thread) 59 return bt->left; 60 else 61 { 62 prenode=bt->left; 63 while(prenode->rflag==SubTree) 64 prenode=prenode->left; 65 return prenode; 66 } 67 } 68 void ThreadBinTree_LDR(ThreadBinTree *bt,void(*oper)(ThreadBinTree *p)) //遍历线索二叉树 69 { 70 if(bt) 71 { 72 while(bt->lflat==SubTree) 73 bt=bt->left; //找最左下节点 74 do{ 75 oper(bt); 76 bt=BinTreeNext_LDR(bt); 77 }while(bt); 78 } 79 }
标签:
原文地址:http://www.cnblogs.com/threezj/p/4456718.html