【二叉树】42 二叉树:层次遍历

这篇博客介绍了如何使用C++模板设计二叉树的抽象数据类型,并实现了层次遍历算法。通过一个二叉树的先序序列,构建了二叉树的结构,并提供了层次遍历的实现,使用了顺序队列进行辅助。层次遍历过程中,对每个节点调用visit函数一次且仅一次。示例代码展示了如何将给定的先序序列转换为二叉树并进行层次遍历。
摘要由CSDN通过智能技术生成

问题描述 :

目的:使用C++模板设计并逐步完善二叉树的抽象数据类型(ADT)。

内容:

(1)请参照链表的ADT模板,设计二叉树并逐步完善的抽象数据类型。(由于该环境目前仅支持单文件的编译,故将所有内容都集中在一个源文件内。在实际的设计中,推荐将抽象类及对应的派生类分别放在单独的头文件中。参考教材、课件,以及网盘中的链表ADT原型文件,自行设计二叉树的ADT。)

注意:二叉树ADT的基本操作的算法设计很多要用到递归的程序设计方法。

 

(2)基本操作5:在二叉树的二叉链表存储形式建立的基础上,设计二叉树的层次遍历算法。完成后将其加入到二叉树的ADT基本操作集中。

注意:二叉树的层次遍历需要使用顺序队列来存储遍历的中间过程。

初始条件:二叉树T存在,visit是对结点操作的应用函数。

操作结果:层次遍历T,对每个结点调用函数visit一次且仅一次。一旦visit()失败,则操作失败。

 

参考函数原型:

(1)visit函数(具体功能根据实际需要,样例仅仅输出data域的信息)t

emplate<class ElemType>

bool visit(BinaryTreeNode<ElemType> * root){

     

    if(!root) return false; 

    else{

        cout<<root->data<<" ";     

        return true;

    }

}

 

(2)层次遍历,成员函数

template<class ElemType>

bool BinaryTree<ElemType>::LayerOrderTraverse(bool (*visit)(BinaryTreeNode<ElemType> *T)) const;

输入说明 :

第一行:表示无孩子或指针为空的特殊分隔符

第二行:二叉树的先序序列(结点元素之间以空格分隔)

 

输出说明 :

第一行:二叉树层次遍历结果

输入范例 :

#
A B # C D # # E # # F # G # H # #

输出范例 :

A B F C G D E H 

解题代码:

// tree.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <string>
#include <vector>
#include <queue>
#include <sstream>
#include <stack>
#include <map>
#include <ctime>
#include <array>
#include <set>
using namespace std;
vector<string> departString_string(string data)
{
	vector<int> back_part;//output type
	int i, j;
	vector<string> part;
	string A_part;
	stringstream room;
	room.str(data);
	while (room >> A_part)
		part.push_back(A_part);
	return part;
}
//————————————————
//版权声明:本文为CSDN博主「systemyff」的原创文章,遵循CC 4.0 BY - SA版权协议,转载请附上原文出处链接及本声明。
//原文链接:https ://blog.csdn.net/u014377763/article/details/113845555
template<class ElemType>
struct tree_point {
	ElemType data;//数据
	struct tree_point* l_child, * r_child;//左、右孩子指针
};
template<class ElemType>
class BinaryTree {
private:
	vector<tree_point<ElemType>*> outlist;
	tree_point<ElemType>* root;   // 头指针
public:
	BinaryTree() :root(NULL)
	{
		//无参数的构造函数
	}
	~BinaryTree()
	{
		//析构函数
	}
	void BinaryTree_fron(vector<ElemType> lis, ElemType nut)
	{
		/*int i, j;
		stack<tree_point<ElemType>* >s;
		if (lis.size()==0)
		{
			root = NULL;
			cout << "空树" << endl;
			return;
		}
		tree_point<ElemType>* now,*creat;
		root->data = lis[0];
		root->l_child = NULL;
		root->r_child = NULL;
		s.push(root);
		for(i=1;i<lis.size();i++)
		{
			creat = new tree_point<ElemType>;
			creat->l_child = NULL;
			creat->r_child = NULL;
			creat->data = lis[i];
			if (lis[i] == nut)
			{
				now = s.top();
				s.pop();
				if (!s.empty())
				{
					now = s.top();
					s.pop();
					now->r_child = new tree_point<ElemType>;
					s.push(now->r_child);
				}
				continue;
			}
			now = s.top();
			s.pop();
			now->r_child = NULL;
			now->l_child = creat;
			s.push(creat);
		}*/
		//=========
		stack<tree_point<ElemType>*> s;
		tree_point<ElemType>* p_Parent = NULL,* p_Child = NULL;
		int i = 0;
		int flag = 0;//控制左右
		p_Parent = new tree_point<ElemType>;
		p_Parent->data = lis[i];
		p_Parent->l_child = p_Parent->r_child = NULL;
		s.push(p_Parent);
		root = p_Parent;
		i = 1;
		flag = 0;
		while (!s.empty())
		{
			if (lis[i] != nut)
			{
				p_Parent = new tree_point<ElemType>;
				p_Parent->data = lis[i];
				p_Parent->l_child = p_Parent->r_child = NULL;
				if (flag == 0)
				{
					p_Child = s.top();
					p_Child->l_child = p_Parent;
				}
				else if (flag == 1)
				{
					p_Child = s.top();
					s.pop();
					p_Child->r_child = p_Parent;
				}
				s.push(p_Parent);
				flag = 0;
			}
			else
			{
				if (flag == 0)
					flag = 1;
				else if (flag == 1)
					s.pop();
			}
			i++;
		}
	}
	
	tree_point<ElemType>* get_root()
	{
		return root;
	}
	void qianxu(tree_point<ElemType>* t)
	{
		outlist.push_back(t);
		if (t->l_child != NULL)
			qianxu(t->l_child);
		if (t->r_child != NULL)
			qianxu(t->r_child);
		return ;
	}
	void zhongxu(tree_point<ElemType>* t)
	{
		if (t->l_child != NULL)
			zhongxu(t->l_child);
		outlist.push_back(t);
		if (t->r_child != NULL)
			zhongxu(t->r_child);
		return;
	}
	void houxu(tree_point<ElemType>* t)
	{
		if (t->l_child != NULL)
			houxu(t->l_child);
		if (t->r_child != NULL)
			houxu(t->r_child);
		outlist.push_back(t);
		return;
	}
	void cengxu(tree_point<ElemType>* t)
	{
		vector<tree_point<ElemType>* > q;
		q.push_back(root);
		int cur = 0, last = 1;
		while (cur < q.size())
		{
			last = q.size();
			while (cur < last)
			{
				outlist.push_back( q[cur]);
				if (q[cur]->l_child)
					q.push_back(q[cur]->l_child);
				if (q[cur]->r_child)
					q.push_back(q[cur]->r_child);
				++cur;
			}
		}
	}
	void out_lis()
	{
		int i;
		for (i = 0; i < outlist.size(); i++)
		{
			cout << outlist[i]->data;
			if (i != outlist.size() - 1)
				cout << ",";
			else
				cout << endl;
		}
		outlist.clear();
	}
	int cnt = 0, p_cnt = 0;
	void t_cnt(tree_point<ElemType>* t)
	{
		p_cnt++;
		if (t->l_child != NULL && t->r_child != NULL)
			cnt++;
		if(t->l_child != NULL)
			t_cnt(t->l_child);
		if (t->r_child != NULL)
			t_cnt(t->r_child);
	}
	int twocnt()
	{
		cnt = 0;
		t_cnt(root);
		return cnt;
	}
	int pointcnt()
	{
		p_cnt = 0;
		t_cnt(root);
		return p_cnt;
	}
};

int main()
{
	string s, ins, nulls;
	vector<string> part_in;
	BinaryTree<string> a;
	cin >> nulls;
	cin.get();
	getline(cin, ins);
	part_in = departString_string(ins);
	//======================================
	/*for (auto i : part_in)
	{
		cout << i << endl;
	}
	cout << part_in.size() << endl;
	cout << nulls << endl;*/
	//======================================
	
	a.BinaryTree_fron(part_in, nulls);
	/*a.qianxu(a.get_root());
	a.out_lis();
	a.zhongxu(a.get_root());
	a.out_lis();
	a.houxu(a.get_root());
	a.out_lis();*/
	//cout<<a.pointcnt()<<endl;
	a.cengxu(a.get_root());
	a.out_lis();
	return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值