算法描述:
输入两颗二叉树A和B,判断B是不是A的子结构
算法实现:
/*************************************************************************
> File Name: main.c
> Author: cyf
> Mail: XXX@qq.com
> Created Time: 2016年05月18日 星期三 13时30分35秒
************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "hasSubTree.h"
void Test(char *testName, struct BinaryTreeNode* pRoot1, struct BinaryTreeNode* pRoot2, int expected)
{
if(hasSubTree(pRoot1, pRoot2) == expected)
printf("%s passed.\n", testName);
else
printf("%s failed.\n", testName);
}
// 树中结点含有分叉,树B是树A的子结构
// 8 8
// / \ / \
// 8 7 9 2
// / \
// 9 2
// / \
// 4 7
void Test1()
{
struct BinaryTreeNode* pNodeA1 = CreateBinaryTreeNode(8);
struct BinaryTreeNode* pNodeA2 = CreateBinaryTreeNode(8);
struct BinaryTreeNode* pNodeA3 = CreateBinaryTreeNode(7);
struct BinaryTreeNode* pNodeA4 = CreateBinaryTreeNode(9);
struct BinaryTreeNode* pNodeA5 = CreateBinaryTreeNode(2);
struct BinaryTreeNode* pNodeA6 = CreateBinaryTreeNode(4);
struct BinaryTreeNode* pNodeA7 = CreateBinaryTreeNode(7);
ConnectTreeNodes(pNodeA1, pNodeA2, pNodeA3);
ConnectTreeNodes(pNodeA2, pNodeA4, pNodeA5);
ConnectTreeNodes(pNodeA5, pNodeA6, pNodeA7);
struct BinaryTreeNode* pNodeB1 = CreateBinaryTreeNode(8);
struct BinaryTreeNode* pNodeB2 = CreateBinaryTreeNode(9);
struct BinaryTreeNode* pNodeB3 = CreateBinaryTreeNode(2);
ConnectTreeNodes(pNodeB1, pNodeB2, pNodeB3);
Test("test1", pNodeA1, pNodeB1, 1);
DestroyTree(pNodeA1);
DestroyTree(pNodeB1);
}
int main()
{
Test1();
return 0;
}
/*************************************************************************
> File Name: hasSubTree.h
> Author: cyf
> Mail: XXX@qq.com
> Created Time: 2016年05月18日 星期三 13时18分39秒
************************************************************************/
#ifndef _HASSUBTREE_H
#define _HASSUBTREE_H
#include <stdio.h>
#include <stdlib.h>
#include "BinaryTree.h"
/*
* 输入两颗二叉树A和B,判断B是不是A的子结构
* */
int hasSubTree(struct BinaryTreeNode *pRoot1, struct BinaryTreeNode *pRoot2);
#endif
/*************************************************************************
> File Name: hasSubTree.c
> Author: cyf
> Mail: XXX@qq.com
> Created Time: 2016年05月18日 星期三 13时21分34秒
************************************************************************/
#include "hasSubTree.h"
int doHasTree(struct BinaryTreeNode *pRoot1, struct BinaryTreeNode *pRoot2)
{
int ret = 0;
if (pRoot2==NULL)
{
ret = 1;
return ret;
}
if (pRoot1 == NULL)
{
return ret;
}
if (pRoot1->value != pRoot2->value)
{
return ret;
}
return doHasTree(pRoot1->left, pRoot2->left)&&doHasTree(pRoot1->right, pRoot2->right);
}
int hasSubTree(struct BinaryTreeNode *pRoot1, struct BinaryTreeNode * pRoot2)
{
int ret = 0;
if (pRoot1 != NULL && pRoot2 != NULL)
{
if (pRoot1->value == pRoot2->value)
ret = doHasTree(pRoot1, pRoot2);
if (!ret)
{
ret = hasSubTree(pRoot1->left, pRoot2);
}
if (!ret)
{
ret = hasSubTree(pRoot1->right, pRoot2);
}
}
return ret;
}
/*************************************************************************
> File Name: Tree.h
> Author: cyf
> Mail: XXX@qq.com
> Created Time: 2016年04月14日 星期四 20时34分23秒
************************************************************************/
#ifndef _TREE_H
#define _TREE_H
#include <stdio.h>
#include <stdlib.h>
struct BinaryTreeNode
{
int value;
struct BinaryTreeNode *left;
struct BinaryTreeNode *right;
};
struct BinaryTreeNode *CreateBinaryTreeNode(int value);
void ConnectTreeNodes(struct BinaryTreeNode *pRoot, struct BinaryTreeNode *pLeft, struct BinaryTreeNode *pRight);
void PrintTreeNode(struct BinaryTreeNode *pNode);
void PrintTree(struct BinaryTreeNode *pRoot);
void DestroyTree(struct BinaryTreeNode *pRoot);
#endif
/*************************************************************************
> File Name: Tree.c
> Author: cyf
> Mail: XXX@qq.com
> Created Time: 2016年04月14日 星期四 20时38分43秒
************************************************************************/
#include "BinaryTree.h"
struct BinaryTreeNode *CreateBinaryTreeNode(int value)
{
struct BinaryTreeNode *pNode = (struct BinaryTreeNode *)malloc(sizeof(struct BinaryTreeNode));
pNode->value = value;
pNode->left = NULL;
pNode->right = NULL;
return pNode;
}
void ConnectTreeNodes(struct BinaryTreeNode *pRoot, struct BinaryTreeNode *pLeft, struct BinaryTreeNode *pRight)
{
if (pRoot != NULL)
{
pRoot->left = pLeft;
pRoot->right = pRight;
}
}
void PrintTreeNode(struct BinaryTreeNode *pNode)
{
if (pNode != NULL)
{
printf("the value of this node is :%d\n", pNode->value);
if (pNode->left != NULL)
{
printf("the value of left child is %d\n",pNode->left->value);
}
else
{
printf("the left child is NULL\n");
}
if (pNode->right != NULL)
{
printf("the value of right child is %d\n",pNode->right->value);
}
else
{
printf("the right child is NULL\n");
}
}
printf("\n");
}
void PrintTree(struct BinaryTreeNode *pRoot)
{
PrintTreeNode(pRoot);
if (pRoot != NULL)
{
if (pRoot->left != NULL)
PrintTree(pRoot->left);
if (pRoot->right != NULL)
PrintTree(pRoot->right);
}
}
void DestroyTree(struct BinaryTreeNode *pRoot)
{
if (pRoot != NULL)
{
struct BinaryTreeNode *pLeft = pRoot->left;
struct BinaryTreeNode *pRight = pRoot->right;
free(pRoot);
//pRoot == NULL;
DestroyTree(pLeft);
DestroyTree(pRight);
}
}
.SUFFIXEX:.c.o
CC = gcc
CFLAGS = -g -O2 -Wall
SRCS = main.c\
BinaryTree.c\
hasSubTree.c
OBJS = $(SRCS:.c=.o)
EXE = main
all:$(OBJS)
$(CC) $(OBJS) -o $(EXE) $(CFLAGS)
.c.o:
$(CC) -o $@ -c $(CFLAGS) $<
clean:
rm -rf $(OBJS) $(EXE)