//本次練習(xí)的是 二叉樹線索化的 前·中·后序 《 線索化 》 的遞歸和非遞歸實(shí)現(xiàn)
#include<iostream>
using namespace std;
enum Type
{
LINK,
THREAD
};
template<class T>
struct BinaryTreeNode
{
T _date;
BinaryTreeNode<T>* _left;
Type _leftType;
BinaryTreeNode<T>* _right;
Type _rightType;
BinaryTreeNode<T>* _parent;
BinaryTreeNode(const T& date)
:_date(date)
, _left(NULL)
, _leftType(LINK)
, _right(NULL)
, _rightType(LINK)
, _parent(NULL)
{}
};
template<class T>
class BinaryTreeThread
{
public:
BinaryTreeThread()
{};
BinaryTreeThread(const T* arr, size_t size)
{
size_t index = 0;
_root = _Create(arr, index, size);
}
// 以下用的是 遞歸
void InOrderThread() //中序線索化
{
BinaryTreeNode<T>* prev = NULL;
_InOrderThread(_root,prev);
}
void InOrder() //中序遍歷
{
_InOrder(_root);
cout << endl;
}
void PrevOrderThread() //前序線索化
{
BinaryTreeNode<T>* prev = NULL;
_PrevOrderThread(_root,prev);
}
void PrevOrder() //前序遍歷
{
_PrevOrder(_root);
cout << endl;
}
void LastOrderThread() //后序線索化
{
BinaryTreeNode<T>* prev = NULL;
_LastOrderThread(_root, prev);
}
void LastOrder() //后序遍歷
{
_LastOrder(_root);
cout << endl;
}
//以下用的不是遞歸
void InOrder_Not() //中序線索化
{
_InOrder_Not(_root);
cout << endl;
}
void PrevOrder_Not() //前序線索化
{
_PrevOrder_Not(_root);
cout << endl;
}
void LastOrder_Not() //后序線索化
{
_LastOrder_Not(_root);
}
// 以下用的是 遞歸
protected:
BinaryTreeNode<T>* _Create(const T* arr, size_t& index, size_t size)
{
if (index >= size || arr[index] == '#')
{
return NULL;
}
BinaryTreeNode<T>* root = new BinaryTreeNode<T>(arr[index]);
root->_left = _Create(arr, ++index, size);
if (root->_left != NULL)
{
root->_left->_parent = root;
}
root->_right = _Create(arr, ++index, size);
if (root->_right != NULL)
{
root->_right->_parent = root;
}
return root;
}
void _InOrderThread(BinaryTreeNode<T>* root,BinaryTreeNode<T>*& prev) //注意 加 引用
{
if (root == NULL)
{
return;
}
_InOrderThread(root->_left,prev); //遞歸左
if (root != NULL && root->_left == NULL) //左鏈接
{
root->_leftType = THREAD;
root->_left = prev;
}
if (prev != NULL && prev->_right == NULL) //右鏈接
{
prev->_rightType = THREAD;
prev->_right = root;
}
prev = root;
_InOrderThread(root->_right,prev); //遞歸右
}
void _InOrder(const BinaryTreeNode<T>* root)
{
if (root == NULL)
{
return;
}
if (root->_leftType == LINK)
{
_InOrder(root->_left);
}
cout << root->_date << " ";
if (root->_rightType == LINK)
{
_InOrder(root->_right);
}
}
void _PrevOrderThread(BinaryTreeNode<T>* root, BinaryTreeNode<T>*& prev)
{
if (root == NULL)
{
return;
}
if (root != NULL && root->_left == NULL)
{
root->_leftType = THREAD;
root->_left = prev;
}
if (prev != NULL && prev->_right == NULL)
{
prev->_rightType = THREAD;
prev->_right = root;
}
prev = root;
if (root->_leftType == LINK) //這個(gè)節(jié)點(diǎn)已經(jīng)線索化 所以要判斷為L(zhǎng)INK
{
_PrevOrderThread(root->_left, prev);
}
if (root->_rightType == LINK) //這個(gè)節(jié)點(diǎn)已經(jīng)線索化
{
_PrevOrderThread(root->_right, prev);
}
}
void _PrevOrder(BinaryTreeNode<T>* root)
{
if (root == NULL)
{
return;
}
cout << root->_date<<" ";
if (root->_leftType == LINK)
{
_PrevOrder(root->_left);
}
if (root->_rightType == LINK)
{
_PrevOrder(root->_right);
}
}
void _LastOrderThread(BinaryTreeNode<T>* root, BinaryTreeNode<T>*& prev)
{
if (root == NULL)
{
return;
}
_LastOrderThread(root->_left,prev);
_LastOrderThread(root->_right,prev);
if (root != NULL && root->_left == NULL)
{
root->_leftType = THREAD;
root->_left = prev;
}
if (prev != NULL && prev->_right == NULL)
{
prev->_rightType = THREAD;
prev->_right = root;
}
prev = root;
}
void _LastOrder(BinaryTreeNode<T>* root)
{
if (root == NULL)
{
return;
}
if (root->_leftType == LINK)
{
_LastOrder(root->_left);
}
if (root->_rightType == LINK)
{
_LastOrder(root->_right);
}
cout << root->_date << " ";
}
// 以下用的不是遞歸
protected:
void _InOrder_Not(BinaryTreeNode<T>* root)
{
BinaryTreeNode<T>* cur = root;
while (cur != NULL)
{
while (cur != NULL && cur->_leftType == LINK)
{
cur = cur->_left;
}
cout << cur->_date << " ";
while (cur != NULL && cur->_rightType == THREAD)
{
cur = cur->_right;
cout << cur->_date << " ";
}
cur = cur->_right;
}
}
void _PrevOrder_Not(BinaryTreeNode<T>* root)
{
BinaryTreeNode<T>* cur = root;
while (cur != NULL)
{
cout << cur->_date << " ";
while (cur != NULL && cur->_leftType == LINK)
{
cur = cur->_left;
cout << cur->_date << " ";
}
while (cur != NULL && cur->_rightType == THREAD)
{
cur = cur->_right;
cout << cur->_date << " ";
}
cur = cur->_right;
}
}
void _LastOrder_Not(BinaryTreeNode<T>* root) //關(guān)鍵問(wèn)題:1.判斷一個(gè)節(jié)點(diǎn)已遍歷 2.找到下一個(gè)節(jié)點(diǎn)
{
if (_root == NULL)
{
return;
}
BinaryTreeNode<T>* cur = root->_left;
BinaryTreeNode<T>* prev = NULL;
while (cur != _root)
{
while (cur != NULL && cur->_leftType == LINK && cur->_left != prev)// 當(dāng)左子節(jié)點(diǎn)已經(jīng)遍歷完成就往右走
{
cur = cur->_left; //跳到最左節(jié)點(diǎn)
}
if (cur != NULL && cur->_rightType == THREAD)
{
cout << cur->_date << " ";
prev = cur;
cur = cur->_right;
}
if (cur == _root) //如果跳到了_root 就表示已經(jīng)遍歷完成
{
cout << cur->_date << " ";
break;
}
//此時(shí) 右樹已經(jīng)遍歷完成
if (cur != NULL && cur->_rightType == LINK && cur->_right == prev)//如果根節(jié)點(diǎn)的右子樹已經(jīng)遍歷完成 則跳到根節(jié)點(diǎn)的父親節(jié)點(diǎn)
{
cout << cur->_date << " ";
prev = cur;
cur = cur->_parent;
}
/*if (cur != NULL && cur->_leftType == LINK && cur->_left == prev)
{
cout << cur->_date << " ";
prev = cur;
cur = cur->_right;
}*/
if (cur != NULL && cur->_rightType == LINK)
{
cur = cur->_right;
}
}
}
protected:
BinaryTreeNode<T>* _root;
};
void Test1()
{
int arr[10] = { 1, 2, 3, '#','#', 4, '#', '#', 5, 6 };
BinaryTreeThread<int> t1(arr, 10);
t1.InOrderThread();
t1.InOrder();
t1.InOrder_Not();
cout << endl;
BinaryTreeThread<int> t2(arr, 10);
t2.PrevOrderThread();
t2.PrevOrder();
t2.PrevOrder_Not();
cout << endl;
BinaryTreeThread<int> t3(arr, 10);
t3.LastOrderThread();
t3.LastOrder();
t3.LastOrder_Not();
}
int main()
{
Test1();
return 0;
}
成都創(chuàng)新互聯(lián)公司是一家集網(wǎng)站建設(shè),瓊中黎族企業(yè)網(wǎng)站建設(shè),瓊中黎族品牌網(wǎng)站建設(shè),網(wǎng)站定制,瓊中黎族網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營(yíng)銷,網(wǎng)絡(luò)優(yōu)化,瓊中黎族網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競(jìng)爭(zhēng)力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶成長(zhǎng)自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。
分享文章:二叉樹的線索化
新聞來(lái)源:http://www.ekvhdxd.cn/article20/iecjjo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供Google、App開發(fā)、網(wǎng)站收錄、網(wǎng)頁(yè)設(shè)計(jì)公司、搜索引擎優(yōu)化、做網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)