bitree.h

来自「编写程序」· C头文件 代码 · 共 84 行

H
84
字号
typedef struct Node
{   
    DataType data;
    struct Node *leftChild;
    struct Node *rightChild;
} BiTreeNode;


void BiTreeInitiate(BiTreeNode **root)
{   
    *root=(BiTreeNode *)malloc(sizeof(BiTreeNode));
    (*root)->leftChild = NULL;
    (*root)->rightChild = NULL;
}

BiTreeNode *InsertLeftNode(BiTreeNode *curr,DataType x)
{   
    BiTreeNode *s,*t;
    
    if(curr==NULL)
        return NULL;
    
    t=curr->leftChild;
    s=(BiTreeNode *)malloc(sizeof(BiTreeNode));
    s->data=x;
    s->leftChild=t;
    s->rightChild=NULL;
    
    curr->leftChild=s;
    
    return curr->leftChild;
}


BiTreeNode *InsertRightNode(BiTreeNode *curr,DataType x)
{
    BiTreeNode *s,*t;
    
    if(curr==NULL)
        return NULL;
    
    t=curr->rightChild;
    s=(BiTreeNode *)malloc(sizeof(BiTreeNode));
    s->data=x;
    s->rightChild=t;
    s->leftChild=NULL;
    
    curr->rightChild=s;
    
    return curr->rightChild;
}


void BiTreeDestroy(BiTreeNode **root)
{   
    if((*root)->leftChild!=NULL&&(*root)!=NULL)
        BiTreeDestroy(&(*root)->leftChild);
    if((*root)->rightChild!=NULL&&(*root)!=NULL)
        BiTreeDestroy(&(*root)->rightChild);
    
    free(*root);
}

BiTreeNode *DeleteLeftTree(BiTreeNode *curr)
{   
    if(curr==NULL||curr->leftChild==NULL)
        return NULL;
    
    BiTreeDestroy(&curr->leftChild);
    curr->leftChild=NULL;
    return curr;
}

BiTreeNode *DeleteRightTree(BiTreeNode *curr)
{   
    if(curr==NULL||curr->rightChild==NULL)
        return NULL;
    
    BiTreeDestroy(&curr->rightChild);
    curr->rightChild=NULL;
    return curr;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?