码迷,mamicode.com
首页 > 其他好文 > 详细

树和二叉树的应用

时间:2015-07-28 21:02:20      阅读:119      评论:0      收藏:0      [点我收藏+]

标签:二叉树

#include <stdio.h>  
#include <stdlib.h>  
#include <string.h>  
#include <conio.h>  
#define MAXSIZE 100  
typedef char ElemType;  
typedef struct Node  
{  
    ElemType data;  
    struct Node *lchild;  
    struct Node *rchild;  
}BitNode,*BitTree;  
  
int Similar(BitTree T1,BitTree T2);//相似二叉树  
void CreateBitTree1(BitTree *T,char *pre,char *in,int len);//由先序和中序构造二叉树  
void CreateBitTree2(BitTree *T,char *in,char *post,int len);//由中序和后序构造二叉树  
void Visit(BitTree T,BitTree pre,char e,int i);//访问结点e  
void PrintLevel(BitTree T);//按层次输出二叉树的结点  
void PreOrderTraverse(BitTree T);//先序遍历二叉树的递归实现  
void PostOrderTraverse(BitTree T);//后序遍历二叉树的递归实现  


#include "LinkBiTree.h"  
  
int Similar(BitTree T1,BitTree T2)  
{  
    if(T1 == NULL && T2 == NULL)  
    {  
        return 1;  
    }  
    else if((T1 == NULL && T2 != NULL) || (T1 != NULL && T2 == NULL))  
    {  
        return 0;  
    }  
    else  
    {  
        return (Similar(T1->lchild,T2->lchild)*Similar(T1->rchild,T2->rchild));  
    }  
}  
void CreateBitTree1(BitTree *T,char *pre,char *in,int len)//由先序和中序构造二叉树  
{  
    int k;  
    char *temp;  
    if(len <= 0)  
    {  
        *T = NULL;  
        return;  
    }  
    *T = (BitTree)malloc(sizeof(BitNode));  
    (*T)->data = *pre;  
    for(temp = in;temp < in+len;temp++)  
    {  
        if(*pre == *temp)  
        {  
            break;  
        }  
    }  
    k = temp-in;  
    CreateBitTree1(&((*T)->lchild),pre+1,in,k);  
    CreateBitTree1(&((*T)->rchild),pre+1+k,temp+1,len-1-k);  
}  
void CreateBitTree2(BitTree *T,char *in,char *post,int len)//由中序和后序构造二叉树  
{  
    int k;  
    char *temp;  
    if(len <= 0)  
    {  
        *T = NULL;  
        return;  
    }  
    for(temp = in;temp < in+len;temp++)  
    {  
        if(*(post+len-1) == *temp)  
        {  
            k = temp-in;  
            (*T) = (BitTree)malloc(sizeof(BitNode));  
            (*T)->data = *temp;  
            break;  
        }  
    }  
    CreateBitTree2(&((*T)->lchild),in,post,k);  
    CreateBitTree2(&((*T)->rchild),in+k+1,post+k,len-1-k);  
}  
void Visit(BitTree T,BitTree pre,char e,int i)//访问结点e  
{  
    if(T == NULL && pre == NULL)  
    {  
        printf("\n对不起!你还没有建立二叉树,先建立再访问!\n");  
        return;  
    }  
    if(T == NULL)  
    {  
        return;  
    }  
    else if(T->data == e)  
    {  
        if(pre != NULL)  
        {  
            printf("%2c的双亲结点是:%2c\n",e,pre->data);  
            printf("%2c的结点在%2d层上\n",e,i);  
        }  
        else  
        {  
            printf("%2c位于第一层,无双亲结点!\n",e);  
        }  
    }  
    else  
    {  
        Visit(T->lchild ,T,e,i+1);  
        Visit(T->rchild ,T,e,i+1);  
    }  
}  
void PrintLevel(BitTree T)//按层次输出二叉树的结点  
{  
    BitTree Queue[MAXSIZE];  
    int front,rear;  
    if(T == NULL)  
    {  
        return;  
    }  
    front = -1;  
    rear = 0;  
    Queue[rear] = T;  
    while(front != rear)  
    {  
        front++;  
        printf("%4c",Queue[front]->data);  
        if(Queue[front]->lchild != NULL)  
        {  
            rear++;  
            Queue[rear] = Queue[front]->lchild ;  
        }  
        if(Queue[front]->rchild != NULL)  
        {  
            rear++;  
            Queue[rear] = Queue[front]->rchild ;  
        }  
    }  
}  
void PreOrderTraverse(BitTree T)//先序遍历二叉树的递归实现  
{  
    if(T)  
    {  
        printf("%4c",T->data);  
        PreOrderTraverse(T->lchild);  
        PreOrderTraverse(T->rchild);  
    }  
}  
void PostOrderTraverse(BitTree T)//后序遍历二叉树的递归实现  
{  
    if(T)  
    {  
        PostOrderTraverse(T->lchild);  
        PostOrderTraverse(T->rchild);  
        printf("%4c",T->data);  
    }  
}  


#include "LinkBiTree.h"  
  
int main(void)  
{  
    BitTree T,ptr = NULL;  
    char ch;  
    int len;  
    char pre[MAXSIZE],in[MAXSIZE],post[MAXSIZE];  
    T = NULL;  
    printf("由先序序列和中序序列构造二叉树:\n");  
    printf("请你输入先序的字符串序列:");  
    gets(pre);  
    printf("请你输入中序的字符串序列:");  
    gets(in);  
    len = strlen(pre);  
    CreateBitTree1(&T,pre,in,len);  
    printf("你建立的二叉树后序遍历结果是:\n");  
    PostOrderTraverse(T);  
    printf("\n");  
    printf("你建立的二叉树层序遍历结果是:\n");  
    PrintLevel(T);  
    printf("\n");  
    printf("请你输入你要访问的结点:");  
    ch = getchar();  
    getchar();  
    Visit(T,ptr,ch,1);  
    printf("由先序序列和中序序列构造二叉树:\n");  
    printf("请你输入中序的字符串序列:");  
    gets(in);  
    printf("请你输入后序的字符串序列:");  
    gets(post);  
    len = strlen(post);  
    CreateBitTree2(&T,in,post,len);  
    printf("你建立的二叉树先序遍历结果是:\n");  
    PreOrderTraverse(T);  
    printf("\n");  
    printf("你建立的二叉树层序遍历结果是:\n");  
    PrintLevel(T);  
    printf("\n");  
    printf("请你输入你要访问的结点:");  
    ch = getchar();  
    getchar();  
    Visit(T,ptr,ch,1);  
    return 0;  
}  

版权声明:本文为博主原创文章,未经博主允许不得转载。

树和二叉树的应用

标签:二叉树

原文地址:http://blog.csdn.net/weichanjuan3/article/details/47111127

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!