自己写的代码,调试过应该没有BUG,还望大神指正
#include<stdio.h>
#include<stdlib.h>
#define N 100
typedef struct tree
{
int data;
struct tree* left;
struct tree* right;
}Tree,*tree_ptr;
int init_tree(Tree **root,int value)
{
*root=(Tree *)malloc(sizeof(Tree));
if (*root==NULL)
{
printf("fail:malloc failed\n");
exit (0);
}
(*root)->left=(*root)->right=NULL;
(*root)->data=value;
return 0;
}
void insert_tree(Tree *root,int value)
{
Tree *newleaf=malloc(sizeof(Tree));
newleaf->left=newleaf->right=NULL;
newleaf->data=value;
while(root)
{
if (value==root->data)
{
free(newleaf);
newleaf = NULL;
return;
}
if (value<root->data)
{
if (root->left)
root=root->left;
else
{
root->left=newleaf;
return;
}
}
else
{
if(root->right)
root=root->right;
else
{
root->right=newleaf;
return;
}
}
}
}
void preorder(Tree* root)
{
if (root==NULL)
return;
printf("%d\n",root->data);
preorder(root->left);
preorder(root->right);
}
void midorder(Tree* root)
{
if(root==NULL)
return;
midorder(root->left);
printf("%d\n",root->data);
midorder(root->right);
}
void afterorder(Tree* root)
{
if (root==NULL)
return;
afterorder(root->left);
afterorder(root->right);
printf("%d\n",root->data);
}
/*********next code for levleloder***************/
typedef struct queue
{
Tree * store[N];
int front;
int rear;
}Queue;
void init_queue(Queue*q )
{
q->front=q->rear=0;
}
void enqueue(Queue *q ,Tree *t)
{
if((q->rear+1)%N==(q->front))
return;
q->store[q->rear]=t;
q->rear=(q->rear+1)%N;
}
Tree* dequeue(Queue* q)
{
if(q->front==q->rear)
return NULL;
Tree* t=q->store[q->front];
q->front=(q->front+1)%N;
return t;
}
int empty_queue(Queue*q)
{
if ( q->rear==q->front)
return 1;
return 0;
}
void level_order(Tree* root)
{
Queue* q=(Queue *)malloc(sizeof(Queue));
init_queue(q);
enqueue(q,root);
Tree *t=NULL;
while(empty_queue(q)!=1)
{
t=dequeue( q);
printf("%d\n",t->data);
if(t->left) enqueue(q,t->left);
if(t->right) enqueue(q,t->right);
}
}
/******** next code for delete tree **********/
Tree * find_node(Tree * root,int value)
{
while(root)
{
if (value==root->data) return root;
if(value<root->data)
root=root->left;
else
root=root->right;
}
return NULL;
}
Tree * find_parent(Tree * root,int value)
{
while(root)
{
if(root->left&&value<root->data)
{
if(root->left->data==value)
return root;
else
{
root=root->left;
}
continue;
}
if(root->right&&value>root->data)
{
if(root->right->data==value)
return root;
else
{
root=root->right;
}
continue;
}
break;
}
return NULL;
}
void delete_node(Tree * root,int value)
{
Tree* deletenode =find_node (root,value);
if (deletenode==NULL) return;
if (root->data==value) return; //to protect the root node from deleting
Tree* parent=find_parent(root,value);
if (deletenode->left==NULL) // 度为1或0的时候
{
if (parent->left==deletenode)
parent->left=deletenode->right;
else
parent->right =deletenode->right;
return;
}
//printf("hello\n");
if(deletenode->right==NULL)
{
if(parent->left==deletenode)
parent->left=deletenode->left;
else
parent->right=deletenode->left;
return;
}
Tree * leftmax=deletenode->left ;//当deletenode左右节点都不为空的时候
int leftmax_value=leftmax->data;
if(leftmax->right==NULL) //当deletenode右节点为空的时候
{
deletenode->left=leftmax->left;
deletenode->data=leftmax_value;
//free(leftmax);
return;
}
while(leftmax->right) //当deletetnode右节点不为空的时候
{
leftmax=leftmax->right;//向右一直找到最大
}
Tree * temp_parent=find_parent( root,leftmax->data);
deletenode->data=leftmax->data;
temp_parent->right=leftmax->left;
}
/******** next code for main programe **********/
int main(int argc,char** argv)
{
int i,j;
Tree* root =malloc(sizeof(Tree));
root->left = NULL;
root->right=NULL;
root->data =N/2;
for(i=0;i<N;i++)
{
insert_tree( root,random()%N);
}
for (j=50;j<N;j++)
{
delete_node(root,j);
}
midorder(root);
/*
insert_tree(root,3);
insert_tree(root,1);
insert_tree(root,4);
insert_tree(root,2);
insert_tree(root,0);
insert_tree(root,7);
insert_tree(root,6);
insert_tree(root,9);
insert_tree(root,8);
delete_node(root,3);
midorder( root);
return 0;
*/
// midorder(root);
//preorder( root);
//afterorder(root);
}