北邮22信通:二叉树层序遍历的非递归算法:A Story Between Two Templates

news2024/9/21 4:38:20

北邮22信通一枚~   

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

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

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

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

目录

一.总纲

二.用队列存储

2.1用模板类实现队列

2.1.1核心思路:

2.1.2一个错误 

2.1.3完整代码

2.1.4运行结果

2.1.5补充:链队列实现

 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>*这个指针啊??所以程序就报错了。

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

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.1.5补充:链队列实现

        本篇文章主要用循环队列实现的,其实也可以用链队列实现,下面扔一个链队列实现的代码,uu们可以拿去玩玩~

#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 node
{
	temp data;
	node<temp>* next;
};

template <class temp>
class linkqueue
{
private:
	node<temp>* front;
	node<temp>* rear;
public:
	linkqueue();
	~linkqueue();
	void enqueue(temp x);
	temp dequeue();
	temp getfront();
	int getlength();
	bool empty()
	{
		return (this->front == this->rear) ? true : false;
	}
};

template<class temp>
linkqueue<temp>::linkqueue()
{
	this->front = this->rear = new node <temp>;
	this->front->next = NULL;
}

template<class temp>
void linkqueue<temp>::enqueue(temp x)
{
	this->rear->next = new node<temp>;
	this->rear = this->rear->next;
	this->rear->data = x;
	this->rear->next = NULL;
}

template<class temp>
temp linkqueue<temp>::dequeue()
{
	node<temp>* p = this->front->next;
	if (!p)throw"underflow";/*如果为空队列,抛出异常*/
	this->front->next = p->next;
	temp x = p->data;
	delete p;
	if (!(this->front->next))
		this->rear = this->front;
	return x;
}

template<class temp>
temp linkqueue<temp>::getfront()
{
	if (!this->front->next)throw"overflow";
	return this->front->next->data;
}

template<class temp>
linkqueue<temp>::~linkqueue()
{
	while (this->front != NULL)
	{
		this->rear = this->front->next;
		delete this->front;
		this->front = this->rear;
	}
}

template<class temp>
int linkqueue<temp>::getlength()
{
	node<temp>* p = this->front;
	int cnt = 0;
	while (p != this->rear)
	{
		cnt++;
		p = p->next;
	}
	return cnt;
}

//二叉树
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)
{
	linkqueue<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;
}#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 node
{
	temp data;
	node<temp>* next;
};

template <class temp>
class linkqueue
{
private:
	node<temp>* front;
	node<temp>* rear;
public:
	linkqueue();
	~linkqueue();
	void enqueue(temp x);
	temp dequeue();
	temp getfront();
	int getlength();
	bool empty()
	{
		return (this->front == this->rear) ? true : false;
	}
};

template<class temp>
linkqueue<temp>::linkqueue()
{
	this->front = this->rear = new node <temp>;
	this->front->next = NULL;
}

template<class temp>
void linkqueue<temp>::enqueue(temp x)
{
	this->rear->next = new node<temp>;
	this->rear = this->rear->next;
	this->rear->data = x;
	this->rear->next = NULL;
}

template<class temp>
temp linkqueue<temp>::dequeue()
{
	node<temp>* p = this->front->next;
	if (!p)throw"underflow";/*如果为空队列,抛出异常*/
	this->front->next = p->next;
	temp x = p->data;
	delete p;
	if (!(this->front->next))
		this->rear = this->front;
	return x;
}

template<class temp>
temp linkqueue<temp>::getfront()
{
	if (!this->front->next)throw"overflow";
	return this->front->next->data;
}

template<class temp>
linkqueue<temp>::~linkqueue()
{
	while (this->front != NULL)
	{
		this->rear = this->front->next;
		delete this->front;
		this->front = this->rear;
	}
}

template<class temp>
int linkqueue<temp>::getlength()
{
	node<temp>* p = this->front;
	int cnt = 0;
	while (p != this->rear)
	{
		cnt++;
		p = p->next;
	}
	return cnt;
}

//二叉树
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)
{
	linkqueue<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;
}

 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/467597.html

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

相关文章

