Realization of Binary Tree

Source: Internet
Author: User

BinaryTree.h

#pragma  oncetemplate <class t>struct binarytreenode{ binarytreenode<t>*  _right; binarytreenode<t>* _left; t _data; binarytreenode (Const T &AMP;&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

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.