【数据结构】【二叉树的基本操作】【二叉树的遍历】

目录

二叉树的构造:输入二叉树的先序遍历,空子树用#代替

前序遍历(递归、非递归)

中序遍历(递归、非递归)

后序遍历(递归、非递归)

层次遍历

二叉树的深度、结点

完整代码如下 


二叉树需要明确的是其左右子树的位置是不可改变的;

下面是一些二叉树的基本操作:

二叉树的构造:输入二叉树的先序遍历,空子树用#代替

Bitree Createtree(Bitree T) //根据先序遍历的序列构造二叉树
{
	char s;
	cin >> s;
	int i = 0;
	if (s == '#')
		T = NULL;
	else {
            T = (Bitnode*)malloc(sizeof(Bitnode));
			T->data = s;
			T->lchild = Createtree(T->lchild);
			T->rchild = Createtree(T->rchild);
	}
	return T;
}

前序遍历(递归、非递归)

void Preorder(Bitree T)//前序遍历
{
	if (T != NULL)
	{
		cout<<T->data<<" ";
		Preorder(T->lchild);
		Preorder(T->rchild);
	}
}

void Preorder_1(Bitree T)//前序遍历(非递归)
{
	Bitree p = T;
	stack<Bitree> S;
	while (p || !S.empty()) {
		if (p) {
			cout << p->data << " ";
			S.push(p);
			p = p->lchild;
		}
		else {
			p=S.top();
			p = p->rchild;
			S.pop();
		}
	}
}

中序遍历(递归、非递归)

void Inorder(Bitree T)//中序遍历
{
	if (T != NULL)
	{
		Inorder(T->lchild);
		cout << T->data << " ";
		Inorder(T->rchild);
	}
}

void Inorder_1(Bitree T)//中序遍历(非递归)
{
	stack<Bitree> S;
	Bitree p = T;
	while (p || !S.empty())
	{
		if (p) {
			S.push(p);
			p = p->lchild;
		}
		else {
			cout << S.top()->data << " ";
			p = S.top();
			p = p->rchild;
			S.pop();
		}
	}
}

后序遍历(递归、非递归)

void Postorder(Bitree T)//后序遍历
{
	if (T != NULL)
	{
		Postorder(T->lchild);
		Postorder(T->rchild);
		cout << T->data << " ";
	}
}

void Postorder_1(Bitree T)//后序遍历(非递归)
{
	stack<Bitree> S;
	Bitree p = T,m=NULL; //m用来标记上次访问过的右子树
	while (p || !S.empty())
	{
		if (p) {
			S.push(p);
			p = p->lchild;
		}
		else {
			p = S.top();
			if (p->rchild && p->rchild != m) //右子树存在,且未被访问
				p = p->rchild;
			else {
				cout << S.top()->data << " ";
				S.pop();
				m = p;
				p = NULL;
			}
		}
	}
}

层次遍历

void Levelorder(Bitree T) //层次遍历
{
	queue<Bitree> Q;
	Bitree p;
	Q.push(T);
	while (!Q.empty())
	{
		p = Q.front();
		cout << p->data<< " ";
		Q.pop();
		if (p->lchild != NULL)
			Q.push(p->lchild);
		if(p->rchild != NULL)
			Q.push(p->rchild);
	}
}

二叉树的深度、结点

int Depthtree(Bitree T, int &depth) //树的深度
{
	if (T == NULL)
		depth = 0;
	else
	{
		int l = Depthtree(T->lchild,depth);
		int r = Depthtree(T->rchild,depth);
		l > r ? depth = l+1 : depth = r+1;
	}
	return depth;
}

int numtree(Bitree T) //树的结点
{
	int num = 0;
	if (T == NULL)
		num == 0;
	else
	{
		if (T->lchild == NULL && T->rchild == NULL)
			num = 1;
		else
			num = numtree(T->lchild) + numtree(T->rchild)+1;
	}
	return num;
}

完整代码如下 

#include<bits/stdc++.h>
using namespace std;
typedef struct Bitnode {
	char data;
	struct Bitnode* lchild, * rchild;
}Bitnode,*Bitree;

void Inittree(Bitree &T) //二叉树的初始化
{
	T->data = '\0';
	T->lchild = NULL;
	T->rchild = NULL;
}
 