丁鹿学堂:使用vite手动构建vue项目的注意事项和步骤总结

使用yarn 默认安装了nodeJS环境&#xff0c;使用yarn&#xff0c;比npm更好用。 npm install --global yarn使用yarn按钻过vite yarn add -D vite使用yarn初始化项目 yarn init -y安装vite yarn add vite -D安装vue yarn add vue项目目录&#xff1a; 创建index.html sr…

分享一个有意思的文字飞入动画(模仿水滴融合)

先上效果图&#xff1a; 代码如下&#xff1a; <!DOCTYPE html> <html lang"en"> <head><meta charset"UTF-8"><title>Title</title><style>* {margin: 0;padding: 0;box-sizing: border-box;}:root {--text-…

前后端分离demo 旅馆管理系统(Angular+Springboot)

模型设计 旅馆管理系统&#xff0c;主要涉及到登记入住&#xff0c;退房以及客房和客人信息管理&#xff1b;经过分析抽像出涉及到的实体以及各实体之间的关系&#xff1a;   可以看出整个业务以客房为中心&#xff0c;入住&#xff0c;退房&#xff0c;定价&#xff0c;收费…

Build an SAP Fiori App(一)后面更新中

1.登录 SAP BTP Trial 地址&#xff1a; https://account.hanatrial.ondemand.com 流程可以参考 点击 serviced marketplace 搜索studio 点击创建 点击创建&#xff0c;点击view subscription 点击go to application 创建完成后 添加新链接 Field Value Name ES5 - if you’…

Shell基础入门实战

写在前面 好久没在项目内做自动化了&#xff0c;主要是现阶段在项目内做自动化收益不大&#xff0c;最近开发做batch run的正好缺人&#xff0c;我看了一下代码&#xff0c;就是通过代码读取jar包和远程服务器连接&#xff0c;然后通过shell脚本&#xff0c;向数据库插入数据&a…

如何成为一名优秀的接口自动化测试工程师?了解这些技能是关键

摘要&#xff1a; 随着互联网行业的不断发展&#xff0c;越来越多的应用程序通过API接口提供服务。因此&#xff0c;接口自动化测试成为了保障软件质量的重要环节。本文将介绍接口自动化测试所需掌握的技能&#xff0c;以及相关的历史进程。 B站首推&#xff01;2023最详细自…

什么是 Java 垃圾回收器~

什么是 Java 垃圾回收器 Java 垃圾回收器是 Java 虚拟机 (JVM) 的三个重要模块 (另外两个是解释器和多线程机制) 之一&#xff0c;为应用程序提供内存的自动分配 (Memory Allocation)、自动回收 (Garbage Collect) 功能&#xff0c;这两个操作都发生在 Java 堆上 (一段内存快)…

sqoop安装

文章目录 1. 上传安装包至虚拟机2. 解压安装包到指定路径3. 修改目录名4. 配置环境变量5. 修改配置文件6. 拷贝mysql驱动包7. 验证安装是否成功8. 测试sqoop连接mysql 注&#xff1a;sqoop安装的前提条件是环境已安装java和hadoop 1. 上传安装包至虚拟机 上传安装包sqoop-1.4.…

信通初试第一:无科研无竞赛一战上岸上海交大819学硕感悟

笔者来自通信考研小马哥23上交819全程班学员 信通初试第一&#xff1a;无科研无竞赛一战上岸上海交大819学硕感悟 原创2023-04-27 11:04通信考研小马哥 笔者来自通信考研小马哥23上交819全程班学员 本人情况&#xff1a; 本人是19届交本&#xff0c;本科成绩很差&#xff0c;…

赎金信(Hash的应用)

给你两个字符串&#xff1a;ransomNote 和 magazine &#xff0c;判断 ransomNote 能不能由 magazine 里面的字符构成。 如果可以&#xff0c;返回 true &#xff1b;否则返回 false 。 magazine 中的每个字符只能在 ransomNote 中使用一次。 来源&#xff1a;力扣&#xff0…

Java 实现 YoloV7 目标检测

