输入两颗二叉树A和B,判断B是不是A的子结构

算法描述:

输入两颗二叉树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)







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yiluohan0307

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值