编辑代码

#include<stdio.h> 
#include<stdlib.h> 
#include<assert.h> 
//#include<conio.h> 
#define DATATYPE  int  
// #include <Random.h>
typedef struct _tree{ 
    int nodecontent; 
    struct _tree *leftchild; 
    struct _tree *rightchild; 
    int Ltag,Rtag;
}TREE; 

typedef  TREE*   PTREE; 

PTREE CrtTree(DATATYPE value){ 
    PTREE proot; 
    proot=(PTREE)malloc(sizeof(TREE)); 
    assert(proot); 
    proot->nodecontent=value; 
    proot->leftchild=NULL; 
    proot->rightchild=NULL; 
    proot->Ltag = 0;
    proot->Rtag = 0;
    return proot; 
} 

void InsertNode(DATATYPE value,PTREE root){ 
    PTREE crrt=root; 
    PTREE *p=&crrt; 
    while(*p != NULL){
        if(value == crrt->nodecontent){ 
            // printf("the same content in the tree"); 
            // exit(1);
            return; 
        }
        if(value < crrt->nodecontent){ 
            p = &crrt->leftchild; 
            crrt = crrt->leftchild; 
        }   
        else{ 
            p = &crrt->rightchild; 
            crrt = crrt->rightchild; 
        } 
    } 
    *p = CrtTree(value); 
} 

int FindTree(PTREE root, DATATYPE value){ 
    PTREE crrt=root;
    while( crrt != NULL){
        if(value == crrt->nodecontent){ 
            printf("there's this value in the tree\n"); 
            return 1; 
        }
        if(value < crrt->nodecontent){ 
            crrt = crrt->leftchild; 
        } 
        else{ 
            crrt = crrt->rightchild; 
        } 
    } 
    printf("there's no this value in the tree\n"); 
    return 0; 
} 
void PreOrder(PTREE bt){ //先序遍历 
     if(bt){//树不为空,则执行如下操作 
        printf("%d ",bt->nodecontent); 
         PreOrder(bt->leftchild); 
         PreOrder(bt->rightchild); 
     } 
     return; 
} 


void MidOrder(PTREE bt){  //中序遍历  
    if(bt){//树不为空,则执行如下操作
        MidOrder(bt->leftchild); //中序遍历左子树 
        printf("%d ",bt->nodecontent); 
        MidOrder(bt->rightchild); //中序遍历右子树/ 
    } 
    return; 
} 
void PosOrder(PTREE bt ) {//后序遍历 
    if(bt){ 
        PosOrder(bt->leftchild); //后序遍历左子树 
        PosOrder(bt->rightchild); //后序遍历右子树/ 
        printf("%d ",bt->nodecontent); //处理结点数据 
    } 
    return; 
} 

void Clear(PTREE bt) {// 清空二叉树,使之变为一棵空树 
    if(bt){ 
        Clear(bt->leftchild); //清空左子树 
        Clear(bt->rightchild);//清空右子树 
        free(bt);//释放当前结点所占内存 
        bt=NULL; 
    }
    return; 
} 
int MMax(int a,int b){
    return a>b?a:b;
}
int TreeDeeps(PTREE bt){
    if(bt == NULL) return 0;
    return MMax(TreeDeeps(bt->leftchild),TreeDeeps(bt->rightchild))+1;   
}
PTREE Mid_pre = NULL;

void midThreadTree(PTREE bt){
    if(bt == NULL) return;
    if(bt->leftchild != NULL){        
        midThreadTree(bt->leftchild);
    }
    else{
        bt->Ltag = 1;
        bt->leftchild = Mid_pre;//point to the prenode;
    }
    if(Mid_pre!=NULL&&Mid_pre->rightchild==NULL){
        Mid_pre->rightchild = bt;
        Mid_pre->Rtag = 1;
    }
    Mid_pre = bt;
    midThreadTree(bt->rightchild);
}
// #region this region is View Mid Thread Tree;
PTREE MidFirstView(PTREE bt){
    while(bt->Ltag==0) bt = bt->leftchild;
    return bt;
}
PTREE MidNextView(PTREE bt){
    if(bt->Rtag == 1 || bt->rightchild == NULL) return bt->rightchild;
    return MidFirstView(bt->rightchild);
}
int ViewThreadTree(PTREE bt){
    for(bt = MidFirstView(bt);bt!=NULL;bt = MidNextView(bt))
        printf("%d ",bt->nodecontent);
}
// #region

// int main(void) 
// {
//     // printf("%d",MMax(1,4));
//     int num; 
//     char ch; 
//     PTREE root=NULL; 
//     int sum;
//     // printf("Please input the nmber of tree:");
//     // scanf("%d",&sum); 
//     // printf("input tree node data\n"); 
//     // scanf("%d",&num); 
//     root = CrtTree(10); 
//     for(int i = 0; i <= 20 ; i++){
//         // scanf("%d",&num); 
//         // InsertNode(num, root); 
//         // printf("1");
//         InsertNode(rand()%20, root); 
//         //InsertNode(i, root); 
//     }
//     // printf("%d",rand()%100);
//     if(1) {
//     printf("pre order is:\n"); 
//     PreOrder(root); 
//     printf("\n"); 
//     printf("mid order is:\n"); 
//     MidOrder(root); 
//     printf("\n"); 
//     printf("pro order is:\n"); 
//     PosOrder(root); 
//     printf("\n"); 
//     printf("The deeps is : %d\n",TreeDeeps(root));
//     // Clear(root);
//     midThreadTree(root);
//     printf("Thread order is : \n");
//     ViewThreadTree(root);

//     //thread tree 
//     }
//     // printf("%d",root); 

//     return 0; 
// }





// Huffman Tree
int main(){
    
}