Bitree Createtree(Bitree T) //根据先序遍历的序列构造二叉树
{
	char s;
	cin >> s;
	int i = 0;
	if (s == '#')
		T = NULL;
	else {
            T = (Bitnode*)malloc(sizeof(Bitnode));
			T->data = s;
			T->lchild = Createtree(T->lchild);
			T->rchild = Createtree(T->rchild);
	}
	return T;
}

void Preorder(Bitree T)//前序遍历
{
	if (T != NULL)
	{
		cout<<T->data<<" ";
		Preorder(T->lchild);
		Preorder(T->rchild);
	}
}

void Preorder_1(Bitree T)//前序遍历(非递归)
{
	Bitree p = T;
	stack<Bitree> S;
	while (p || !S.empty()) {
		if (p) {
			cout << p->data << " ";
			S.push(p);
			p = p->lchild;
		}
		else {
			p=S.top();
			p = p->rchild;
			S.pop();
		}
	}
}

void Inorder(Bitree T)//中序遍历
{
	if (T != NULL)
	{
		Inorder(T->lchild);
		cout << T->data << " ";
		Inorder(T->rchild);
	}
}

void Inorder_1(Bitree T)//中序遍历(非递归)
{
	stack<Bitree> S;
	Bitree p = T;
	while (p || !S.empty())
	{
		if (p) {
			S.push(p);
			p = p->lchild;
		}
		else {
			cout << S.top()->data << " ";
			p = S.top();
			p = p->rchild;
			S.pop();
		}
	}
}
void Postorder(Bitree T)//后序遍历
{
	if (T != NULL)
	{
		Postorder(T->lchild);
		Postorder(T->rchild);
		cout << T->data << " ";
	}
}

void Postorder_1(Bitree T)//后序遍历(非递归)
{
	stack<Bitree> S;
	Bitree p = T,m=NULL; //m用来标记上次访问过的右子树
	while (p || !S.empty())
	{
		if (p) {
			S.push(p);
			p = p->lchild;
		}
		else {
			p = S.top();
			if (p->rchild && p->rchild != m) //右子树存在,且未被访问
				p = p->rchild;
			else {
				cout << S.top()->data << " ";
				S.pop();
				m = p;
				p = NULL;
			}
		}
	}
}

void Levelorder(Bitree T) //层次遍历
{
	queue<Bitree> Q;
	Bitree p;
	Q.push(T);
	while (!Q.empty())
	{
		p = Q.front();
		cout << p->data<< " ";
		Q.pop();
		if (p->lchild != NULL)
			Q.push(p->lchild);
		if(p->rchild != NULL)
			Q.push(p->rchild);
	}
}

int Depthtree(Bitree T, int &depth) //树的深度
{
	if (T == NULL)
		depth = 0;
	else
	{
		int l = Depthtree(T->lchild,depth);
		int r = Depthtree(T->rchild,depth);
		l > r ? depth = l+1 : depth = r+1;
	}
	return depth;
}

int numtree(Bitree T) //树的结点
{
	int num = 0;
	if (T == NULL)
		num == 0;
	else
	{
		if (T->lchild == NULL && T->rchild == NULL)
			num = 1;
		else
			num = numtree(T->lchild) + numtree(T->rchild)+1;
	}
	return num;
}

int main()
{
	//测试:ABDF###E##C##
	Bitnode* T;
	T = (Bitnode*)malloc(sizeof(Bitnode));
	Inittree(T);

	cout << "输入二叉树的先序遍历,空子树用#代替:" << endl;
	T=Createtree(T);

	cout << "前序遍历结果为:";
	Preorder(T);
	cout << endl;

	cout << "前序遍历(非递归)结果为:";
	Preorder_1(T);
	cout << endl<<endl;

	cout << "中序遍历结果为:";
	Inorder(T);
	cout << endl; 

	cout << "中序遍历(非递归)结果为:";
	Inorder_1(T);
	cout << endl<<endl;

	cout << "后序遍历结果为:";
	Postorder(T);
	cout << endl;

	cout << "后序遍历(非递归)结果为:";
	Postorder_1(T);
	cout << endl<<endl;

	cout << "层次遍历结果为:";
	Levelorder(T);
	cout << endl<<endl;

	cout << "树的深度为:";
	int depth = 0;
	Depthtree(T,depth);
	cout << depth << endl;

	cout << "树的结点为:";
	cout << numtree(T) << endl;
	return 0;
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值