二叉树的各种遍历过程

目的:

  1. 领会二叉树链式存储结构,掌握二叉树中各种基本运算的算法设计;
  2. 领会二叉树的各种遍历过程,掌握遍历算法设计。
    内容:
    编写程序,实现二叉树的各种基本运算和遍历,并在此基础上设计一个程序,完成以下功能:
  3. 建立二叉树bt,该二叉树的括号表示串为
    “A(B(D,E(H(J,K(L,M(,N))))),C(F,G(,I)))”;
  4. 输出二叉树bt;
  5. 输出二叉树的高度;
  6. 输出二叉树的叶子及个数;
  7. 输出结点‘H’的左右孩子的结点值;
    7.输出二叉树的先序遍历(递归和非递归)序列;
    8.输出二叉树的中序遍历(递归和非递归)序列;
    9.输出二叉树的后序遍历(递归和非递归)序列;
    10.输出二叉树的层次遍历序列;
#include<iostream>
#include<cstdlib>
#include<stack>
#include<queue>
using namespace std;
typedef struct Node
{
	char data;
	Node *L,*R;
}Bt;
//创建二叉树 
Bt* Create(char a[])
{
	Bt *bt = NULL,*p,*vis[100];
	char c;
	int i = 0,top = -1,k;
	while(a[i] != '\0')
	{
		c = a[i++];
		switch(c)
		{
			case '(':
				vis[++top] = p;
				k = 1;
				break;
			case ',':
				k = 2;
				break;
			case ')':
				top--;
				break;
			default:
				p = (Bt*)malloc(sizeof(Bt));
				p->data = c;
				p->L = p->R = NULL;
				if(bt == NULL)
					bt = p;
				else
				{
					if(k == 1)vis[top]->L = p;
					else vis[top]->R = p;
				}
		}
	}
	return bt;
}
//输出二叉树。先序递归 
void OutBt(Bt *bt) 
{
	if(bt != NULL)
	{
		printf("%2c",bt->data);
		OutBt(bt->L);
		OutBt(bt->R);
	}
}
//输出二叉树的高度 
int BtHeight(Bt *bt)
{
	if(bt == NULL) return 0;
	int l,r;
	l = BtHeight(bt->L)+1;
	r = BtHeight(bt->R)+1;
	return l>r?l:r;
}
//输出二叉树的叶子及个数 
int OutLeaf(Bt *bt)
{
	int count = 0;
	if(bt == NULL) return count;
	if(bt->L == NULL && bt->R == NULL)
	{
		printf("%2c",bt->data);
		count++;
	}
	count += OutLeaf(bt->L);
	count += OutLeaf(bt->R);
	return count;
}
//查找节点 
Bt* find(Bt *bt,char a) 
{
	if(bt != NULL)
	{
		if(bt->data == a)
			cout<<bt->L->data<<" "<<bt->R->data;
		else
		{
			find(bt->L,a);
			find(bt->R,a);
		}
	}
}
//先序遍历。非递归 
void Preorder2(Bt *bt)
{
	if(bt)
	{
		Bt *p;
		stack<Bt*>st;
		st.push(bt);
		while(!st.empty())
		{	
			p = st.top();
			st.pop();
			printf("%2c",p->data);
			if(p->R)
				st.push(p->R);
			if(p->L)
				st.push(p->L);
		}
	}
}
//中序。递归 
void Inorder(Bt *bt) 
{
	if(bt)
	{
		Inorder(bt->L);
		printf("%2c",bt->data);
		Inorder(bt->R);
	}
}
//中序。非递归 
void Inorder2(Bt *bt)
{
	if(bt != NULL)
	{
		Bt *p = bt;
		stack<Bt*>st;
		while(!st.empty() || p)
		{
			while(p)
			{
				st.push(p);
				p = p->L;
			}
			if(!st.empty())
			{
				p = st.top();
				st.pop();
				printf("%2c",p->data);
				p = p->R;
			}
		}
	}
}
//后序遍历。递归 
void Postorder(Bt* bt)
{
	if(bt)
	{
		Postorder(bt->L);
		Postorder(bt->R);
		printf("%2c",bt->data);
	}
}
//后序遍历。非递归
void Postorder2(Bt* bt)
{
	if(bt)
	{
		Bt *p = bt,*r;
		int a;
		stack<Bt*>st;
		do{
			while(p)
			{
				st.push(p);
				p = p->L;
			}
			r = NULL;
			a = 1;
			while(!st.empty() && a == 1)
			{
				p = st.top();
				if(p->R == r)
				{
					printf("%2c",p->data);
					st.pop();
					r = p;
				}
				else
				{
					p = p->R;
					a = 2;
				}
			}
		}while(!st.empty());
	}
}
//层次遍历
void Level(Bt* bt)
{
	Bt *p = bt;
	queue<Bt*>q;
	if(bt)
	{
		q.push(p);
		while(!q.empty())
		{
			p = q.front();
			q.pop();
			printf("%2c",p->data);
			if(p->L)q.push(p->L);
			if(p->R)q.push(p->R);
		}
	}
}
 
int main()
{
	Bt *bt,*p;
	char a[] = "A(B(D,E(H(J,K(L,M(,N))))),C(F,G(,I)))";
	//建立二叉树
	cout<<"建立二叉树"; 
	bt = Create(a);
	
	//输出二叉树
	cout<<"\n\n输出二叉树:";
	OutBt(bt);
	
	//输出二叉树的高度
	cout<<"\n\n输出二叉树的高度:"<<BtHeight(bt);
	
	//输出二叉树的叶子及个数
	cout<<"\n\n输出二叉树的叶子及个数:";
	int count = OutLeaf(bt);
	cout<<"\t个数:"<<count;
	
	//输出节点'H'的左右孩子的值
	cout<<"\n\n输出节点'H'的左右孩子的值:"; 
	find(bt,'H');
	
	//输出二叉树的先序遍历
	cout<<"\n\n先序遍历:\n"<<"\t递  归:";
	OutBt(bt);
	cout<<"\n\t非递归:";
	Preorder2(bt);
	
	//输出二叉树的中序遍历
	cout<<"\n\n中序遍历:\n"<<"\t递  归:";
	Inorder(bt);
	cout<<"\n\t非递归:";
	Inorder2(bt);
	
	//输出二叉树的中序遍历
	cout<<"\n\n后序遍历:\n"<<"\t递  归:";
	Postorder(bt);
	cout<<"\n\t非递归:";
	Postorder2(bt);
	
	//层次遍历
	cout<<"\n\n层次遍历:\t";
	Level(bt);
	
	return 0;
}

在这里插入图片描述

展开阅读全文
©️2019 CSDN 皮肤主题: 深蓝海洋 设计师: CSDN官方博客
应支付0元
点击重新获取
扫码支付

支付成功即可阅读