1 #include"iostream"
  2 #include"stdio.h"
  3 #include"math.h"
  4 using namespace std;
  5 
  6 struct BinaryTreeNode
  7 {
  8     double m_Value;
  9     BinaryTreeNode* m_pLeft;
 10     BinaryTreeNode* m_pRight;
 11 };
 12 
 13 BinaryTreeNode* CreateBinaryTreeNode(double value)
 14 {
 15     BinaryTreeNode* pNode=new BinaryTreeNode();
 16     pNode->m_Value=value;
 17     pNode->m_pLeft=nullptr;
 18     pNode->m_pRight=nullptr;
 19 
 20     return pNode;
 21 }
 22 
 23 void ConnectTreeNodes(BinaryTreeNode* pParent,BinaryTreeNode* pLeft,BinaryTreeNode* pRight)
 24 {
 25     if(pParent!=nullptr)
 26     {
 27         pParent->m_pLeft=pLeft;
 28         pParent->m_pRight=pRight;
 29     }
 30 }
 31 
 32 void PrintTreeNode(const BinaryTreeNode* pNode)
 33 {
 34     if(pNode!=nullptr)
 35     {
 36         cout<<"value of this node is:"<<pNode->m_Value<<endl;
 37 
 38         if(pNode->m_pLeft!=nullptr)
 39             cout<<"value of its left child is:"<<pNode->m_pLeft->m_Value<<endl;
 40         else
 41             cout<<"left child is nullptr."<<endl;
 42         if(pNode->m_pRight!=nullptr)
 43             cout<<"value of its right child is:"<<pNode->m_pRight->m_Value<<endl;
 44         else
 45             cout<<"right child is nullptr."<<endl;
 46     }
 47     else
 48         cout<<"this node is nullptr."<<endl;
 49     cout<<endl;
 50 }
 51 
 52 void PrintTree(const BinaryTreeNode* pRoot)
 53 {
 54     PrintTreeNode(pRoot);
 55 
 56     if(pRoot!=nullptr)
 57     {
 58         if(pRoot->m_pLeft!=nullptr)
 59             PrintTreeNode(pRoot->m_pLeft);
 60 
 61         if(pRoot->m_pRight!=nullptr)
 62             PrintTreeNode(pRoot->m_pRight);
 63     }
 64 }
 65 
 66 void DestroyTree(BinaryTreeNode* pRoot)
 67 {
 68     if(pRoot!=nullptr)
 69     {
 70         BinaryTreeNode* pLeft=pRoot->m_pLeft;
 71         BinaryTreeNode* pRight=pRoot->m_pRight;
 72 
 73         delete pRoot;
 74         pRoot=nullptr;
 75 
 76         DestroyTree(pLeft);
 77         DestroyTree(pRight);
 78     }
 79 }
 80 
 81 bool Equal(const double &a,const double &b)
 82 {
 83     if(fabs(a-b)<0.0000001)
 84         return true;
 85     return false;
 86 }
 87 
 88 bool DoesTreeAHaveTreeB(BinaryTreeNode* pRootA,BinaryTreeNode* pRootB)
 89 {
 90     if(pRootB==nullptr)
 91         return true;
 92     if(pRootA==nullptr)
 93         return false;
 94 
 95     if(Equal(pRootA->m_Value,pRootB->m_Value))
 96     {
 97        return DoesTreeAHaveTreeB(pRootA->m_pLeft,pRootB->m_pLeft)&&DoesTreeAHaveTreeB(pRootA->m_pRight,pRootB->m_pRight);
 98     }
 99     else
100     {
101         return false;
102     }
103 }
104 bool HasSubTree(BinaryTreeNode* pRootA,BinaryTreeNode* pRootB)
105 {
106     if(pRootB==nullptr)
107         return false;
108     if(pRootA==nullptr)
109         return false;
110 
111     bool result=false;
112 
113     if(Equal(pRootA->m_Value,pRootB->m_Value))
114     {
115         result=DoesTreeAHaveTreeB(pRootA,pRootB);
116     }
117     if(!result)
118     {
119         result=HasSubTree(pRootA->m_pLeft,pRootB);
120     }
121     if(!result)
122     {
123         result=HasSubTree(pRootA->m_pRight,pRootB);
124     }
125 
126     return result;
127 }
函数

相关文章:

  • 2022-12-23
  • 2021-10-20
  • 2021-06-28
  • 2021-11-27
  • 2022-12-23
  • 2021-08-04
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-05-31
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案