Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 基于队列的层次二叉树遍历_C_Data Structures_Tree_Queue_Iteration - Fatal编程技术网

C 基于队列的层次二叉树遍历

C 基于队列的层次二叉树遍历,c,data-structures,tree,queue,iteration,C,Data Structures,Tree,Queue,Iteration,我正在尝试使用队列进行级别顺序遍历。但仅打印第一个值,即10。之后调用了dequeue()函数,但仍然没有打印值。显然,dequeue()函数存在一些问题。请帮忙 所有功能都在工作 #include<stdio.h> #include<stdlib.h> #define max 100 int a[max]; int front=-1;

我正在尝试使用队列进行级别顺序遍历。但仅打印第一个值,即10。之后调用了dequeue()函数,但仍然没有打印值。显然,dequeue()函数存在一些问题。请帮忙

所有功能都在工作

            #include<stdio.h>
            #include<stdlib.h>
            #define max 100
            int a[max];
            int front=-1;
            int rear=-1;
            struct node
            {
                int data;
                struct node *left, *right;
            };
            struct node *newNode(int data)
            {
                struct node *nn;
                nn=(struct node *)malloc(sizeof(struct node));
                if(!nn)
                    return;
                nn->data=data;
                nn->left=nn->right=NULL;
                return nn;
            };
            void enqueue(struct node *root)
            {
                a[++rear]=root;
                front++;
            }
            struct node *dequeue()
            {
                printf("inside dequeue\n");
                struct node *temp;
                temp=a[front];
                front++;
                return temp;
            }
            int isempty(){
                return(front==-1);
            }
            void levelorder(struct node *root)
            {
                struct node *temp=NULL;
                if(!root)
                    return;
                enqueue(root);
                while(!isempty())
                {
                    temp=dequeue();
                    printf("%d\t",temp->data);
                    if(temp->left)
                        enqueue(temp->left);
                    if(temp->right)
                        enqueue(temp->right);
                }
            }
            int main()
            {
                int data;
                struct node *root=newNode(10);
                root->left = newNode(11);
                root->left->left = newNode(7);
                root->right = newNode(9);
                root->right->left = newNode(15);
                root->right->right = newNode(8);
                levelorder(root);
                return 0;
            }
#包括
#包括
#定义最大值100
int a[max];
int front=-1;
int rear=-1;
结构节点
{
int数据;
结构节点*左、*右;
};
结构节点*新节点(整型数据)
{
结构节点*nn;
nn=(结构节点*)malloc(sizeof(结构节点));
如果(!nn)
返回;
nn->data=数据;
nn->left=nn->right=NULL;
返回nn;
};
无效排队(结构节点*根)
{
a[++后]=根;
前端++;
}
结构节点*dequeue()
{
printf(“内部出队列\n”);
结构节点*temp;
温度=a[前];
前端++;
返回温度;
}
int isempty(){
返回(前==-1);
}
void levelorder(结构节点*根)
{
结构节点*temp=NULL;
如果(!root)
返回;
排队(根);
而(!isempty())
{
temp=dequeue();
printf(“%d\t”,临时->数据);
如果(临时->左)
排队(临时->左);
如果(临时->右侧)
排队(临时->右侧);
}
}
int main()
{
int数据;
结构节点*root=newNode(10);
根->左=新节点(11);
根->左->左=新节点(7);
根->右=新节点(9);
根->右->左=新节点(15);
根->右->右=新节点(8);
levelorder(根);
返回0;
}

我认为队列实现背后的逻辑存在一些错误。我可以给你一些建议

        int front=0,rear=0;

        void enqueue(struct node *root)
        {
            a[rear]=root;
            rear++;
        }

        struct node *dequeue()
        {
            //handle case where queue is empty
            printf("inside dequeue\n");
            struct node *temp;
            temp=a[front];
            front++;
            return temp;
        }

        int isEmpty(){
            return(front==rear);
        }
为了使数组的大小更可靠,您应该使用具有如下索引的模函数

        int front=0,rear=0;

        void enqueue(struct node *root)
        {
            a[rear%max]=root;
            rear++;
        }

        struct node *dequeue()
        {
            //handle case where queue is empty
            printf("inside dequeue\n");
            struct node *temp;
            temp=a[front%max];
            front++;
            return temp;
        }

        int isEmpty(){
            return(front==rear);
        }