来自大二上学期实验报告,请多指教。
一、实验任务:
二叉树的实现,包含创建二叉树,二叉树的前序遍历,中序遍历,后序遍历,层序遍历。求二叉树结点个数,求二叉树中叶子结点个数,求二叉树的高度
二、代码:
//bitree.h
#ifndef bitree_H
template<class X>
struct binode
{
X data;
binode<X>* lchild, * rchild;
};
template<class X>
class bitree
{
public:
bitree() { root = creat(); }
~bitree() { release(root); }
void preorder() { preorder(root); }
void inorder() { inorder(root); }
void postorder() { postorder(root); }
void leverorder() { leverorder(root); }
int depth() { return depth(root); }
void num() { num(root); }
void ynum() { ynum(root); }
private:
binode<X>* root;
binode<X>* creat();
void release(binode<X>* bt);
void preorder(binode<X>* bt);
void inorder(binode<X>* bt);
void postorder(binode<X>* bt);
void leverorder(binode<X>* bt);
int depth(binode<X>* bt);
void num(binode<X>* bt);
void ynum(binode<X>* bt);
};
#endif
//bitree.cpp
#include<iostream>
#include "bitree.h"
using namespace std;
int count1 = 0,count2=0, dl = 0, dr = 0, deep = 0;
template<class X>
binode<X>*bitree<X>::creat()
{
binode<X>*bt;
char ch;
cin >> ch;
if (ch == '#')bt = NULL;
else {
bt = new binode<X>;
bt->data = ch;
cout << "请输入" << ch << "的左子树" << endl;
bt->lchild = creat();
cout << "请输入" << ch << "的右子树" << endl;
bt->rchild = creat();
}
return bt;
}
template<class X>
void bitree<X>::release(binode<X>* bt)
{
if (bt != NULL) {
release(bt->lchild);
release(bt->rchild);
delete bt;
}
}
template<class X>
void bitree<X>::preorder(binode<X>* bt)
{
if (bt ==NULL)return;
else {
cout << bt->data<<"→";
preorder(bt->lchild);
preorder(bt->rchild);
}
}
template<class X>
void bitree<X>::inorder(binode<X>* bt)
{
if (bt ==NULL)return;
else {
inorder(bt->lchild);
cout << bt->data<<"→";
inorder(bt->rchild);
}
}
template<class X>
void bitree<X>::postorder(binode<X>* bt)
{
if (bt ==NULL)return;
else {
postorder(bt->lchild);
postorder(bt->rchild);
cout << bt->data<<"→";
}
}
template<class X>
void bitree<X>::leverorder(binode<X>* bt)
{
binode<X> Q[20] = {};
binode<X> q;
int front = -1, real = -1;
if (root ==NULL)return;
Q[++real] = *bt;
while (front != real) {
q = Q[++front];
cout << q.data<< "→";
if (q.lchild != NULL) Q[++real] = *q.lchild;
if (q.rchild != NULL) Q[++real] = *q.rchild;
}
}
template<class X>
int bitree<X>::depth(binode<X>* bt)
{
int dl, dr;
if (bt ==NULL)return 0;
else
{
dl = depth(bt->lchild);
dr = depth(bt->rchild);
deep = (dl > dr) ? (dl +1) : (dr + 1);
return deep;
}
}
template<class X>
void bitree<X>::num(binode<X>* bt)
{
if (bt == NULL)return ;
else {
count1++;
num(bt->lchild);
num(bt->rchild);
}
}
template<class X>
void bitree<X>::ynum(binode<X>* bt)
{
if (bt == NULL) return;
else {
if (bt->lchild==NULL && bt->rchild==NULL)
count2++;
ynum(bt->lchild);
ynum(bt->rchild);
}
}
int main() {
cout << "请输入二叉树的根" << endl;
bitree<char>LLX;
cout << "二叉树的前序遍历为:" ;
LLX.preorder();
cout <<endl<< "二叉树的中序遍历为:";
LLX.inorder();
cout <<endl<< "二叉树的后序遍历为:";
LLX.postorder();
cout <<endl<< "二叉树的层序遍历为:";
LLX.leverorder();
LLX.num();
cout << endl << "二叉树的结点个数为:"<<count1;
LLX.depth();
cout << endl << "二叉树的高度为:"<< deep;
LLX.ynum();
cout << endl << "二叉树的叶子结点个数为:" <<count2;
}
三、运行结果: