北邮22信通:二叉树层序遍历的两种方法首发模板类交互

news2024/10/7 12:17:14

北邮22信通一枚~   

跟随课程进度每周更新数据结构与算法的代码和文章 

持续关注作者  解锁更多邮苑信通专属代码~

获取更多文章  请访问专栏~

北邮22信通_青山如墨雨如画的博客-CSDN博客

目录

一.总纲

二.用队列存储

2.1用模板类实现队列

2.1.1核心思路:

2.1.2一个错误 

2.1.3完整代码

2.1.4运行结果

 2.2使用STL中的队列

2.2.1核心代码

2.2.2完整代码

2.2.3运行结果

三.用数组存储

3.1核心代码:

3.2完整代码

3.3运行结果:

 四.完整代码


一.总纲

***说明***

1.本篇文章将为你介绍二叉树层序遍历的两类常见方法。根据数据存储结构的不同,我们可以将方法大致分为以下两类:使用数组储存和使用队列储存。使用数组储存的实现方法就是函数的递归调用,之前的文章中有介绍过;本篇文章重点讲解使用队列存储的方法。

2.队列的层序遍历:在进行层序遍历时,对某一层节点访问完毕之后,在按照他们的访问顺序一次对各个节点的左孩子和右孩子顺序访问,这样一层一层的进行,先访问的节点其左孩子也要先访问,这与队列的特性比较吻合。因此,我们可以利用队列来实现二叉树的层序遍历。

用队列实现层序遍历的方法:先让根节点入队;根节点出队的同时根节点的左右孩子依次入队;每次出队一个,出队那个节点的左孩子和右孩子再从队尾依次入队,以此类推。

基本思想:

   根结点非空,入队。

   如果队列不空  

   {

       队头元素出队

       访问该元素

       若该结点的左孩子非空,则左孩子入队;

       若该结点的右孩子非空,则右孩子入队;

   }

3.A Story Between Two Templates 一提到队列,我们会不约而同的想到第三章扩展线性表中,我们通过模板类实现了队列。那么在这里如果使用队列存储,可不可以实现队列的模板类和二叉树的模板类之间的交互呢?具体实现过程怎样?

4.第四章的主要任务是实现树,队列的要求并不高。所以,我们不仅可以通过模板类来实现队列,我们也可以调用STL中的队列。

5.有问题随时补充~欢迎评论区留言~

***说明完毕***

二.用队列存储

2.1用模板类实现队列

2.1.1核心思路:

        首先,在已经构建bintree二叉树的基础上,我们要向代码中添加队列模板类的代码。这种感觉有点像拼积木。有一块积木可以实现二叉树,有一块积木可以实现队列,我们唯一要做的就是找到这两个木块之间的接口,然后把他们“拼起来”。 

        队列实现模板类的代码请参考博客北邮22信通:(11)第三章 3.3队列的实现_青山如墨雨如画的博客-CSDN博客

       下面也贴一份循环队列的。

template<class temp>
class circlequeue
{
private:
	temp data[queuesize];
	int front;
	int rear;
public:
	circlequeue() { this->front = this->rear = 0; }
	void enqueue(temp x);
	temp dequeue();
	temp getfront();
	int getlength();
	bool empty()
	{
		return this->front == this->rear ? true : false;
	}
};
 
template<class temp>
void circlequeue<temp>::enqueue(temp x)
{
	if ((this->rear + 1) % queuesize == this->front) throw "overflow";
	this->rear = (this->rear + 1) % queuesize;
	this->data[this->rear] = x;
}
 
template<class temp>
temp circlequeue<temp>::dequeue()
{
	if (this->rear == this->front)throw"underflow";
	this->front = (this->front + 1) % queuesize;
	return this->data[this->front];
}
 
template<class temp>
temp circlequeue<temp>::getfront()
{
	if (this->rear == this->front)throw"underflow";
	return this->data[(this->front + 1) % queuesize];
}
 
template<class temp>
int circlequeue<temp>::getlength()
{
	return (this->rear - this->front + queuesize);
}

有了队列的“积木”,我们现在考虑在bintree中调用队列的积木,来实现队列方式的层序遍历。

template<class temp>
void bintree<temp>::queuelevelorder(binnode<temp>* r)
{
	circlequeue<binnode<temp>*>q;
	if (r != NULL)q.enqueue(r);//根节点入队
	while (!q.empty())//如果队列非空
	{
		binnode<temp>* p = q.dequeue();//队首元素出队
		cout << p->data;//访问该元素
		if (p->leftchild != NULL)q.enqueue(p->leftchild);
        //若该节点的左孩子非空,则左孩子入队
		if (p->rightchild != NULL)q.enqueue(p->rightchild);
        //若该节点的右孩子非空,则右孩子入队
	}
}

