二叉树思维导图
队列思维导图
链式队列
创建
//创建
// 定义链表节点
typedef struct Node {
int data;
struct Node* next;
} Node;
// 定义队列
typedef struct {
Node* front; // 队头
Node* rear; // 队尾
} Queue, *que_p;
// 初始化队列
que_p createQueue() {
que_p q = (que_p)malloc(sizeof(Queue));
if(q==NULL)
{
printf("空间申请失败")
return NULL
}
q->front = q->rear = NULL;
return q;
}
判空
//判空
int is_empty(que_p q){
if(q!= NULL){
printf("参数为空\n");
return -1;
}
if (q->rear== NULL) {
printf("队列为空\n");
return 1;
}
}
入队
//入队
void enqueue(que_p q, int value) {
que_p newNode = (que_p)malloc(sizeof(Queue));
newNode->data = value; 、
//标记链表结束
newNode->next = NULL;
//此时队列为空
if (q->rear == NULL) {
q->front = q->rear = newNode;
} else {
q->rear->next = newNode;
q->rear = newNode;
}
}
出队
// 出队操作
int dequeue(que_p q) {
if (q->front == NULL) {
printf("队列为空\n");
return -1; // 队列为空
}
que_p temp = q->front;
int value = temp->data;
q->front = q->front->next;
if (q->front == NULL) {
q->rear = NULL;
}
free(temp);
return value;
}
输出队列
// 打印队列元素
void printQueue(que_p q) {
que_p temp = q->front;
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}
二叉树
二叉树中序遍历
// 中序遍历二叉树的函数
void inorder_traversal(node_p root) {
if (root == NULL) {
return;
}
// 遍历左子树
inorder_traversal(root->lchild);
// 访问根节点
printf("%c ", root->data);
// 遍历右子树
inorder_traversal(root->rchild);
}
二叉树后续遍历
// 后序遍历二叉树的函数
void postorder_traversal(node_p root) {
if (root == NULL) {
return;
}
// 遍历左子树
postorder_traversal(root->lchild);
// 遍历右子树
postorder_traversal(root->rchild);
// 访问根节点
printf("%c ", root->data);
}
-
画二叉树:先序:FCADBEHGM
中序:ACBDFHEMG