Sub-structure of the tree

Source: Internet
Author: User

# Include
 
  
Struct {int m_nValue; BinaryTreeNode * m_pLeft; BinaryTreeNode * m_pRight;}; BinaryTreeNode * struct (int value) {struct * pNewNode = new BinaryTreeNode (); pNewNode-> m_nValue = value; pNewNode-> m_pLeft = NULL; pNewNode-> m_pRight = NULL; return pNewNode;} void terminate (BinaryTreeNode * pParent, BinaryTreeNode * pLeftChild, BinaryTreeNode * pRightChild) {if (! PParent) return; pParent-> m_pLeft = pLeftChild; pParent-> m_pRight = pRightChild;}/* bool hasTheSubTeeWithRoot (BinaryTreeNode * pBigTree, BinaryTreeNode * pSmallTree ); // determine whether the pSmallTree is a sub-tree bool isSubtreeInBinaryTree (BinaryTreeNode * pBigTree, BinaryTreeNode * pSmallTree) {if (pSmallTree = NULL | pBigTree = NULL) return false; bool result = false; bool result1 = false; bool result2 = false; result = hasThe SubTeeWithRoot (pBigTree, pSmallTree); if (pBigTree-> m_pLeft) result1 = isSubtreeInBinaryTree (pBigTree-> m_pLeft, pSmallTree); if (pBigTree-> m_pRight) result2 = isSubtreeInBinaryTree (pBigTree-> m_pRight, pSmallTree); return (result | result1 | result2);} // The two trees must contain the root node, determine whether it is a subtree bool hasTheSubTeeWithRoot (BinaryTreeNode * pBigTree, BinaryTreeNode * pSmallTree) {if (pSmallTree = NULL) return true; if (pBigTree = NULL) re Turn false; bool result = false; bool result1 = false; bool result2 = false; if (pBigTree-> m_nValue = pSmallTree-> m_nValue) {result = true; result1 = hasTheSubTeeWithRoot (pBigTree-> m_pLeft, pSmallTree-> m_pLeft); result2 = hasTheSubTeeWithRoot (pBigTree-> m_pRight, pSmallTree-> m_pRight );} return result & result1 & result2;} * // The Code Annotated above can implement basic functions, but the efficiency is poor, the reason is that even if the subtree is identified at the beginning, the method will continue to traverse each node under the larger tree. // The following method can avoid this problem: bool destroy (BinaryTreeNode * pBigTree, BinaryTreeNode * pSmallTree); bool destroy (BinaryTreeNode * pBigTree, BinaryTreeNode * pSmallTree) {bool result = false; if (pBigTree! = NULL & pSmallTree! = NULL) {if (pBigTree-> m_nValue = pSmallTree-> m_nValue) result = hasTheSubTeeWithRoot (pBigTree, pSmallTree); if (! Result) // if the subtree is found, stop searching. result = isSubtreeInBinaryTree (pBigTree-> m_pLeft, pSmallTree); if (! Result) result = isSubtreeInBinaryTree (pBigTree-> m_pRight, pSmallTree);} return result;} bool aggregate (BinaryTreeNode * pBigTree, BinaryTreeNode * pSmallTree) {if (pSmallTree = NULL) return true; if (pBigTree = NULL) return false; if (pBigTree-> m_nValue! = PSmallTree-> m_nValue) return false; // use the return statement to recursively return hasTheSubTeeWithRoot (pBigTree-> m_pLeft, pSmallTree-> m_pLeft) & terminate (pBigTree-> m_pRight, pSmallTree-> m_pRight);} // unit test void test1 () {BinaryTreeNode * pNode1 = createBinaryTreeNode (8); BinaryTreeNode * pNode2 = createBinaryTreeNode (8 ); binaryTreeNode * pNode3 = round (7); BinaryTreeNode * pNode4 = round (9); BinaryTreeNode * pNode5 = round (2); BinaryTreeNode * pNode6 = createBinaryTreeNode (4 ); binaryTreeNode * pNode7 = bytes (7); connectBinaryTreeNode (pNode1, pNode2, pNode3); bytes (pNode2, pNode4, pNode5); connectBinaryTreeNode (pNode5, pNode6, pNode7 ); binaryTreeNode * pNode21 = bytes (8); BinaryTreeNode * pNode22 = bytes (9); BinaryTreeNode * pNode23 = bytes (2); connectBinaryTreeNode (pNode21, pNode22, pNode23 ); if (isSubtreeInBinaryTree (pNode1, pNode21) printf ("the smalltree is a subtree in bigtree"); elseprintf ("not subtree");} void test2 () {BinaryTreeNode * pNode1 = second (8); BinaryTreeNode * pNode2 = second (8); BinaryTreeNode * pNode3 = second (7); BinaryTreeNode * pNode4 = createBinaryTreeNode (9 ); binaryTreeNode * pNode5 = partition (2); BinaryTreeNode * pNode6 = partition (4); BinaryTreeNode * pNode7 = partition (7); partition (pNode1, pNode2, pNode3); partition (pNode2, pNode4, pNode5); connectBinaryTreeNode (pNode5, pNode6, pNode7); BinaryTreeNode * pNode21 = bytes (8); BinaryTreeNode * pNode22 = createBinaryTreeNode (9 ); binaryTreeNode * pNode23 = partition (2); BinaryTreeNode * pNode24 = partition (1); BinaryTreeNode * pNode25 = partition (2); partition (pNode21, pNode22, pNode23); partition (pNode22, pNode24, pNode25); if (isSubtreeInBinaryTree (pNode1, pNode21) printf ("the smalltree is a subtree in bigtree"); elseprintf ("not subtree");} int main () {test1 (); printf ("\ n"); test2 (); return 0 ;}
 

Binary Tree-related code has a lot of pointer operations. Every time we use pointers, we have to ask ourselves this.

Is it possible that the pointer is NULL? What should I do if it is NULL.


= Reference offoffoffoffoffer


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.