泛型二叉搜索树(初测版)~
											包含编译文件----"再来二叉树main.cpp" "Common.cpp" "再来二叉树.cpp"当然文件名可以自己改改~
现阶段这只是一个普通的二叉搜索树~还有很多功能没有完善~有待更新~~~
再来二叉树main.cpp
程序代码:
/*"再来二叉树main.cpp" "Common.cpp" "再来二叉树.cpp" */
#include"StdAfx.h"
#include"再来二叉树.h"
int main()
{
    int s[]={5,76,2,62,1,6,7};
    int i=0;
    int* p=NULL;
    Tree t=Tree_Init(sizeof(*s),COMMON_Comp_Max_Int);
    for (i=0;i<sizeof(s)/sizeof(*s);++i)
        Tree_Insert(t,&s[i]);
    Set_Order(t);
    while (p=(int* )Tree_Pre_Order(t))
        printf("%d\n",*p);
    return 0;
}
再来二叉树.h
程序代码:#ifndef Tree_CREATBY_20170705
#define Tree_CREATBY_20170705
#include<stdio.h>
#include<stddef.h>
#include"Common.h"
#ifdef __cplusplus
#if __cplusplus
extern "C"{
#endif
#endif
typedef int Tree;
Tree Tree_Init(const size_t size,int (*Comp)(const void*,const void* ));
void* Tree_Insert(Tree t_n,const void* data);
void Set_Order(Tree t_n);              //重置遍历指针
void* Tree_Pre_Order(Tree t_n);        // 前序遍历
void* Tree_In_Order(Tree t_n);         //中序遍历
void* Tree_Post_Order(Tree t_n);       //后序遍历
#ifdef __cplusplus
#if __cplusplus
}
#endif
#endif
#endifCommon.h
程序代码:
#ifndef __COMMON__
#define __COMMON__  
#ifdef __cplusplus
#if __cplusplus
extern "C"{
#endif
#endif
#include<stdio.h>
#include<string.h>
#include<assert.h>
#include<stdlib.h>
#include<ctype.h>
void COMMON_Creat_Node(void** p,size_t size);      //创建节点
void COMMON_Free_Node(void** p);                   //删除节点
int COMMON_Comp_Max_Int(const void* a,const void* b);       //比较函数
int COMMON_Comp_Max_Float(const void* a,const void* b);
int COMMON_Comp_Max_Double(const void* a,const void* b);
int COMMON_Comp_Max_Char(const void* a,const void* b);
int COMMON_Comp_Max_String(const void* a,const void* b);
int COMMON_Comp_Max_Short(const void* a,const void* b);
int COMMON_Comp_Max_Int64(const void* a,const void* b);
int COMMON_Comp_Min_Int(const void* a,const void* b);       //比较函数
int COMMON_Comp_Min_Float(const void* a,const void* b);
int COMMON_Comp_Min_Double(const void* a,const void* b);
int COMMON_Comp_Min_Char(const void* a,const void* b);
int COMMON_Comp_Min_String(const void* a,const void* b);
int COMMON_Comp_Min_Short(const void* a,const void* b);
int COMMON_Comp_Min_Int64(const void* a,const void* b);
int COMMON_Comp_UnMax_Int(const void* a,const void* b);     //无符号比较函数
int COMMON_Comp_UnMax_Char(const void* a,const void* b);
int COMMON_Comp_UnMax_Short(const void* a,const void* b);
int COMMON_Comp_UnMax_Int64(const void* a,const void* b);
int COMMON_Comp_UnMin_Int(const void* a,const void* b);     //无符号比较函数
int COMMON_Comp_UnMin_Char(const void* a,const void* b);
int COMMON_Comp_UnMin_Short(const void* a,const void* b);
int COMMON_Comp_UnMin_Int64(const void* a,const void* b);
void COMMON_Input_String(char** p);                   //从输入字符串
/*****************************************辅助函数声明********************************************/
void COMMON_Input_Slove(char** p,char** pt,size_t* length);     //对输入数据进行处理
void COMMON_Input_Absorb_Buffer(char** );                       //对缓冲区残留数据进行处理
void COMMON_Input_Catch(char* ,char** );                       //异常处理
int  COMMON_Input_Data(const char* ,const char* );             //输入数据
/*********************************************************************************************/
typedef struct COMMON_FUN
{
    void (*Creat_Node)(void** p,size_t size);
    void (*Free_Node)(void** p);
    int (*Comp_Max_Int)(const void* a,const void* b);
    int (*Comp_Max_Float)(const void* a,const void* b);
    int (*Comp_Max_Double)(const void* a,const void* b);
    int (*Comp_Max_Char)(const void* a,const void* b);
    int (*Comp_Max_String)(const void* a,const void* b);
    int (*Comp_Min_Int)(const void* a,const void* b);
    int (*Comp_Min_Float)(const void* a,const void* b);
    int (*Comp_Min_Double)(const void* a,const void* b);
    int (*Comp_Min_Char)(const void* a,const void* b);
    int (*Comp_Min_String)(const void* a,const void* b);
    int (*Comp_UnMax_Int)(const void* a,const void* b);     //无符号比较函数
    int (*Comp_UnMax_Char)(const void* a,const void* b);
    int (*Comp_UnMax_Short)(const void* a,const void* b);
    int (*Comp_UnMax_Int64)(const void* a,const void* b);
    int (*Comp_UnMin_Int)(const void* a,const void* b);     //无符号比较函数
    int (*Comp_UnMin_Char)(const void* a,const void* b);
    int (*Comp_UnMin_Short)(const void* a,const void* b);
    int (*Comp_UnMin_Int64)(const void* a,const void* b);
    void (*Input_String)(char** p);
    int (*Input_Data)(const char* format,const char* s);
}COMMON_FUN;
extern const COMMON_FUN Common;
#ifdef __cplusplus
#if __cplusplus
}
#endif
#endif
#endifCommon.c
程序代码:#include"StdAfx.h"
#include"Common.h"
#define BUFF_MAX 10
int COMMON_Comp_Max_Int(const void* a,const void* b)                        //比较INT型数据
{
    int ta=*(int* )a;
    int tb=*(int* )b;
    if (ta<tb)
        return -1;
    if (ta>tb)
        return 1;
    return 0;
}
int COMMON_Comp_Max_Float(const void* a,const void* b)                     //比较Float型数据
{
    float ta=*(float* )a;
    float tb=*(float* )b;
    if (ta<tb)
        return -1;
    if (ta>tb)
        return 1;
    return 0;
}
int COMMON_Comp_Max_Double(const void* a,const void* b)                   //比较Doulbe型数据
{
    double ta=*(double* )a;
    double tb=*(double* )b;
    if (ta<tb)
        return -1;
    if (ta>tb)
        return 1;
    return 0;
}
int COMMON_Comp_Max_Char(const void* a,const void* b)                     //比较Char型数据
{
    char ta=*(char* )a;
    char tb=*(char* )b;
    if (ta<tb)
        return -1;
    if (ta>tb)
        return 1;
    return 0;
}
int COMMON_Comp_Max_String(const void* a,const void* b)                  //比较字符串类数据
{
    int t=strcmp((char* )a,(char* )b);
    if (t<0)
        return -1;
    if (t>0)
        return 1;
    return 0;
}
int COMMON_Comp_Max_Short(const void* a,const void* b)
{
    short ta=*(short* )a;
    short tb=*(short* )b;
    if (ta<tb)
        return -1;
    if (ta>tb)
        return 1;
    return 0;
}
int COMMON_Comp_Max_Int64(const void* a,const void* b)
{
    _int64 ta=*(_int64*)a;
    _int64 tb=*(_int64*)b;
    if (ta<tb)
        return -1;
    if (ta>tb)
        return 1;
    return 0;
}
int COMMON_Comp_Min_Int(const void* a,const void* b)       //比较函数
{
    int ta=*(int* )a;
    int tb=*(int* )b;
    if (ta<tb)
        return 1;
    if (ta>tb)
        return -1;
    return 0;
}
int COMMON_Comp_Min_Float(const void* a,const void* b)
{
    float ta=*(float* )a;
    float tb=*(float* )b;
    if (ta<tb)
        return 1;
    if (ta>tb)
        return -1;
    return 0;
}
int COMMON_Comp_Min_Double(const void* a,const void* b)
{
    double ta=*(double* )a;
    double tb=*(double* )b;
    if (ta<tb)
        return 1;
    if (ta>tb)
        return -1;
    return 0;
}
int COMMON_Comp_Min_Char(const void* a,const void* b)
{
    char ta=*(char* )a;
    char tb=*(char* )b;
    if (ta<tb)
        return 1;
    if (ta>tb)
        return -1;
    return 0;
}
int COMMON_Comp_Min_String(const void* a,const void* b)
{
    int t=strcmp((char* )a,(char* )b);
    if (t<0)
        return 1;
    if (t>0)
        return -1;
    return 0;
}
int COMMON_Comp_Min_Short(const void* a,const void* b)
{
    short ta=*(short* )a;
    short tb=*(short* )b;
    if (ta<tb)
        return 1;
    if (ta>tb)
        return -1;
    return 0;
}
int COMMON_Comp_Min_Int64(const void* a,const void* b)
{
    _int64 ta=*(_int64*)a;
    _int64 tb=*(_int64*)b;
    if (ta<tb)
        return 1;
    if (ta>tb)
        return -1;
    return 0;
}
int COMMON_Comp_UnMax_Int(const void* a,const void* b)     //无符号比较函数
{
    unsigned int ta=*(unsigned int* )a;
    unsigned int tb=*(unsigned int* )b;
    if (ta<tb)
        return -1;
    if (ta>tb)
        return 1;
    return 0;
}
int COMMON_Comp_UnMax_Char(const void* a,const void* b)
{
    unsigned char ta=*(unsigned char* )a;
    unsigned char tb=*(unsigned char* )b;
    if (ta<tb)
        return -1;
    if (ta>tb)
        return 1;
    return 0;
}
int COMMON_Comp_UnMax_Short(const void* a,const void* b)
{
    unsigned short ta=*(unsigned short* )a;
    unsigned short tb=*(unsigned short* )b;
    if (ta<tb)
        return -1;
    if (ta>tb)
        return 1;
    return 0;
}
int COMMON_Comp_UnMax_Int64(const void* a,const void* b)
{
    unsigned _int64 ta=*(unsigned _int64* )a;
    unsigned _int64 tb=*(unsigned _int64* )b;
    if (ta<tb)
        return -1;
    if (ta>tb)
        return 1;
    return 0;
}
int COMMON_Comp_UnMin_Int(const void* a,const void* b)     //无符号比较函数
{
    unsigned int ta=*(unsigned int* )a;
    unsigned int tb=*(unsigned int* )b;
    if (ta<tb)
        return 1;
    if (ta>tb)
        return -1;
    return 0;
}
int COMMON_Comp_UnMin_Char(const void* a,const void* b)
{
    unsigned char ta=*(unsigned char* )a;
    unsigned char tb=*(unsigned char* )b;
    if (ta<tb)
        return 1;
    if (ta>tb)
        return -1;
    return 0;
}
int COMMON_Comp_UnMin_Short(const void* a,const void* b)
{
    unsigned short ta=*(unsigned short* )a;
    unsigned short tb=*(unsigned short* )b;
    if (ta<tb)
        return 1;
    if (ta>tb)
        return -1;
    return 0;
}
int COMMON_Comp_UnMin_Int64(const void* a,const void* b)
{
    unsigned _int64 ta=*(unsigned _int64* )a;
    unsigned _int64 tb=*(unsigned _int64* )b;
    if (ta<tb)
        return 1;
    if (ta>tb)
        return -1;
    return 0;
}
void COMMON_Input_String(char** p)   //输入字符串
{
    char* pt=NULL;
    size_t length=0;
    COMMON_Creat_Node((void** )p,(BUFF_MAX)*sizeof(char));
    pt=*p;
    while ((*pt=getchar())!='\n')
        COMMON_Input_Slove(p,&pt,&length);
    *pt='\0';
    pt=(char* )realloc(*p,strlen(*p)*sizeof(char));  //压缩空间
    COMMON_Input_Catch(pt,p);
    *p=pt;
}
int COMMON_Input_Data(const char* format,const char* s)
{
    int k=0;
    if ((k=scanf(format,s))!=1)
        while (getchar()!='\n');
    return k;
}
/************************辅助函数调用处理********************************************************/
void COMMON_Input_Slove(char** p,char** pt,size_t* length)     //对输入数据进行处理
{
    ++*length;
    if (*length%(BUFF_MAX-1)==0)
    {
        char* ps=(char* )realloc(*p,(*length+BUFF_MAX)*sizeof(char));
        COMMON_Input_Catch(ps,p);
        *p=ps;
         memset(*p+*length,0,BUFF_MAX*sizeof(char));
         *pt=*p+*length-1;
    }
        ++*pt;
}
void COMMON_Input_Absorb_Buffer(char** p)
{
    while (getchar()!='\n');
    COMMON_Free_Node((void** ) p);
    *p=strdup("");
}
void COMMON_Input_Catch(char* ps,char** p)
{
    if (ps!=NULL)
        return ;
    COMMON_Free_Node((void** )p);
    assert(1);
}
/*********************************************************************************/
void COMMON_Creat_Node(void** p,size_t size)   //创建节点
{
    *p=malloc(size);
    assert(*p);
    memset(*p,0,size);
}
void COMMON_Free_Node(void** p)    //删除节点
{
    if (*p==NULL)
        return ;
    free(*p);
    *p=NULL;
}
const COMMON_FUN Common=
{
    COMMON_Creat_Node,
    COMMON_Free_Node,
    COMMON_Comp_Max_Int,
    COMMON_Comp_Max_Float,
    COMMON_Comp_Max_Double,
    COMMON_Comp_Max_Char,
    COMMON_Comp_Max_String,
    COMMON_Comp_Min_Int,
    COMMON_Comp_Min_Float,
    COMMON_Comp_Min_Double,
    COMMON_Comp_Min_Char,
    COMMON_Comp_Min_String,
    COMMON_Comp_UnMax_Int,
    COMMON_Comp_UnMax_Char,
    COMMON_Comp_UnMax_Short,
    COMMON_Comp_UnMax_Int64,
    COMMON_Comp_UnMin_Int,
    COMMON_Comp_UnMin_Char,
    COMMON_Comp_UnMin_Short,
    COMMON_Comp_UnMin_Int64,
    COMMON_Input_String,
    COMMON_Input_Data,
};
#undef BUFF_MAX
再来二叉树.c
程序代码:#include"StdAfx.h"
#include"再来二叉树.h"
#define LEN_Tree_Node sizeof(Tree_Node)
#define LEN_Tree_Root sizeof(Tree_Node)
#define GET_TREE_ID(t,TYPE_t1,TYPE_t2,ID) (TYPE_t2*)(*(int* )((TYPE_t1*)t+offsetof(TYPE_t1,ID)))
typedef enum{LEFT_CHILD,RIGHT_CHILD,ROOT,NIL}Tree_Location;
struct Stack_Head;
struct Stack;
struct Tree_Root;
struct Tree_Node;
typedef struct Stack
{
    int flag;
    struct Stack* next;
}Stack;
typedef struct Stack_Head   //栈
{
    struct Stack* base;
    struct Stack* top;
}Stack_Head;
typedef struct Tree_Root
{
    size_t size;
    int count;
    int (*Comp)(const void*,const void* );
    struct Stack_Head stack;
    struct Tree_Node* nil;
    struct Tree_Node* root;
    struct Tree_Node* Order_Save;
}Tree_Root;
typedef struct Tree_Node
{
    Tree_Root* ID;
    Tree_Location location;
    struct Tree_Node* child[2];
    struct Tree_Node* p;
    void* data;
}Tree_Node;
static void Stack_Init(Stack_Head* const s);
static int Stack_Is_Empty(const Stack_Head* const s);
static void Stack_Push(Stack_Head* const s,int flag);
static void Stack_Pop(Stack_Head* const s);
static int Stack_Get_Top(const Stack_Head* s);
static void Stack_Del(Stack_Head* const s);
static int Tree_Get_Statle(Tree_Root* t_r);         //获取节点的状态
static void Tree_Solve_Pre_Order(Tree_Root* t_r,Tree_Node** t_n);
Tree Tree_Init(const size_t size,int (*Comp)(const void*,const void* ))
{
    Tree_Root* t=NULL;
    COMMON_Creat_Node((void** )&t,LEN_Tree_Root);
    t->size=size;
    t->Comp=Comp;
    COMMON_Creat_Node((void** )&t->nil,LEN_Tree_Node);
    t->root=t->nil;
    t->nil->location=NIL;
    t->nil->ID=t;
    t->Order_Save=t->nil;
    return (Tree)t->nil;
}
void* Tree_Insert(Tree t_n,const void* data)
{
    Tree_Root* t_r=NULL;
    Tree_Node* t_t=NULL;
    Tree_Node* t_p=NULL;
    Tree_Node* t_nil=NULL;
    int (*Comp)(const void* ,const void* )=NULL;
    int k=0;
    if (t_n==0)
        return NULL;
    t_r=GET_TREE_ID(t_n,Tree_Node,Tree_Root,ID);
    t_p=t_t=t_r->root;
    t_nil=t_r->nil;
    Comp=t_r->Comp;
    while (t_t!=t_nil)
    {
        t_p=t_t;
        k=Comp(data,t_t->data); 
        if (k<0)
            t_t=t_t->child[0];
        else if (k>0)
            t_t=t_t->child[1];
        else
            break;
    }
    if (t_t!=t_nil)
        return t_t->data;
    COMMON_Creat_Node((void** )&t_t,LEN_Tree_Node);
    COMMON_Creat_Node((void** )&t_t->data,t_r->size);
    t_t->p=t_p;
    t_t->ID=t_p->ID;
    t_t->child[0]=t_t->child[1]=t_r->nil;
    memmove(t_t->data,data,t_r->size);
    ++t_r->count;
    if (k<0)
    {
        t_p->child[0]=t_t;
        t_t->location=LEFT_CHILD;
    }
    else if (k>0)
    {
        t_p->child[1]=t_t;
        t_t->location=RIGHT_CHILD;
    }
    else
    {
        t_r->root=t_t;
        t_r->root->location=ROOT;
    }
    return t_t->data;
}
void Set_Order(Tree t_n)              //重置遍历指针
{
    Tree_Root* t_r=NULL;
    if (t_n==0)
        return ;
    t_r=GET_TREE_ID(t_n,Tree_Node,Tree_Root,ID);
    t_r->Order_Save=t_r->root;
}
void* Tree_Pre_Order(Tree t_n)        // 前序遍历
{
    int k=0;
    void* data=NULL;
    Tree_Root* t_r=NULL;
    Tree_Node* t_nil=NULL;
    Tree_Node** t_t=NULL;
    if (t_n==0)
        return NULL;
    t_r=GET_TREE_ID(t_n,Tree_Node,Tree_Root,ID);
    k=Tree_Get_Statle(t_r);
    t_nil=t_r->nil;
    t_t=&t_r->Order_Save;
    if (k==-2)
        return NULL;
    data=(*t_t)->data;
    if (data==NULL)
    {
        Set_Order(t_n);
        return NULL;
    }
    if (k==0)
        Tree_Solve_Pre_Order(t_r,t_t);
    else if (k==-1)
        *t_t=(*t_t)->child[1];
    else 
        *t_t=(*t_t)->child[0];
    return data;
}
void* Tree_In_Order(Tree t_n)         //中序遍历
{
    return NULL;
}
void* Tree_Post_Order(Tree t_n)       //后序遍历
{
    return NULL;
}
/*******************私有函数实现部分*****************************/
static void Stack_Init(Stack_Head* const s)
{
    COMMON_Creat_Node((void** )&s->base,sizeof(Stack));
    s->base=s->top;
}
static void Stack_Push(Stack_Head* const  s,int flag)
{
    Stack* s_t=NULL;
    if (Stack_Is_Empty(s)==-1)
        Stack_Init(s);
    COMMON_Creat_Node((void**)&s_t,sizeof(Stack));
    s_t->flag=flag;
    s->top=s_t->next=s->top;
}
static void Stack_Pop(Stack_Head* const s)
{
    Stack* s_t=NULL;
    if (Stack_Is_Empty(s)!=0)
        return ;
    s_t=s->top->next;
    COMMON_Free_Node((void** )&s->top);
    s->top=s_t;
}
static int Stack_Get_Top(const Stack_Head* s)
{
    return s->top->flag;
}
static int Stack_Is_Empty(const Stack_Head* const s)
{
    if (s->base==NULL||s->top==NULL)
        return -1;
    return s->base==s->top;
}
static void Stack_Del(Stack_Head* const s)
{
    while (Stack_Is_Empty(s)!=0)
        Stack_Pop(s);
}
static int Tree_Get_Statle(Tree_Root* t_r)         //获取节点的状态
{
    Tree_Node* t_nil=NULL;
    Tree_Node* left_child=NULL;
    Tree_Node* right_child=NULL;
    if (t_r==NULL)
        return -3;
    if (t_r->root==t_r->nil)
        return -2;
    t_nil=t_r->nil;
    left_child=t_r->Order_Save->child[0];
    right_child=t_r->Order_Save->child[1];
    if (left_child==t_nil&&right_child==t_nil)
        return 0;
    
    if (left_child==t_nil&&right_child!=t_nil)
        return -1;
    if (left_child!=t_nil&&right_child==t_nil)
        return 1;
    return 2;
}
static void Tree_Solve_Pre_Order(Tree_Root* t_r,Tree_Node** t_n)
{
    while (1)
    {
        Tree_Location location=(*t_n)->location;
        *(t_n)=(*t_n)->p;
        if (*t_n==t_r->nil)
            break;
        if (location==RIGHT_CHILD)
            continue;
        if (location==LEFT_CHILD&&(*t_n)->child[1]==t_r->nil)
            continue;
        (*t_n)=(*t_n)->child[1];
        break;
    }
}[此贴子已经被作者于2017-7-11 14:50编辑过]



											


	    

	
