数据结构与算法--C++实现二叉树的基本操作

数据结构与算法–C++实现二叉树的基本操作

在学习数据结构与算法这门课中,老师布置了一个关于二叉树的实验作业。实验作业的要求如下所示:
数据结构与算法--C++实现二叉树的基本操作_第1张图片
经过了几天的不懈努力(到处学习补知识),终于能够实现二叉树实验。

下面就是该实验的过程,成果图以及代码:
主要内容:主要使用了循环递归的思想,其中还使用了链表,栈与队列的操作。

根据该二叉树图来创建二叉树。(图中的#是表示空结点)

效果图:
创建二叉树:
根据先序遍历的方法,依次输入权值来创建二叉树。
数据结构与算法--C++实现二叉树的基本操作_第2张图片
输出中序遍历:
中序遍历(左结点-根节点-右结点)–D G B A E C F
数据结构与算法--C++实现二叉树的基本操作_第3张图片
输出先序遍历:
先序遍历(根节点-左结点-右结点)–A B D G C E F
数据结构与算法--C++实现二叉树的基本操作_第4张图片

输出后序遍历:
后序遍历(左结点-右结点-根节点)–G D B E F C A
数据结构与算法--C++实现二叉树的基本操作_第5张图片
后面的内容就不逐一演示了,大概就这样,最后附上这次实验的代码(供大家学习参考)。

附上代码:

#include 
#include 
using namespace std;
#define ElemType char

typedef struct BiTNode {//结点结构体
	ElemType data;
	struct BiTNode* lchild, * rchild;//左结点,右结点
}BiTNode, * BiTree;


BiTNode* creat() {//创建二叉链表
	struct BiTNode* bt;
	char ch;
	cout << "输出你想赋予的权值:";
	cin >> ch;
	if (ch == '#')bt = nullptr;
	else {
		bt = new struct BiTNode;
		bt->data = ch;
		bt->lchild = creat();
		bt->rchild = creat();
	}
	return bt;
}

void m_traverse(BiTNode *bt) {//中序遍历(递归)
	if (bt == nullptr)return;
	else {
		m_traverse(bt->lchild);
		cout << bt->data << "\t";
		m_traverse(bt->rchild);
	}
}

void b_traverse(BiTNode* bt) {//后序遍历(递归)
	if (bt == nullptr)return;
	else {
		b_traverse(bt->lchild);
		b_traverse(bt->rchild);
		cout << bt->data << "\t";
	}
}

void f_traverse(BiTNode* bt) {//先序遍历(递归)
	if (bt == nullptr)return;
	else {
		cout << bt->data << "\t";
		f_traverse(bt->lchild);
		f_traverse(bt->rchild);
	}
}

void mm_traverse(BiTNode* bt) {//中序遍历(非递归)
	BiTNode* a[10];//充当一个栈,该栈的最大容量为10
	int count = -1;//充当一个栈的指针
	if (bt == nullptr)return;
	while (bt != nullptr || count != -1) {
		while (bt!= nullptr) {
			a[++count] = bt;
			bt = bt->lchild;
		}
		if (count != -1) {
			bt = a[count];
			cout << bt->data << "\t";
			count--;
			bt = bt->rchild;
		}
	}
}

void c_traverse(BiTNode* bt) {//层序遍历
	if (bt == nullptr)return;
	BiTNode* a[20] = { nullptr };//充当队列
	int count = -1;//充当队列的队尾
	int top = 0;//充当队列的队头
	while (bt != nullptr || top != count + 2){
		cout << bt->data << "\t";
		if (bt->lchild != nullptr)a[++count] = bt->lchild;
		if (bt->rchild != nullptr)a[++count] = bt->rchild;
		bt = a[top++];
	}
}

int layer(BiTNode* bt) {//高度
	int h1;
	int h2;
	if (bt == nullptr)return 0;
	else {
		h1 = layer(bt->lchild);
		h2 = layer(bt->rchild);
		return h1 > h2 ? ++h1 : ++h2;
	}
}

int node(0);
void Node(BiTNode* bt) {//结点
	if (bt == nullptr)return;
	else {
		node++;
		Node(bt->lchild);
		Node(bt->rchild);
	}
}

int leaf_node(0);
void Leaf_Node(BiTNode* bt) {//叶子结点
	if (bt == nullptr)return ;
	else {
		if (bt->lchild == nullptr && bt->rchild == nullptr)leaf_node++;
		 Leaf_Node(bt->lchild);  
		 Leaf_Node(bt->rchild);  
	}
}

void change_Node(BiTNode* &bt) {//交换左结点与右结点
	BiTNode *cbt = new struct BiTNode;
	if (bt!=nullptr) {
		if (bt->lchild == nullptr && bt->rchild == nullptr)return;
		cbt = bt->lchild;
		bt->lchild = bt->rchild;
		bt->rchild = cbt;
		change_Node(bt->lchild);
		change_Node(bt->rchild);
	}
}

int main() {//主函数菜单
	struct BiTNode* tree;
	tree = new struct BiTNode;
	char n;
	cout << "欢迎使用小小城序员的树与二叉树:" << endl;
	while (true) {//menu
		cout <<endl<< "1.创建二叉树的二插链表" << endl;
		cout << "2.输出二叉树的中序遍历(递归)" << endl;
		cout << "3.输出二叉树的先序遍历" << endl;
		cout << "4.输出二叉树的后序遍历" << endl;
		cout << "5.输出二叉树的中序遍历(非递归)" << endl;
		cout << "6.输出二叉树的层序遍历" << endl;
		cout << "7.输出二叉树的高度" << endl;
		cout << "8.输出二叉树的结点个数" << endl;
		cout << "9.输出二叉树的叶子个数" << endl;
		cout << "0.交换二叉树每个结点的左子树与右子树" << endl;
		cout << "退出请按q" << endl;
		cout << "请选择你的操作:";
		cin >> n;
		switch (n) {
		case '1'://创建二叉树
			tree=creat();
			cout << "创建二叉树成功!" << endl;
			break;
		case '2'://中序遍历(递归)
			cout << "该二叉树的中序遍历(递归)结果为:";
			m_traverse(tree);
			cout << "\n";
			break;
		case '3'://先序遍历
			cout << "该二叉树的先序遍历结果为:";
			f_traverse(tree);
			cout << "\n";
			break;
		case '4'://后序遍历
			cout << "该二叉树的后序遍历结果为:";
			b_traverse(tree);
			cout << "\n";
			break;
		case '5'://中序遍历(非递归)
			cout << "该二叉树的中序遍历(非递归)结果为:";
			mm_traverse(tree);
			cout << "\n";
			break;
		case '6'://层序遍历
			cout << "该二叉树的层序遍历结果为:";
			c_traverse(tree);
			cout << "\n";
			break;
		case '7'://高度
			cout<<"该二叉树的高度为;"<<layer(tree);
			cout << "\n";
			break;
		case '8'://结点
			Node(tree);
			cout << "该二叉树的结点个数为:" << node << "\n";
			break;
		case '9'://叶子
			Leaf_Node(tree);
			cout <<"该二叉树的叶子个数为:"<< leaf_node << "\n";
			break;
		case '0'://交换左右结点
			change_Node(tree);
			cout << "交换成功!" << endl;
			break;
		case 'q'://退出
			cout << "再见" << endl;
			return 0;
		default://输入不合法
			cout << "你的输入有问题,请重新输入" << endl;
			break;
		}
	}
}

写下这篇博客,不仅仅还是为了记录下自己的学习,更是希望能够帮助到别人。希望这篇博客对你学习二叉树的实现能有一定的帮助。

你可能感兴趣的:(笔记,c++,算法,二叉树,数据结构,算法)