Two-fork Tree:
A binary tree is a special tree with a maximum of two child nodes per node, called left child and right child, respectively.
Instance:
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/7F/3C/wKioL1cXTQOhO33gAAAYGdz7lKk183.png "title=" Image [ 8].png "alt=" Wkiol1cxtqoho33gaaaygdz7lkk183.png "/>
Node structure:
Template <class t>struct Binarytreenode{binarytreenode (T data): _data (data), _left (null), _right (null) {}t _data; binarytreenode<t>* _left; Binarytreenode<t>* _right;};
binary tree member variables;
protected:binarytreenode<t>* _root;
Constructor: Call _createtree
node* _createtree (const t* a,size_t size,size_t& index,const t& Invalid)//Note Index with reference {node* root=null;if (index <size) && (a[index]!=invalid) {root=new Node (A[index]); Root->_left=_createtree (A,size,++index, Invalid); Root->_right=_createtree (a,size,++index,invalid);} return root;}
destructor: Call _destory
void _destory (node* root) {node* cur=root; node* del=cur;if (cur->_left!=null) {_destory (cur->_left);} if (cur->_right!=null) {_destory (cur->_right);} Delete del;}
Copy constructor: Call _copy
Node* _copy (node* root) {if (root==null) return NULL; node* newroot=new Node (root->_data); node* cur=newroot;cur->_left=_copy (Root->_left); cur->_right=_copy (root->_right); return newroot;}
Overloaded assignment operators
binarytree& operator= (BinaryTree tree) {swap (_root,tree._root); return *this;}
Pre-order traversal: calling _prevorder
void _prevorder (node* root) {if (root==null) return;cout<<root->_data<< ""; _prevorder (Root->_left) ; _prevorder (root->_right);}
Middle sequence traversal: calling _inorder
void _inorder (node* root) {if (root==null) Return;_inorder (root->_left);cout<<root->_data<< ""; _ Inorder (root->_right);}
Post-post traversal: Call _poseorder
void _postorder (node* root) {if (root==null) Return;_postorder (root->_left); _postorder (root->_right);cout< <root->_data<< "";}
Seek depth: Call _depth
size_t _depth (node* root) {node* cur=root;size_t depth=0;if (cur==null) return 0;else{depth=1;size_t leftdepth=_Depth ( Cur->_left); size_t rightdepth=_depth (Cur->_right); if (leftdepth>=rightdepth) {return depth+leftdepth;} Else{return depth+rightdepth;}}}
Request Size: Call _size
size_t _size (node* root) {size_t count=0;if (root==null) return 0;else{++count;} Count+=_size (Root->_left); count+=_size (root->_right); return count;}
Find the number of leaf nodes: Call _leafsize
size_t _leafsize (node* root) {size_t size=0;if (root==null) return 0;else{if ((root->_left==null) && (root- >_right==null)) {Size=1;return size;} else{size_t left=_leafsize (root->_left); size_t right=_leafsize (Root->_right); return left+right;}}}
Two-fork Tree