//本次练习的是 二叉树线索化的 前·中·后序 《 线索化 》 的递归和非递归实现
#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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。