二叉树是一种非线性结构,遍历二叉树几乎都是通过递归或者用栈辅助实现非递归的遍历。用二叉树作为存储结构时,取到一个节点,只
能获取节点的左孩子和右孩子,不能直接得到节点的任一遍历序列的前驱或者后继。
为了保存这种在遍历中需要的信息,我们利用二叉树中指向左右子树的空指针来存放节点的前驱和后继信息。
enum PointerTag
{
LINK, //0
THREAD //1
};
template <typename T>
struct BitreeNodeTH
{
T Data;
BitreeNodeTH<T> *left; //左孩子
BitreeNodeTH<T> *right; //右孩子
PointerTag left_Tag; //左孩子线索标志
PointerTag right_Tag; //右孩子线索标志
BitreeNodeTH(T data = T())
:Data(data)
,left(NULL)
,right(NULL)
,left_Tag(LINK)
,right_Tag(LINK)
{}
};
一个线索二叉树的节点:
left | left_Tag | Data | reght_Tag | reght |
线索化二叉树的主要源代码:
建树:
BitreeNodeTH<T>* Create_tree(T *arr,int sz,int &i) { if(*arr==NULL||arr[i]=='#'||i>=sz) return NULL; else { BitreeNodeTH<T> *cur=new BitreeNodeTH<T>; cur->Data=arr[i]; i++; cur->left=Create_tree(arr,sz,i); i++; cur->right=Create_tree(arr,sz,i); return cur; } }
前序线索化:
void FirstOrderTH(BitreeNodeTH<T>* root, BitreeNodeTH<T>* &prev)// &表示没有开辟临时变量 { if (root == NULL) return; BitreeNodeTH<T>* cur = root; if (cur->left == NULL) { cur->left_Tag = THREAD; cur->left = prev; } if (prev&&prev->right == NULL) { prev->right_Tag = THREAD; prev->right = cur; } prev = cur; if (cur->left_Tag == LINK) //cur->left FirstOrderTH(cur->left,prev); if(cur->right_Tag == LINK) //cur->right FirstOrderTH(cur->right, prev); }
前序遍历:
void FirstOrderTHing(BitreeNodeTH<T>* root) { if (root == NULL) return; BitreeNodeTH<T>* cur = root; while (cur) { while(cur->left_Tag == LINK) { cout<<cur->Data<<" "; cur = cur->left; } cout << cur->Data<<" "; if (cur->left_Tag == THREAD) { cur = cur->right; } } }
中序线索化:
void MidOrderTH(BitreeNodeTH<T>* root, BitreeNodeTH<T>* &prev) { if (root == NULL) return; BitreeNodeTH<T>* cur = root; MidOrderTH(cur->left,prev); if (cur->left == NULL) { cur->left_Tag = THREAD; cur->left = prev; } if (prev && prev->right == NULL) { prev->right = cur; prev->right_Tag = THREAD; } prev = cur; MidOrderTH(cur->right,prev); }
中序遍历:
void MidOrderTHing(BitreeNodeTH<T>* root) { BitreeNodeTH<T>* cur = root; while(cur) { while (cur->left_Tag == LINK) { cur = cur->left; } cout<<cur->Data<<" "; while (cur->right_Tag == THREAD) { cur = cur->right; cout << cur->Data<< " "; } if (cur->right_Tag == LINK) { cur = cur->right; } } }
后序线索化:
void LaterOrderTH(BitreeNodeTH<T>* root, BitreeNodeTH<T>* &prev) { if (root == NULL) return; BitreeNodeTH<T>* cur = root; LaterOrderTH(cur->left,prev); LaterOrderTH(cur->right,prev); if (cur->left == NULL) { cur->left_Tag = THREAD; cur->left = prev; } if (prev&&prev->right == NULL) { prev->right = cur; prev->right_Tag = THREAD; } prev = cur; }
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。