BinaryTree.h
#pragma oncetemplate <class t>struct binarytreenode{ binarytreenode<t>* _right; binarytreenode<t>* _left; t _data; binarytreenode (Const T &&NBSP;D) :_right (null) ,_left (NULL) ,_data (d) {}};template <class T>class BinaryTree{ typedef BinaryTreeNode<T> Node;public: BinaryTree () :_root (NULL) {} binarytree (Const t* a, size_t size, const t& invalid) { size_t index = 0; _root = _creattree (A, size, index, invalid); } binarytree (Const BinaryTree <t>& t) { _root = _copytree (t._root); } ~binarytree () { _destory (_root); _root = null; } size_t size () //to find the number of two-fork tree nodes { return _size (_root); } size_t depth () //finding binary tree depth { return _depth (_root); } void prevorder () //pre-sequence traversal { _prevorder (_root); cout<<endl; }protected: node* _creattree (const t* a, size_t size, size_t& index, const t& invalid) { node* root = null; if (Index<size && a[index] !=invalid) { root = new node (A[index]); root->_ Left = _creattree (a, size, ++index, invalid); root->_right = _creattree (a, size, ++index, invalid); } return root; } node* _copytree (Const node* root) { if (root == null) { Return null; } node* newroot = new node (Root->_data); newroot->_left = _copytree (root->_left); newroot->_right = _ Copytree (root->_right); return newroot; } void _destory (Node* root) { if (root == null) { return; } _ Destory (Root->_left); _destory (root->_right); delete root; } size_t _size (Node* root) { if (root == null) { return 0; } return _size (Root->_left) +_Size (root->_ right) +1; } size_t _depth (node* root) { if (root == null) { return 0; } size_t leftdepth = _depth (root- >_left); siZe_t rightdepth = _depth (root->_right); return leftdepth > Rightdepth ? leftdepth+1 : rightdepth+1; } void _prevorder (Node* root) { if (root == null) { return; } cout<<root->_data<< ","; _prevorder (Root->_left); _prevorder (root->_ right); }private: node* _root;
Realization of Binary Tree