二叉树(含有duplicates字段来实现懒惰删除,还有完全删除节点)

 
#include  < stdio.h >
#include 
< malloc.h >
#include 
< time.h >
#include 
< stdlib.h >
typedef 
struct  BiTree
{
    
int value;
    
int duplicates;
    
struct BiTree *left,*right;
}
BiTree;
BiTree
*  CreateBiTree() /**/ /*return dummy root */
{
    BiTree 
*node=(BiTree*)malloc(sizeof(BiTree));
    node
->left=NULL;
    node
->right=NULL;
    
return node;
}

int  SearchNode(BiTree  * root,BiTree  ** parent,BiTree  ** node, int  x)
/**/ /*if exist return 1,else return 0*/
/**/ /*parent:return the parent of the node with value of x*/
{
    BiTree 
*p1;/**//*used as node*/
    BiTree 
*p2/**//*used as parent node*/;
    p1
=root->left,p2=root;
    
while(p1!=NULL)
    
{
        
if(x==p1->value)
        
{
            
*node=p1;/**//*motify the node and parent*/
            
*parent=p2;
            
return 1;
        }

        
else if(x<p1->value)
        
{
            p2
=p1;
            p1
=p1->left;
        }

        
else if(x>p1->value)
        
{
            p2
=p1;
            p1
=p1->right;
        }

    }

    
*node=p1;/**//*motify the node and parent*/
    
*parent=p2;
    
return 0;
}

int  InsertNode(BiTree  * root, int  x)
{
/**//*if x exist in BiTree ,return 0*/
/**//*else return 1;*/
    BiTree 
*parent=NULL,*node=NULL;
    
if(SearchNode(root,&parent,&node,x)==1)/**//*if x eixst in Bitree */
    
/**//*duplicates++*/
    
{
        node
->duplicates++;
        
return 0;
    }

    
else
    
{
        node
=(BiTree*)malloc(sizeof(BiTree));
        node
->value=x;
        node
->duplicates=1;
        node
->left=NULL;
        node
->right=NULL;
        
if(parent==root)
            root
->left=node;
        
else if(x<parent->value)
            parent
->left=node;
        
else if(x>parent->value)
            parent
->right=node;
        
return 1;
    }

}

void  FreeBiTree(BiTree *  root)
{
    BiTree 
*left=NULL,*right=NULL;
    
if(root!=NULL)
    
{
        left
=root->left;
        right
=root->right;
        free(root);
        FreeBiTree(left);
        FreeBiTree(right);
    }

}

int  DeleteNode(BiTree  * root, int  x)
{
/**//*if x exist in BiTree ,return 1*/
/**//*else return 0;*/
    BiTree 
*node=NULL,*parent=NULL;
    BiTree 
*p1=NULL,*p2=NULL;
    
int result;
    result
=SearchNode(root,&parent,&node,x);
    
if(result==0)
        
return 0;/**//*x doesn't exist in the BiTree */
    
else
    
{
        
if(node->duplicates>1)
        
{
            printf(
"Duplicate>1 ");
              node
->duplicates--;/**//*lazy delete*/
        }
          
        
else/**//*delete node which duplicates is zero*/
        
{
            
if((node->left==NULL)&&(node->right==NULL))
            
{
                
if(node==parent->left)
                    parent
->left=NULL;
                
else if(node==parent->right)
                    parent
->right=NULL;
                free(node);
            }
 
            
else if((node->left!=NULL)&&(node->right!=NULL))
            
{
                p1
=node;
                p2
=node->right;
                
if(p2->left==NULL)
                
{
                    p1
->value=p2->value;
                    p1
->duplicates=p2->duplicates;
                    p1
->right=p2->right;
                    free(p2);
                }

                
else
                
{
                    
while(p2->left!=NULL)
                    
{
                        p1
=p2;
                        p2
=p2->left;
                    }

                    node
->value=p2->value;
                    node
->duplicates=p2->duplicates;
                    p1
->left=p2->right;
                    free(p2);
                }

            }

            
else if(node->left!=NULL)
            
{
                
if(parent->left==node)
                    parent
->left=node->left;
                
else if(parent->right==node)
                    parent
->right=node->left;
                free(node);
            }

            
else if(node->right!=NULL)
            
{
                
if(parent->left==node)
                    parent
->left=node->right;
                
else if(parent->right==node)
                    parent
->right=node->right;
                free(node);
            }

        }

        
return 1;
    }

}

void  Traverse(BiTree  * root, int  depth)
{
    
int i;
    
if(root==NULL)
        
return ;
    
else
    
{
        Traverse(root
->left,depth+1);
        
for(i=0;i<depth;i++)
            putchar(
'*');
        printf(
"%d ",root->value);
        Traverse(root
->right,depth+1);
        
return ;
    }

}

int  main()
{
    
int i,size=100,x;
    BiTree 
*root;
    srand((unsigned)time(NULL));
    root
=CreateBiTree();
    
for(i=0;i<size;i++)
    
{
        x
=rand();
        InsertNode(root,x);        
    }

    Traverse(root
->left,0);
    
while(x!=0)
    
{
        scanf(
"%d",&x);
        DeleteNode(root,x);
        printf(
"After DeleteNode ");
        Traverse(root
->left,0);
    }

    FreeBiTree(root);
/**//*because is't dummy*/
    
return 0;
}

你可能感兴趣的:(struct,null,include,Duplicates)