C++ 判断是否为2叉树的子树

题目:

输入A.B 两棵树,判断B是否为A的子树:

IsSubTree.cpp:

#include <iostream>
#include <stdio.h>
#include "BinaryTree.h"
using namespace std;

bool IsSubTree(TreeNode* pRoot_s, TreeNode* pRoot_t);
bool HasSubtree(TreeNode* pRoot_s, TreeNode* pRoot_t)
{
	bool result = false;
	if(pRoot_t != NULL && pRoot_s != NULL)
	{
		if(pRoot_s -> m_value == pRoot_t -> m_value)
			result = IsSubTree(pRoot_s,pRoot_t);
		
		if(!result)
			result = HasSubtree(pRoot_s ->pLeft, pRoot_t);
		if(!result)
			result = HasSubtree(pRoot_s ->pRight, pRoot_t);
	}
	return result;
}
bool IsSubTree(TreeNode* pRoot_s, TreeNode* pRoot_t)
{
	if(pRoot_t == NULL)
		return true;
	if(pRoot_s == NULL)
		return false;
			
	if(pRoot_s ->m_value == pRoot_t -> m_value)
		return IsSubTree(pRoot_s -> pLeft , pRoot_t -> pLeft) && IsSubTree(pRoot_s -> pRight, pRoot_t -> pRight);
	else
		return false;
}
// ====================测试代码====================  
void Test(char* testName, TreeNode* pRoot1, TreeNode* pRoot2, bool 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()  
{  
    TreeNode* pNodeA1 = CreateNode(8);  
    TreeNode* pNodeA2 = CreateNode(8);  
    TreeNode* pNodeA3 = CreateNode(7);  
    TreeNode* pNodeA4 = CreateNode(9);  
    TreeNode* pNodeA5 = CreateNode(2);  
    TreeNode* pNodeA6 = CreateNode(4);  
    TreeNode* pNodeA7 = CreateNode(7);  
  
    ConnectNodes(pNodeA1, pNodeA2, pNodeA3);  
    ConnectNodes(pNodeA2, pNodeA4, pNodeA5);  
    ConnectNodes(pNodeA5, pNodeA6, pNodeA7);  
  
    TreeNode* pNodeB1 = CreateNode(8);  
    TreeNode* pNodeB2 = CreateNode(9);  
    TreeNode* pNodeB3 = CreateNode(2);  
  
    ConnectNodes(pNodeB1, pNodeB2, pNodeB3);
	//后序遍历
	cout << "The source tree is: ";
	PrintTree(pNodeA1);
	cout <<endl;
	cout << "The test tree is: ";
	PrintTree(pNodeB1);
	cout <<endl;
  
    Test("Test1", pNodeA1, pNodeB1, true);  
  
    DestroyTree(pNodeA1);  
    DestroyTree(pNodeB1);  
}  
  
// 树中结点含有分叉,树B不是树A的子结构  
//                  8                8  
//              /       \           / \  
//             8         7         9   2  
//           /   \  
//          9     3  
//               / \  
//              4   7  
void Test2()  
{  
    TreeNode* pNodeA1 = CreateNode(8);  
    TreeNode* pNodeA2 = CreateNode(8);  
    TreeNode* pNodeA3 = CreateNode(7);  
    TreeNode* pNodeA4 = CreateNode(9);  
    TreeNode* pNodeA5 = CreateNode(3);  
    TreeNode* pNodeA6 = CreateNode(4);  
    TreeNode* pNodeA7 = CreateNode(7);  
  
    ConnectNodes(pNodeA1, pNodeA2, pNodeA3);  
    ConnectNodes(pNodeA2, pNodeA4, pNodeA5);  
    ConnectNodes(pNodeA5, pNodeA6, pNodeA7);  
  
    TreeNode* pNodeB1 = CreateNode(8);  
    TreeNode* pNodeB2 = CreateNode(9);  
    TreeNode* pNodeB3 = CreateNode(2);  
  
    ConnectNodes(pNodeB1, pNodeB2, pNodeB3); 
	//后序遍历
	cout << "The source tree is: ";
	PrintTree(pNodeA1);
	cout <<endl;
	cout << "The test tree is: ";
	PrintTree(pNodeB1);
	cout <<endl;
  
    Test("Test2", pNodeA1, pNodeB1, false);  
  
    DestroyTree(pNodeA1);  
    DestroyTree(pNodeB1);  
}  
  
// 树中结点只有左子结点,树B是树A的子结构  
//                8                  8  
//              /                   /   
//             8                   9     
//           /                    /  
//          9                    2  
//         /        
//        2          
//       /  
//      5  
void Test3()  
{  
    TreeNode* pNodeA1 = CreateNode(8);  
    TreeNode* pNodeA2 = CreateNode(8);  
    TreeNode* pNodeA3 = CreateNode(9);  
    TreeNode* pNodeA4 = CreateNode(2);  
    TreeNode* pNodeA5 = CreateNode(5);  
  
    ConnectNodes(pNodeA1, pNodeA2, NULL);  
    ConnectNodes(pNodeA2, pNodeA3, NULL);  
    ConnectNodes(pNodeA3, pNodeA4, NULL);  
    ConnectNodes(pNodeA4, pNodeA5, NULL);  
  
    TreeNode* pNodeB1 = CreateNode(8);  
    TreeNode* pNodeB2 = CreateNode(9);  
    TreeNode* pNodeB3 = CreateNode(2);  
  
    ConnectNodes(pNodeB1, pNodeB2, NULL);  
    ConnectNodes(pNodeB2, pNodeB3, NULL); 
	//后序遍历
	cout << "The source tree is: ";
	PrintTree(pNodeA1);
	cout <<endl;
	cout << "The test tree is: ";
	PrintTree(pNodeB1);
	cout <<endl;
  
    Test("Test3", pNodeA1, pNodeB1, true);  
  
    DestroyTree(pNodeA1);  
    DestroyTree(pNodeB1);  
}  
  
// 树中结点只有左子结点,树B不是树A的子结构  
//                8                  8  
//              /                   /   
//             8                   9     
//           /                    /  
//          9                    3  
//         /        
//        2          
//       /  
//      5  
void Test4()  
{  
    TreeNode* pNodeA1 = CreateNode(8);  
    TreeNode* pNodeA2 = CreateNode(8);  
    TreeNode* pNodeA3 = CreateNode(9);  
    TreeNode* pNodeA4 = CreateNode(2);  
    TreeNode* pNodeA5 = CreateNode(5);  
  
    ConnectNodes(pNodeA1, pNodeA2, NULL);  
    ConnectNodes(pNodeA2, pNodeA3, NULL);  
    ConnectNodes(pNodeA3, pNodeA4, NULL);  
    ConnectNodes(pNodeA4, pNodeA5, NULL);  
  
    TreeNode* pNodeB1 = CreateNode(8);  
    TreeNode* pNodeB2 = CreateNode(9);  
    TreeNode* pNodeB3 = CreateNode(3);  
  
    ConnectNodes(pNodeB1, pNodeB2, NULL);  
    ConnectNodes(pNodeB2, pNodeB3, NULL);  
	//后序遍历
	cout << "The source tree is: ";
	PrintTree(pNodeA1);
	cout <<endl;
	cout << "The test tree is: ";
	PrintTree(pNodeB1);
	cout <<endl;
  
    Test("Test4", pNodeA1, pNodeB1, false);  
  
    DestroyTree(pNodeA1);  
    DestroyTree(pNodeB1);  
}  
  
// 树中结点只有右子结点,树B是树A的子结构  
//       8                   8  
//        \                   \   
//         8                   9     
//          \                   \  
//           9                   2  
//            \        
//             2          
//              \  
//               5  
void Test5()  
{  
    TreeNode* pNodeA1 = CreateNode(8);  
    TreeNode* pNodeA2 = CreateNode(8);  
    TreeNode* pNodeA3 = CreateNode(9);  
    TreeNode* pNodeA4 = CreateNode(2);  
    TreeNode* pNodeA5 = CreateNode(5);  
  
    ConnectNodes(pNodeA1, NULL, pNodeA2);  
    ConnectNodes(pNodeA2, NULL, pNodeA3);  
    ConnectNodes(pNodeA3, NULL, pNodeA4);  
    ConnectNodes(pNodeA4, NULL, pNodeA5);  
  
    TreeNode* pNodeB1 = CreateNode(8);  
    TreeNode* pNodeB2 = CreateNode(9);  
    TreeNode* pNodeB3 = CreateNode(2);  
  
    ConnectNodes(pNodeB1, NULL, pNodeB2);  
    ConnectNodes(pNodeB2, NULL, pNodeB3);  
	//后序遍历
	cout << "The source tree is: ";
	PrintTree(pNodeA1);
	cout <<endl;
	cout << "The test tree is: ";
	PrintTree(pNodeB1);
	cout <<endl;
  
    Test("Test5", pNodeA1, pNodeB1, true);  
  
    DestroyTree(pNodeA1);  
    DestroyTree(pNodeB1);  
}  
  
// 树A中结点只有右子结点,树B不是树A的子结构  
//       8                   8  
//        \                   \   
//         8                   9     
//          \                 / \  
//           9               3   2  
//            \        
//             2          
//              \  
//               5  
void Test6()  
{  
    TreeNode* pNodeA1 = CreateNode(8);  
    TreeNode* pNodeA2 = CreateNode(8);  
    TreeNode* pNodeA3 = CreateNode(9);  
    TreeNode* pNodeA4 = CreateNode(2);  
    TreeNode* pNodeA5 = CreateNode(5);  
  
    ConnectNodes(pNodeA1, NULL, pNodeA2);  
    ConnectNodes(pNodeA2, NULL, pNodeA3);  
    ConnectNodes(pNodeA3, NULL, pNodeA4);  
    ConnectNodes(pNodeA4, NULL, pNodeA5);  
  
    TreeNode* pNodeB1 = CreateNode(8);  
    TreeNode* pNodeB2 = CreateNode(9);  
    TreeNode* pNodeB3 = CreateNode(3);  
    TreeNode* pNodeB4 = CreateNode(2);  
  
    ConnectNodes(pNodeB1, NULL, pNodeB2);  
    ConnectNodes(pNodeB2, pNodeB3, pNodeB4);  
	//后序遍历
	cout << "The source tree is: ";
	PrintTree(pNodeA1);
	cout <<endl;
	cout << "The test tree is: ";
	PrintTree(pNodeB1);
	cout <<endl;
  
    Test("Test6", pNodeA1, pNodeB1, false);  
  
    DestroyTree(pNodeA1);  
    DestroyTree(pNodeB1);  
}  
  
// 树A为空树  
void Test7()  
{  
    TreeNode* pNodeB1 = CreateNode(8);  
    TreeNode* pNodeB2 = CreateNode(9);  
    TreeNode* pNodeB3 = CreateNode(3);  
    TreeNode* pNodeB4 = CreateNode(2);  
  
    ConnectNodes(pNodeB1, NULL, pNodeB2);  
    ConnectNodes(pNodeB2, pNodeB3, pNodeB4);  
	//后序遍历
	cout << "The source tree is: ";
	PrintTree(NULL);
	cout <<endl;
	cout << "The test tree is: ";
	PrintTree(pNodeB1);
	cout <<endl;
  
    Test("Test7", NULL, pNodeB1, false);  
  
    DestroyTree(pNodeB1);  
}  
  
// 树B为空树  
void Test8()  
{  
    TreeNode* pNodeA1 = CreateNode(8);  
    TreeNode* pNodeA2 = CreateNode(9);  
    TreeNode* pNodeA3 = CreateNode(3);  
    TreeNode* pNodeA4 = CreateNode(2);  
  
    ConnectNodes(pNodeA1, NULL, pNodeA2);  
    ConnectNodes(pNodeA2, pNodeA3, pNodeA4);  
  //后序遍历
	cout << "The source tree is: ";
	PrintTree(pNodeA1);
	cout <<endl;
	cout << "The test tree is: ";
	PrintTree(NULL);
	cout <<endl;
    Test("Test8", pNodeA1, NULL, false);  
  
    DestroyTree(pNodeA1);  
}  
  
// 树A和树B都为空  
void Test9()  
{  
	//后序遍历
	cout << "The source tree is: ";
	PrintTree(NULL);
	cout <<endl;
	cout << "The test tree is: ";
	PrintTree(NULL);
	cout <<endl;
    Test("Test9", NULL, NULL, false);  
}  
  
int main()  
{  
    Test1();  
    Test2();  
    Test3();  
    Test4();  
    Test5();  
    Test6();  
    Test7();  
    Test8();  
    Test9();  
	system("PAUSE");
    return 0;  
}  

BinaryTree.h:

#include <iostream>
#include <stdio.h>
using namespace std;
struct TreeNode
{
	TreeNode* pLeft;
	TreeNode* pRight;
	int m_value;
};
TreeNode* CreateNode(int value);  
void ConnectNodes(TreeNode* pParent, TreeNode* pLeft, TreeNode* pRight);  
void PrintNode(TreeNode* pNode);  
void PrintTree(TreeNode* pRoot);  
void DestroyTree(TreeNode* pRoot);  

BinaryTree.cpp:

#include <iostream>
#include <stdio.h>
#include "BinaryTree.h"
using namespace std;
TreeNode* CreateNode(int value)
{
	TreeNode* pNode = new TreeNode;
	pNode -> pLeft = NULL;
	pNode -> pRight = NULL;
	pNode -> m_value = value;
	return pNode;
}
void ConnectNodes(TreeNode* parent,TreeNode* pleft,TreeNode* pright)
{
	if(parent != NULL)
	{
		parent -> pLeft = pleft;
		parent -> pRight = pright;
	}else
		return ;		
}
void PrintNode(TreeNode* pNode)
{
	if(pNode != NULL)
	{
		cout << pNode->m_value;
		if(pNode -> pLeft != NULL)
			cout<<"The left child of this node is:"<<pNode -> pLeft ->m_value;
		else
			cout<< "The left child of this node is null";
		if(pNode ->pRight != NULL)
			cout<<"The right child of this node is "<<pNode ->pRight ->m_value;
		else
			cout <<"The right child of this node is null";

	}
	else
		cout <<"The node is null"<<endl;
}
void PrintTree(TreeNode* pRoot)
{
	if(pRoot != NULL)
	{
		if(pRoot ->pLeft != NULL)
			PrintTree(pRoot -> pLeft);
		if(pRoot -> pRight != NULL)
			PrintTree(pRoot->pRight);
		cout << pRoot->m_value;
	}else
		return;
}
void DestroyTree(TreeNode* pRoot)
{
	if(pRoot -> pLeft != NULL)
		DestroyTree(pRoot ->pLeft);
	if(pRoot -> pRight != NULL)
		DestroyTree(pRoot -> pRight);
	if(pRoot)
	{
		delete pRoot;
		pRoot = NULL;
	}
	else
		return;
}

运算结果:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值