数据结构与算法李春葆之判断二叉树子树

#include <stdio.h>
#include <malloc.h>
#include <stdbool.h>

#define MAX_SIZE 100

typedef char ElemType;
typedef struct node
{
    ElemType data; 
    struct node *lchild; 
    struct node *rchild;
}BTNode; 

typedef struct
{
    char data[MAX_SIZE];
    int length; 
}SqString;

static void create_btree(BTNode *&b, char *str) 
{
    BTNode *p;
    BTNode *St[MAX_SIZE];
    int k;
    int j = 0;
    int top = -1; 
    char ch;

    b = NULL; 
    ch = str[j];
    while(ch != '\0')
    {
        switch(ch)
        {
        case '(':
            top++;
            St[top] = p;
            k = 1;
            break;
        case ')':
            top--;
            break;
        case ',': 
            k = 2;
            break;
        default:
            p = (BTNode *)malloc(sizeof(BTNode)); 
            p->data = ch;
            p->lchild = p->rchild = NULL;
            if(b == NULL) 
                b = p;
            else
            {
                switch(k)
                {
                case 1:
                    St[top]->lchild = p;
                    break;
                case 2:
                    St[top]->rchild = p;
                    break;
                }
            }
            break;
        }
       
        j++;
        ch = str[j];
    }
}



static void disp_btree(BTNode *b)
{
    if(b != NULL)
    {
        printf("%c", b->data);
        if(b->lchild != NULL || b->rchild != NULL)
        {
            printf("("); 
            disp_btree(b->lchild); 
            if(b->rchild != NULL)
                printf(",");
            disp_btree(b->rchild);
            printf(")"); 
        }
    }
}
static void destroy_btree(BTNode *&b)
{
    if(b != NULL)
    {
        destroy_btree(b->lchild);
        destroy_btree(b->rchild);
        free(b);
    }
}
static BTNode *left_child_node(BTNode *b)
{
    return b->lchild;
}
static BTNode *right_child_node(BTNode *b)
{
    return b->rchild;
}
static BTNode *find_node(BTNode *b, ElemType x) 
{
    BTNode *p;

    if(b == NULL)
        return NULL;
    else if(b->data == x)
        return b;
    else
    {
        p = find_node(b->lchild, x);
        if(p != NULL)
            return p;
        else
            return find_node(b->rchild, x);
    }
}
static int btree_height(BTNode *b)
{
    int left_child_height, right_child_height;

    if(b == NULL) 
        return 0;
    else
    {
       
        left_child_height = btree_height(b->lchild);
        right_child_height = btree_height(b->rchild);
        return (left_child_height > right_child_height) ? (left_child_height + 1) : (right_child_height + 1);
    }
}
static void str_assign(SqString &s, char cstr[])
{
    int i;

    for(i = 0; cstr[i] != '\0'; i++)
        s.data[i] = cstr[i];
    s.length = i;
}
static void destroy_str(SqString &s)
{
	
}
static void str_copy(SqString &s, SqString t)
{
    int i;
    for(i = 0; i < t.length; i++)
        s.data[i] = t.data[i];
    s.length = t.length;
}
static bool str_equal(SqString s, SqString t)
{
    bool same = true;
    if(s.length != t.length)
        same = false;
    else
    {
         for(int i = 0; i < s.length; i++)
         {
             if(s.data[i] != t.data[i]) 
             {
                 same = false;
                 break;
             }
         }
    }
    return same;
}
static int str_length(SqString s)
{
    return s.length;
}
static SqString str_concat(SqString s, SqString t)
{
     int index;
     SqString str;

     str.length = 0;
     for(index = 0; index < s.length; index++) 
        str.data[index] = s.data[index];
     for(index = 0; index < t.length; index++) 
        str.data[s.length + index] = t.data[index];
     str.length = s.length + t.length;

     return str;
}

static void disp_str(SqString s)
{
    int i;

    if(s.length > 0)
    {
        for(i = 0; i < s.length; i++)
        {
            printf("%c", s.data[i]);
        }
        printf("\n");
    }
}

int i;                                            
static SqString pre_order_seq(BTNode *b)
{
    SqString str;
    SqString left_str;
    SqString str1;
    SqString right_str;

    if(b == NULL)                           
    {
        str_assign(str, "#");               
        return str;
    }
    str.data[0] = b->data;                 
    str.length = 1;
    left_str = pre_order_seq(b->lchild);
    str1 = str_concat(str, left_str);
    right_str = pre_order_seq(b->rchild);
    str = str_concat(str1, right_str);

    return str;
}
static BTNode *create_pre_seq(SqString str)
{
    BTNode *b;
    char value;

    if(i >= str.length)                     
        return NULL;

    value = str.data[i];                 
    printf("create_pre_seq %c\n", value);
    i++;
    if(value == '#')                        
        return NULL;
    b = (BTNode *)malloc(sizeof(BTNode));   
    b->data = value;
    b->lchild = create_pre_seq(str);        
    b->rchild = create_pre_seq(str);       

    return b;                              
}
static void get_next(SqString t, int next[])
{
    int j = 0;
    int k = -1;
    next[0] = -1;
    while(j < t.length - 1)
    {
         if(k == -1 || t.data[j] == t.data[k])
         {
             j++;
             k++;
             next[j] = k;
         }
         else
              k = next[k];
    }
}
static int KMP_index(SqString s, SqString t)
{
    int i = 0;
    int j = 0;
    int next[MAX_SIZE];

    get_next(t, next);
    while(j < s.length && j < t.length)
    {
        if(j == -1 || s.data[i] == t.data[j])
        {
            
             i++;
             j++;
        }
        else
             j = next[j]; 
    }
    if(j >= t.length)
        return (i - t.length); 
    else
        return -1; 
}
static bool is_sub_tree(BTNode *b1, BTNode *b2)            
{
    SqString s1 = pre_order_seq(b1);                        
    SqString s2 = pre_order_seq(b2);                       

    if(KMP_index(s1, s2) != -1)                             
        return true;
    else                                                    
        return false;
}
int main(void)
{
    BTNode *b1, *b2;
    create_btree(b1, "A(B(D,E(H(J,K(L,M(,N))))),C(F,G(,I)))");
    printf("二叉树b1:");
    disp_btree(b1);
    printf("\n");
    create_btree(b2, "O(P,Q(,R))");
    printf("二叉树b2:");
    disp_btree(b2);
    printf("\n");
    if(is_sub_tree(b1, b2))
        printf("结果:b2是b1的子树\n");
    else
        printf("结果:b2不是b1的子树\n");
    destroy_btree(b1);
    destroy_btree(b2);
    return 0;
}
posted @ 2019-12-06 16:21  AmosAlbert  阅读(145)  评论(0编辑  收藏  举报