笔记1221

//图
#include"test.h"
#define MaxSize 10
typedef struct Queue {
	int data[MaxSize];
	int rear;
	int front;
}Queue;
int DeQueue(Queue* Q)
{

}
void EnQueue(Queue* Q, int x)
{

}
isEmpty(Queue* Q)
{

}
InitQueue(Queue* Q)
{

}
typedef char vextype;
typedef int edgetype;
//邻接表
typedef struct MGarph {
	vextype vexlist[MaxSize];
	edgetype edgelist[MaxSize][MaxSize];
}MGarph;
bool visited[MaxSize];
//邻接矩阵广度优先
void MBFS(MGarph* G, int k)
{
	Queue* Q = (Queue*)malloc(sizeof(Queue));
	InitQueue(Q);
	visited[k];
	EnQueue(Q, k);
	while (isEmpty(Q) == false)
	{
		int x = DeQueue(Q);
		for (int i = 0; i < MaxSize; i++)
		{
			if (G->edgelist[x][i] != 0 && visited[i] == false)
			{
				EnQueue(Q, i);
				visited[i] = true;
			}
		}
	}
}

//邻接矩阵深度优先
void MDFS(MGarph* G, int k)
{
	visited[k] = true;
	for (int i = 0; i < MaxSize; i++)
	{
		if (G->edgelist[k][i] != 0 && visited[i] == false)
		{
			MDFS(G, i);
		}
	}
}

//邻接矩阵
typedef struct ArcNode {
	edgetype adj;
	struct ArcNode* next;
}ArcNode;
typedef struct AdjNode {
	vextype data;
	struct ArcNode* next;
}AdjNode;
typedef struct AlGarph {
	int vexnum;
	int edgenum;
	struct AdjNode Adjlist[MaxSize];
}AlGarph;
//邻接表深度优先
void AlDFS(AlGarph* G, int k)
{
	visited[k] = true;
	for (ArcNode* pnode = G->Adjlist[k].next; pnode != NULL; pnode = pnode->next)
	{
		if (visited[pnode->adj] == false)
			AlDFS(G, pnode->adj);
	}
}
//邻接表广度优先
void AlBFS(AlGarph* G, int k)
{
	Queue* Q = (Queue*)malloc(sizeof(Queue));
	InitQueue(Q);
	visited[k] = true;
	EnQueue(Q, k);
	while (isEmpty(Q) == false)
	{
		int x = DnQueue(Q);
		for (ArcNode* pnode = G->Adjlist[k].next; pnode != NULL; pnode = pnode->next)
		{
			if (visited[pnode->adj] == false)
			{
				visited[pnode->adj] = true;
				EnQueue(Q, pnode->adj);
			}
		}
	}
}

bool Findk(AlGarph* G, int a, int b, int k)
{
	if (a == b && k == 0)
	{
		return true;
	}
	else if (k > 0)
	{
		visited[a] = true;
		for (ArcNode* pnode = G->Adjlist[a].next; pnode != NULL; pnode = pnode->next)
		{
			if (visited[pnode->adj] == false)
			{
				if (Findk(G, pnode->adj, b, k - 1))
					return true;
			}
		}
		visited[a] = false;
	}
	return false;
}

二叉树

#include"test.h"
#define Maxsize 10
typedef int treedatatype;
//typedef struct Queue {
//	struct TreeNode* data[Maxsize];
//	int rear;
//	int front;
//}Queue;

typedef struct TreeNode {
	treedatatype data;
	struct TreeNode* lchild;
	struct TreeNode* rchild;
}TreeNode;

//void InitQueue(Queue* Q)
//{
//	Q->front = 0;
//	Q->rear = 0;
//}
//
//bool isEmpty(Queue* Q)
//{
//	if (Q->front == Q->rear)
//	{
//		return true;
//	}
//	else
//	{
//		return false;
//	}
//}
//
//bool EnQueue(Queue* Q, TreeNode* x)
//{
//	if ((Q->rear + 1) % Maxsize == Q->front)
//		return false;
//	Q->data[Q->rear] = x;
//	Q->rear = (Q->rear + 1) % Maxsize;
//	return true;
//}
//
//TreeNode* DeQueue(Queue* Q)
//{
//	assert(Q->front != Q->rear);
//	TreeNode* x = Q->data[Q->front];
//	Q->front = (Q->front + 1) % Maxsize;
//	return x;
//}
//


