使用队列的二叉树级顺序遍历?
Binary Tree level order Traversal using queues?
请帮我找出代码中的错误。
在这个程序中,我创建了一个二叉树,我想使用队列按级别顺序遍历。
打印第一个父级后我的输出卡住root.I认为我在队列中犯了一些错误functions.But我找不到任何错误。
下面是我的代码:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *left,*right;
};
struct node* create_root(int data)
{
struct node *root=(struct node*)malloc(sizeof(struct node));
root->data=data;
root->left=root->right=NULL;
return root;
}
struct node* create_tree(int data,struct node *root)
{
char ch;
if(root==NULL)
return create_root(data);
printf("\nEnter R/L of %d ? ",root->data);
fflush(stdin);
scanf("%c",&ch);
if(ch=='R' || ch=='r')
root->right=create_tree(data,root->right);
else
root->left=create_tree(data,root->left);
return root;
}
struct queue
{
struct node *info;
struct queue *next;
};
struct queue *start=NULL;
struct queue* enQueue(struct node *root)
{
struct queue *new_node,*ptr;
new_node=(struct queue*)malloc(sizeof(struct queue));
if(start==NULL)
start=new_node;
else
{
ptr=start;
while(ptr!=NULL)
{
if(ptr->next==NULL)
{
ptr->next=new_node;
new_node->next=NULL;
}
}
}
new_node->info=root;
return start;
}
struct queue* deQueue()
{
struct queue *temp;
if(start==NULL)
{
printf("\nEmpty!!!!!");
return;
}
temp=start;
if(start->next==NULL) start=NULL;
else start=start->next;
return temp;
}
int isEmpty()
{
if(start==NULL)
return 1;
else
return 0;
}
void level_order(struct node *root)
{
struct queue *ptr;
if(root==NULL)
{
printf("\nEmpty!!!!!");
return ;
}
start=enQueue(root);
while(!isEmpty())
{
ptr=deQueue();
printf("%d ",ptr->info->data);
if(ptr->info->left)
enQueue(ptr->info->left);
else if(ptr->info->right)
enQueue(ptr->info->right);
}
}
int main()
{
int n=0,num;
struct node *root=NULL;
printf("\nEnter data:");
scanf("%d",&num);
root=create_tree(num,root);
while(n<5)
{
printf("\nEnter data:");
scanf("%d",&num);
create_tree(num,root);
n++;
}
level_order(root);
return 0;
}
level_order 应该对自身进行递归调用,而不是 enqueue()。
您的入队函数已损坏:您继续循环直到 ptr
为 NULL
,但在循环内您根本没有更改 ptr!
while(ptr!=NULL)
{
if(ptr->next==NULL)
{
ptr->next=new_node;
new_node->next=NULL;
}
}
相反,您必须在每次迭代时在列表中前进,直到到达末尾:
while(ptr!=NULL)
{
if(ptr->next==NULL)
{
ptr->next=new_node;
new_node->next=NULL;
break;
}
ptr = ptr->next;
}
这应该可以解决死循环问题。
此外,您应该将 new_node->next
的初始化移动到 malloc()
之后,以便在 start == NULL
:
的情况下也进行初始化
new_node=(struct queue*)malloc(sizeof(struct queue));
new_node->next = NULL;
if(start==NULL)
start=new_node;
请帮我找出代码中的错误。 在这个程序中,我创建了一个二叉树,我想使用队列按级别顺序遍历。
打印第一个父级后我的输出卡住root.I认为我在队列中犯了一些错误functions.But我找不到任何错误。
下面是我的代码:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *left,*right;
};
struct node* create_root(int data)
{
struct node *root=(struct node*)malloc(sizeof(struct node));
root->data=data;
root->left=root->right=NULL;
return root;
}
struct node* create_tree(int data,struct node *root)
{
char ch;
if(root==NULL)
return create_root(data);
printf("\nEnter R/L of %d ? ",root->data);
fflush(stdin);
scanf("%c",&ch);
if(ch=='R' || ch=='r')
root->right=create_tree(data,root->right);
else
root->left=create_tree(data,root->left);
return root;
}
struct queue
{
struct node *info;
struct queue *next;
};
struct queue *start=NULL;
struct queue* enQueue(struct node *root)
{
struct queue *new_node,*ptr;
new_node=(struct queue*)malloc(sizeof(struct queue));
if(start==NULL)
start=new_node;
else
{
ptr=start;
while(ptr!=NULL)
{
if(ptr->next==NULL)
{
ptr->next=new_node;
new_node->next=NULL;
}
}
}
new_node->info=root;
return start;
}
struct queue* deQueue()
{
struct queue *temp;
if(start==NULL)
{
printf("\nEmpty!!!!!");
return;
}
temp=start;
if(start->next==NULL) start=NULL;
else start=start->next;
return temp;
}
int isEmpty()
{
if(start==NULL)
return 1;
else
return 0;
}
void level_order(struct node *root)
{
struct queue *ptr;
if(root==NULL)
{
printf("\nEmpty!!!!!");
return ;
}
start=enQueue(root);
while(!isEmpty())
{
ptr=deQueue();
printf("%d ",ptr->info->data);
if(ptr->info->left)
enQueue(ptr->info->left);
else if(ptr->info->right)
enQueue(ptr->info->right);
}
}
int main()
{
int n=0,num;
struct node *root=NULL;
printf("\nEnter data:");
scanf("%d",&num);
root=create_tree(num,root);
while(n<5)
{
printf("\nEnter data:");
scanf("%d",&num);
create_tree(num,root);
n++;
}
level_order(root);
return 0;
}
level_order 应该对自身进行递归调用,而不是 enqueue()。
您的入队函数已损坏:您继续循环直到 ptr
为 NULL
,但在循环内您根本没有更改 ptr!
while(ptr!=NULL)
{
if(ptr->next==NULL)
{
ptr->next=new_node;
new_node->next=NULL;
}
}
相反,您必须在每次迭代时在列表中前进,直到到达末尾:
while(ptr!=NULL)
{
if(ptr->next==NULL)
{
ptr->next=new_node;
new_node->next=NULL;
break;
}
ptr = ptr->next;
}
这应该可以解决死循环问题。
此外,您应该将 new_node->next
的初始化移动到 malloc()
之后,以便在 start == NULL
:
new_node=(struct queue*)malloc(sizeof(struct queue));
new_node->next = NULL;
if(start==NULL)
start=new_node;