我为使用队列遍历二叉搜索树编写的C代码中存在错误
我使用队列首先遍历BST宽度 代码编译并运行。我也得到了正确的答案。但是,对于最后一个节点,字符重复4次,然后出现错误消息。错误消息表示我使用无效指针调用了free()。我无法调试此问题,请提供任何帮助 输出: F D J B E G K A C I H H双重自由或腐败(fasttop) 流产我为使用队列遍历二叉搜索树编写的C代码中存在错误,c,queue,binary-search-tree,breadth-first-search,C,Queue,Binary Search Tree,Breadth First Search,我使用队列首先遍历BST宽度 代码编译并运行。我也得到了正确的答案。但是,对于最后一个节点,字符重复4次,然后出现错误消息。错误消息表示我使用无效指针调用了free()。我无法调试此问题,请提供任何帮助 输出: F D J B E G K A C I H H双重自由或腐败(fasttop) 流产 //bfs-级别顺序遍历-使用队列 #包括 #包括 #包括 结构节点{ 字符数据; 结构节点*左; 结构节点*右; }; struct bstnode*front=NULL; struct bstnod
//bfs-级别顺序遍历-使用队列
#包括
#包括
#包括
结构节点{
字符数据;
结构节点*左;
结构节点*右;
};
struct bstnode*front=NULL;
struct bstnode*rear=NULL;
结构bstnode*newnode(chard);
结构bstnode*插入(结构bstnode*根,字符d);
void bfs(结构节点*根节点);
void排队(结构bstnode*root);
结构bstnode*dequeue();
内部主(空){
struct bstnode*root=NULL;
根=插入(根,'F');
根=插入(根,'D');
根=插入(根,'B');
根=插入(根,'A');
根=插入(根,'E');
根=插入(根,'C');
根=插入(根,'J');
根=插入(根,'K');
根=插入(根,'G');
根=插入(根,“I”);
根=插入(根,'H');
bfs(根);
}
//创建节点
结构bstnode*新节点(字符d){
struct bstnode*newnode=(struct bstnode*)malloc(sizeof(struct bstnode));
newnode->data=d;
newnode->left=newnode->right=NULL;
返回newnode;
}
//插入bst
结构bstnode*插入(结构bstnode*根,字符d){
if(root==NULL){
根=新节点(d);
}
else if(d数据){
根->左=插入(根->左,d);
}
否则{
根->右=插入(根->右,d);
}
返回根;
}
//使用队列的层次顺序遍历
void bfs(结构节点*根){
结构bstnode*当前=根;
while(当前!=NULL){
printf(“%c”,当前->数据);
如果(当前->左!=空){
排队(当前->左侧);
}
如果(当前->右侧!=NULL){
排队(当前->右侧);
}
current=dequeue();//删除前面的字符
}
}
//让孩子排队
无效排队(结构bstnode*根){
struct bstnode*current=(struct bstnode*)malloc(sizeof(struct bstnode));
当前->数据=根->数据;
当前->左=根;
当前->右=空;
如果(前==NULL和后==NULL){
前=后=电流;
返回;
}
后->右=当前;
后=电流;
}
//流行元素在前面
结构bstnode*dequeue(){
结构bstnode*ptr=front;
if(front==NULL){
printf(“没有队列\n”);
}
如果(前==后){
前=后=ptr;
}否则{
前=前->右;
}
结构bstnode*next=ptr->left;
免费(ptr);
下一步返回;
}
我不确定我是否100%理解了您的代码-
我只是盯着它看了大约20分钟,
我还没有试着运行它-
但我相信我在dequeue()
中看到了一个问题
当您在队列中只剩下一个节点的情况下调用它时(即,front==rear
),
是的
前=后=ptr;
其中ptr
等于front
因此,这一行设置了前部
和后部
他们已经拥有的价值观,
以及不从队列中删除节点
因此,下次调用dequeue()
时,
队列仍然包含H
节点-
而且您永远不会将其从队列中取消链接,即使您确实释放了它
我怀疑你是有意的
front=retar=NULL;
//bfs - level order traversal - USING QUEUE
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
struct bstnode {
char data;
struct bstnode *left;
struct bstnode *right;
};
struct bstnode *front = NULL;
struct bstnode *rear = NULL;
struct bstnode *newnode (char d);
struct bstnode *insert (struct bstnode *root, char d);
void bfs (struct bstnode *root);
void enqueue (struct bstnode *root);
struct bstnode *dequeue();
int main (void) {
struct bstnode *root = NULL;
root = insert (root, 'F');
root = insert (root, 'D');
root = insert (root, 'B');
root = insert (root, 'A');
root = insert (root, 'E');
root = insert (root, 'C');
root = insert (root, 'J');
root = insert (root, 'K');
root = insert (root, 'G');
root = insert (root, 'I');
root = insert (root, 'H');
bfs(root);
}
//create node
struct bstnode *newnode (char d) {
struct bstnode *newnode = (struct bstnode *)malloc(sizeof(struct bstnode));
newnode->data = d;
newnode->left = newnode->right = NULL;
return newnode;
}
//insert in bst
struct bstnode *insert (struct bstnode *root, char d) {
if (root == NULL) {
root = newnode(d);
}
else if (d <= root->data) {
root->left = insert (root->left, d);
}
else {
root->right = insert (root->right, d);
}
return root;
}
//level order traversal using queue
void bfs (struct bstnode *root) {
struct bstnode *current = root;
while (current != NULL) {
printf("%c ", current->data);
if (current->left != NULL) {
enqueue(current->left);
}
if (current->right != NULL) {
enqueue(current->right);
}
current = dequeue(); //remove char at front
}
}
//enqueue children
void enqueue (struct bstnode *root) {
struct bstnode *current = (struct bstnode *)malloc(sizeof(struct bstnode));
current->data = root->data;
current->left = root;
current->right = NULL;
if (front == NULL && rear == NULL) {
front = rear = current;
return;
}
rear->right = current;
rear = current;
}
//pop element at front
struct bstnode *dequeue() {
struct bstnode *ptr = front;
if (front == NULL) {
printf("there is no queue\n");
}
if (front == rear) {
front = rear = ptr;
} else {
front = front->right;
}
struct bstnode *next = ptr->left;
free(ptr);
return next;
}