#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
#define maxSize 101
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
#define maxSize 101
/*
//从图的邻接表转换成邻接矩阵 王道P192.4
typedef struct ENode
{
int adjvex;
struct ENode *next;
}ENode; //边表节点
typedef struct VNode
{
int data;
struct ENode *firstchild;
}VNode,VNodeL[maxSize]; //顶点表节点
typedef struct Graph
{
int VNum,ENum;
VNodeL adjlist;
}Graph;
void Create(Graph &g)
{
cin>>g.VNum>>g.ENum;
for(int i=0;i>g.adjlist[i].data;
g.adjlist[i].firstchild=NULL;
}
for(int i=0;i>x>>y;
ENode *p=(ENode*)malloc(sizeof(ENode));
p->adjvex=x;
p->next=g.adjlist[y].firstchild;
g.adjlist[y].firstchild=p;
ENode *q=(ENode*)malloc(sizeof(ENode));
q->adjvex=y;
q->next=g.adjlist[x].firstchild;
g.adjlist[x].firstchild=q;
}
}
void Transfer(Graph g,int matrix[][maxSize])
{
for(int i=0;iadjvex]=1;
p=p->next;
}
}
}
int main()
{
Graph g;
Create(g);
int matrix[maxSize][maxSize];
for(int i=0;i>g.VNum>>g.ENum;
for(int i=0;i>g.adjlist[i].data;
g.adjlist[i].firstchild=NULL;
}
for(int i=0;i>x>>y;
ENode *p=(ENode*)malloc(sizeof(ENode));
p->adjvex=x;
p->next=g.adjlist[y].firstchild;
g.adjlist[y].firstchild=p;
ENode *q=(ENode*)malloc(sizeof(ENode));
q->adjvex=y;
q->next=g.adjlist[x].firstchild;
g.adjlist[x].firstchild=q;
}
}
int visited[maxSize];
void DFS(Graph g,int v,int &d,int &e)
{
visited[v]=1;
d++; //统计经过的点的个数!!
ENode *p=g.adjlist[v].firstchild;
while (p) {
e++; //统计经过的边的个数!!
if(visited[p->adjvex]==0)
{
DFS(g,p->adjvex,d,e);
}
p=p->next;
}
}
bool Judge(Graph g,int d,int e)
{
if(d==g.VNum&&e/2==g.VNum-1)
{
return true;
}
return false;
}
int main()
{
Graph g;
Create(g);
int d=0;
int e=0;
DFS(g,0,d,e);
cout<>g.VNum>>g.ENum;
for(int i=0;i>g.adjlist[i].data;
g.adjlist[i].firstchild=NULL;
}
for(int i=0;i>x>>y;
ENode *p=(ENode*)malloc(sizeof(ENode));
p->adjvex=x;
p->next=g.adjlist[y].firstchild;
g.adjlist[y].firstchild=p;
ENode *q=(ENode*)malloc(sizeof(ENode));
q->adjvex=y;
q->next=g.adjlist[x].firstchild;
g.adjlist[x].firstchild=q;
}
}
int visited[maxSize];
void DFS(Graph g,int v)
{
visited[v]=1;
int stack[maxSize];
int top=-1;
stack[++top]=v;
while(top!=-1)
{
v=stack[top--];
cout<adjvex]==0)
{
visited[p->adjvex]=1;
stack[++top]=p->adjvex;
}
p=p->next;
}
}
}
int main()
{
Graph g;
Create(g);
for(int i=0;i>g.VNum>>g.ENum;
for(int i=0;i>g.adjlist[i].data;
g.adjlist[i].firstchild=NULL;
}
for(int i=0;i>x>>y;
ENode *p=(ENode*)malloc(sizeof(ENode));
p->adjvex=x;
p->next=g.adjlist[y].firstchild;
g.adjlist[y].firstchild=p;
ENode *q=(ENode*)malloc(sizeof(ENode));
q->adjvex=y;
q->next=g.adjlist[x].firstchild;
g.adjlist[x].firstchild=q;
}
}
int visited[maxSize];
int BFS(Graph g,int i,int j)
{
int queue[maxSize];
int front=1;
int rear=1;
queue[rear++]=i;
visited[i]=1;
while(front!=rear)
{
i=queue[front++];
if(i==j)
{
return 1;
}
ENode *p=g.adjlist[i].firstchild;
while(p)
{
if(visited[p->adjvex]==0)
{
visited[p->adjvex]=1;
queue[rear++]=p->adjvex;
}
p=p->next;
}
}
return 0;
}
int DFS(Graph g,int i,int j)
{
int stack[maxSize];
int top=-1;
visited[i]=1;
stack[++top]=i;
while(top!=-1)
{
i=stack[top--];
if(i==j)
{
return 1;
}
ENode *p=g.adjlist[i].firstchild;
while(p)
{
if(visited[p->adjvex]==0)
{
visited[p->adjvex]=1;
stack[++top]=p->adjvex;
}
p=p->next;
}
}
return 0;
}
int main()
{
Graph g;
Create(g);
for(int k=0;k>g.VNum>>g.ENum;
for(int i=0;i>g.adjlist[i].data;
g.adjlist[i].firstchild=NULL;
}
for(int i=0;i>x>>y;
ENode *q=(ENode*)malloc(sizeof(ENode));
q->adjvex=y;
q->next=g.adjlist[x].firstchild;
g.adjlist[x].firstchild=q;
}
}
int visited[maxSize];
void DFS(Graph g,int i,int j,int d,int path[])
{
visited[i]=1;
int stack[maxSize];
int top=-1;
stack[++top]=i;
d++;
path[d]=i;
while(top!=-1)
{
i=stack[top];
cout<adjvex]==1) {
p=p->next;
}
if(p==NULL)
{
// visited[stack[top]]=0;
top--;
}
else
{
d++;
path[d]=p->adjvex;
stack[top++]=p->adjvex;
visited[p->adjvex]=1;
}
}
}
//void DFS(Graph g,int i,int j,int d,int path[])
//{
// visited[i]=1;
// d++;
// path[d]=i;
// if(i==j)
// {
// for(int k=0;k<=d;k++)
// {
// cout<adjvex]==0)
// {
// DFS(g,p->adjvex,j,d,path);
// visited[p->adjvex]=0;
// }
// p=p->next;
// }
//}
int main()
{
Graph g;
Create(g);
for(int i=0;ij)
i=i/2;
else if(i>i>>j;
int n;
int tree[maxSize];
cin>>n;
for(int i=0;i>tree[i];
int d=FindParent(tree,i,j,n);
cout<lchild;
}
t=stack[top];
if(t->lchild==NULL&&t->rchild!=r)
{
t=t->rchild;
}
else
{
top--;
cout<data<<" ";
r=t;
t=NULL;
}
}
}
Tree *Create(Tree *&t,int ch)
{
if(t==NULL)
{
t=(Tree*)malloc(sizeof(Tree));
t->data=ch;
t->lchild=NULL;
t->rchild=NULL;
}
else if(ch<=t->data)
t->lchild=Create(t->lchild,ch);
else if(ch>t->data)
t->rchild=Create(t->rchild,ch);
return t;
}
int main()
{
Tree *t=NULL;
int n,a[maxSize];
cin>>n;
for(int i=0;i>a[i];
t=Create(t,a[i]);
}
PostTraval(t);
}
*/
/*
//给出自下而上从右到左的层次遍历算法 P123 4 先把从上到下从左到右的层次遍历结果存到栈中,再从栈中输出
typedef struct Tree{
int data;
Tree *lchild,*rchild;
}Tree;
int s[maxSize];
int top=-1;
void LayerTraval(Tree *t)
{
Tree *queue[maxSize];
int front=1;
int rear=1;
queue[rear++]=t;
while(front!=rear)
{
Tree *q=queue[front];
s[++top]=q->data;
front++;
if(q->lchild)
{
queue[rear++]=q->lchild;
}
if(q->rchild)
{
queue[rear++]=q->rchild;
}
}
}
Tree *insert(Tree *&t,int ch)
{
if(t==NULL)
{
t=(Tree*)malloc(sizeof(Tree));
t->data=ch;
t->lchild=NULL;
t->rchild=NULL;
}
if(chdata)
{
t->lchild=insert(t->lchild,ch);
}
else if(ch>t->data)
{
t->rchild=insert(t->rchild,ch);
}
return t;
}
int main()
{
Tree *t=NULL;
int n,a[maxSize];
cin>>n;
for(int i=0;i>a[i];
t=insert(t,a[i]);
}
LayerTraval(t);
while(top!=-1)
{
cout<data=ch;
t->lchild=t->rchild=NULL;
}
else if(chdata)
{
insert(t->lchild,ch);
}
else if(ch>t->data)
{
insert(t->rchild,ch);
}
return t;
}
void DFS(Tree *t,int &max)
{
Tree *stack[maxSize];
int top=-1;
int h=0;
while(t||top!=-1)
{
while(t)
{
stack[++top]=t;
t=t->lchild;
h++;
if(h>max)
max=h;
}
if(top!=-1)
{
t=stack[top--];
h--;
t=t->rchild;
}
}
}
int main()
{
Tree *t=NULL;
int n;
int a[maxSize];
cin>>n;
for(int i=0;i>a[i];
t=insert(t,a[i]);
}
int max=0;
DFS(t,max);
cout<lchild=NULL;
p->rchild=NULL;
p->data=pre[preL];
int k;
for(int i=inL;i<=inR;i++)
{
if(in[i]==pre[preL])
{
k=i;
break;
}
}
p->lchild=CreatTree(pre,in,preL+1,preL+(k-inL),inL,k-1);
p->rchild=CreatTree(pre,in,preL+(k-inL)+1,preR,k+1,inR);
return p;
}
return NULL;
}
void postTraval(Tree *t)
{
if(t)
{
postTraval(t->lchild);
postTraval(t->rchild);
cout<data<<" ";
}
}
int main()
{
int pre[maxSize],in[maxSize];
int n;
cin>>n;
for(int i=0;i>pre[i];
}
for(int i=0;i>in[i];
}
Tree *t=NULL;
t=CreatTree(pre,in,0,n-1,0,n-1);
postTraval(t);
}
//6
//5 3 2 1 6 8
//1 2 3 5 6 8
//1 2 3 8 6 5
*/
/*
//链表形式,判断二叉树是否是完全二叉树 P123 7 //检查每一层的节点数
typedef struct Tree{
int data,lev;
Tree *lchild,*rchild;
}Tree;
Tree *insert(Tree *&t,int ch)
{
if(t==NULL)
{
t=(Tree*)malloc(sizeof(Tree));
t->data=ch;
t->lchild=t->rchild=NULL;
}
else if(chdata)
{
insert(t->lchild,ch);
}
else if(ch>t->data)
{
insert(t->rchild,ch);
}
return t;
}
int BFS (Tree *t)
{
Tree *queue[maxSize];
int front=0;
int rear=0;
int l;
queue[rear]=t;
queue[rear]->lev=1;
rear++;
while(front!=rear)
{
Tree *p=queue[front];
l=queue[front]->lev;
front++;
if(p->lchild)
{
queue[rear]=p->lchild;
queue[rear]->lev=l+1;
rear++;
}
if(p->rchild)
{
queue[rear]=p->rchild;
queue[rear]->lev=l+1;
rear++;
}
}
for(int i=1;ilev==i)
{
cnt++;
}
}
if(cnt!=pow(2,i-1))
return 0;
}
return 1;
}
int main()
{
Tree *t=NULL;
int n;
int a[maxSize];
cin>>n;
for(int i=0;i>a[i];
t=insert(t,a[i]);
}
int d=BFS(t);
cout<>g.VNum>>g.ENum;
for(int i=0;i>g.adjlist[i].data;
g.adjlist[i].firstchild=NULL;
}
for(int i=0;i>x>>y;
ENode *q=(ENode*)malloc(sizeof(ENode));
q->adjvex=y;
q->next=g.adjlist[x].firstchild;
g.adjlist[x].firstchild=q;
}
}
int visited[maxSize];
void DFS(Graph g,int i,int j,int d,int path[])
{
visited[i]=1;
int stack[maxSize];
int top=-1;
stack[++top]=i;
d++;
path[d]=i;
while(top!=-1)
{
i=stack[top];
cout<adjvex]==1) {
p=p->next;
}
if(p==NULL)
{
// visited[stack[top]]=0;
top--;
}
else
{
d++;
path[d]=p->adjvex;
stack[top++]=p->adjvex;
visited[p->adjvex]=1;
}
}
}
//void DFS(Graph g,int i,int j,int d,int path[])
//{
// visited[i]=1;
// d++;
// path[d]=i;
// if(i==j)
// {
// for(int k=0;k<=d;k++)
// {
// cout<adjvex]==0)
// {
// DFS(g,p->adjvex,j,d,path);
// visited[p->adjvex]=0;
// }
// p=p->next;
// }
//}
int main()
{
Graph g;
Create(g);
for(int i=0;ij)
i=i/2;
else if(i>i>>j;
int n;
int tree[maxSize];
cin>>n;
for(int i=0;i>tree[i];
int d=FindParent(tree,i,j,n);
cout<