0
点赞
收藏
分享

微信扫一扫

二叉树实验数据结构

眼君 2022-03-30 阅读 39

```cpp
《数据结构》实验报告
            
程序名: 二叉树实验                                              
一、上机实验的问题和要求:
设计一个程序来对二叉树进行操作,具体功能要求如下:
1.通过输入带虚结点(用#表示)的前序序列来生成一棵二叉树(参考实验课本的验证实验)。
2.按前序序列顺序输出只有一个孩子的结点。
3.求解二叉树的镜像并输出(交换左右孩子)。
4.求解二叉树的高度并输出

二、程序设计的基本思想,原理和算法描述:
(分析问题中的数据及数据元素之间的关系,确定选择何种逻辑结构;再根据操作特点分析选择何种存储结构,最后再具体分析每个功能,看需要用到哪些基本操作,如何用基本操作组合出该功能。)
1通过输入带虚结点(用#表示)的前序序列来生成一棵二叉树(参考实验课本的验证实验)。
Create()
2按前序序列顺序输出只有一个孩子的结点。
六个,带参数,不带参数重载
3求解二叉树的镜像并输出(交换左右孩子)。
4求解二叉树的高度并输出









三、源程序及注释(将代码直接复制于此):
#pragma once
#ifndef BiTree_H
#define BiTree_H
struct BiNode
{
	char data;
	BiNode* lchild, * rchild;
};
class BiTree
{
public:
	BiTree() { root = Creat(root); }
	~BiTree() { Release(root); }
	void PreOrder() { PreOrder(root); }
	void InOrder() { InOrder(root); }
	void PostOrder() { PostOrder(root); }
	void Single() { Single(root); }
	void Mirror() { Mirror(root); }
    int Height() { return Height(root); }
private:
	BiNode* root;
	BiNode* Creat(BiNode* bt);
	void Release(BiNode* bt);
	void PreOrder(BiNode* bt);
	void InOrder(BiNode* bt);
	void PostOrder(BiNode* bt);
	void Single(BiNode* bt);
    void Mirror(BiNode* bt);
    int Height(BiNode* bt);
	};
#endif
#include <iostream>
using namespace std;
#include"BiTree.h"
BiNode* BiTree::Creat(BiNode* bt)
{
	char ch;
	cin >> ch;
	if (ch == '#')return NULL;
	else {
		bt = new BiNode;
		bt->data = ch;
		bt->lchild = Creat(bt->lchild);
		bt->rchild = Creat(bt->rchild);
	}
	return bt;
}
void BiTree::Release(BiNode* bt) {
	if (bt != NULL)
	{
		Release(bt->lchild);
		Release(bt->rchild);
		delete bt;

	}
}
void BiTree::PreOrder(BiNode* bt) {
	if (bt == NULL)return;
	else {
		cout << bt->data << " ";
		PreOrder(bt->lchild);
		PreOrder(bt->rchild);

	}
}
void BiTree::InOrder(BiNode* bt) {
	if (bt == NULL)return;
	else {
		InOrder(bt->lchild);
		cout << bt->data << " ";
		InOrder(bt->rchild);
	}

}
void BiTree::PostOrder(BiNode* bt)
{
	if (bt == NULL)return;
	else {
		PostOrder(bt->lchild);
		PostOrder(bt->rchild);
		cout << bt->data << " ";
	}
}
void BiTree::Single(BiNode* bt)
{
	if (bt == NULL) return;
	if (bt->lchild == NULL && bt->rchild != NULL)
	{
		cout << bt->data;
		Single(bt->rchild);
	}
	if ((bt->lchild != NULL && bt->rchild == NULL))
	{
		cout << bt->data;
		Single(bt->lchild);
	}
	if ( bt->lchild != NULL && bt->rchild != NULL)
	{
		Single(bt->lchild);
		Single(bt->rchild);
	}

}
 void BiTree:: Mirror(BiNode* bt) 
 {
    if (bt == NULL) return ;
	else {
	BiNode *temp = bt->lchild;
		bt->lchild= bt->rchild;
		bt->rchild = temp;
		if (bt->lchild!= NULL)  Mirror(bt->lchild);
		if (bt->rchild != NULL)  Mirror(bt->rchild);
	 }

}
 int BiTree::Height(BiNode * bt)
 {
	 if (bt == NULL)return 0;
	 else {
		 int m = Height(bt->lchild);
		 int n = Height(bt->rchild);
		 if (m >= n) return m + 1;
		 else return n + 1;
		 
	 }
 }
#include<iostream>
using namespace std;
#include"BiTree.h"
/*按前序序列顺序输出只有一个孩子的结点。
3求解二叉树的镜像并输出(交换左右孩子)。
4求解二叉树的高度并输出*/
int main()
{
	cout << "请输入创建一棵二叉树的结点数据" << endl;
	BiTree T;
	int h;
	cout << "------前序遍历------" << endl;
	T.PreOrder();
	cout << endl;
	cout << "------中序遍历------" << endl;
	T.InOrder();
	cout << endl;
	cout << "------后序遍历------" << endl;
	T.PostOrder();
	cout << endl;
	cout << "只有一个孩子的结点为" << endl;
	T.Single();
	cout << endl;
	cout << "二叉树高度为" << endl;
	h=T.Height();
	cout << h << endl;
	cout << "二叉树镜像为" << endl;
	T.Mirror(); cout << "------前序遍历------" << endl;
	T.PreOrder();
	cout << endl;
	cout << "------中序遍历------" << endl;
	T.InOrder();
	cout << endl;
	cout << "------后序遍历------" << endl;
	T.PostOrder();
	cout << endl;
	cout << "只有一个孩子的结点为" << endl;
	T.Single();
	cout << endl;
	cout << "二叉树高度为" << endl;
	h = T.Height();
	cout << h << endl;
	}

四、运行输出结果:
(可以将运行结果抓图贴至此处)






五、调试和运行程序过程中产生的问题及采取的措施,吸取的经验教训:
在判断是否为一个子节点时左右结点都为0的情况有所遗漏。
在前序遍历输入的时候有的时候会遗漏#号。
在镜像之后的输出不够完备,后来将遍历函数重新调用。





六、对算法的时间和空间复杂度进行分析并提出改进设想:





七、对数据结构教学的意见和建议:


举报

相关推荐

0 条评论