二叉树基础 遍历与还原 整理

整理一下,RT。

先定义一下各种东西~~

typedef struct TreeNode *BinTree;  
typedef char ElementType ;
typedef BinTree Position;
struct TreeNode     //定义结构体
{
    ElementType Data;
    BinTree Left;
    BinTree Right;
};
char st[55];
int cnt;

首先是先序输入二叉树

struct TreeNode *creat()   //先序遍历建立二叉树
{
    struct TreeNode * root;
    if(st[++cnt]==',')       
    {
        root= NULL;   //逗号表示空
    }
    else
    {
        root = (struct TreeNode *)malloc(sizeof(struct TreeNode));  //建立新空间
        root->Data = st[cnt];
        root->Left = creat();  //递归
        root->Right = creat();  //递归
    }
   return root;   //返回结构体
};

之后是四种遍历方式

1,先序遍历

void xianxu(struct TreeNode *root)   //先序遍历
{
    if(root)
    {
        printf("%c",root->Data);
        zhongxu(root->Left);
        zhongxu(root->Right);
    }
}

2.中序遍历

void zhongxu(struct TreeNode *root)   //中序遍历
{
    if(root)
    {
        zhongxu(root->Left);
        printf("%c",root->Data);
        zhongxu(root->Right);
    }
}

3.后序遍历

void houxu(struct TreeNode *root)   //后序遍历
{
    if(root)
    {
        houxu(root->Left);
        houxu(root->Right);
        printf("%c",root->Data);
    }
}

4.层次遍历

void cend(struct node *root)//层次遍历二叉树
{
    int in=0,out=0;
    struct node *temp[100];
    temp[in++]=root;
    while(in>out)
    {
        if(temp[out])
        {
            
            printf("%c",temp[out]->data);
            temp[in++]=temp[out]->Left;
            temp[in++]=temp[out]->Right;
        }
        out++;
    }
printf("\n");
}

下面是两种二叉树的还原

1,先序中序还原二叉树

//char st1[100],st2[100];
struct TreeNode *creat(int n,char *st1,char *st2)
{
    struct TreeNode *root;
    char *p;
    if(n == 0)return NULL;
    root = (struct TreeNode *)malloc(sizeof(struct TreeNode));
    root->Data = st1[0];
    for(p = st2;p != '\0';p++)
    {
        if(*p == st1[0])
            break;
    }
    int t;
    t = p - st2;
    root->Left = creat(t,st1+1,st2);
    root->Right = creat(n - t- 1,st1 + t+1,p+1);return root;

}

2.中序后序还原二叉树

struct TreeNode *creat2(int n,char *st1,char *st2)  //中后还原
{
    struct TreeNode *root;
    int i;

    if(n <= 0)return NULL;
    else
    {
        root = (struct TreeNode *)malloc(sizeof(struct TreeNode));
        root->Data = st2[n - 1];
        for( i =0; i < n; i++)        //此处用i来处理位置,即下标问题    
        { 
            if(st1[i] == st2[n - 1])
                break;
        }

        root->Left = creat2(i,st1,st2);     
        root->Right = creat2(n - i - 1,st1 + i + 1,st2 + i);
        return root;
    }
};

然后是关于深度和叶子数量以及输出叶子

1.树的深度或高度

int depth(struct TreeNode *root) //深度的判断
{
    int i,j;
    if(!root)return 0;
    if(root->Left) i= depth(root->Left);
    else i = 0;
    if(root->Right)j = depth(root->Right);
    else j = 0;
    return i>j?i + 1:j+1;
}

2.叶子数量

int leave(struct TreeNode *root)  //叶子数量
{
    if(root == NULL)
        return 0;
    if(root->Left == NULL&& root->Right == NULL)
        return 1;
    else return leave(root->Left) + leave(root -> Right);
}

3.输出每片叶子

ps:这里给出的是层次遍历的方式输出叶子,可以自行更改遍历方式

void  leave(struct TreeNode *root)
{
    struct TreeNode *temp[55];
    int in = 0,out = 0;
    temp[in++] = root;
    while(in > out)
    {
        if(temp[out])
        {
            if(temp[out]->Left == NULL && temp[out]->Right==NULL)  //层次遍历 加上了叶子判断
            {printf("%c",temp[out]->Data);}
            temp[in++] = temp[out]->Left;
            temp[in++] = temp[out]->Right;
        }
        out++;
    }
}

结束~

猜你喜欢

转载自blog.csdn.net/weixin_40616644/article/details/81514070
今日推荐