Tree...

從鍵盤接收擴展先序序列,以二叉鏈表作爲存儲結構,建立二叉樹。輸出這棵二叉樹的先序、中序和後序遍歷序列。

二叉樹結點的data是字符類型數據, 其中#表示空格字符。

/*************************************************************************
        > File Name: 先序創建二叉樹.c
      > Author: dongmengyuan
      > Mail: 1322762504@qq.com
      > Created Time: 20161120日 星期日 184250秒
 ************************************************************************/

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

typedef char datatype;
typedef struct Node 
{
    datatype data;
    struct Node *lchild;
    struct Node *rchild;
}BiTNode, *BiTree;

//先序創建二叉樹
void Creat(BiTree *root)
{
    char ch;
    ch = getchar();
    if(ch == '#') {
        *root = NULL;
    }
    else {
        *root = (BiTree)malloc(sizeof(BiTNode));
        (*root) -> data = ch;
        Creat(&((*root) -> lchild));
        Creat(&((*root) -> rchild));
    }
}

//先序遍歷輸出二叉樹
void PreOrder(BiTree root) 
{
    if(root) {
        printf("%c",root->data);
        PreOrder(root->lchild);
        PreOrder(root->rchild);
    }
}

//中序遍歷輸出二叉樹
void InOrder(BiTree root)
{
    if(root) {
        InOrder(root->lchild);
        printf("%c",root->data);
        InOrder(root->rchild);
    }

}

//後序遍歷輸出二叉樹
void PostOrder(BiTree root)
{
    if(root) {
        PostOrder(root->lchild);
        PostOrder(root->rchild);
        printf("%c",root->data);
    }
}

int main()
{
    BiTree root;
    Creat(&root);
    PreOrder(root);
    printf("\n");
    InOrder(root);
    printf("\n");
    PostOrder(root);
    printf("\n");
    return 0;
}

從鍵盤接收擴展先序序列,以二叉鏈表作爲存儲結構,建立二叉樹。分別統計二叉樹中葉子結點、度爲1的結點、度爲2的結點的個數,並輸出。

第一行依次輸出葉子結點個數、度爲1的結點個數、度爲2的結點個數,以空格隔開。
第二行連續輸出葉子結點,中間不間隔。

/*************************************************************************
        > File Name: 結點個數.c
      > Author: dongmengyuan
      > Mail: [email protected]
      > Created Time: 2016年11月20日 星期日 22時34分26秒
 ************************************************************************/

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

typedef char datatype;
typedef struct Node
{
    datatype data;
    struct Node *lchild;
    struct Node *rchild;
}BiTNode, *BiTree;

//先序創建二叉樹
BiTree creat()
{
    char ch;
    ch = getchar();
    if(ch == '#') {
        return NULL;
    }
    else {
        BiTree root = malloc(sizeof(BiTNode));
        root->data = ch;
        root -> lchild = creat();
        root -> rchild = creat();
        return root;
    }
}

int a = 0,b = 0,c = 0;
char s[10000];
int k = 0;

void printleaf(BiTree root)
{
    if(root) {
        if(root->lchild == NULL && root->rchild == NULL) {
            s[k++] = root->data;
            a++;
        }
        if((root->lchild == NULL && root->rchild != NULL) || (root->lchild != NULL && root->rchild == NULL) ) {
            b++;
        }
        if(root->lchild != NULL && root->rchild !=NULL) {
            c++;
        }
        printleaf(root->lchild);
        printleaf(root->rchild);
    }
}

void PreOrder(BiTree root)
{
    if(root) {
        printf("%c",root->data);
        PreOrder(root->lchild);
        PreOrder(root->rchild);
    }  
}

int main()
{
    BiTree root;
    root = creat();
    printleaf(root);
    printf("%d %d %d\n",a,b,c);
    for(int i = 0; i < k; i++)
    printf("%c",s[i]);
    return 0;
}

從鍵盤接收擴展先序序列,以二叉鏈表作爲存儲結構,建立二叉樹。按先序遍歷次序輸出各結點的內容及相應的層次數,要求以二元組的形式輸出,其所對應的輸出結果爲:(data,level)

data是二叉樹結點數據域值,level是該結點所在的層次。
設根節點在第一層。
輸出的元素間不用間隔,()都是英文字符

/*************************************************************************
        > File Name: 輸出結點及相應的層數.c
      > Author: dongmengyuan
      > Mail: 1322762504@qq.com
      > Created Time: 20161122日 星期二 104137秒
 ************************************************************************/

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