void visit(TreeNode* T)
{

}
//遍历
int order(TreeNode* T)
{
	if (T == NULL)
	{
		return 0;
	}
	//前序
	visit(T);
	order(T->lchild);
	order(T->rchild);

}

void levelorder(TreeNode* T)
{
	visit(T);
	Queue* Q = (Queue*)malloc(sizeof(Queue));
	InitQueue(Q);
	EnQueue(Q, T);
	while (isEmpty(Q) != true)
	{
		TreeNode* temp;
		temp = DeQueue(Q);
		visit(temp);
		if (temp->lchild != NULL)
		{
			EnQueue(Q, temp->lchild);
		}
		if (temp->rchild != NULL)
		{
			EnQueue(Q, temp->rchild);
		}
	}
}

//树高
int A_order(TreeNode* T)
{
	if (T == NULL)
	{
		return 0;
	}
	int left = A_order(T->lchild);
	int right = A_order(T->rchild);
	return left > right ? left + 1 : right + 1;
}

//度为2/叶子/1
int B_order(TreeNode* T)
{
	if (T == NULL)
	{
		return 0;
	}
	int left = B_order(T->lchild);
	int right = B_order(T->rchild);
	if (T->lchild != NULL && T->rchild != NULL)
	{
		return left + right + 1;
	}
	return left + right;
}

//找值的层次
int C_order(TreeNode* T, int k)
{
	if (T == NULL)
	{
		return 0;
	}
	if (T->data == k)
	{
		return 1;
	}
	int left = C_order(T->lchild, k);
	int right = C_order(T->rchild, k);
	if (left != 0 || right != 0)
	{
		return left + right + 1;
	}
	else
	{
		return 0;
	}
}

//叶子节点个数
int D_order(TreeNode* T)
{
	if (T == NULL)
		return 0;
	if (T->rchild == NULL && T->lchild == NULL)
	{
		return 1;
	}
	int left = D_order(T->lchild);
	int right = D_order(T->rchild);
	return left + right;
}

//第k层节点的个数
int E_order(TreeNode* T, int k)
{
	if (T == NULL)
	{
		return 0;
	}
	if (k == 1)
		return 1;
	int left = E_order(T->lchild, k - 1);
	int right = E_order(T->rchild, k - 1);
	return left + right;
}

#include"test.h"

#define MaxSize 100
typedef int datatype;
typedef struct Stack {
	datatype data[MaxSize];
	int top;
}Stack;



bool PushStack(Stack* S, datatype data)
{
	if (S->top == MaxSize - 1)
	{
		return false;
	}
	else
	{
		S->top++;
		S->data[S->top] = data;
		return true;
	}
}

bool PopStack(Stack* S, datatype* a)
{
	if (S->top == -1)
	{
		return false;
	}
	else
	{
		*a = S->data;
		S->top--;
		return true;
	}
}

队列

#include"test.h"
#define Maxsize 10
typedef int datatype;
元素个数(rear-front+Maxsize)%Maxsize
typedef struct Queue {
	datatype data[Maxsize];
	int rear;
	int front;
}Queue;

void InitQueue(Queue* Q)
{
	Q->front = 0;
	Q->rear = 0;
}

bool isEmpty(Queue* Q)
{
	if (Q->front == Q->rear)
	{
		return true;
	}
	else
	{
		return false;
	}
}

bool EnQueue(Queue* Q, datatype x)
{
	if ((Q->rear + 1) % Maxsize == Q->front)
		return false;
	Q->data[Q->rear] = x;
	Q->rear = (Q->rear + 1) % Maxsize;
	return true;
}

datatype DeQueue(Queue* Q)
{
	assert(Q->front != Q->rear);
	int x = Q->data[Q->front];
	Q->front = (Q->front + 1) % Maxsize;
	return x;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值