森林與二叉樹的轉換-

出自http://download.csdn.net/detail/hansong1989/4277774node

#include <iostream>
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>ios

using namespace std;
//存有孩子結點索引的單鏈表結點
typedef struct TreeLinkNode
{
    int index;
    struct TreeLinkNode *next;
} TreeLinkNode,*pTreeLinkNode;
//孩子鏈表結點(樹結點)
typedef struct TreeNode
{
    pTreeLinkNode link;
    int data;
} TreeNode,*pTreeNode;
//森林結點
typedef struct FTreeNode
{
    pTreeNode tree;
    struct FTreeNode *next;
} FTreeNode,*pFTreeNode;
//森林鏈表的初始化操做
void InitFTree(pFTreeNode &fTree)
{
    fTree = (pFTreeNode)malloc(sizeof(FTreeNode));
    fTree->next = NULL;
}
//森林鏈表的插入操做
void InsertFTree(pFTreeNode &fTree,pTreeNode node)
{
    pFTreeNode t_node = fTree;
    pFTreeNode newnode  = (pFTreeNode)malloc(sizeof(FTreeNode));函數

    while(t_node->next != NULL)
    {
        t_node = t_node->next;
    }spa

    newnode->tree = node;
    t_node->next = newnode;
    newnode->next = NULL;
}
//廣義表創建樹鏈表(孩子鏈表)
void GListToTree(pTreeNode &tree,int &current_index,int parent_index)
{
    char ch;
    pTreeLinkNode newnode;.net

    pTreeLinkNode link = (pTreeLinkNode)malloc(sizeof(TreeLinkNode));
    link->index = current_index;
    link->next = NULL;
    tree[parent_index].link = link;索引

    while(true)
    {
        scanf("%c",&ch);get

        if(ch == '(')
        {
            parent_index = current_index;
            current_index ++;
            scanf("%d",&tree[current_index].data);
            tree[current_index].link = NULL;it

            GListToTree(tree,current_index,parent_index);
        }
        else if(ch == ',')
        {
            current_index ++;
            scanf("%d",&tree[current_index].data);
            tree[current_index].link = NULL;io

            newnode = (pTreeLinkNode)malloc(sizeof(TreeLinkNode));
            newnode->index = current_index;
            newnode->next = NULL;
            link->next = newnode;
            link = link->next;
        }
        else
        {
            return;
        }
    }
}
//廣義表組創建森林鏈表
void GListToFTree(pFTreeNode &fTree)
{
    char ch;
    int current_index,parent_index;
    pTreeNode tree;
    scanf("%c",&ch);stream

    while(ch != '#')
    {
        current_index = -1;
        tree=(pTreeNode)malloc(50*sizeof(TreeNode));

        current_index ++;
        scanf("%d",&tree[current_index].data);
        tree[current_index].link = NULL;

        scanf("%c",&ch);

        if(ch != ')')
        {
            parent_index = current_index;
            current_index ++;
            scanf("%d",&tree[current_index].data);
            tree[current_index].link = NULL;

            GListToTree(tree,current_index,parent_index);
            scanf("%c",&ch);
        }

        InsertFTree(fTree,tree);
        scanf("%c",&ch);
    }
}
//存儲先序遍歷序列記錄的單鏈表結點
typedef struct LinkNode
{
    int data;
    struct LinkNode *next;
} LinkNode,*pLinkNode;
//單鏈表的初始化
void InitLinkList(pLinkNode &link)
{
    link=(pLinkNode)malloc(sizeof(LinkNode));
    link->next =NULL;
}
//單鏈表的創建
void CreateLinkList(pLinkNode &link,int m)
{
    int i;
    pLinkNode newnode,p_Link=link;

    printf("請輸入先序遍歷序列:");
    for(i=0; i<m; i++)
    {
        newnode=(pLinkNode)malloc(sizeof(LinkNode));
        scanf("%d",&newnode->data);
        newnode->next =NULL;
        p_Link->next =newnode;
        p_Link = p_Link->next;
    }
}
//單鏈表的頭刪
void DeleteFirst(pLinkNode &link)
{
    pLinkNode p_Link=link->next ;
    if(p_Link != NULL)
    {
        link->next =p_Link->next ;
        free(p_Link);
    }
}
//存儲中序遍歷序列的雙向鏈表結點
typedef struct DuLinkNode
{
    int data;
    bool isVisited;
    struct DuLinkNode *next;
    struct DuLinkNode *prior;
} DuLinkNode,*pDuLinkNode;
//雙向鏈表的初始化
void InitDuLinkList(pDuLinkNode &DuLink)
{
    DuLink=(pDuLinkNode)malloc(sizeof(DuLinkNode));
    DuLink->isVisited = true;
    DuLink->prior =NULL;
    DuLink->next =(pDuLinkNode)malloc(sizeof(DuLinkNode));
    DuLink->next->prior=DuLink;
    DuLink->next ->isVisited = true;
    DuLink->next ->next =NULL;
}
//雙向鏈表的創建
void CreateDuLinkList(pDuLinkNode &DuLink,int m)
{
    int i;
    pDuLinkNode newnode,p_DuLink=DuLink;

    printf("請輸入中序遍歷序列:");
    for (i=0; i<m; i++)
    {
        newnode = (pDuLinkNode)malloc(sizeof(DuLinkNode));
        scanf("%d",&newnode->data);
        newnode->isVisited = false;
        newnode->next = p_DuLink->next ;
        p_DuLink->next ->prior = newnode;
        newnode->prior = p_DuLink;
        p_DuLink->next = newnode;

        p_DuLink = p_DuLink->next;
    }
}
//二叉樹結點
typedef struct BiTreeNode
{
    int data;
    struct BiTreeNode *left;
    struct BiTreeNode *right;
} BiTreeNode,*pBiTreeNode;
//二叉樹
typedef struct BiTree
{
    pBiTreeNode tree;
} BiTree;
//二叉樹初始化
void InitBiTree(BiTree &biTree)
{
    biTree.tree = NULL;
}
//先序中序重建二叉樹
void CreateBiTree(pBiTreeNode &biTree,pLinkNode &link,
                  pDuLinkNode &DuLink)
{
    pLinkNode p_Link = link->next;
    pDuLinkNode p_DuLink = DuLink->next;
    if(p_Link != NULL)
    {
        biTree = (pBiTreeNode)malloc(sizeof(BiTreeNode));
        biTree->left = NULL;
        biTree->right = NULL;
        biTree->data = p_Link->data ;

        while(p_Link->data != p_DuLink->data)
        {
            p_DuLink = p_DuLink->next;
        }

        p_DuLink->isVisited = true;
        DeleteFirst(link);
        if(p_DuLink->prior->isVisited == false)
        {
            CreateBiTree(biTree->left,link,DuLink);
        }
        if(p_DuLink->next ->isVisited == false)
        {
            CreateBiTree(biTree->right,link,DuLink);
        }
    }
}
//二叉樹轉化爲樹,是二叉樹轉化爲森林的子函數
void BiTreeToTree(pTreeNode &tree,pBiTreeNode &biTree,
                  int &current_index,int parent_index)
{
    pBiTreeNode p_biTree = biTree->left;
    pTreeLinkNode newnode;

    if(p_biTree != NULL)
    {
        current_index ++;
        tree[current_index].data = p_biTree->data;
        tree[current_index].link = NULL;

        newnode = (pTreeLinkNode)malloc(sizeof(TreeLinkNode));
        newnode->index = current_index;
        newnode->next = NULL;
        tree[parent_index].link = newnode;

        BiTreeToTree(tree,p_biTree,current_index,newnode->index);

        while(p_biTree->right != NULL)
        {
            p_biTree = p_biTree->right;

            current_index ++;
            tree[current_index].data = p_biTree->data;
            tree[current_index].link = NULL;

            newnode->next = (pTreeLinkNode)malloc(sizeof(TreeLinkNode));
            newnode = newnode->next;
            newnode->index = current_index;
            newnode->next = NULL;

            BiTreeToTree(tree,p_biTree,current_index,newnode->index);
        }
    }
}
//二叉樹轉化爲森林
void BiTreetoFTree(pFTreeNode &fTree,pBiTreeNode &biTree)
{
    int current_index,parent_index;
    pTreeNode newnode;
    pBiTreeNode p_biTree = biTree;

    do
    {
        newnode = (pTreeNode)malloc(sizeof(TreeNode) * 50);

        current_index = 0 ;
        parent_index = current_index;
        newnode[current_index].data = p_biTree->data;
        newnode[current_index].link = NULL;

        BiTreeToTree(newnode,p_biTree,current_index,parent_index);
        InsertFTree(fTree,newnode);

        p_biTree = p_biTree->right;
    }
    while(p_biTree != NULL);
}
//樹轉化爲二叉樹,是森林轉化爲二叉樹的子函數
void TreeToBiTree(pTreeNode &tree,pBiTreeNode &biTree,int index)
{
    pBiTreeNode newnode = (pBiTreeNode)malloc(sizeof(BiTreeNode));
    newnode->data = tree[index].data;
    newnode->left = NULL;
    newnode->right = NULL;
    biTree = newnode;

    pTreeLinkNode link = tree[index].link;
    pBiTreeNode tempNode = biTree;

    if(link != NULL)
    {
        TreeToBiTree(tree,tempNode->left,link->index);
        link = link->next;
        tempNode = tempNode->left;
        while(link != NULL)
        {
            TreeToBiTree(tree,tempNode->right,link->index);
            link = link->next;
            tempNode = tempNode->right;
        }
    }
}
//森林轉化爲二叉樹
void FTreeToBiTree(pFTreeNode &fTree,BiTree &biTree)
{
    pFTreeNode p_fTree = fTree->next;
    pBiTreeNode p_biTree = biTree.tree;

    while(p_fTree != NULL)
    {
        if(biTree.tree == NULL)
        {
            TreeToBiTree(p_fTree->tree,biTree.tree,0);
            p_biTree = biTree.tree;
        }
        else
        {
            TreeToBiTree(p_fTree->tree,p_biTree->right,0);
            p_biTree = p_biTree->right;
        }
        p_fTree = p_fTree->next;
    }
}
//樹轉化爲廣義表,是森林轉化爲廣義表的子函數
void TreeToGList(pTreeNode &tree,int parent_index)
{
    pTreeLinkNode p_treeLink = tree[parent_index].link;

    if(p_treeLink != NULL)
    {
        printf("(");
        printf("%d",tree[p_treeLink->index].data);
        TreeToGList(tree,p_treeLink->index);
        p_treeLink = p_treeLink->next;

        while(p_treeLink != NULL)
        {
            printf(",");
            printf("%d",tree[p_treeLink->index].data);
            TreeToGList(tree,p_treeLink->index);

            p_treeLink = p_treeLink->next;
        }

        printf(")");
    }
}
//森林轉化爲廣義表
void FTreeToGList(pFTreeNode &fTree)
{
    int parent_index;
    pFTreeNode p_fTree = fTree->next;

    while(p_fTree != NULL)
    {
        parent_index = 0;

        printf("(");
        printf("%d",p_fTree->tree[0].data);
        TreeToGList(p_fTree->tree,parent_index);
        printf(")");

        p_fTree = p_fTree->next;
    }
    printf("#");
}
//樹的先跟遍歷,是森林的先根遍歷的子函數
void TreePreTraverse(pTreeNode &tree,int index)
{
    pTreeLinkNode link ;
    printf("%d ",tree[index].data);

    link = tree[index].link;
    while(link != NULL)
    {
        TreePreTraverse(tree,link->index);
        link = link->next;
    }
}
//森林的先根遍歷
void FTreePreTraverse(pFTreeNode &fTree)
{
    pFTreeNode link = fTree->next;
    while(link != NULL)
    {
        TreePreTraverse(link->tree,0);
        link = link->next;
    }
}
//樹的後根遍歷,是森林的後根遍歷的子函數
void TreePostTraverse(pTreeNode &tree,int index)
{
    pTreeLinkNode link ;

    link = tree[index].link;
    while(link != NULL)
    {
        TreePostTraverse(tree,link->index);
        link = link->next;
    }
    printf("%d ",tree[index].data);
}
//森林的後根遍歷
void FTreePostTraverse(pFTreeNode &fTree)
{
    pFTreeNode link = fTree->next;
    while(link != NULL)
    {
        TreePostTraverse(link->tree,0);
        link = link->next;
    }
}
//二叉樹的先序遍歷
void BiTreePreTraverse(pBiTreeNode &tree)
{
    if(tree != NULL)
    {
        printf("%d ",tree->data);

        if(tree->left != NULL)
        {
            BiTreePreTraverse(tree->left);
        }

        if(tree->right != NULL)
        {
            BiTreePreTraverse(tree->right);
        }
    }
}
//二叉樹的中序遍歷
void BiTreeInOrderTraverse(pBiTreeNode &tree)
{
    if(tree !=NULL)
    {
        if(tree->left != NULL)
        {
            BiTreeInOrderTraverse(tree->left);
        }

        printf("%d ",tree->data);

        if(tree->right != NULL)
        {
            BiTreeInOrderTraverse(tree->right);
        }
    }
}
//主函數
int main(void)
{


    printf("\n##################################################################\n");
    printf("\n樹(森林)的廣義表(括號表示法)重建樹(森林)\n");
    printf("請輸入樹(森林)的廣義表(括號表示法)(以#結束):\n");

    pFTreeNode fTree1;
    InitFTree(fTree1);
    GListToFTree(fTree1);

    printf("\n樹(森林)的廣義表(括號表示法)重建樹(森林)後的遍歷\n");
    printf("樹(森林)的先根遍歷:\n");
    FTreePreTraverse(fTree1);
    printf("\n");
    printf("樹(森林)的後根遍歷:\n");
    FTreePostTraverse(fTree1);
    printf("\n");

    printf("\n##################################################################\n");
    printf("\n樹(森林)轉化成二叉樹:\n\n");
    BiTree biTree1;
    InitBiTree(biTree1);
    FTreeToBiTree(fTree1,biTree1);

    printf("樹(森林)轉換成二叉樹後二叉樹的遍歷\n");
    printf("樹(森林)轉換成的二叉樹的先序遍歷:\n");
    BiTreePreTraverse(biTree1.tree);
    printf("\n樹(森林)轉換成的二叉樹的中序遍歷:\n");
    BiTreeInOrderTraverse(biTree1.tree);
    printf("\n");

    printf("\n##################################################################\n");
    printf("\n由樹的先序遍歷序列和樹的中序遍歷序列重建二叉樹:\n");
    int m;
    printf("請輸入二叉樹的結點數:");
    scanf("%d",&m);

    pLinkNode link;
    InitLinkList(link);
    CreateLinkList(link,m);

    pDuLinkNode DuLink;
    InitDuLinkList(DuLink);
    CreateDuLinkList(DuLink,m);

    BiTree biTree2;
    InitBiTree(biTree2);
    CreateBiTree(biTree2.tree,link,DuLink);

    printf("\n重建二叉樹後的先序遍歷:\n");
    BiTreePreTraverse(biTree2.tree);
    printf("\n重建二叉樹後的中序遍歷:\n");
    BiTreeInOrderTraverse(biTree2.tree);
    printf("\n");

    printf("\n##################################################################\n");
    pFTreeNode fTree2;
    InitFTree(fTree2);

    printf("\n重建的二叉樹轉化成樹(森林):\n\n");
    BiTreetoFTree(fTree2,biTree2.tree);
    printf("重建的二叉樹轉換成樹(森林)後,樹(森林)的遍歷:\n");
    printf("樹(森林)的先根遍歷:\n");
    FTreePreTraverse(fTree2);
    printf("\n樹(森林)的後根遍歷:\n");
    FTreePostTraverse(fTree2);
    printf("\n\n");

    printf("##################################################################\n");
    printf("\n上面生成的樹(森林)生成其相應的廣義表(括號表示法)\n");
    FTreeToGList(fTree2);
    printf("\n\n##################################################################\n");

    return 0;}

相關文章
相關標籤/搜索