这段代码中存在一些未定义的类型和函数,例如Queue、createQueue、enqueue、isEmpty、getSize和dequeue。因此,我们需要先定义它们或者从其他地方引用它们的定义。此外,还需要添加一些必要的头文件,以便正确地使用这些函数和类型。

以下是修改后的代码:

#include <stdio.h> #include <stdlib.h> #include <string.h>

typedef struct node { char name[20]; struct node *left; struct node *right; } Node;

typedef struct queue { Node *data; struct queue *next; } Queue;

Queue *createQueue() { Queue q = (Queue)malloc(sizeof(Queue)); q->data = NULL; q->next = NULL; return q; }

void enqueue(Queue *q, Node *n) { Queue *newNode = createQueue(); newNode->data = n; if (q->next == NULL) { q->next = newNode; } else { Queue *temp = q->next; while (temp->next != NULL) { temp = temp->next; } temp->next = newNode; } }

Node *dequeue(Queue *q) { if (q->next == NULL) { return NULL; } Queue *temp = q->next; Node *result = temp->data; q->next = temp->next; free(temp); return result; }

int isEmpty(Queue *q) { return q->next == NULL; }

int getSize(Queue *q) { int count = 0; Queue *temp = q->next; while (temp != NULL) { count++; temp = temp->next; } return count; }

Node *root;

void printByLevel(Node *root) { Queue *q = createQueue(); if (root == NULL) { return; } enqueue(q, root); while (!isEmpty(q)) { int size = getSize(q); for (int i = 0; i < size; i++) { Node *node = dequeue(q); printf("%s ", node->name); if (node->left != NULL) { enqueue(q, node->left); } if (node->right != NULL) { enqueue(q, node->right); } } printf("\n"); } }

void printLowest(Node *root) { if (root == NULL) { return; } if (root->left == NULL && root->right == NULL) { printf("%s\n", root->name); } else { printLowest(root->left); printLowest(root->right); } }

void printCategories(Node *root) { if (root == NULL) { return; } printf("%s\n", root->name); printCategories(root->left); printCategories(root->right); }

void delete(Node *root, char *category, char *name) { if (root == NULL) { return; } if (strcmp(root->name, category) == 0) { if (root->left != NULL && strcmp(root->left->name, name) == 0) { free(root->left); root->left = NULL; } if (root->right != NULL && strcmp(root->right->name, name) == 0) { free(root->right); root->right = NULL; } } else { delete(root->left, category, name); delete(root->right, category, name); } }

void insert(Node *root, char category, char name) { if (root == NULL) { return; } if (strcmp(root->name, category) == 0) { if (root->left == NULL) { root->left = (Node)malloc(sizeof(Node)); strcpy(root->left->name, name); } else if (root->right == NULL) { root->right = (Node)malloc(sizeof(Node)); strcpy(root->right->name, name); } } else { insert(root->left, category, name); insert(root->right, category, name); } }

int main() { root = (Node*)malloc(sizeof(Node)); strcpy(root->name, "缺陷分类"); root->left = (Node*)malloc(sizeof(Node)); strcpy(root->left->name, "一类缺陷"); root->left->left = (Node*)malloc(sizeof(Node)); strcpy(root->left->left->name, "发电机线圈温度异常"); root->left->right = (Node*)malloc(sizeof(Node)); strcpy(root->left->right->name, "煤机清扫电机异音"); root->right = (Node*)malloc(sizeof(Node)); strcpy(root->right->name, "二类缺陷"); root->right->left = (Node*)malloc(sizeof(Node)); strcpy(root->right->left->name, "设备缺陷");

printf("按层级遍历:\n");
printByLevel(root);

printf("\n最底层节点:\n");
printLowest(root);

printf("\n所有节点:\n");
printCategories(root);

printf("\n删除节点后:\n");
delete(root, "一类缺陷", "煤机清扫电机异音");
printCategories(root);

printf("\n插入节点后:\n");
insert(root, "一类缺陷", "煤机清扫电机异音");
printCategories(root);

return 0;
#includestdioh#includestdlibh#includestringhtypedef struct node char name20; struct node left; struct node right; Node;Node root = NodemallocsizeofNode;strcpyroot-name 缺陷分类;root-left = Nodem

原文地址: https://www.cveoy.top/t/topic/hllx 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录