您好,登錄后才能下訂單哦!
//本次練習的是 二叉樹線索化的 前·中·后序 《 線索化 》 的遞歸和非遞歸實現
#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) //這個節點已經線索化 所以要判斷為LINK
{
_PrevOrderThread(root->_left, prev);
}
if (root->_rightType == LINK) //這個節點已經線索化
{
_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) //關鍵問題:1.判斷一個節點已遍歷 2.找到下一個節點
{
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)// 當左子節點已經遍歷完成就往右走
{
cur = cur->_left; //跳到最左節點
}
if (cur != NULL && cur->_rightType == THREAD)
{
cout << cur->_date << " ";
prev = cur;
cur = cur->_right;
}
if (cur == _root) //如果跳到了_root 就表示已經遍歷完成
{
cout << cur->_date << " ";
break;
}
//此時 右樹已經遍歷完成
if (cur != NULL && cur->_rightType == LINK && cur->_right == prev)//如果根節點的右子樹已經遍歷完成 則跳到根節點的父親節點
{
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;
}
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。