typedef char datatype;
typedef struct Node
{
    datatype data;
    struct Node *lchild;
    struct Node *rchild;
}BiTNode, *BiTree;

//先序創建二叉樹
void creat(BiTree *root)
{
    char ch;
    ch = getchar();
    if(ch == '#') {
        *root = NULL;
    }
    else {
        *root = (BiTree) malloc (sizeof(BiTNode));
        (*root) -> data = ch;
        creat(&((*root)->lchild));
        creat(&((*root)->rchild));
    }
}

void preorder(BiTree root, int n)
{
    if(root) {
        printf("(%c,%d)",root->data,n);
        preorder(root->lchild,n+1);
        preorder(root->rchild,n+1);
    }
}

int main()
{
    int n;
    BiTree root;
    creat(&root);
    preorder(root,n+1);
    printf("\n");
    return 0;
}

從鍵盤接收擴展先序序列,以二叉鏈表作爲存儲結構,建立二叉樹。輸出從根結點到每個葉子結點的路徑。

輸出的元素間不用間隔,都是英文字符,每個路徑均佔一行。

/*************************************************************************
        > File Name: 輸出路徑.c
      > Author: dongmengyuan
      > Mail: [email protected]
      > Created Time: 2016年11月22日 星期二 10時12分48秒
 ************************************************************************/

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

typedef char datatype;
typedef struct Node
{
    datatype data;
    struct Node *lchild;
    struct Node *rchild;
}BiTNode, *BiTree;

void creat(BiTree *root)
{
    char ch;
    ch = getchar();
    if(ch == '#') {
        *root = NULL;
    }
    else {
        *root = (BiTree) malloc (sizeof(BiTNode));
        (*root) -> data = ch;
        creat(&((*root)->lchild));
        creat(&((*root)->rchild));
    }
}

void outpath(BiTree root, char path[], int n)
{
    if(root->lchild == NULL && root->rchild == NULL) {
        printf("%c:",root->data);
        for(int i = 0; i < n; i++) {
            printf("%c",path[i]);
        }
        printf("\n");
    }
    else {
        path[n] = root->data;
        if(root->lchild != NULL) {
            outpath(root->lchild,path,n+1);
        }
        if(root->rchild != NULL) {
            outpath(root->rchild,path,n+1);
        }
    }
}

int main()
{
    char path[1000];
    int n = 0;
    BiTree root;
    creat(&root);
    outpath(root,path,n);
    return 0;
}

從鍵盤接收擴展先序序列,以二叉鏈表作爲存儲結構,建立二叉樹。將這棵二叉樹的左右子樹進行交換,並輸出交換後二叉樹的先序、中序、後序遍歷序列

/*************************************************************************
        > File Name: 交換左右子樹.c
      > Author: dongmengyuan
      > Mail: [email protected]
      > Created Time: 2016年11月22日 星期二 12時02分23秒
 ************************************************************************/

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

typedef char datatype;
typedef struct Node 
{
    datatype data;
    struct Node *lchild;
    struct Node *rchild;
}BiTNode, *BiTree;

void creat(BiTree *root)
{
    char ch;
    ch  = getchar();
    if(ch == '#') {
        *root = NULL;
    }
    else {
        *root = (BiTree) malloc (sizeof(BiTNode));
        (*root) -> data = ch;
        creat(&(*root) -> lchild);
        creat(&(*root) -> rchild);

    }
}

BiTree change(BiTree root)
{
    BiTree t;
    if(root == NULL || (root -> lchild == NULL && root -> rchild == NULL)) {
        return root;
    }
    t = root -> lchild;
    root->lchild = root->rchild;
    root->rchild = t;
    if(root -> lchild) {
        root -> lchild = change(root->lchild);
    }
    if(root -> rchild) {
        root -> rchild = change(root->rchild);
    }
    return root;
}

void preorder(BiTree root)
{
    if(root) {
        printf("%c",root->data);
        preorder(root->lchild);
        preorder(root->rchild);
    }
}

void inorder(BiTree root)
{
    if(root) {
        inorder(root->lchild);
        printf("%c",root->data);
        inorder(root->rchild);
    }
}

void postorder(BiTree root)
{
    if(root) {
        postorder(root->lchild);
        postorder(root->rchild);
        printf("%c",root->data);
    }
}

int main()
{
    BiTree root;
    creat(&root);
    change(root);
    preorder(root);
    printf("\n");
    inorder(root);
    printf("\n");
    postorder(root);
    printf("\n");
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章