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

📄 form1.h

📁 二叉树建立
💻 H
📖 第 1 页 / 共 3 页
字号:
			 Print(root,pen,300,40,300,40);
			g->DrawString( "先序遍历序列: "+ PreOrder(root),ft,br,20,0);
		 }
		 private: String ^ PreOrder(BiTree *root){
			 if(root==0x0)return "";
			 String ^ s;
			 String ^ sL;
			 String ^ sR;
			 s=String::Format("{0}",root->data);
			 sL=PreOrder(root->Lchild);
			 sR=PreOrder(root->Rchild);
			 s=s+sL+sR;
			 return s;
		 }
		private: String ^ InOrder(BiTree *root){
			 if(root==0x0)return "";
			 String ^ s;
			 String ^ sL;
			 String ^ sR;			 
			 sL=InOrder(root->Lchild);
			 s=String::Format("{0}",root->data);
			 sR=InOrder(root->Rchild);
			 s=sL+s+sR;
			 return s;
		 }
		private: String ^ LastOrder(BiTree *root){
			 if(root==0x0)return "";
			 String ^ s;
			 String ^ sL;
			 String ^ sR;			 
			 sL=LastOrder(root->Lchild);			 
			 sR=LastOrder(root->Rchild);
			 s=String::Format("{0}",root->data);
			 s=sL+sR+s;
			 return s;
		 }
private:  void CreateGraph(cli::array<String ^ >  ^ s){
							 cli::array<String ^ >  ^ snode= s[0]->Split(' ');
							 cli::array<String ^ >  ^ edge;
							   Edge *e1,*e2;
							   int j,k;
							   
							   for(int i=0;i<snode->Length ;++i){
								    graph->nodenum++;
									graph->node[i].data=Char::Parse( snode[i]);
							   }
							   for(int i=1;i<s->Length;++i){
									#pragma region 处理每一条边
								   edge=s[i]->Split(' ');
								   j=int::Parse(edge[0]);
								   k=int::Parse(edge[1]);
								   e1=new Edge(); e2=new Edge();
								   e1->node=j;e1->node2=k;
								   e2->node=k;e2->node2=j;
								   graph->edgenum++;
								   graph->node[j].degree++;
								   graph->node[k].degree++;
								   if(graph->node[j].firstedge==0x0) {
									   graph->node[j].firstedge=e1;
									   graph->node[j].lastedge =e1;									   
								   }
								   else{
									   graph->node[j].lastedge->nextedge=e1;
									   graph->node[j].lastedge=e1;
								   }
								   if(graph->node[k].firstedge==0x0) {
									   graph->node[k].firstedge=e2;
									   graph->node[k].lastedge =e2;									   
								   }
								   else{
									   graph->node[k].lastedge->nextedge=e2;
									   graph->node[k].lastedge=e2;
								   }
								   #pragma endregion
							   }
							  
						
						  }
private : String ^ DFSTravel(Graph *g){
			String ^ s=gcnew String("");
			for(int i=0;i<g->nodenum;++i)g->node[i].Mark=false;
			for(int i=0;i<g->nodenum;++i){
				if(g->node[i].Mark)continue;
				s=s+DFS(g,i);
			}
			return s;
}
private: String ^ DFS(Graph *g,int i){
		   Edge *e;
		   int j;
		   String ^ s=String::Format("{0}",g->node[i].data);
		   g->node[i].Mark=true;
		   e=g->node[i].firstedge;		   
		   while(e!=0x0){
			   
			   if(e->node==i)j=e->node2;
			   else j=e->node;
				if(!g->node[j].Mark){   s=s+DFS(g,j); }
			   e=e->nextedge;
		   }
		   return s;
	   }
private : String ^ BFSTravel(Graph *g)
{
	String ^ s=gcnew String("");
	for(int i=0;i<g->nodenum;++i)g->node[i].Mark=false;
	for(int i=0;i<g->nodenum;++i){
		if(g->node[i].Mark)continue;
		s=s+BFS(g,i);
	}
	return s;
}
private: String ^ BFS(Graph *g,int i)
{
   Edge *e;
   int j,j0,front=0,rear=0;
   int Q[MaxNode];
   String ^ s=String::Format("{0}",g->node[i].data);
   g->node[i].Mark=true;
   Q[rear++]=i;
   while(rear!=front){
      j=Q[front++];
	  e=g->node[j].firstedge;
	   while(e!=0x0){
		   
		   if(e->node==j)j0=e->node2;
		   else j0=e->node;
		   if(!g->node[j0].Mark){  g->node[j0].Mark=true;
			   s=s+String::Format("{0}",g->node[j0].data); 
			   Q[rear++]=j0; }
		   e=e->nextedge;
	   }
   }
   return s;
}
private: void Print(Graph *graph){
		this->pictureBox1->Top=150;
		this->pictureBox1->Left=150;
		this->pictureBox1->Width=this->Width-200;
		this->pictureBox1->Height=this->Height-200;
	this->pictureBox1->Refresh();
	Edge *e;
	System::Drawing::Pen ^ pen=gcnew Pen(Color::Blue);
	SolidBrush ^ br =gcnew SolidBrush(Color::Blue);		
	SolidBrush ^ brc =gcnew SolidBrush(Color::Red);		
	System::Drawing::Font ^ ft = gcnew System::Drawing::Font("Time New Norman", 15);
	
	int w=15,wc=13,dw=80,j= Math::Sqrt(graph->nodenum),j0=0;
	int x=dw,y=0,s=1,step=5,s1=1,y0=dw,steps=3;
	for(int i=0;i<graph->nodenum;++i){
		if(j0++%j==0) {x=dw+i*step*s;s=-s;step=(step+4)%10;y=y0;y0=y0+dw;}
		else { x+=dw;
			y=y+j0*steps*s1;s1=-s1;
		}
		graph->node[i].x=x;
		graph->node[i].y=y;
	}
	for(int i=0;i<graph->nodenum;++i){
		e=graph->node[i].firstedge;
		while(e!=0x0){
			if(e->node<i | e->node2<i){e=e->nextedge;continue;}
			g->DrawLine(pen,graph->node[e->node].x,graph->node[e->node].y,
				graph->node[e->node2].x,graph->node[e->node2].y);
			e=e->nextedge;											
		}
		g->FillEllipse(br,graph->node[i].x-w,graph->node[i].y-w,2*w,2*w);
		g->DrawString(String::Format("{0}",  graph->node[i].data),ft,brc,graph->node[i].x-wc,graph->node[i].y-wc);


	}
}
private: System::Void 退出ToolStripMenuItem_Click(System::Object^  sender, System::EventArgs^  e) {
			 System::Windows::Forms::Application::Exit();
		 }
