1.给定两棵二叉树A和B, 判断B是不是A的子结构。
二叉树的结构如下:
struct BinaryTreeNode
{
int m_nValue;
BinaryTreeNode* m_pLeft;
BinaryTreeNode* m_pRight;
};
分析:
如图示的两个二叉树,首先是判断B树的根节点在A树种的位置,找到之后才能进行下一步的判断,再判断左右子树是否相等。
源码:
/* 二叉树子结构确定*/
#include<iostream>
#include <cstdlib>
using namespace std;
#define MAXLEN 10
#define NLAYER 4
#define MAX(a,b) (((a)>(b))?(a):(b))
#define INF 127
int PrintTree_h_height;
char PrintTree_h_buffer[6][128];
int PrintTree_h_x;
struct BinaryTreeNode
{
int m_nValue;
BinaryTreeNode* m_pLeft;
BinaryTreeNode* m_pRight;
};
//创建二叉树结点
BinaryTreeNode* CreateBinaryTreeNode(int value)
{
BinaryTreeNode* pNode = new BinaryTreeNode();
pNode->m_nValue = value;
pNode->m_pLeft = NULL;
pNode->m_pRight = NULL;
return pNode;
}
//连接三个结点
void ConnectTreeNodes(BinaryTreeNode* pParent, BinaryTreeNode* pLeft, BinaryTreeNode* pRight)
{
if (pParent != NULL)
{
pParent->m_pLeft = pLeft;
pParent->m_pRight = pRight;
}
}
//打印树节点
void PrintTreeNode(BinaryTreeNode* pNode)
{
if (pNode != NULL)
{
printf("value of this node is: %d\n", pNode->m_nValue);
if (pNode->m_pLeft != NULL)
printf("value of its left child is: %d.\n", pNode->m_pLeft->m_nValue);
else
printf("left child is null.\n");
if (pNode->m_pRight != NULL)
printf("value of its right child is: %d.\n", pNode->m_pRight->m_nValue);
else
printf("right child is null.\n");
}
else
{
printf("this node is null.\n");
}
printf("\n");
}
//打印二叉树
void PrintTree(BinaryTreeNode* pRoot)
{
PrintTreeNode(pRoot);
if (pRoot != NULL)
{
if (pRoot->m_pLeft != NULL)
PrintTree(pRoot->m_pLeft);
if (pRoot->m_pRight != NULL)
PrintTree(pRoot->m_pRight);
}
}
//销毁二叉树
void DestroyTree(BinaryTreeNode* pRoot)
{
if (pRoot != NULL)
{
BinaryTreeNode* pLeft = pRoot->m_pLeft;
BinaryTreeNode* pRight = pRoot->m_pRight;
delete pRoot;
pRoot = NULL;
DestroyTree(pLeft);
DestroyTree(pRight);
}
}
//确定剩下的结点是否一致
bool DoesTree1HaveTree2(BinaryTreeNode* pRoot1, BinaryTreeNode* pRoot2)
{
if (pRoot2 == NULL)
return true;
if (pRoot1 == NULL)
return false;
if (pRoot1->m_nValue != pRoot2->m_nValue)//
return false;
return DoesTree1HaveTree2(pRoot1->m_pLeft, pRoot2->m_pLeft) &&
DoesTree1HaveTree2(pRoot1->m_pRight, pRoot2->m_pRight);
}
//判断是否有相同的结点
bool HasSubtree(BinaryTreeNode* pRoot1, BinaryTreeNode* pRoot2)
{
bool result = false;
if (pRoot1 != NULL && pRoot2 != NULL)
{
if (pRoot1->m_nValue == pRoot2->m_nValue)
result = DoesTree1HaveTree2(pRoot1, pRoot2);
if (!result)
result = HasSubtree(pRoot1->m_pLeft, pRoot2);
if (!result)
result = HasSubtree(pRoot1->m_pRight, pRoot2);
}
return result;
}
//求取高度
int PrintTree_h_treeHeight(BinaryTreeNode* tree){
if (tree == NULL) return 0;
int heightLeft = PrintTree_h_treeHeight(tree->m_pLeft);
int heightRight = PrintTree_h_treeHeight(tree->m_pRight);
return MAX(heightLeft, heightRight) + 1;
}
void PrintTree_h_corePrintTree(BinaryTreeNode* tree, int level){
if (tree == NULL){
PrintTree_h_x += (pow(2, PrintTree_h_height - level) - 1);
return;
}
char(*a)[128] = PrintTree_h_buffer;
PrintTree_h_corePrintTree(tree->m_pLeft, level + 1);
a[level][PrintTree_h_x++] = tree->m_nValue;
PrintTree_h_corePrintTree(tree->m_pRight, level + 1);
}
//树形打印二叉树
void ShowTree(BinaryTreeNode* tree)
{
if (tree == NULL) return;
char(*a)[128] = PrintTree_h_buffer;
for (int i = 0; i<6; i++){
for (int j = 0; j<128; j++){
a[i][j] = INF;
}
}
//先获取树高度
PrintTree_h_height = PrintTree_h_treeHeight(tree);
if (PrintTree_h_height > 6){
cout << "树超过6层,无法打印" << endl;
return;
}
PrintTree_h_corePrintTree(tree, 0);
for (int i = 0; i < PrintTree_h_height; i++){//按实际高度打印
for (int j = 0; j < 128; j++){
if (a[i][j] == INF) cout << " ";
else cout << (int)a[i][j];
}
cout << endl;
}
}
int main()
{
BinaryTreeNode* pNodeA1 = CreateBinaryTreeNode(8);
BinaryTreeNode* pNodeA2 = CreateBinaryTreeNode(8);
BinaryTreeNode* pNodeA3 = CreateBinaryTreeNode(7);
BinaryTreeNode* pNodeA4 = CreateBinaryTreeNode(9);
BinaryTreeNode* pNodeA5 = CreateBinaryTreeNode(2);
BinaryTreeNode* pNodeA6 = CreateBinaryTreeNode(4);
BinaryTreeNode* pNodeA7 = CreateBinaryTreeNode(7);
ConnectTreeNodes(pNodeA1, pNodeA2, pNodeA3);
ConnectTreeNodes(pNodeA2, pNodeA4, pNodeA5);
ConnectTreeNodes(pNodeA5, pNodeA6, pNodeA7);
BinaryTreeNode* pNodeB1 = CreateBinaryTreeNode(8);
BinaryTreeNode* pNodeB2 = CreateBinaryTreeNode(9);
BinaryTreeNode* pNodeB3 = CreateBinaryTreeNode(2);
ConnectTreeNodes(pNodeB1, pNodeB2, pNodeB3);
ShowTree(pNodeA1);
ShowTree(pNodeB1);
bool result = HasSubtree(pNodeA1, pNodeB1);
if (result == true)
{
printf("find it \n");
}
else
printf("none \n");
DestroyTree(pNodeA1);
DestroyTree(pNodeB1);
system("PAUSE");
return 0;
}