The previous blog has basically introduced the basic algorithm of the binary tree. This article mainly introduces the search of nodes in the binary tree and the number of subtrees in the Kth row.
First, the binary tree search< /p>
Need to use recursive algorithm
Code to implement internal functions
Node* _Find(Node* root,const T& x) {if (root==NULL) return NULL; if(root->_date==x) return root; Node * left=_Find(root->left,x); Node* right=_Find(root->right,x); if(left) {return _Find(root->left,x);} if(right) {return _Find(root->right,x);} }
External call function
Node* Find(const T& x) {return _Find(_root,x); }
The following describes the number of K-th Zeng subtrees
Recursive implementation, as follows
int _GetKLevel(Node* root,size_t k ) { if(root==NULL) return 0;if(k==1) return 1;else return _GetKLevel(root->left,k-1)+_GetKLevel(root->right,k-1); }External call function
size_t GetKLevel(size_t k) {return _GetKLevel (_root,k); }
How to call
void test(){ int a1[10]={1,2,3,'#','#',4,'#','#',5,6}; BinTreet1( a1,10,'#'); BinTreeNode *searchNode = t1.Find(3); if(NULL == searchNode){ cout<<"The node was not found"< _date<< endl; }cout<