直接放代码??
我只是写了一个大概,很多方法都没有补全,但是构建树那一块有递归和非递归,还有二叉树各种顺序的线索化
#pragma once
#include<queue>
#include<stack>
template <typename T>
struct BinaryNode
{
BinaryNode* _left;
BinaryNode* _right;
T _data;
BinaryNode(const T& d)
:_data(d)
,_left(NULL)
,_right(NULL)
{}
};
template<typename T>
class BinaryTree
{
typedef BinaryNode<T> Node;
public:
BinaryTree()
:root(NULL)
{}
BinaryTree(const T* a, size_t size, const T& invalid);
BinaryTree(const BinaryTree<T>* t);
BinaryTree<T>& operator=(const BinaryTree<T>* t);
~BinaryTree()
{
Release(root);
}
//前序
void PrevOrder()
{
_PrevOrder(root);
}
//中序
void InOrder()
{
_InOrder(root);
}
//后序
void PostOrder()
{
_PostOrder(root);
}
//层序
void LevelOrder()
{
_LevelOrder(root);
}
//非递归的前序
void PrevOrder_NonR()
{
if (root != NULL)
_PrevOrderNonR(root);
}
//非递归的中序
void InOrder_NonR()
{
if (root != NULL)
_InOrderNonR(root);
}
//非递归的后序
void PostOrder_NonR()
{
if (root != NULL)
_PostOrderNonR(root);
}
size_t Size()
{
return _Size(root);
}
size_t LeafSize()
{
return _LeafSize(root);
}
size_t Depth()
{
return _Depth(root);
}
size_t GetKLevel(size_t k)
{
return _GetKLevel(root,k);
}
protected:
void _BinaryTree(Node* &root, const T* arr, size_t size, const T& invalid, size_t& index);
//void _BinaryTreeNode(Node* &root1, const Node* &root2);
//Node* _OPNode(const Node* &Troot);
void _PrevOrder(Node* &root);
void _InOrder(Node* &root);
void _PostOrder(Node* &root);
void _LevelOrder(Node* &root);
size_t _Size(Node* &root);
size_t _LeafSize(Node* &root);
size_t _Depth(Node* &root);
size_t _GetKLevel(Node* &root,size_t k);
void _PrevOrderNonR(Node* root)
{
if (root == NULL)
return;
stack<Node*> Q;
//Node* root = root;
Q.push(root);
while (!Q.empty())
{
Node* top = Q.top();
cout << top->_data << " ";
Q.pop();
if (top->_right)
Q.push(top->_right);
if (top->_left)
Q.push(top->_left);
}
cout << endl;
}
void _InOrderNonR(Node* root)
{
stack<Node*> Q;
Node* cur = root;
while (!Q.empty()||cur)
{
while (cur)//cur为了找左
{
Q.push(cur);
cur = cur->_left;//指向NULL
}
Node* top = Q.top();//输出左 3 2 4 1 6 5
cout << top->_data << " ";
Q.pop();
if (top->_right)
{
cur = top->_right;//
}
}
cout << endl;
}
void _PostOrderNonR(Node* root)
{
stack<Node*> Q;
Node* prev = NULL;
Node* cur = root;
while (cur || !Q.empty())
{
while (cur)
{
Q.push(cur);
cur = cur->_left;
}
Node* top = Q.top();
if (top->_right == NULL ||top->_right==prev)
{
cout << top->_data << " ";
Q.pop();
prev = top;
}
else
{
cur = top->_right;
}
}
}
void Release(Node* root)
{
if (root != NULL)
{
Release(root->_left);
Release(root->_right);
delete root;
}
}
private:
Node* root;//节点指针
};
//构造函数
template<typename T>
void BinaryTree<T>::_BinaryTree(Node* &root, const T* arr, size_t size, const T& invalid, size_t& index)
{
if (index >= size || arr[index] == invalid)
{
return;
}
root = new Node(arr[index]);
_BinaryTree(root->_left, arr, size, invalid, ++index);
_BinaryTree(root->_right, arr, size, invalid, ++index);
}
template <typename T>
BinaryTree<T>::BinaryTree(const T* a, size_t size, const T& invalid)
:root(NULL)
{
if (a == NULL)
{
return;
}
size_t index=0;
_BinaryTree(root, a, size, invalid, index);
}
//拷贝构造函数
template <typename T>
BinaryTree<T>::BinaryTree(const BinaryTree<T>* t)
:root(NULL)
{
if (t.root == NULL)
{
return;
}
root = new Node(t.root->_data);
if(t.root->_left != NULL)
{
_left = new BinaryTree(t.root->_left);
}
if(t.root->_right != NULL)
{
_right = new BinaryTree(t.root->_right);
}
}
//template<typename T>
//void BinaryTree<T>::_BinaryTreeNode(Node* &root1, const Node* &root2)
//{
// if (root2 = NULL)
// return;
// root1 = new Node(root2->_data);
// _BinaryTreeNode(root1->_left,root2->_left);
// _BinaryTreeNode(root1->_right,root2->_right);
//}
//赋值运算符重载
template <typename T>
BinaryTree<T>& BinaryTree<T>::operator=(const BinaryTree<T>* t)
{
if (t.root == NULL)
return;
root = new Node(t->root->_data);
if (t->root->_left != NULL)
{
_left = new operator=(t->root->_left);
}
if (t->root->_right != NULL)
{
_right = new operator=(t->root->_right);
}
}
//template <typename T>
//Node<T>* BinaryNode<T>::_OPNode(const Node* &Troot)
//{
// Node<T>* root = new Node(Troot->_data);
// if (Troot->_left != NULL)
// {
// root->_left = new _operator(Troot->_left);
// }
// if (Troot->_right != NULL)
// {
// root->_right = new _operator(Troot->_right);
// }
// return root;
//}
template <typename T>
void BinaryTree<T>::_PrevOrder(Node* &root)
{
if (root == NULL)
{
return;
}
cout << root->_data << " ";
_PrevOrder(root->_left);
_PrevOrder(root->_right);
}
template <typename T>
void BinaryTree<T>::_InOrder(Node* &root)
{
if (root == NULL)
return;
_InOrder(root->_left);
cout << root->_data << " ";
_InOrder(root->_right);
}
template <typename T>
void BinaryTree<T>::_PostOrder(Node* &root)
{
if (root == NULL)
{
return;
}
_PostOrder(root->_left);
_PostOrder(root->_right);
cout << root->_data << " ";
}
template <typename T>
void BinaryTree<T>::_LevelOrder(Node* &root)
{
if (root == NULL)
return;
queue<Node*> Q;
Node* p = root;
Q.push(p);
while (!Q.empty())
{
p = Q.front();
Q.pop();
cout << p->_data << " ";
if (p->_left)
{
Q.push(p->_left);
}
if (p->_right)
{
Q.push(p->_right);
}
}
}
template <typename T>
size_t BinaryTree<T>::_Size(Node* &root)
{
if (root == NULL)
return 0;
return _Size(root->_left) + _Size(root->_right)+1;
}
template <typename T>
size_t BinaryTree<T>::_LeafSize(Node* &root)
{
static size_t count = 0;
if (root == NULL)
return 0;
if (root->_left == NULL&&root->_right == NULL)
{
count++;
}
_LeafSize(root->_left);
_LeafSize(root->_right);
return count;
}
template <typename T>
size_t BinaryTree<T>::_Depth(Node* &root)
{
static size_t count = 0;
if (root == NULL)
return 0;
size_t left = _Depth(root->_left);
size_t right = _Depth(root->_right);
return left > right ? left + 1 : right + 1;
}
template <typename T>
size_t BinaryTree<T>::_GetKLevel(Node* &root,size_t k)
{
if (root == NULL)
return 0;
if ( 0 >= k > _Depth(root))
{
return 0;
}
if (k == 1)
return 1;
size_t left = _GetKLevel(root->_left, k - 1);
size_t right = _GetKLevel(root->_right, k - 1);
return left + right;
}
二叉树线索化:
#pragma once
// 二叉树线索化
enum PointerTag
{
THREAD,
LINK
};
template<typename T>
struct BinaryTreeNodeThd
{
T _data;
BinaryTreeNodeThd<T>* _left;
BinaryTreeNodeThd<T>* _right;
PointerTag _leftTag;
PointerTag _rightTag;
BinaryTreeNodeThd(const T& data)
:_data(data)
, _left(NULL)
, _right(NULL)
, _leftTag(LINK)
, _rightTag(LINK)
{}
};
template<typename T>
class BinaryTreeThd
{
typedef BinaryTreeNodeThd<T> Node;
public:
BinaryTreeThd()
:_root(NULL)
{}
BinaryTreeThd(const T* arr, size_t size, const T& invalid)
:_root(NULL)
{
if (arr == NULL)
return;
size_t index = 0;
_BinaryTreeThd(_root, arr, size,index,invalid);
}
//中序二叉树
void InOrder()
{
if (_root == NULL)
return;
_InOrder(_root);
}
//中序线索化二叉树
void InOrderThreading()
{
Node* prev = NULL;
_InOrderThreading(_root, prev);
}
//前序线索化二叉树
void PrevOrderThreading()
{
Node* prev = NULL;
_PrevOrderThreading(_root,prev);
}
//后序线索化二叉树
void PostOrderThreading()
{
Node* prev = NULL;
_PostOrderThreading(_root, prev);
}
//遍历前序线索化二叉树
void PrevOrderThd()
{
Node* cur = _root;
while (cur)
{
while (cur->_leftTag == LINK)
{
cout << cur->_data << " ";
cur = cur->_left;
}
cout << cur->_data << " ";
cur = cur->_right;
}
}
//遍历中序线索化二叉树
void InOrderThd()
{
Node* cur = _root;
Node* prev = NULL;
while (cur)
{
while (cur->_leftTag == LINK&&cur->_left!=prev)//先找到最左节点
{
cur = cur->_left;
}
cout << cur->_data << " "; //输出
if(cur->_rightTag == LINK)//右子树
{
cur = cur->_right;
}
else if(cur->_rightTag == THREAD)
{
cur = cur->_right;
prev = cur->_left;
}
}
}
protected:
void _BinaryTreeThd(Node* &root, const T* arr, size_t size, size_t& index, const T& invalid)
{
if (index >= size || arr[index] == invalid)
{
return;
}
root = new Node(arr[index]);
_BinaryTreeThd(root->_left, arr, size, ++index, invalid);
_BinaryTreeThd(root->_right, arr, size, ++index, invalid);
}
void _InOrder(Node* &root)
{
if (root == NULL)
return;
_InOrder(root->_left);
cout << root->_data << " ";
_InOrder(root->_right);
}
//前序线索化二叉树
void _PrevOrderThreading(Node* cur, Node* &prev)
{
if (cur == NULL)
return;
if (cur->_left == NULL)
{
cur->_leftTag = THREAD;
cur->_left = prev;
}
if (prev&&prev->_right==NULL)
{
prev->_rightTag = THREAD;
prev->_right = cur;
}
prev = cur;
if (cur->_leftTag == LINK)
{
_PrevOrderThreading(cur->_left,prev);
}
if (cur->_rightTag == LINK)
{
_PrevOrderThreading(cur->_right, prev);
}
}
//中序线索化
void _InOrderThreading(Node* cur, Node* &prev)//3 2 4 1 6 5
{
if (cur == NULL)
return;
//找最左
_InOrderThreading(cur->_left, prev);
if (cur->_left == NULL)
{
cur->_leftTag = THREAD;
cur->_left = prev;
}
if (prev&&prev->_right == NULL)
{
prev->_rightTag = THREAD;
prev->_right = cur;
}
prev = cur;
_InOrderThreading(cur->_right, prev);
}
//后序线索化二叉树
void _PostOrderThreading(Node* cur, Node* &prev)
{
if (cur == NULL)
return;
_PostOrderThreading(cur->_left, prev);
_PostOrderThreading(cur->_right, prev);
if (cur->_left == NULL)
{
cur->_leftTag == THREAD;
cur->_left = prev;
}
if (prev&&prev->_right == NULL)
{
prev->_rightTag = THREAD;
prev->_right = cur;
}
prev = cur;
}
private:
Node* _root;
};