数据结构和算法学习:二叉树的层序、先序和中序遍历,C++递归与非递归算法实现。

本文展示了如何使用C++编程实现二叉树的数据结构,包括节点类Treenode和二叉树类Bitree,以及这些类支持的中序、先序和层序遍历方法。代码包括递归和非递归两种遍历方式,通过示例代码展示了如何创建和遍历二叉树。
摘要由CSDN通过智能技术生成

需求二叉树:

 代码实现:

"tree.hpp"文件:

#define _CRT_SECURE_NO_WARNINGS 1

#include <stdbool.h>
#include <iostream>
#include <stdlib.h>
#include <ctime>
#include <stack>
#include<queue>
using namespace std;


class Treenode
{
	friend class Bitree;
public:
	
	Treenode();
	Treenode(char a);
	//Treenode(char c);
	Treenode& setnode(char a)
	{
		this->val = a;
		return *this;
	}

	void setfirstchild(Treenode* a);
	void setnextchild(Treenode* a);
	void setrtag(bool a);
	void setltag(bool a);
	char getval();
	Treenode& getfirstchild();
	Treenode& getnextchild();

private:
	
	char val;//数据部分
	Treenode* firstchild;
	Treenode *nextchild;//同一化的树的存储
	bool ltag , rtag ;//0表示有左或右孩子,1表示该结点没有左或右孩子,且此链域是线索

};

Treenode& Treenode::getfirstchild()
{
	return *this->firstchild;
}
Treenode& Treenode::getnextchild()
{
	return *this->nextchild;
}
char Treenode::getval()
{
	return this->val;
}
void Treenode::setfirstchild(Treenode* a)
{
	this->firstchild = a;
}
void Treenode::setnextchild(Treenode *a)
{
	this->nextchild = a;
}

void Treenode::setltag(bool a)
{
	this->ltag = a;
}
void Treenode::setrtag(bool a)
{
	this->rtag = a;
}
Treenode::Treenode(char a)
{
	this->val = a;
	if (this->firstchild != NULL)
	{
		delete this->firstchild;
	}
	this->firstchild = NULL;
	if (this->nextchild != NULL)
	{
		delete this->nextchild;
	}
	this->nextchild = NULL;
	this->ltag = false;
	this->rtag = false;
}
Treenode::Treenode()
{
	this->val = '#';
	if (this->firstchild != NULL)
	{
		delete this->firstchild;
	}
	this->firstchild = NULL;
	if (this->nextchild != NULL)
	{
		delete this->nextchild;
	}
	this->nextchild = NULL;
	this->ltag = false;
	this->rtag = false;
}

//Treenode::Treenode(char c)
//{
//	this->val = (char)('A' + rand()%);//输出大写的
//	this->firstchild = NULL;
//	this->nextchild = NULL;
//	this->ltag = false;
//	this->rtag = false;
//}

class Bitree
{
	friend class Treenode;
public:
	Bitree()
	{
		this->root = new(class Treenode);
	}
	Treenode& setroot(Treenode* a);

	//void createBitree();
	void visit(Treenode* a);
	Treenode* getroot()
	{
		return this->root;
	}
	

	void inorder_traversal(Treenode* t);//中序遍历递归算法

	void inorder_nonrecursion_traversal();//中序遍历非递归算法

	void preorder_traversal(Treenode* t);
	void preorder_nonrecursion_traversal();
	int getcount()
	{
		return this->count;
	}

	void sequence_traversal();//层序遍历

private:
	Treenode* root;
	int count = 0;
};
void Bitree::sequence_traversal()//层序遍历
{
	queue<Treenode*>Q;
	Treenode* p=this->root;
	Q.push(p);
	while (!Q.empty())
	{
		this->visit(Q.front());
		p = Q.front();
		Q.pop();
		if (p->firstchild != NULL)
			Q.push(p->firstchild);
		if (p->nextchild != NULL)
			Q.push(p->nextchild);
	}
}
void Bitree::inorder_nonrecursion_traversal()//中序遍历非递归算法
{
	stack<Treenode*> S;
	Treenode*p=this->root;
	//Treenode* pre = NULL;
	while (p || !S.empty())
	{
		if (p)
		{
			S.push(p);
			//if(p->firstchild!=NULL)
			//pre = p;
			p = p->firstchild;
		
		}
		else
		{
			this->visit(S.top());
			p = S.top()->nextchild;
			S.pop();
	
		}
	}

}

void Bitree::preorder_traversal(Treenode* t)//先序遍历递归算法
{
	if (t == NULL)
		return;
	visit(t);
	preorder_traversal(t->firstchild);
	preorder_traversal(t->nextchild);
}
void Bitree::preorder_nonrecursion_traversal()//先序遍历非递归算法
{
	stack<Treenode*> S;
	Treenode* p = this->root;
	//Treenode* pre = NULL;
	while (p || !S.empty())
	{
		if (p)
		{
			this->visit(p);
			S.push(p);
			//if(p->firstchild!=NULL)
			//pre = p;
			p = p->firstchild;

		}
		else
		{
			
			p = S.top()->nextchild;
			S.pop();

		}
	}
}

Treenode& Bitree::setroot(Treenode* a)
{
	
	this->root = a;
	return *this->root;
}
void Bitree::visit(Treenode*a)
{
	if (a->val != '#')
	{
		cout << a->val;
		this->count++;
	}

}

void Bitree::inorder_traversal(Treenode* t)//中序遍历递归算法
{
	if (t == NULL)
		return;
	inorder_traversal(t->firstchild);
	visit(t);
	inorder_traversal(t->nextchild);
}
	

"源.cpp":

#define _CRT_SECURE_NO_WARNINGS 1

#include	"tree.hpp"



int main()
{
	//srand((unsigned)time(NULL));
	//输出字母的测试代码
	//cout <<(char) ('A' + rand() % 26);
	//int count = 0;//避免重复的字母

	//先创建空树再遍历赋值
	Bitree T;
	Treenode m1('A');
	Treenode m2('B');
	Treenode m3('C');
	Treenode m4('D');
	Treenode m5('E');
	Treenode m6('F');
	Treenode m7('G');

	T.setroot(&m1);
	m1.setfirstchild(&m2);
	m1.setnextchild(&m3);

	m2.setfirstchild(&m4); 
	m2.setnextchild(&m5);
	m3.setfirstchild(&m6);
	m3.setrtag(true) ;
	m4.setnextchild(&m7);
	//cout << m4.getnextchild().getval();
	m4.setrtag(true);
	m5.setrtag(true);
	m5.setrtag(true);
	m6.setrtag(true);
	m6.setrtag(true);
	m7.setrtag(true);
	m7.setrtag(true) ;


	//T.createBitree();

	T.inorder_traversal(T.getroot());//中序递归遍历
	cout << endl;
	T.inorder_nonrecursion_traversal();//中序非递归遍历
	cout << endl;
	T.preorder_traversal(T.getroot());//先序递归遍历
	cout << endl;
	T.preorder_nonrecursion_traversal();//先序非递归遍历
	cout << endl;
    T.sequence_traversal();//层序遍历
    cout<<endl;
	cout << T.getcount();
	
	
	



	system("pause");
	return 0;
}

 中序遍历结果:

 先序遍历结果:

层序遍历结果:

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值