线索化二叉树
二叉树是一种非线性结构,遍历二叉树几乎都是通过递归或者用栈辅助实现非递归的遍历。用二叉树作为存储结构时,取到一个节点,只
10年积累的成都网站建设、网站建设经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先做网站后付款的网站建设流程,更有潮州免费网站建设让你可以放心的选择与我们合作。
能获取节点的左孩子和右孩子,不能直接得到节点的任一遍历序列的前驱或者后继。
为了保存这种在遍历中需要的信息,我们利用二叉树中指向左右子树的空指针来存放节点的前驱和后继信息。
enum PointerTag
{
LINK, //0
THREAD //1
};
template
struct BitreeNodeTH
{
T Data;
BitreeNodeTH
BitreeNodeTH
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* Create_tree(T *arr,int sz,int &i) { if(*arr==NULL||arr[i]=='#'||i>=sz) return NULL; else { BitreeNodeTH *cur=new BitreeNodeTH ; 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* root, BitreeNodeTH * &prev)// &表示没有开辟临时变量 { if (root == NULL) return; BitreeNodeTH * 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* root) { if (root == NULL) return; BitreeNodeTH * cur = root; while (cur) { while(cur->left_Tag == LINK) { cout< Data<<" "; cur = cur->left; } cout << cur->Data<<" "; if (cur->left_Tag == THREAD) { cur = cur->right; } } }
中序线索化:
void MidOrderTH(BitreeNodeTH* root, BitreeNodeTH * &prev) { if (root == NULL) return; BitreeNodeTH * 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* root) { BitreeNodeTH * cur = root; while(cur) { while (cur->left_Tag == LINK) { cur = cur->left; } cout< Data<<" "; while (cur->right_Tag == THREAD) { cur = cur->right; cout << cur->Data<< " "; } if (cur->right_Tag == LINK) { cur = cur->right; } } }
后序线索化:
void LaterOrderTH(BitreeNodeTH* root, BitreeNodeTH * &prev) { if (root == NULL) return; BitreeNodeTH * 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; }
分享标题:线索化二叉树
新闻来源:http://scyanting.com/article/pddosg.html