二叉樹 c實現

來源:互聯網
上載者:User

 

#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);
    }
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.