二叉树 c实现

 

#include<stdio.h>
#include<stdlib.h>

#define MAXLen 100 //最大节点数
typedef char DATA;//元素类型
typedef struct CBT
{
    DATA data;
    struct CBT *left;//左子树节点指针
    struct CBT *right;//右子树节点指针
}CBTType;

CBTType *InitTree()//初始化二叉树根
{
    CBTType *node;
    if(node=(CBTType*)malloc(sizeof(CBTType)))
    {
        printf("请先输入一个根节点数据:\n");
        scanf("%s",&node->data);
        node->left=NULL;
        node->right=NULL;
        if(node!=NULL)
        {
            return node;
        }else
        {
            return NULL;
        }
    }
    return NULL;
}

CBTType *TreeFindNode(CBTType *treeNode,DATA data)
{
    CBTType *ptr;
    if(treeNode==NULL)
    {
        return NULL;
    }
    else
    {
        if(treeNode->data==data)
        {
            return treeNode;
        }
        else
        {
            if(ptr==TreeFindNode(treeNode->left,data))
            {
                return ptr;
            }
            else if(ptr==TreeFindNode(treeNode->right,data))
            {
                return ptr;
            }else
            {
                return NULL;
            }
        }
    }
}

void AddTreeNode(CBTType *treeNode)
{
    CBTType *pnode,*parent;
    DATA data;
    char menusel;

    if(pnode=(CBTType *)malloc(sizeof(CBTType)))
    {
        printf("输入二叉树节点数据:\n");
        scanf("%s",&pnode->data);
        pnode->left=NULL;
        pnode->right=NULL;

        printf("输入该节点的父节点数据:\n");
        scanf("%s",&data);
       
        parent=TreeFindNode(treeNode,data);
        if(!parent)
        {
            printf("未找到该父节点 !\n");
            free(pnode);
            return;
        }
        printf(" 1.添加该节点的左子树\n2.添加该节点的右子树\n");
        do
        {
            menusel=getchar();
            menusel-='0';
            if(menusel==1||menusel==2)
            {
                if(parent==NULL)
                {
                    printf("不存在父节点,请先设置父节点!\n");
                }
                else
                {
                    switch(menusel)
                    {
                    case 1:
                        if(parent->left)
                        {
                            printf("左子树节点不为空!\n");
                        }
                        else
                        {
                            parent->left=pnode;
                        }
                        break;
                    case 2:
                        if(parent->right)
                        {
                            printf("右子树节点不为空!\n");
                        }
                        else
                        {
                            parent->right=pnode;
                        }
                        break;
                    default:
                        printf("无效参数!\n");
                    }
                }
            }
   
        }while(menusel!=1&&menusel!=2);
    }
}

CBTType *TreeLeftNode(CBTType *treeNode)
{
    if(treeNode)
    {
        return treeNode->left;
    }else
    {
        return NULL;
    }
}

CBTType *TreeRightNode(CBTType *treeNode)
{
    if(treeNode)
    {
        return treeNode->right;
    }else
    {
        return NULL;
    }
}

int TreeIsEmpty(CBTType* treeNode)
{
    if(treeNode)
    {
        return 0;
    }
    else
    {
        return 1;
    }
}

int TreeDepth(CBTType *treeNode)
{
    int depleft,depright;
    if(treeNode==NULL)
    {
        return 0;
    }
    else
    {
        depleft=TreeDepth(treeNode->left);
        depright=TreeDepth(treeNode->right);
        if(depleft>depright)
        {
            return depleft+1;
        }
        else
        {
            return depright+1;
        }
    }
}

void ClearTree(CBTType *treeNode)
{
    if(treeNode)
    {
        ClearTree(treeNode->left);
        ClearTree(treeNode->right);
        free(treeNode);
        treeNode=NULL;
    }
}
void TreeNodeData(CBTType *p)
{
    printf("%c",p->data);
}

void LevelTree(CBTType *treeNode,void(*TreeNodeData)(CBTType *p))
{
    CBTType *p;
    CBTType *q[MAXLen];
    int head=0,tail=0;

    if(treeNode)
    {
        tail=(tail+1)%MAXLen;
        q[tail]=treeNode;
    }
    while(head!=tail)
    {
        head=(head+1)%MAXLen;
        p=q[head];
        TreeNodeData(p);
        if(p->left)
        {
            tail=(tail+1)%MAXLen;
            q[tail]=p->left;
        }
        if(p->right)
        {
            tail=(tail+1)%MAXLen;
            q[tail]=p->right;
        }
    }
}
void DLRTree(CBTType *treeNode,void(*TreeNodeData)(CBTType *p))
{
    if(treeNode)
    {
        TreeNodeData(treeNode);
        DLRTree(treeNode->left,TreeNodeData);
        DLRTree(treeNode->right,TreeNodeData);
    }
}
void LDRTree(CBTType *treeNode,void(*TreeNodeData)(CBTType *p))
{
    if(treeNode)
    {
        LDRTree(treeNode->left,TreeNodeData);
        TreeNodeData(treeNode);
        LDRTree(treeNode->right,TreeNodeData);
    }
}
void RDLTree(CBTType *treeNode,void(*TreeNodeData)(CBTType *p))
{
    if(treeNode)
    {
        RDLTree(treeNode->right,TreeNodeData);
        TreeNodeData(treeNode);
        RDLTree(treeNode->left,TreeNodeData);
    }
}

posted @ 2011-11-15 14:41  朱旭东  阅读(347)  评论(0编辑  收藏  举报