⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 a.cpp

📁 深度遍历一个二叉树
💻 CPP
字号:
#include<iostream.h>
#define maxnode 10
int a[maxnode];
struct node 
{
	int mark;
	node*next;
};
struct Graph
{
	int n,e;
	node *graphvertex[maxnode];
};
Graph*creatgraph()
{
	Graph*graph=new Graph;
	if(graph==NULL)
	{
		cout<<"memory is not available"<<endl;
		return NULL;
	}

	cout<<"please put in the node of the graph "<<endl;
	cin>>graph->n;
	cout<<"please put in the edge of the graph"<<endl;
	cin>>graph->e;
	for(int i=0;i<graph->n;i++)
	{
		node*tp=new node;
		tp->mark=i;
		tp->next=NULL;
		graph->graphvertex[i]=tp;
	}
	for(int j=0;j<graph->e;j++)
	{
		node*temp=new node;
		if(temp==NULL)
		{
			cout<<"no memory available"<<endl;
			return NULL;
		}
	temp->mark=0;
	temp->next=NULL;
	int a,b;
	cout<<"please put in the edge from a to b "<<endl;
	cin>>a>>b;
	temp->mark=b;
	temp->next=graph->graphvertex[a]->next;
	graph->graphvertex[a]->next=temp;
	node*s=new node;
		if(s==NULL)
		{
			cout<<"no memory available"<<endl;
			return NULL;
		}
	s->mark=0;
	s->next=NULL;
	s->mark=a;
	s->next=graph->graphvertex[b]->next;
	graph->graphvertex[b]->next=s;
	}
	return graph;
}
int count=0;
travel(Graph*graph,node *nd)
{
	if(a[nd->mark]==0)
	{
		cout<<nd->mark<<"  ";
	    count++;
	    a[nd->mark]=1;
	    node*p=nd->next;
	while(p)
	{
		if(a[p->mark]==0&&count<graph->n)
		travel(graph,graph->graphvertex[p->mark]);
		p=p->next;
	}
	}
}
main()
	{
		Graph *graph;
		graph=creatgraph();
		travel(graph,graph->graphvertex[0]);
	}








⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -