C语言中使用队列的二叉树级顺序遍历

C语言中使用队列的二叉树级顺序遍历,c,data-structures,binary-tree,C,Data Structures,Binary Tree,我对C相当陌生。我开始研究数据结构,对链表、堆栈和队列都很在行。现在我正在实施BST,它们看起来相当复杂。在这里,我尝试为levelordertransversal编写代码,但没有得到所需的输出 请帮我修复。它没有显示任何错误。在运行程序时,我得到无限的“G”作为我的输出。我希望以某种顺序打印所有字符(我在主函数中输入了这些字符) #include <stdio.h> #include <stdlib.h> struct Node { char data;

我对C相当陌生。我开始研究数据结构,对链表、堆栈和队列都很在行。现在我正在实施BST,它们看起来相当复杂。在这里,我尝试为levelordertransversal编写代码,但没有得到所需的输出

请帮我修复。

它没有显示任何错误。在运行程序时,我得到无限的“G”作为我的输出。我希望以某种顺序打印所有字符(我在主函数中输入了这些字符)

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

struct Node
{
    char data;
    struct Node* right;
    struct Node* left;

    };

void enq(struct Node* temproot);
struct Node* deq();
struct Node* Insert(struct Node* ,char x);
struct Node* newNode (char data);
void LevelOrder(struct Node* root);

struct Node* front = NULL;
struct Node* rear = NULL;

int main()
{

    struct Node* root;
    root = NULL;
    root = Insert(root,'F');
    root = Insert(root,'B');
    root = Insert(root,'C');
    root = Insert(root,'D');
    root = Insert(root,'E');
    root = Insert(root,'G');
    root = Insert(root,'A');
    root = Insert(root,'H');
    root = Insert(root,'I');
    LevelOrder(root);

    return 0;

    }

struct Node* Insert(struct Node* root,char x)
{
     if (root == NULL)
     {
         root = newNode(x); 
         }
    else if (x <= root->data)
    {
        root->left = Insert(root->left, x);
        }

    else
    {
        root->right = Insert(root->right,x);
        }
    return root;
}

struct Node* newNode(char x)
{
    struct Node* temp1 = (struct Node*)malloc(sizeof(struct Node)); 
    temp1->data = x;
    temp1->right = NULL;
    temp1->left = NULL;
    return  temp1;
    }


void enq(struct Node* temproot)
{
    struct Node* temp = (struct Node*)malloc(sizeof(struct Node));
    temp->data = temproot->data;
    temp->right = temp->left= NULL;

    if(front == NULL && rear == NULL)
    {
        front = rear = temp;
        return;
        }
    rear->right = temp;
    rear = temp;
    }

struct Node* deq()
{
    struct Node* temp = front;
    if (front == NULL)
    {
        printf("The queue is empty!");
        }
    else if (rear == front)
    {

        front = rear = NULL;
        }
    else
    {
        front = front->right;
        }
    return temp;
    }

void LevelOrder(struct Node* root)
{
    struct Node* temproot = root;

    while(temproot != NULL)
    {
        printf("%c ", temproot->data);
        if(temproot->left != NULL)
        {enq(temproot->left);
            }
        if (temproot->right !=NULL)
        {enq(temproot->right);
            }
        temproot = deq();
        }
    }
#包括
#包括
结构体类型
{
字符数据;
结构节点*右;
结构节点*左;
};
void enq(结构节点*temproot);
结构节点*deq();
结构节点*插入(结构节点*,字符x);
结构节点*新节点(字符数据);
void LevelOrder(结构节点*根节点);
结构节点*front=NULL;
结构节点*rear=NULL;
int main()
{
结构节点*根;
root=NULL;
根=插入(根,'F');
根=插入(根,'B');
根=插入(根,'C');
根=插入(根,'D');
根=插入(根,'E');
根=插入(根,'G');
根=插入(根,'A');
根=插入(根,'H');
根=插入(根,'I');
LevelOrder(根);
返回0;
}
结构节点*插入(结构节点*根,字符x)
{
if(root==NULL)
{
root=newNode(x);
}
else if(x数据)
{
根->左=插入(根->左,x);
}
其他的
{
根->右=插入(根->右,x);
}
返回根;
}
结构节点*新节点(字符x)
{
结构节点*temp1=(结构节点*)malloc(sizeof(结构节点));
temp1->data=x;
temp1->right=NULL;
temp1->left=NULL;
返回temp1;
}
void enq(结构节点*temproot)
{
结构节点*temp=(结构节点*)malloc(sizeof(结构节点));
临时->数据=临时根->数据;
临时->右=临时->左=空;
如果(前==NULL和后==NULL)
{
前=后=温度;
返回;
}
后->右=温度;
后部=温度;
}
结构节点*deq()
{
结构节点*temp=front;
if(front==NULL)
{
printf(“队列为空!”);
}
否则,如果(后==前)
{
前=后=零;
}
其他的
{
前=前->右;
}
返回温度;
}
void LevelOrder(结构节点*根)
{
结构节点*temproot=root;
while(temproot!=NULL)
{
printf(“%c”,temproot->data);
if(temproot->left!=NULL)
{enq(临时根->左);
}
if(temproot->right!=NULL)
{enq(临时根->右);
}
temproot=deq();
}
}

如注释中所述,您需要将指向树结构中节点的指针排队,而不是数据。您可以通过使用
left
元素指向节点,使用
right
元素指向队列中的下一项来完成此操作

我在评论中说:

我没有测试过它,但也许(只是也许),你需要更换它

temp->data = temproot->data;
temp->right = temp->left= NULL;
enq()
中使用

temp->left = temproot;
temp->right = NULL;
然后在
deq()

