二叉树的相关操作
一、二叉树的相关概念
1、二叉树的定义
一棵二叉树是一个结点的集合,这个集合要么为空,要么满足一下条件:
(1) 他仅有一个被称为根的结点;
(2) 除了根节点,其他的结点可以分为两个不相交的子集T1和T2,他们也是二叉树,分别叫做根的左子树和右子树。
2、二叉树的分类
· 满二叉树
一棵深度为K且有2^k-1个结点的二叉树称为满二叉树。
· 完全二叉树
一棵深度为K的完全二叉树,满足以下条件:
(1) 叶子结点只可能在层次最大的两层上出现;
(2) 最下面一层的结点集中分布在左侧。
二、二叉树的运算
1、遍历
访问二叉树的所有结点,每个结点能且只能访问一次,他包括前序遍历、中序遍历和后序遍历。
前序遍历
(1) 访问根结点;
(2) 前序遍历根结点的左子树;
(3) 前序遍历根结点的右子树;
中序遍历
(1) 中序遍历根结点的左子树;
(2) 访问根结点;
(3) 访问遍历根结点的右子树;
后序遍历
(1) 后序遍历根结点的左子树;
(2) 后序遍历根结点的右子树;
(3) 访问根结点;
2、结点个数
(1) 根结点计数;
(2) 根节点的左子树计数;
(3) 根结点的右子树计数;
3、二叉树高度(深度)
(1) 二叉树左结点计数;
(2) 二叉树右结点计数;
(2) 左右子树高度最大的计数 + 1(根结点)
三、二叉树的链式存储
二叉树的链式存储 * left _ data _ * left。
四、代码实现
binary_tree.cpp
#include <iostream>
using namespace std;
// 构造结点;
template <class Record>
struct Binary_node
{
Record data;
Binary_node<Record> *left;
Binary_node<Record> *right;
Binary_node()
{
left=right=NULL;
}
Binary_node(Record item, Binary_node<Record> *lptr=NULL, Binary_node<Record> *rptr=NULL)
{
data = item;
left = lptr;
right = rptr;
}
};
// 构造二叉树
template <class Record>
class Binary_tree
{
protected:
Binary_node<Record> *root;
public:
Binary_tree()
{
root=NULL;
}
void create_tree();
void preorder()
{
recursive_preorder(root);
}
void inorder()
{
recursive_inorder(root);
}
void postorder()
{
recursive_postorder(root);
}
int size(int num)
{
recursive_size(root, num);
return num;
}
int height()
{
return recursive_height(root);
}
private:
void recursive_preorder(Binary_node<Record> *sub_root);
void recursive_inorder(Binary_node<Record> *sub_root);
void recursive_postorder(Binary_node<Record> *sub_root);
void recursive_size(Binary_node<Record> *sub_root, int &num);
int recursive_height(Binary_node<Record> * sub_root);
};
template <class Record>
void Binary_tree<Record>::create_tree()
{
Binary_node<Record> *xn, *yn, *an, *bn, *cn, *dn, *en;
dn = new Binary_node<Record>('D');
en = new Binary_node<Record>('E');
bn = new Binary_node<Record>('B', NULL, dn);
cn = new Binary_node<Record>('C', en, NULL);
an = new Binary_node<Record>('A', bn, cn);
yn = new Binary_node<Record>('Y');
root = xn = new Binary_node<Record> ('X', yn, an);
}
//前序遍历
template <class Record>
void Binary_tree<Record>::recursive_preorder(Binary_node<Record> *sub_root)
{
if (sub_root == NULL)
return;
cout<<sub_root->data<<" ";
recursive_preorder(sub_root->left);
recursive_preorder(sub_root->right);
};
//中序遍历
template <class Record>
void Binary_tree<Record>::recursive_inorder(Binary_node<Record> *sub_root)
{
if (sub_root == NULL)
return;
recursive_inorder(sub_root->left);
cout<<sub_root->data<<" ";
recursive_inorder(sub_root->right);
};
//后序遍历
template <class Record>
void Binary_tree<Record>::recursive_postorder(Binary_node<Record> *sub_root)
{
if (sub_root == NULL)
return;
recursive_postorder(sub_root->left);
recursive_postorder(sub_root->right);
cout<<sub_root->data<<" ";
};
// 二叉树结点个数
template <class Record>
void Binary_tree<Record>::recursive_size(Binary_node<Record> *sub_root, int &num)
{
if(sub_root == NULL)
return;
num++;
recursive_size(sub_root->left, num);
recursive_size(sub_root->right, num);
};
//二叉树深度
template <class Record>
int Binary_tree<Record>::recursive_height(Binary_node<Record> *sub_root)
{
int l, r, h;
if(sub_root==NULL)
return 0;
l = recursive_height(sub_root->left);
r = recursive_height(sub_root->right);
h = 1 + (l>r?l:r);
return h;
}
binary_tree_operation.cpp
#include <iostream>
#include "binary_tree.cpp"
int main()
{
int number = 0;
Binary_tree<char> btree;
cout<<"二叉树是: X(Y(NULL,NULL),A(B(NULL,D(NULL,NULL)),C(E(NULL,NULL),NULL)))"<<endl<<endl;
btree.create_tree();
cout<<"前序遍历结果是:";
btree.preorder();
cout<<endl;
cout<<"中序遍历结果是:";
btree.inorder();
cout<<endl;
cout<<"后序遍历结果是:";
btree.postorder();
cout<<endl;
cout<<"结点个数是:";
cout<<btree.size(number)<<endl;
cout<<"高度是:";
cout<<btree.height()<<endl;
return 0;
}
五、结果