一,树的相关概念
1,树是n(n>=0)个有限个数组的元素的集合,形状像一棵倒过来的树。
2,
3,
结点:结点包含数据和指向其他结点的指针。
根结点:树第一个结点称为根结点。
结点的度:结点拥有的子结点个数。
叶结点:没有子结点的结点。
父子结点:一个结点father指向另一个结点child,则child为孩子结点,father为父亲结点。
兄弟结点:具有相同父结点的结点互为兄弟结点。
结点的祖先:从根结点开始到该结点所经的所有结点都可以成为该结点的祖先。
子孙:以某结点为根的子树中任一结点都成为该结点的子孙。
树的高度:树中距离根结点最远结点的路径长度。
4,
图一:
如果有环状的结构就不能称为树。
五,树的储存
六,二叉树的数组表示
七,二叉树的链表储存表示
二叉树的每个结点有三个域:_data(数据),_left(左孩子),_right(右孩子)
八,满二叉树和完全二叉树
九,二叉树的相关概念
二叉树:二叉树是一棵特殊的树,二叉树每个结点最多有两个孩子结点,分别称为左孩子和右孩子。
满二叉树:高度为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):一层层节点依次遍历 【1 2 5 3 4 6】。
十,测试用例
十一,代码实现
#pragma once
#include<iostream>
#include<assert.h>
#include<queue>
using namespace std;
template<class T>
struct BinaryTreeNode
{
BinaryTreeNode<T>* _left;
BinaryTreeNode<T>* _right;
T _data;
BinaryTreeNode(const T& x)
: _data(x) //数
, _left(NULL) //左孩子
, _right(NULL) //右孩子
{}
};
template<class T>
class BinaryTree
{
typedef BinaryTreeNode<T> Node;
public:
BinaryTree() // 默认构造函数
:_root(NULL)
{}
BinaryTree(const T* a, size_t size,const T& invalid)// invalid 是为空
:_root(NULL)
{
size_t index = 0;
_root = _CreatTree(a, size, index, invalid);
}
BinaryTree(const BinaryTree<T>& t)
:_root(NULL)
{
_root = _CopyTree(t._root);
}
BinaryTree<T>& operator=(const BinaryTree<T>& t)
{
if (this != &t)// 判断自赋值
{
Node*root = _CopyTree(t._root);
Clear(_root);
_root = root;
}
return *this;
}
~BinaryTree()
{
Clear(_root);
}
public:
Node*_CopyTree(Node* root)
{
Node* head = NULL;
if (root)
{
head = new Node(root->_data);
head->_left = _CopTree(root->_left);
head->_right = _CopTree(root->_right);
}
return head;
}
void PrevOrder()
{
_PrevOrder(_root);
cout << endl;
}
void InOrder()
{
_InOrder(_root);
cout << endl;
}
void PostOrder()
{
_PostOrder(_root);
cout << endl;
}
rsize_t LeafSize()
{
return _LeafSize(_root);
}
void LevelOrder()// 层序遍历
{
queue<Node*> q;
if (_root)
{
q.push(_root);
}
while (q.size())
{
BinaryTreeNode<T>* front = q.front();
cout << front->_data << " ";
if (front->_left)
{
q.push(front->_left);
}
if (front->_right)
{
q.push(front->_right);
}
q.pop();
}
cout << endl;
}
size_t size()
{
return _size(_root);
}
size_t Depth()
{
return _Depth(_root);
}
protected:
BinaryTreeNode<T> * _CreatTree(const T* a, size_t size, size_t &index, const T& invalid)
{
assert(a);
Node* root = NULL;
if (index < size && a[index] != invalid)
{
root = new Node(a[index]);
root->_left = _CreatTree(a, size, ++index, invalid);
root->_right = _CreatTree(a, size, ++index, invalid);
}
return root;
}
void Clear(Node* root)
{
Node* del;
Node* cur = root;
if (cur)
{
del = cur;
Clear(cur->_left);
Clear(cur->_right);
}
}
void _PrevOrder(Node* root)
{
if (root == NULL)
{
return;
}
cout << root->_data << " ";
_PrevOrder(root->_left);
_PrevOrder(root->_right);
}
void _InOrder(Node* root)
{
if (root == NULL)
{
return;
}
else
{
_InOrder(root->_left);
cout << root->_data << " ";
_InOrder(root->_right);
}
}
void _PostOrder(Node* root)
{
if (root == NULL)
{
return;
}
else
{
_PostOrder(root->_left);
_PostOrder(root->_right);
cout << root->_data << " ";
}
}
size_t _size(Node* root)
{
if (root==NULL)
{
return 0;
}
return _size(root->_left) + _size(root->_right) + 1;
}
size_t _Depth(Node* root)
{
if (root == NULL)
{
return 0;
}
int leftDepth = _Depth(root->_left) + 1;
int righrDepth = _Depth(root->_right) + 1;
return leftDepth > righrDepth ? leftDepth : righrDepth;
}
size_t _LeafSize(Node* root)//叶子结点个数
{
if (root == NULL)
{
return 0;
}
if ((root->_left == NULL) && (root->_right == NULL))
{
return 1;
}
return _LeafSize(root->_left) + _LeafSize(root->_right);
}
protected:
BinaryTreeNode<T> * _root;
};
void Test1()
{
int array1[10] = { 1, 2, 3, '#', '#', 4, '#', '#', 5, 6 };
//int array2[15] = { 1, 2, '#', 3, '#', '#', 4, 5, '#', 6, '#', 7, '#', '#', 8 };
BinaryTree<int> t1(array1, 10 ,'#');
//BinaryTree<int> t2(array2, 15, '#');
t1.PrevOrder();//先根遍历
t1.InOrder();//中序遍历
t1.PostOrder();//后根遍历
t1.LevelOrder();//层序遍历
cout << "size:" << t1.size() << endl;
cout << "depth:" << t1.Depth() << endl;
cout << "leavesize:" << t1.LeafSize() << endl;
}
主函数:
#define _CRT_SECURE_NO_WARNINGS 1
#include"Binarytree.h"
#include<stdlib.h>
int main()
{
Test1();
getchar();
system("pause");
return 0;
}
十二:运行结果
十三:遍历的非递归写法
中序非递归:
void _InOrder(Node* root)
{
Node*cur = _root;
stack<Node*> S;
while (cur || S.size())
{
while (cur)
{
S.push(cur);
cur = cur->_left;
}
Node* top = S.top();
cout << top->_data << " ";
S.pop();
cur = top->_right;
}
cout << endl;
}
前序非递归:
void _PrevOrder(Node* root)//先压右子树再压左子树
{
stack<Node*> S;
if (_root)
{
S.push(root);
}
while (S.size())
{
Node* top = S.top();
S.pop();
cout << top->_data << " ";
if (top->_right)
{
S.push(top->_right);
}
if (top->_left)
{
S.push(top->_left);
}
}
cout << endl;
}
后序非递归:
void _PostOrder(Node* root)// 非递归后序遍历
{
stack<Node*> S;
Node* cur = _root;
Node* prev = NULL;
while (cur || S.size())
{
while (cur)
{
S.push(cur);
cur = cur->_left;
}
Node* top = S.top();
if((top->_right == NULL||top->_right ==prev))
{
cout << top->_data << " ";
S.pop();
prev = top;
}
else
{
cur = top->_right;
}
}
cout << endl;
}
十四,查找x
protected:
Node* _Find(const T&x, Node* root)
{
if (root == NULL)
{
return NULL;
}
if (root->_data == x)
{
return root;
}
Node* ret = _Find(x, root->_left);
if (ret)
{
return ret;
}
return _Find(x, root->_right);
}
public:
bool Find(const T& x)
{
if (_Find(x, _root))
{
return true;
}
return false;
}
十五,第k层有多少结点
protected:
rsize_t _Getlevel(Node * root, int k)
{
if (root == NULL)
{
return 0;
}
if (k == 1)
{
return 1;
}
return _Getlevel(root->_left, k - 1) + _Getlevel(root->_right, k - 1);
}
public:
size_t Getlevel(int k)
{
return _Getlevel(_root, k);
}