#include<stdio.h>
#include<malloc.h>
typedef struct node{
int data;
struct node* left;
struct node* right;
}Node;
typedef struct {
Node* root;
}Tree;
typedef struct LNode{
struct node* nn;
struct node* next;
};
typedef struct Queue{
struct LNode* head;
struct LNode* tail;
};
void insert(Tree* tree,int value);
void inorder(Node* node);
bool IsEmpty(Tree* tree);
void CengXu(Tree* tree);
Queue* CreatQueue();
void IsQueueEmpty(Queue* Que,Node* N);
Node* Del(Queue* Que);
bool IsQueueEmpty(Queue* Que);
void InsertQueue(Queue* Que,Node* N);
int main(){
Tree tree;
tree.root=NULL;
int n=3,a[3]={1,2,3};
for(int i=0;i<n;i++){
insert(&tree,a[i]);
}
if(tree.root==NULL) printf("OK\n");
printf("Inorder Output the Tree:");
inorder(tree.root);
//printf("GetElement the Tree: ");
//GetElement(&tree);
printf("CengXu the Tree: ");
CengXu(&tree);
getchar();
return 0;
}
void insert(Tree* tree,int value){
Node* node=(Node*)malloc(sizeof(Node));
node->data=value;
node->left=NULL;
node->right=NULL;
if(tree->root==NULL){
tree->root=node;
}
else{
Node* temp=tree->root;
while(temp!=NULL){
if(value<temp->data){
if(temp->left==NULL){
temp->left=node;
return;
}
else{
temp=temp->left;
}
}
else{
if(temp->right==NULL){
temp->right=node;
return;
}
else{
temp=temp->right;
}
}
}
}
return;
}
void inorder(Node* node){
if(node!=NULL){
inorder(node->left);
printf("%d ",node->data);
inorder(node->right);
}
printf("\n");
}
bool IsEmpty(Tree* tree){
if(tree->root==NULL) return true;
else return false;
}
void CengXu(Tree* tree){
if(tree->root==NULL) return;
Queue* Que;
Que=CreatQueue();
Node* T;
T=tree->root;
InsertQueue(Que,T);
printf("csre1 ok\n");
while(Que->tail-=NULL){
printf("cre1 ok\n");
T=Del(Que);
printf("%d ",T->data);
if(T->left!=NULL) InsertQueue(Que,T->left);
if(T->right!=NULL) InsertQueue(Que,T->right);
}
printf("\n");
}
Node* Del(Queue* Que){
if(IsQueueEmpty(Que)) return NULL;
else if(Que->head->next==Que->tail->nn){
LNode* LL;
LL=Que->tail;
Que->head->next=Que->tail->nn;
Que->tail=NULL;
return LL->nn;
}
else{
LNode* LL;
LL->next=Que->head->next;
Que->head->next=LL->next;
return LL->nn;
}
}
void InsertQueue(Queue* Que,Node* N){
printf("1111111111111111111111111111111\n");
LNode *LL=(LNode*)malloc(sizeof(LNode));
printf("22222222222222222222222222222222222\n");
LL->nn=N;
LL->next=NULL;
if(IsQueueEmpty(Que)){
Que->head->next=LL->nn;
Que->tail=LL;
printf("Insert is ok1");
}
else{
Que->tail->next=LL->nn;
Que->tail=LL;
printf("Insert is ok2");
}
}
bool IsQueueEmpty(Queue* Que){
if(Que->head->next==NULL) return true;
else return false;
}
Queue* CreatQueue(){
Queue* Q;
Q=(Queue*)malloc(sizeof(Queue));
Q->tail->nn=NULL;
Q->head->next=Q->tail->nn;
Q->tail->next=NULL;
}
我编的这个程序不知道为什么运行到图片 蓝色框 这个函数里面 就运行不了了。。。
求大佬 指导,小弟五体投地。。。