private: System::Void 中序遍历ToolStripMenuItem_Click(System::Object^  sender, System::EventArgs^  e) {
			  SolidBrush ^ br=gcnew SolidBrush(Color::Red);
			System::Drawing::Font ^ ft = gcnew System::Drawing::Font("Time New Norman", 15);
			ft->Bold::get();
			Pen ^ pen=gcnew Pen(Color::Red);
			this->pictureBox1->Refresh();
			 Print(root,pen,300,40,300,40);
			g->DrawString( "中序遍历序列: "+ InOrder(root),ft,br,20,0);

		 }
private: System::Void 后序遍历ToolStripMenuItem_Click(System::Object^  sender, System::EventArgs^  e) {
			SolidBrush ^ br=gcnew SolidBrush(Color::Red);
			System::Drawing::Font ^ ft = gcnew System::Drawing::Font("Time New Norman", 15);
			Pen ^ pen=gcnew Pen(Color::Red);
			this->pictureBox1->Refresh();
			ft->Bold::get();
			 Print(root,pen,300,40,300,40);
			g->DrawString( "后序遍历序列: "+ LastOrder(root),ft,br,20,0);
		 }
		 
private: System::Void 图的建立ToolStripMenuItem_Click(System::Object^  sender, System::EventArgs^  e) {
			 this->groupBoxGraph->Top=50;
			 this->groupBoxGraph->Left=50;

		 }
private: System::Void buttonGraphCreate_Click(System::Object^  sender, System::EventArgs^  e) {
			 this->groupBoxGraph->Top=5000;
			  cli::array<String ^ >  ^ s=this->textBoxGraph->Lines;
			  graph=new Graph();
			  DSchen::Form1::CreateGraph(s);
			  g=this->pictureBox1->CreateGraphics();
			  Print(graph);
			  this->pictureBox1->Refresh();
			  Print(graph);

		 }
private: System::Void pictureBox1_DoubleClick(System::Object^  sender, System::EventArgs^  e) {
			 Print(graph);
		 }
private: System::Void 刷新ToolStripMenuItem_Click(System::Object^  sender, System::EventArgs^  e) {
			 this->groupBoxGraph->Top=5000;
			  cli::array<String ^ >  ^ s=this->textBoxGraph->Lines;
			  graph=new Graph();
			  DSchen::Form1::CreateGraph(s);
			  g=this->pictureBox1->CreateGraphics();
			  Print(graph);
		 }
private: System::Void 深度优先ToolStripMenuItem_Click(System::Object^  sender, System::EventArgs^  e) {

			
			SolidBrush ^ br=gcnew SolidBrush(Color::Red);
			System::Drawing::Font ^ ft = gcnew System::Drawing::Font("Time New Norman", 15);
			Pen ^ pen=gcnew Pen(Color::Red);
			this->pictureBox1->Refresh();
			ft->Bold::get();
			 Print(graph);
			 g->DrawString( "深度优先遍历序列: "+DFSTravel(graph),ft,br,20,0);
			  
		 }
private: System::Void 广度优先ToolStripMenuItem_Click(System::Object^  sender, System::EventArgs^  e) {
			SolidBrush ^ br=gcnew SolidBrush(Color::Red);
			System::Drawing::Font ^ ft = gcnew System::Drawing::Font("Time New Norman", 15);
			Pen ^ pen=gcnew Pen(Color::Red);
			this->pictureBox1->Refresh();
			ft->Bold::get();
			 Print(graph);
			 g->DrawString( "广度优先遍历序列: "+BFSTravel(graph),ft,br,20,0);
		 }
private: System::Void 图广度优先ToolStripMenuItem_Click(System::Object^  sender, System::EventArgs^  e) {
			 //源码查看
			 this->richTextBoxBFS->Top=50;
			 this->richTextBoxBFS->Left=50;
			 this->richTextBoxDFS->Top=5000;

		 }
private: System::Void 关闭源码ToolStripMenuItem_Click(System::Object^  sender, System::EventArgs^  e) {
			 this->richTextBoxBFS->Top=5000;
			  this->richTextBoxDFS->Top=5000;
		 }
private: System::Void 图深度优先ToolStripMenuItem_Click(System::Object^  sender, System::EventArgs^  e) {
			 this->richTextBoxDFS->Top=50;
				 this->richTextBoxDFS->Left=50;
			 this->richTextBoxBFS->Top=5000;
		 }
};
}

⌨️ 快捷键说明

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