树是n(n>=0)个有限个数据的元素集合,形状像一颗倒过来的树。
二叉树:二叉树是一棵特殊的树,二叉树每个节点最多有两个孩子结点,分别称为左孩子和右孩子。
满二叉树:高度为N的满二叉树有2^N - 1个节点的二叉树。
完全二叉树: 若设二叉树的深度为h,除第 h 层外,其它各层 (1~h-1) 的结点数都达到最大个数,第 h 层所有的结点都连续集中在最左边,这就是完全二叉树
树的遍历
举例:
前序遍历(先根遍历):1,先访问根节点;2,前序访问左子树;3,前序访问右子树; 【1 2 3 4 5 6】
中序遍历: 1,中序访问左子树;2,访问根节点;3,中序访问右子树; 【3 2 4 1 6 5】
后序遍历(后根遍历),1,后序访问左子树;2,后序访问右子树;3,访问根节点; 【3 4 2 6 5 1】
层序遍历: 一层层节点依次遍历。 【1 2 5 3 4 6】
树的链表结构
template<class T>
class BinaryTree
{
public:
BinaryTree()
:_root(NULL)
{}
BinaryTree(const T* str, size_t size,const T& invalid)
{
size_t index = 0;
_root = _CreateTree(str, size,index, invalid);
}
BinaryTree(const BinaryTree<T>& b)
{
_root = _Copy(b._root);
}
BinaryTree<T>& operator=(BinaryTree<T> b)
{
swap(_root, b._root);
return *this;
}
~BinaryTree()
{
_Destory(_root);
}
protected:
BinaryTreeNode<T>* _CreateTree(const T* str, size_t size, size_t& index, const T& invalid)
{
//传引用是为了防止回退
BinaryTreeNode<T>* root = NULL;
if ((index < size) && (str[index] != invalid))
{
root = new BinaryTreeNode<T>(str[index]);
//先构建左子树
root->_left = _CreateTree(str, size, ++index, invalid);
//再构建右子树
root->_right = _CreateTree(str, size, ++index, invalid);
}
return root;
}
BinaryTreeNode<T>* _Copy(const BinaryTreeNode<T>* root)
{
if (root)
{
BinaryTreeNode<T>* newRoot = new BinaryTreeNode<T>(root->_data);
newRoot->_left = _Copy(root->_left);
newRoot->_right = _Copy(root->_right);
return newRoot;
}
return NULL;
}
void _Destory(BinaryTreeNode<T>* root)
{
BinaryTreeNode<T>* cur = root;
if (root)
{
_Destory(root->_left);
_Destory(root->_right);
delete cur;
cur = NULL;
root = NULL;
}
return;
}
protected:
BinaryTreeNode<T>* _root;
};
树的递归遍历
public:
void PrevOrder() //前序递归
{
_PrevOrder(_root);
cout << endl;
}
void InOrder() //中序递归
{
_InOrder(_root);
cout << endl;
}
void PostOrder() //后序递归
{
_PostOrder(_root);
cout << endl;
}
void LevelOrder() //层序递归
{
_LevelOrder(_root);
}
protected:
void _PrevOrder(const BinaryTreeNode<T>* root)
{
if (root == NULL)
{
return;
}
cout << root->_data << " ";
_PrevOrder(root->_left);
_PrevOrder(root->_right);
}
void _InOrder(const BinaryTreeNode<T>* root)
{
if (root == NULL)
{
return;
}
_InOrder(root->_left);
cout << root->_data << " ";
_InOrder(root->_right);
return;
}
void _PostOrder(const BinaryTreeNode<T>* root)
{
if (root == NULL)
{
return;
}
_PostOrder(root->_left);
_PostOrder(root->_right);
cout << root->_data << " ";
}
void _LevelOrder(BinaryTreeNode<T>* root)
{
queue<BinaryTreeNode<T>*> q;
q.push(root);
while (0 < q.size())
{
BinaryTreeNode<T>* tmp = q.front();
cout << tmp->_data << " ";
q.pop();
if (tmp->_left != NULL)
{
q.push(tmp->_left);
}
if (tmp->_right != NULL)
{
q.push(tmp->_right);
}
}
}
树的非递归遍历
void PrevOrder_NonR() //非递归前序
{
stack<BinaryTreeNode<T>* > v1;
v1.push(_root);
BinaryTreeNode<T>* cur = _root;
cout << v1.top()->_data << " ";
while (!v1.empty())
{
v1.pop();
if (cur->_right)
{
v1.push(cur->_right);
}
if (cur->_left)
{
v1.push(cur->_left);
}
if (v1.size() == 0)
{
return;
}
cout << v1.top()->_data << " ";
cur = v1.top();
}
}
void InOrder_NonR() //非递归中序
{
stack<BinaryTreeNode<T>* > v2;
BinaryTreeNode<T>* cur = _root;
while (cur||!v2.empty())
{
while (cur)
{
v2.push(cur);
cur = cur->_left;
}
cout << v2.top()->_data << " ";
BinaryTreeNode<T>* top = v2.top();
v2.pop();
cur = top->_right;
}
}
void PostOrder_NonR() //非递归后序
{
BinaryTreeNode<T>* cur = _root;
BinaryTreeNode<T>* prev = NULL;
stack<BinaryTreeNode<T>* > s;
while (cur || !s.empty())
{
while (cur)
{
s.push(cur);
cur = cur->_left;
}
BinaryTreeNode<T>* top = s.top();
if (top->_right == NULL || top->_right == prev)
{
cout << top->_data << " ";
s.pop();
prev = top;
}
else
{
cur = top->_right;
}
}
}
树的大小,深度....
size_t Size() //树的大小
{
size_t size = _Size(_root);
return size;
}
size_t Depth() //树的深度
{
size_t dep = _Depth(_root);
return dep;
}
size_t LeafSize() //叶子节点的个数
{
size_t count = _LeafSize(_root);
return count;
}
BinaryTreeNode<T>* Find(const T& d) //寻找结点
{
return _Find(_root, d);
}
//第K层有多少个结点
/*size_t GetKLevel(const size_t& k)
{
return _GetKLevel(_root, k);
}*/
size_t GetKLevel(const size_t& k)
{
size_t level = 1;
_GetKLevel(_root, 1evel, k, size);
return size;
}
size_t _Size(const BinaryTreeNode<T>* root)
{
size_t size = 1;
if (root==NULL)
{
return 0;
}
size += _Size(root->_left);
size += _Size(root->_right);
return size;
}
size_t _Depth(const BinaryTreeNode<T>* root)
{
size_t dep1 = 1;
size_t dep2 = 1;
if (root == NULL)
{
return 0;
}
dep1 += _Depth(root->_left);
dep2 += _Depth(root->_right);
if (dep1 > dep2)
{
return dep1;
}
else
{
return dep2;
}
}
size_t _LeafSize(const BinaryTreeNode<T>* root)
{
size_t count = 0;
if (root == NULL)
{
return 0;
}
if ((root->_left == NULL) && (root->_right == NULL))
{
return 1;
}
count += _LeafSize(root->_left);
count += _LeafSize(root->_right);
return count;
}
BinaryTreeNode<T>* _Find(BinaryTreeNode<T>* root, const T& d)
{
if (root == NULL)
{
return NULL;
}
if (root->_data == d)
{
return root;
}
BinaryTreeNode<T>* ret = _Find(root->_left, d);
if (ret)
{
return ret;
}
return _Find(root->_right, d);
}
/*size_t _GetKLevel(BinaryTreeNode<T>* root, size_t k)
{
if (root == NULL)
{
return 0;
}
if (k == 1)
{
return 1;
}
return _GetKLevel(root->_left, k - 1) + _GetKLevel(root->_right, k - 1);
}*/
void _GetKLevel(BinaryTreeNode<T>* root, size_t level, size_t k, size_t& size)
{
if (root == NULL)
{
return;
}
if (k == level)
{
size++;
return;
}
_GetKLevel(root->_left, level + 1, k, size);
_GetKLevel(root->_right, level + 1, k, size);
}
转载于:https://blog.51cto.com/10797037/1772606