Complete class implementation: Construction, structure analysis, binary tree traversal, and binary tree calendar
According to the previous blog post, we have explained how to perform the following operations based on the two traversal methods.Build a binary tree
Recursive Method is used here.Traverse Binary TreesThe recursion method is relatively simple, and other non-recursive methods will be supplemented later
This mainly improves the usage of the class:
The focus is:Interface Definition
Tree Structure Deletion
And if a pointer exists in the class member variables, and involves copying constructor and value assignment operator functionsSmart pointer
If the interface definition is not good enough, please forgive me
If you do not understand smart pointers, you can move to the http://blog.csdn.net/xietingcandice/article/details/39670269
. H file
# Include <iostream> # include <xtr1common> # include <stack> using namespace std; struct BinaryTreeNode {int Value; BinaryTreeNode * pLeft; BinaryTreeNode * pRight; BinaryTreeNode () {Value = 0; pLeft = NULL; pRight = NULL ;}}; BinaryTreeNode * ReconstructTree (int * startBackorder, int * endBackorder, int * startInorder, int * endInorder ); void DeleteRoot (BinaryTreeNode * pRoot); class ScanBinaryTreeNode; // <the pointer appears in the traversal class, to prevent The copy function and the assignment operator have memory leakage. The class BinaryRoot {friend class ScanBinaryTreeNode corresponding to the smart pointer is defined. // <youyuan structure BinaryTreeNode * mpRoot; size_t mUse; BinaryRoot (BinaryTreeNode * pRoot ): mpRoot (pRoot), mUse (1) {}// <initialized to 1 BinaryRoot (): mpRoot (NULL), mUse (0) {}; // <default constructor ~ BinaryRoot () ;}; class ScanBinaryTreeNode {public: ScanBinaryTreeNode (int * startBackOrder, int * endBackOrder, int * startInOrder, int * endInOrder); ScanBinaryTreeNode (){}; scanBinaryTreeNode (const ScanBinaryTreeNode & CopyTemp); ScanBinaryTreeNode & operator = (const ScanBinaryTreeNode & CopyTemp );~ ScanBinaryTreeNode (); void ScanPreOrder (); void ScanInOrder (); void ScanBackOrder (); private: BinaryRoot * mpRootNode;}; </span>
. C file:
<Span style = "font-size: 14px;"> # include "BinaryTree. h "# define _ CRTDBG_MAP_ALLOC # include <crtdbg. h> # include <stdlib. h> BinaryTreeNode * ReconstructTree (int * startBackorder, int * endBackorder, int * startInorder, int * endInorder) // <build a binary tree {BinaryTreeNode * root = new BinaryTreeNode Based on subsequent and mid-order traversal; root-> Value = * endBackorder; if (startBackorder = endBackorder) // <returns the Value of the corresponding root node if (startInorder = endInorder & (* s TartInorder = * startBackorder) {return root;} else {throw std: exception ("Invalid input") ;}} int * rootInoder = startInorder; while (rootInoder <= endInorder) & (* rootInoder! = Root-> Value) {rootInoder ++;} if (rootInoder> endInorder) {throw std: exception ("Invalid input");} int leftLength = rootInoder-startInorder; if (leftLength> 0) {root-> pLeft = ReconstructTree (startBackorder, startBackorder + leftLength-1, startInorder, rootInoder-1);} if (endBackorder-startBackorder)> leftLength) {root-> pRight = ReconstructTree (startBackorder + leftLength, endBackorder-1, rootInoder + 1, endInord Er);} return root;} void DeleteRoot (BinaryTreeNode * pRoot) // <Delete the entire tree based on the root Node {if (pRoot = NULL) {return ;} binaryTreeNode * pLeft = pRoot-> pLeft; BinaryTreeNode * pRight = pRoot-> pRight; delete pRoot; pRoot = NULL; if (pLeft) {DeleteRoot (pLeft);} if (pRight) {DeleteRoot (pRight);} return;} BinaryRoot ::~ BinaryRoot () {DeleteRoot (mpRoot);} metadata: substring (int * startBackOrder, int * endBackOrder, int * startInOrder, int * endInOrder) {BinaryTreeNode * node = ReconstructTree (startBackOrder, endBackOrder, startInOrder, endInOrder); mpRootNode = new BinaryRoot (node); // <initialize a pointer to the root node} ScanBinaryTreeNode: ScanBinaryTreeNode (const ScanBinaryTreeNode & CopyTemp) {mpRootNode = CopyTemp. mpRootNode; ++ MpRootNode-> mUse;} ScanBinaryTreeNode ::~ ScanBinaryTreeNode () {if (-- mpRootNode-> mUse) = 0) // delete {delete mpRootNode; mpRootNode = NULL when no object exists in the pointer ;}} scanBinaryTreeNode & ScanBinaryTreeNode: operator = (const ScanBinaryTreeNode & CopyTemp) {++ CopyTemp. mpRootNode-> mUse; // if (-- mpRootNode-> mUse = 0) delete mpRootNode; mpRootNode = CopyTemp. mpRootNode; return * this;} void PreOrder (BinaryTreeNode * pRoot) {if (pRoot = NULL) {return;} printf ("% d", pRoot-> Value ); binaryTreeNode * pLeft = pRoot-> pLeft; BinaryTreeNode * pRight = pRoot-> pRight; if (pLeft) {PreOrder (pLeft);} if (pRight) {PreOrder (pRight );} return;} void BackOrder (BinaryTreeNode * pRoot) {if (pRoot = NULL) {return;} BinaryTreeNode * pLeft = pRoot-> pLeft; BinaryTreeNode * pRight = pRoot-> pRight; if (pLeft) {BackOrder (pLeft) ;}if (pRight) {BackOrder (pRight) ;}printf ("% d", pRoot-> Value); return ;} void InOrder (BinaryTreeNode * pRoot) {if (pRoot = NULL) {return;} BinaryTreeNode * pLeft = pRoot-> pLeft; BinaryTreeNode * pRight = pRoot-> pRight; if (pLeft) {InOrder (pLeft);} printf ("% d", pRoot-> Value); if (pRight) {InOrder (pRight);} return ;} void ScanBinaryTreeNode: ScanPreOrder () {PreOrder (mpRootNode-> mpRoot);} void ScanBinaryTreeNode: ScanBackOrder () {BackOrder (mpRootNode-> mpRoot);} void ScanBinaryTreeNode :: scanInOrder () {InOrder (mpRootNode-> mpRoot);} void Order () {int BackArry [8] = }; int MiddleArry [8] = {,}; if (BackArry = NULL | MiddleArry = NULL) {return;} ScanBinaryTreeNode root (BackArry, backArry + 7, MiddleArry, MiddleArry + 7); ScanBinaryTreeNode root1 = root; ScanBinaryTreeNode root2 (root1); ScanBinaryTreeNode root3 (root); root. scanBackOrder (); printf ("\ n"); root. scanInOrder (); printf ("\ n"); root. scanPreOrder () ;}int main () {Order (); // int * I = new int; _ CrtDumpMemoryLeaks (); return 0 ;}</span>