1 OpenCV 环境的准备 这个项目中需要用到 opencv 进行图片的读取与处理操作&#xff0c;因此我们需要先配置一下 opencv 在 java 中运行的配置。 首先前往 opencv 官网下载 opencv-4.6 &#xff1a;点此下载&#xff1b;下载好后仅选择路径后即可完成安装。 此时将 opencv\b…

WMS是什么?

WMS&#xff08;Warehouse Management System&#xff09;中文译作仓库管理系统&#xff0c;是一种专用于物流仓储管理的IT系统。它主要应用于企业物流中心、配送中心、供应商物料储备中心、电子商务配送中心等仓库管理过程中。 WMS系统可以帮助企业管理和控制其物流仓储流程。…

线程池的设计

一.什么是线程池? 线程池就是创建若干个可执行的线程放到容器中&#xff0c;有任务处理时&#xff0c;会提交到线程池中的任务队列中&#xff0c;线程处理完不是销毁&#xff0c;而是阻塞等待下一个任务。 二.为何要使用线程池? 降低资源消耗。重复利用创建好的线程减少线…

NLP原理和应用入门:paddle(梯度裁剪、ONNX协议、动态图转静态图、推理部署)

目录 一、梯度裁剪 1.1设定范围值裁剪 1. 全部参数裁剪&#xff08;默认&#xff09; 2. 部分参数裁剪 1.2 通过L2范数裁剪 1.3通过全局L2范数裁剪 二. 模型导出ONNX协议 三、动态图转静态图 3.1两种图定义 3.2 什么场景下需要动态图转静态图 3.3为什么动态图模式越来…

k8s 部署 seata1.6.0 集群 基于 nacos 注册中心 + mysql 数据库

k8s 部署 seata1.6.0 集群 基于 nacos 注册中心 mysql 数据库 大纲 1 镜像制作2 准备configmap3 准备deploy 部署文件4 部署seata到k8s 镜像制作 下载seata 选择1.6.0。下载后得到 seata-server-1.6.0.zip 已经上传到百度云盘 下载地址&#xff1a;http://seata.io/zh-cn…

Maven 依赖下载失败解决方案——配置国内源 + 具体解决办法

目录 前言 一、配置 Maven 国内源 二、重新下载jar包 三、其他问题 前言 最近发现 spring-boot 框架更新到 2.7.11 了&#xff0c;由于以前一直使用的是 2.7.9 &#xff0c;所以一直出现依赖下载失败的问题&#xff0c;实际上这是由于 IDEA 会先加载之前下载好的依赖&#xf…

openharmony内核中不一样的双向链表

不一样的双向链表 链表初识别遍历双向链表参考链接 链表初识别 最近看openharmony的内核源码时看到一个有意思的双向链表&#xff0c;结构如下 typedef struct LOS_DL_LIST{struct LOS_DL_LIST *pstPrev; //前驱节点struct LOS_DL_LIST *pstNext; //后继节点 }LOS_DL_LIST;不…

FPGA入门系列12--RAM的使用

文章简介 本系列文章主要针对FPGA初学者编写&#xff0c;包括FPGA的模块书写、基础语法、状态机、RAM、UART、SPI、VGA、以及功能验证等。将每一个知识点作为一个章节进行讲解&#xff0c;旨在更快速的提升初学者在FPGA开发方面的能力&#xff0c;每一个章节中都有针对性的代码…

Spring IOC DI - 整合MyBatis

Spring IOC目录 主要内容Spring 框架介绍Spring 框架的优势(对比以前项目的缺点)Spring 框架引入历史发展框架学习三要素Spring 模块介绍 Spring IoC/DI - 引入IoC/DI 概念辨析使用IoC/DI的好处IoC/DI具体应用场景 Spring IoC/DI - 代码实现环境准备Spring 框架环境搭建创建Mav…

图的遍历和应用

文章目录 图的遍历深度优先遍历对于无向图的邻接矩阵的深度优先遍历无向非连通图的深度优先遍历 对于无向图的邻接表的深度优先遍历非递归实现深度优先遍历无向图的邻接矩阵代码实现无向图的邻接表代码实现递归和非递归的同异 广度优先遍历邻接表BFS邻接矩阵BFS 图的应用生成树…