struct Node *next = temp->left;
free(temp);
return next;
这似乎是正确的处方。此代码有一个函数
dump\u BST
,用于转储树中的数据以进行调试(还有一个辅助函数
dump\u BST\u node()
,用于在打印时实现预排序遍历

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

struct Node
{
    char data;
    struct Node *right;
    struct Node *left;
};

void enq(struct Node *temproot);
struct Node *deq(void);
struct Node *Insert(struct Node *, char x);
struct Node *newNode(char data);
void LevelOrder(struct Node *root);
void dump_BST(const char *tag, const struct Node *node);

struct Node *front = NULL;
struct Node *rear = NULL;

int main(void)
{
    struct Node *root = NULL;
    dump_BST("Empty", root);
    root = Insert(root, 'F');
    dump_BST("Added F", root);
    root = Insert(root, 'B');
    dump_BST("Added B", root);
    root = Insert(root, 'C');
    //dump_BST("Added C", root);
    root = Insert(root, 'D');
    //dump_BST("Added D", root);
    root = Insert(root, 'E');
    //dump_BST("Added E", root);
    root = Insert(root, 'G');
    //dump_BST("Added G", root);
    root = Insert(root, 'A');
    //dump_BST("Added A", root);
    root = Insert(root, 'H');
    //dump_BST("Added H", root);
    root = Insert(root, 'I');
    dump_BST("Added I", root);
    LevelOrder(root);

    return 0;
}

struct Node *Insert(struct Node *root, char x)
{
    if (root == NULL)
    {
        root = newNode(x);
    }
    else if (x <= root->data)
    {
        root->left = Insert(root->left, x);
    }

    else
    {
        root->right = Insert(root->right, x);
    }
    return root;
}

struct Node *newNode(char x)
{
    struct Node *temp1 = (struct Node *)malloc(sizeof(struct Node));
    temp1->data = x;
    temp1->right = NULL;
    temp1->left = NULL;
    return temp1;
}

void enq(struct Node *temproot)
{
    struct Node *temp = (struct Node *)malloc(sizeof(struct Node));
    temp->right = NULL;
    temp->left = temproot;
    temp->data = 'Z';

    if (front == NULL && rear == NULL)
    {
        front = rear = temp;
    }
    else
    {
        rear->right = temp;
        rear = temp;
    }
}

struct Node *deq(void)
{
    struct Node *temp = front;
    if (front == NULL)
    {
        printf("The queue is empty!\n");
    }
    else if (rear == front)
    {
        front = rear = NULL;
    }
    else
    {
        front = front->right;
    }
    struct Node *next = (temp == NULL) ? NULL : temp->left;
    free(temp);
    return next;
}

void LevelOrder(struct Node *root)
{
    struct Node *temproot = root;

    while (temproot != NULL)
    {
        printf("%c ", temproot->data);
        if (temproot->left != NULL)
        {
            enq(temproot->left);
        }
        if (temproot->right != NULL)
        {
            enq(temproot->right);
        }
        temproot = deq();
    }
    putchar('\n');
}

static void dump_BST_nodes(const struct Node *node)
{
    if (node != NULL)
    {
        printf("Node: '%c' (ptr = %p, left = %p, right = %p)\n",
                node->data, (void *)node, (void *)node->left, (void *)node->right);
        dump_BST_nodes(node->left);
        dump_BST_nodes(node->right);
    }
}

void dump_BST(const char *tag, const struct Node *node)
{
    printf("%s:\n", tag);
    dump_BST_nodes(node);
    printf("%s: end\n", tag);
}

您需要清理代码-队列为空的消息是一个讨厌的消息,您不需要打印调试。但这确实说明了我是如何检查代码的(它第一次正常工作是一个意外但令人愉快的结果)。

您尝试过使用调试器吗?引用:“我没有得到所需的输出”嗯,好吧……那么你得到了什么输出?你期望得到什么?它没有显示错误。在运行程序时,我得到无限的“G”作为我的输出。我期望得到所有字符(我在主函数中输入了这些字符)按一定顺序打印。@RohitBajaj用这些信息编辑您的问题。同时,预期输出您将右大括号缩进
}
比右大括号缩进
{
一级的系统是非常不寻常的(但非常一致)-我以前没见过。我认为它对可读性没有帮助。它肯定不是正统的。它最接近Allman(我喜欢的样式),但它的右大括号与右大括号的缩进程度相同。我强烈推荐Allman或1TB样式。
Empty:
Empty: end
Added F:
Node: 'F' (ptr = 0x7fc63fc02750, left = 0x0, right = 0x0)
Added F: end
Added B:
Node: 'F' (ptr = 0x7fc63fc02750, left = 0x7fc63fc02770, right = 0x0)
Node: 'B' (ptr = 0x7fc63fc02770, left = 0x0, right = 0x0)
Added B: end
Added I:
Node: 'F' (ptr = 0x7fc63fc02750, left = 0x7fc63fc02770, right = 0x7fc63fc027f0)
Node: 'B' (ptr = 0x7fc63fc02770, left = 0x7fc63fc02810, right = 0x7fc63fc02790)
Node: 'A' (ptr = 0x7fc63fc02810, left = 0x0, right = 0x0)
Node: 'C' (ptr = 0x7fc63fc02790, left = 0x0, right = 0x7fc63fc027b0)
Node: 'D' (ptr = 0x7fc63fc027b0, left = 0x0, right = 0x7fc63fc027d0)
Node: 'E' (ptr = 0x7fc63fc027d0, left = 0x0, right = 0x0)
Node: 'G' (ptr = 0x7fc63fc027f0, left = 0x0, right = 0x7fc63fc02830)
Node: 'H' (ptr = 0x7fc63fc02830, left = 0x0, right = 0x7fc63fc02850)
Node: 'I' (ptr = 0x7fc63fc02850, left = 0x0, right = 0x0)
Added I: end
F B G A C H D I E The queue is empty!