2.1.2一个错误 

需要注意的是:circlequeue的<>中传入的参数一定是binnode<temp>*而不是binnode<temp>

        实际上这里传入的参数决定着队列中每一个元素的数据类型。实际上,访问二叉树时,对每个节点的引用都是通过指针来实现的,换句话说,虽然二叉树中每个节点的存储类型确实是binnode<temp>,但是我们想要访问二叉树的某个节点,还是通过每个节点内嵌指针来实现的,所以对节点的引用,注意,是对节点的“引用”,是通过binnode<temp>*指针来实现的。所以我们构建队列的时候,不妨直接让队列中每个元素的数据类型都是binnode<temp>*指针类型,从而构建了一个指针队列。        说明:如果队列circlequeue中传入的数据类型我就叛逆我就不写binnode<temp>*写成binnode<temp>,

  那么你将会收获一份报错:

         这个报错是什么意思呢?

        我们想,我们queuelevelorder这个层序遍历的函数传入的形参,必然是binnode<temp>*root但是呢,我队列定义的是binnode<temp>类型的,那所有队列中的成员函数传入的参数就都得是binnode<temp>类型的。你现在在enqueue函数中传入了一个binnode<temp>*,程序就蒙了:这是几个意思啊?不是应该给我一个binnode<temp>么?怎么给我binnode<temp>*这个指针啊??所以程序就报错了。

        这个问题我改了好久问了好几次助教 直接导致好多文章发不出来(找到合适借口(bishi

2.1.3完整代码

核心代码实现咯,我们来看完整代码

#include<iostream>
#define MAXSIZE 100000
using namespace std;
const int queuesize = 10000;
class student
{
private:
	int ID;
	string name;
public:
	int existence;
	student()
	{
		this->ID = 0;
		this->name = "unknown name";
		this->existence = 0;
	}
	student(int ID, string name)
	{
		this->ID = ID;
		this->name = name;
		this->existence = 1;
	}
	friend ostream& operator<<(ostream& output, student& s)
	{
		output << s.ID << " " << s.name << endl;
		return output;
	}
};

//队列
template<class temp>
class circlequeue
{
private:
	temp data[queuesize];
	int front;
	int rear;
public:
	circlequeue() { this->front = this->rear = 0; }
	void enqueue(temp x);
	temp dequeue();
	temp getfront();
	int getlength();
	bool empty()
	{
		return this->front == this->rear ? true : false;
	}
};

template<class temp>
void circlequeue<temp>::enqueue(temp x)
{
	if ((this->rear + 1) % queuesize == this->front) throw "overflow";
	this->rear = (this->rear + 1) % queuesize;
	this->data[this->rear] = x;
}

template<class temp>
temp circlequeue<temp>::dequeue()
{
	if (this->rear == this->front)throw"underflow";
	this->front = (this->front + 1) % queuesize;
	return this->data[this->front];
}

template<class temp>
temp circlequeue<temp>::getfront()
{
	if (this->rear == this->front)throw"underflow";
	return this->data[(this->front + 1) % queuesize];
}

template<class temp>
int circlequeue<temp>::getlength()
{
	return (this->rear - this->front + queuesize);
}

//二叉树
template<class temp>
struct binnode
{
	temp data;
	binnode<temp>* leftchild;
	binnode<temp>* rightchild;
};

template<class temp>
class bintree
{
private:
	void create(binnode<temp>*& r, temp data[], int i, int n);
	void release(binnode<temp>* r);
public:
	binnode<temp>* root;
	bintree(temp data[], int n);
	void preorder(binnode<temp>* r);
	void inorder(binnode<temp>* r);
	void postorder(binnode<temp>* r);
	void levelorder(binnode<temp>* r);
	void queuelevelorder(binnode<temp>* r);
	~bintree();
};

template<class temp>
void bintree<temp>::create(binnode<temp>*& r, temp data[], int i, int n)
{
	if (i <= n && data[i - 1].existence != 0)
	{
		r = new binnode<temp>;
		r->data = data[i - 1];
		r->leftchild = r->rightchild = NULL;
		create(r->leftchild, data, 2 * i, n);/*书上代码错误1:向函数传入实参时少传入一个n*/
		create(r->rightchild, data, 2 * i + 1, n);/*书上代码错误同上*/
	}
}

template<class temp>
bintree<temp>::bintree(temp data[], int n)
{
	create(this->root, data, 1, n);
}

template<class temp>
void bintree<temp>::preorder(binnode<temp>* r)
{
	if (r != NULL)
	{
		cout << r->data;
		preorder(r->leftchild);
		preorder(r->rightchild);
	}
}

template<class temp>
void bintree<temp>::inorder(binnode<temp>* r)
{
	if (r != NULL)
	{
		inorder(r->leftchild);
		cout << r->data;
		inorder(r->rightchild);
	}
}

template<class temp>
void bintree<temp>::postorder(binnode<temp>* r)
{
	if (r != NULL)
	{
		postorder(r->leftchild);
		postorder(r->rightchild);
		cout << r->data;
	}
}

template<class temp>
void bintree<temp>::levelorder(binnode<temp>* R)
{
	binnode<temp>* queue[MAXSIZE];
	int f = 0, r = 0;
	if (R != NULL)
		queue[++r] = R;
	while (f != r)
	{
		binnode<temp>* p = queue[++f];
		cout << p->data;//出队打印
		if (p->leftchild != NULL)
			queue[++r] = p->leftchild;
		if (p->rightchild != NULL)
			queue[++r] = p->rightchild;
	}
}

template<class temp>
void bintree<temp>::queuelevelorder(binnode<temp>* r)
{
	circlequeue<binnode<temp>*>q;
	if (r != NULL)q.enqueue(r);
	while (!q.empty())
	{
		binnode<temp>* p = q.dequeue();
		cout << p->data;
		if (p->leftchild != NULL)q.enqueue(p->leftchild);
		if (p->rightchild != NULL)q.enqueue(p->rightchild);
	}
}

template <class temp>
void bintree<temp>::release(binnode<temp>* r)
{
	if (r != NULL)
	{
		release(r->leftchild);
		release(r->rightchild);
		delete r;
	}
}

template<class temp>
bintree<temp>::~bintree()
{
	release(this->root);
}

int main()
{
	system("color 0A");
	student stu[5] = { {1,"zhang"},{2,"wang"},{3,"li"},{4,"zhao"},{5,"liu"} };
	bintree<student>bintreee(stu, 5);
	cout << "前序遍历:" << endl;
	bintreee.preorder(bintreee.root);
	cout << endl << "中序遍历:" << endl;
	bintreee.inorder(bintreee.root);
	cout << endl << "后序遍历:" << endl;
	bintreee.postorder(bintreee.root);
	cout << endl << "层序遍历:" << endl;
	bintreee.levelorder(bintreee.root);
	cout << endl << "队列层序遍历" << endl;
	bintreee.queuelevelorder(bintreee.root);
	return 0;
}

2.1.4运行结果

代码效果图:

程序运行结果:

 

 2.2使用STL中的队列

第四章核心内容是讲解树,所以对队列的要求没那么严格(我说的)

所以还是想通过STL中的队列简化一下代码,当然了,如果你的老师不让用,那我也没办法咯(〃'▽'〃)

2.2.1核心代码

template<class temp>
void bintree<temp>::STLlevelorder(binnode<temp>* r)
{
	queue<binnode<temp>*>q;
	if (r != NULL)q.push(r);
	while (!q.empty())
	{
		binnode<temp>* p = q.front();
		q.pop();
		cout << p->data;
		if (p->leftchild != NULL) q.push(p->leftchild);
		if (p->rightchild != NULL)q.push(p->rightchild);
	}
}

2.2.2完整代码

#include<iostream>
#include<queue>
#define MAXSIZE 100000
using namespace std;
const int queuesize = 10000;
class student
{
private:
	int ID;
	string name;
public:
	int existence;
	student()
	{
		this->ID = 0;
		this->name = "unknown name";
		this->existence = 0;
	}
	student(int ID, string name)
	{
		this->ID = ID;
		this->name = name;
		this->existence = 1;
	}
	friend ostream& operator<<(ostream& output, student& s)
	{
		output << s.ID << " " << s.name << endl;
		return output;
	}
};
//二叉树
template<class temp>
struct binnode
{
	temp data;
	binnode<temp>* leftchild;
	binnode<temp>* rightchild;
};

template<class temp>
class bintree
{
private:
	void create(binnode<temp>*& r, temp data[], int i, int n);
	void release(binnode<temp>* r);
public:
	binnode<temp>* root;
	bintree(temp data[], int n);
	void preorder(binnode<temp>* r);
	void inorder(binnode<temp>* r);
	void postorder(binnode<temp>* r);
	void levelorder(binnode<temp>* r);
	void STLlevelorder(binnode<temp>* r);
	~bintree();
};

template<class temp>
void bintree<temp>::create(binnode<temp>*& r, temp data[], int i, int n)
{
	if (i <= n && data[i - 1].existence != 0)
	{
		r = new binnode<temp>;
		r->data = data[i - 1];
		r->leftchild = r->rightchild = NULL;
		create(r->leftchild, data, 2 * i, n);/*书上代码错误1:向函数传入实参时少传入一个n*/
		create(r->rightchild, data, 2 * i + 1, n);/*书上代码错误同上*/
	}
}

template<class temp>
bintree<temp>::bintree(temp data[], int n)
{
	create(this->root, data, 1, n);
}

template<class temp>
void bintree<temp>::preorder(binnode<temp>* r)
{
	if (r != NULL)
	{
		cout << r->data;
		preorder(r->leftchild);
		preorder(r->rightchild);
	}
}

template<class temp>
void bintree<temp>::inorder(binnode<temp>* r)
{
	if (r != NULL)
	{
		inorder(r->leftchild);
		cout << r->data;
		inorder(r->rightchild);
	}
}

template<class temp>
void bintree<temp>::postorder(binnode<temp>* r)
{
	if (r != NULL)
	{
		postorder(r->leftchild);
		postorder(r->rightchild);
		cout << r->data;
	}
}

template<class temp>
void bintree<temp>::levelorder(binnode<temp>* R)
{
	binnode<temp>* queue[MAXSIZE];
	int f = 0, r = 0;
	if (R != NULL)
		queue[++r] = R;
	while (f != r)
	{
		binnode<temp>* p = queue[++f];
		cout << p->data;//出队打印
		if (p->leftchild != NULL)
			queue[++r] = p->leftchild;
		if (p->rightchild != NULL)
			queue[++r] = p->rightchild;
	}
} 

template<class temp>
void bintree<temp>::STLlevelorder(binnode<temp>* r)
{
	queue<binnode<temp>*>q;
	if (r != NULL)q.push(r);
	while (!q.empty())
	{
		binnode<temp>* p = q.front();
		q.pop();
		cout << p->data;
		if (p->leftchild != NULL) q.push(p->leftchild);
		if (p->rightchild != NULL)q.push(p->rightchild);
	}
}

template <class temp>
void bintree<temp>::release(binnode<temp>* r)
{
	if (r != NULL)
	{
		release(r->leftchild);
		release(r->rightchild);
		delete r;
	}
}

template<class temp>
bintree<temp>::~bintree()
{
	release(this->root);
}

int main()
{
	system("color 0A");
	student stu[5] = { {1,"zhang"},{2,"wang"},{3,"li"},{4,"zhao"},{5,"liu"} };
	bintree<student>bintreee(stu, 5);
	cout << "前序遍历:" << endl;
	bintreee.preorder(bintreee.root);
	cout << endl << "中序遍历:" << endl;
	bintreee.inorder(bintreee.root);
	cout << endl << "后序遍历:" << endl;
	bintreee.postorder(bintreee.root);
	cout << endl << "层序遍历:" << endl;
	bintreee.levelorder(bintreee.root);
	cout << endl << "STL层序遍历" << endl;
	bintreee.STLlevelorder(bintreee.root);
	return 0;
}

2.2.3运行结果

代码效果图:

 运行结果:

三.用数组存储

3.1核心代码:

template<class temp>
void bintree<temp>::arraylevelorder(binnode<temp>* root)
{
	binnode<temp>* queue[MAXSIZE];
	int f = 0, r = 0;//初始化空队列
	if (root != NULL)queue[++r] = root;//根节点入队
	while (f != r)
	{
		binnode<temp>* p = queue[++f];//队首元素出队
		cout << p->data;//访问根节点
		if (p->leftchild != NULL)queue[++r] = p->leftchild;//左孩子入队
		if (p->rightchild != NULL)queue[++r] = p->rightchild;//右孩子入队
	}
}

3.2完整代码

#include<iostream>
#include<queue>
#define MAXSIZE 100000
using namespace std;
const int queuesize = 10000;
class student
{
private:
	int ID;
	string name;
public:
	int existence;
	student()
	{
		this->ID = 0;
		this->name = "unknown name";
		this->existence = 0;
	}
	student(int ID, string name)
	{
		this->ID = ID;
		this->name = name;
		this->existence = 1;
	}
	friend ostream& operator<<(ostream& output, student& s)
	{
		output << s.ID << " " << s.name << endl;
		return output;
	}
};

template<class temp>
struct binnode
{
	temp data;
	binnode<temp>* leftchild;
	binnode<temp>* rightchild;
};

template<class temp>
class bintree
{
private:
	void create(binnode<temp>*& r, temp data[], int i, int n);
	void release(binnode<temp>* r);
public:
	binnode<temp>* root;
	bintree(temp data[], int n);
	void preorder(binnode<temp>* r);
	void inorder(binnode<temp>* r);
	void postorder(binnode<temp>* r);
	void levelorder(binnode<temp>* r);
	void arraylevelorder(binnode<temp>* r);
	~bintree();
};

template<class temp>
void bintree<temp>::create(binnode<temp>*& r, temp data[], int i, int n)
{
	if (i <= n && data[i - 1].existence != 0)
	{
		r = new binnode<temp>;
		r->data = data[i - 1];
		r->leftchild = r->rightchild = NULL;
		create(r->leftchild, data, 2 * i, n);/*书上代码错误1:向函数传入实参时少传入一个n*/
		create(r->rightchild, data, 2 * i + 1, n);/*书上代码错误同上*/
	}
}

template<class temp>
bintree<temp>::bintree(temp data[], int n)
{
	create(this->root, data, 1, n);
}

template<class temp>
void bintree<temp>::preorder(binnode<temp>* r)
{
	if (r != NULL)
	{
		cout << r->data;
		preorder(r->leftchild);
		preorder(r->rightchild);
	}
}

template<class temp>
void bintree<temp>::inorder(binnode<temp>* r)
{
	if (r != NULL)
	{
		inorder(r->leftchild);
		cout << r->data;
		inorder(r->rightchild);
	}
}

template<class temp>
void bintree<temp>::postorder(binnode<temp>* r)
{
	if (r != NULL)
	{
		postorder(r->leftchild);
		postorder(r->rightchild);
		cout << r->data;
	}
}

template<class temp>
void bintree<temp>::levelorder(binnode<temp>* R)
{
	binnode<temp>* queue[MAXSIZE];
	int f = 0, r = 0;
	if (R != NULL)
		queue[++r] = R;
	while (f != r)
	{
		binnode<temp>* p = queue[++f];
		cout << p->data;//出队打印
		if (p->leftchild != NULL)
			queue[++r] = p->leftchild;
		if (p->rightchild != NULL)
			queue[++r] = p->rightchild;
	}
}

template<class temp>
void bintree<temp>::arraylevelorder(binnode<temp>* root)
{
	binnode<temp>* queue[MAXSIZE];
	int f = 0, r = 0;
	if (root != NULL)queue[++r] = root;
	while (f != r)
	{
		binnode<temp>* p = queue[++f];
		cout << p->data;
		if (p->leftchild != NULL)queue[++r] = p->leftchild;
		if (p->rightchild != NULL)queue[++r] = p->rightchild;
	}
}

template <class temp>
void bintree<temp>::release(binnode<temp>* r)
{
	if (r != NULL)
	{
		release(r->leftchild);
		release(r->rightchild);
		delete r;
	}
}

template<class temp>
bintree<temp>::~bintree()
{
	release(this->root);
}

int main()
{
	system("color 0A");
	student stu[5] = { {1,"zhang"},{2,"wang"},{3,"li"},{4,"zhao"},{5,"liu"} };
	bintree<student>bintreee(stu, 5);
	cout << "前序遍历:" << endl;
	bintreee.preorder(bintreee.root);
	cout << endl << "中序遍历:" << endl;
	bintreee.inorder(bintreee.root);
	cout << endl << "后序遍历:" << endl;
	bintreee.postorder(bintreee.root);
	cout << endl << "层序遍历:" << endl;
	bintreee.levelorder(bintreee.root);
	cout << endl << "数组层序遍历" << endl;
	bintreee.arraylevelorder(bintreee.root);
	return 0;
}

3.3运行结果:

代码效果图:

运行结果:

 四.完整代码

#include<iostream>
#include<queue>
#define MAXSIZE 100000
using namespace std;
const int queuesize = 10000;
class student
{
private:
	int ID;
	string name;
public:
	int existence;
	student()
	{
		this->ID = 0;
		this->name = "unknown name";
		this->existence = 0;
	}
	student(int ID, string name)
	{
		this->ID = ID;
		this->name = name;
		this->existence = 1;
	}
	friend ostream& operator<<(ostream& output, student& s)
	{
		output << s.ID << " " << s.name << endl;
		return output;
	}
};

//队列
template<class temp>
class circlequeue
{
private:
	temp data[queuesize];
	int front;
	int rear;
public:
	circlequeue() { this->front = this->rear = 0; }
	void enqueue(temp x);
	temp dequeue();
	temp getfront();
	int getlength();
	bool empty()
	{
		return this->front == this->rear ? true : false;
	}
};

template<class temp>
void circlequeue<temp>::enqueue(temp x)
{
	if ((this->rear + 1) % queuesize == this->front) throw "overflow";
	this->rear = (this->rear + 1) % queuesize;
	this->data[this->rear] = x;
}

template<class temp>
temp circlequeue<temp>::dequeue()
{
	if (this->rear == this->front)throw"underflow";
	this->front = (this->front + 1) % queuesize;
	return this->data[this->front];
}

template<class temp>
temp circlequeue<temp>::getfront()
{
	if (this->rear == this->front)throw"underflow";
	return this->data[(this->front + 1) % queuesize];
}

template<class temp>
int circlequeue<temp>::getlength()
{
	return (this->rear - this->front + queuesize);
}

//二叉树
template<class temp>
struct binnode
{
	temp data;
	binnode<temp>* leftchild;
	binnode<temp>* rightchild;
};

template<class temp>
class bintree
{
private:
	void create(binnode<temp>*& r, temp data[], int i, int n);
	void release(binnode<temp>* r);
public:
	binnode<temp>* root;
	bintree(temp data[], int n);
	void preorder(binnode<temp>* r);
	void inorder(binnode<temp>* r);
	void postorder(binnode<temp>* r);
	void levelorder(binnode<temp>* r);
	void arraylevelorder(binnode<temp>* r);
	void queuelevelorder(binnode<temp>* r);
	void STLlevelorder(binnode<temp>* r);
	~bintree();
};

template<class temp>
void bintree<temp>::create(binnode<temp>*& r, temp data[], int i, int n)
{
	if (i <= n && data[i - 1].existence != 0)
	{
		r = new binnode<temp>;
		r->data = data[i - 1];
		r->leftchild = r->rightchild = NULL;
		create(r->leftchild, data, 2 * i, n);/*书上代码错误1:向函数传入实参时少传入一个n*/
		create(r->rightchild, data, 2 * i + 1, n);/*书上代码错误同上*/
	}
}

template<class temp>
bintree<temp>::bintree(temp data[], int n)
{
	create(this->root, data, 1, n);
}

template<class temp>
void bintree<temp>::preorder(binnode<temp>* r)
{
	if (r != NULL)
	{
		cout << r->data;
		preorder(r->leftchild);
		preorder(r->rightchild);
	}
}

template<class temp>
void bintree<temp>::inorder(binnode<temp>* r)
{
	if (r != NULL)
	{
		inorder(r->leftchild);
		cout << r->data;
		inorder(r->rightchild);
	}
}

template<class temp>
void bintree<temp>::postorder(binnode<temp>* r)
{
	if (r != NULL)
	{
		postorder(r->leftchild);
		postorder(r->rightchild);
		cout << r->data;
	}
}

template<class temp>
void bintree<temp>::levelorder(binnode<temp>* R)
{
	binnode<temp>* queue[MAXSIZE];
	int f = 0, r = 0;
	if (R != NULL)
		queue[++r] = R;
	while (f != r)
	{
		binnode<temp>* p = queue[++f];
		cout << p->data;//出队打印
		if (p->leftchild != NULL)
			queue[++r] = p->leftchild;
		if (p->rightchild != NULL)
			queue[++r] = p->rightchild;
	}
}

template<class temp>
void bintree<temp>::arraylevelorder(binnode<temp>* root)
{
	binnode<temp>* queue[MAXSIZE];
	int f = 0, r = 0;
	if (root != NULL)queue[++r] = root;
	while (f != r)
	{
		binnode<temp>* p = queue[++f];
		cout << p->data;
		if (p->leftchild != NULL)queue[++r] = p->leftchild;
		if (p->rightchild != NULL)queue[++r] = p->rightchild;
	}
}

template<class temp>
void bintree<temp>::queuelevelorder(binnode<temp>* r)
{
	circlequeue<binnode<temp>*>q;
	if (r != NULL)q.enqueue(r);
	while (!q.empty())
	{
		binnode<temp>* p = q.dequeue();
		cout << p->data;
		if (p->leftchild != NULL)q.enqueue(p->leftchild);
		if (p->rightchild != NULL)q.enqueue(p->rightchild);
	}
}

template<class temp>
void bintree<temp>::STLlevelorder(binnode<temp>* r)
{
	queue<binnode<temp>*>q;
	if (r != NULL)q.push(r);
	while (!q.empty())
	{
		binnode<temp>* p = q.front();
		q.pop();
		cout << p->data;
		if (p->leftchild != NULL) q.push(p->leftchild);
		if (p->rightchild != NULL)q.push(p->rightchild);
	}
}

template <class temp>
void bintree<temp>::release(binnode<temp>* r)
{
	if (r != NULL)
	{
		release(r->leftchild);
		release(r->rightchild);
		delete r;
	}
}

template<class temp>
bintree<temp>::~bintree()
{
	release(this->root);
}

int main()
{
	system("color 0A");
	student stu[5] = { {1,"zhang"},{2,"wang"},{3,"li"},{4,"zhao"},{5,"liu"} };
	bintree<student>bintreee(stu, 5);
	cout << "前序遍历:" << endl;
	bintreee.preorder(bintreee.root);
	cout << endl << "中序遍历:" << endl;
	bintreee.inorder(bintreee.root);
	cout << endl << "后序遍历:" << endl;
	bintreee.postorder(bintreee.root);
	cout << endl << "层序遍历:" << endl;
	bintreee.levelorder(bintreee.root);
	cout << endl << "数组层序遍历" << endl;
	bintreee.arraylevelorder(bintreee.root);
	cout << endl << "队列层序遍历" << endl;
	bintreee.queuelevelorder(bintreee.root);
	cout << endl << "STL层序遍历" << endl;
	bintreee.STLlevelorder(bintreee.root);
	return 0;
}

运行效果:

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/455047.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

FL Studio电音编曲软件V21中文完整版 安装下载教程

目前水果软件最版本是FL Studio 21&#xff0c;它让你的计算机就像是全功能的录音室&#xff0c;大混音盘&#xff0c;非常先进的制作工具&#xff0c;让你的音乐突破想象力的限制。喜欢音乐制作的小伙伴千万不要错过这个功能强大&#xff0c;安装便捷的音乐软件哦&#xff01;…

PTA L2-045 堆宝塔 (25 分)

堆宝塔游戏是让小朋友根据抓到的彩虹圈的直径大小&#xff0c;按照从大到小的顺序堆起宝塔。但彩虹圈不一定是按照直径的大小顺序抓到的。聪明宝宝采取的策略如下&#xff1a; 首先准备两根柱子&#xff0c;一根 A 柱串宝塔&#xff0c;一根 B 柱用于临时叠放。把第 1 块彩虹圈…

掌握ChatGPT:全面指南和GPT-3.5与GPT-Plus的对比分析

在人工智能领域&#xff0c;最近的一大重磅炸弹是OpenAI发布了GPT-4架构下的ChatGPT。这款先进的自然语言处理模型已经引起了很多关注&#xff0c;让我们来深入了解怎么使用这个强大的工具&#xff0c;以及比较GPT-3.5与GPT-Plus的差异。 什么是ChatGPT&#xff1f; ChatGPT是…

JavaScript经典教程(三)-- JavaScript -- 基础数据类型详解

183&#xff1a;基础数据类型详解 1、复习 1、全局变量&#xff1a; 依托于window环境下的的变量。 在window大环境下&#xff08;即最外层&#xff09;申明的变量a&#xff0c;即为全局变量。 2、window&#xff1a; window下的变量为全局变量。 window可省略。 3、作用域…

021 - C++ 构造函数

我们继续学习 C 的面向对象编程&#xff0c;本期主要是讲其中的 构造函数。 什么是构造函数呢&#xff1f; 构造函数基本上是一种特殊类型的方法&#xff0c;它在每次实例化对象时运行。 我们直接来看一个例子吧。 例子时间 我们将要通过创建一个 Entity 类来深入了解这个…

vscode python3.6配置pcl点云库 obj3d模型转pcd点云图

配置vscode python3.6的环境我就跳过了,网上都有 1.下载PCL1.9 github:pcl-1.9.1 百度云:PCL-1.9.1-AllInOne-msvc2017-win64提取码adcx 2.安装硬盘任意位置,我是E盘,在安装过程中会弹出openni的安装提示,将它安装路径选择在E:\PCL 1.9.1\3rdParty\OpenNI2,等待安装完成 3.…

.netCHATING 10.4 for NET6-7.0-Crack

.NET 6.0图表支持--dotnetcharting .netCHATING 10.4添加了.NET 6.0图表nuget包和.NET 6.0图表示例包&#xff08;需要Visual Studio 2022&#xff09;&#xff0c;.NET 5是.NET Core 3.1和.NET Framework 4.8的继任者&#xff0c;旨在为.NET开发人员提供新的跨平台开发体验。…

Mysql列的类型定义(字符串类型)

文章目录 一、CHAR 类型和 VARCHAR 类型 1.字符串字符(M)2.实战类型二、TEXT 类型 1.类型表2.特别注意3.实战建议4.实战练习三、ENUM 和 SET 类型 1.ENUM类型2.SET类型总结 一、CHAR 类型和 VARCHAR 类型 CHAR类型和VARCHAR类型都在创建表时指定了最大长度&#xff0c;其基本形…

Java版工程行业管理系统源码-专业的工程管理软件-提供一站式服务

Java版工程项目管理系统 Spring CloudSpring BootMybatisVueElementUI前后端分离 功能清单如下&#xff1a; 首页 工作台&#xff1a;待办工作、消息通知、预警信息&#xff0c;点击可进入相应的列表 项目进度图表&#xff1a;选择&#xff08;总体或单个&#xff09;项目显示…

对数据结构的初步认识

前言: 牛牛开始更新数据结构的知识了.本专栏后续会分享用c语言实现顺序表,链表,二叉树,栈和队列,排序算法等相关知识,欢迎友友们互相学习,可以私信互相讨论哦! &#x1f388;个人主页:&#x1f388; :✨✨✨初阶牛✨✨✨ &#x1f43b;推荐专栏: &#x1f354;&#x1f35f;&a…

拿下多家车企定点!4D毫米波雷达「域」系统首发出道

从1R、2R、3R到整车360感知方案&#xff0c;毫米波雷达的前装市场需求量依然保持着快速增长的态势。 高工智能汽车研究院监测数据显示&#xff0c;2022年中国市场&#xff08;不含进出口&#xff09;前装标配搭载ADAS毫米波雷达&#xff08;前向后向盲区&#xff09;交付1795.…

mov是什么格式的视频,mov怎么转mp4

mov是什么格式的视频&#xff0c;MOV即QuickTime影片格式&#xff0c;它是Apple公司开发的一种音频、视频文件格式&#xff0c;用于存储常用数字媒体类型。MOV部分编码在没有quicktime的电脑中不能播放&#xff0c;不能后期剪辑制作MP4的通用率高于MOV格式支持MP4格式的播放器绝…

获得将要生成的资源的GUID

1&#xff09;获得将要生成的资源的GUID ​2&#xff09;多个小资源包合并为大资源包的疑问 3&#xff09;模型Meta中的hasExtraRoot参数的作用和历史原因 4&#xff09;合批注意点 这是第333篇UWA技术知识分享的推送&#xff0c;也是《厚积薄发 | 技术分享》第三回&#xff0c…

PMP-上班摸鱼整理的知识点

1、主要解决流程:问题-风险-变更: 先分析是问题还是风险&#xff0c;解决问题、可以减少新的风险&#xff0c;登记风险&#xff0c;可以随时应对问题,2、变更管理流程 变更原则: 需提正式变更申请&#xff0c;先分析评估后变更&#xff0c;不改变基准项目经理审批&#xff0c;改…

2-07 使用JMeter测试单节点与集群的并发异常率

2-07 使用JMeter测试单节点与集群的并发异常率 [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-YVXaAkn2-1682304913240)(https://static.editool.cn/upload/47093438fcec4683a50626ae46a49942/pic-371.jpg)] [外链图片转存失败,源站可能有防盗链机制…

一些海洋资料收集及磁力tiff的数据提取

以下资料都来自于网络和公开发表的文献&#xff0c;欢迎下载 1、第一批至第十一批农业部国家级种质资源保护区的范围&#xff1a; 链接&#xff1a;https://pan.baidu.com/s/1fGcVcdbOUb3tOlYB8d4JUg 提取码&#xff1a;kgix 2、EGM2008 链接&#xff1a;https://pan.baidu…

matlab实现在画图的图窗里播放点数据的循环

数据准备 我准备好了打包的数据文件供演示下载&#xff0c;只需要小白式的操作。传送门 文件里集成了处理好的点云文件&#xff0c;如果你想显示曲线&#xff0c;只需要把你的数据批量更换上去即可。   每一个里面包含了以下信息&#xff1a; location&#xff1a;不同点的…

【GDOUCTF2023】wp

【GDOUCTF2023】 WEB hate eat snake js小游戏&#xff0c;玩游戏得到flag&#xff0c;修改一下js源码 EZ WEB 访问 /super-secret-route-nobody-will-guess 发送PUT请求&#xff1a; 受不了一点 <?php error_reporting(0); header("Content-type:text/html;char…

如何创建 SAP PM 通知

目的 了解如何根据创建通知的要求将通知详细信息从一个屏幕发送到另一个屏幕。为了解释这一点&#xff0c;我们将引导您完成以下步骤。 使用 title&#xff08;&#xff09; 更改屏幕标题删除“引用”组框根据交易自定义屏幕添加用于复制和发送通知详细信息的函数 在脚本文件…

面试官灵魂一问:SELECT COUNT(*) 会造成全表扫描吗?

SELECT COUNT(*) 会造成全表扫描吗&#xff1f; 前言SQL 选用索引的执行成本如何计算实例说明总结 前言 SELECT COUNT(*)会不会导致全表扫描引起慢查询呢&#xff1f; SELECT COUNT(*) FROM SomeTable网上有一种说法&#xff0c;针对无 where_clause 的 COUNT(*)&#xff0…