Implementation of binary tree with C + +

Source: Internet
Author: User

Template<class t>

Node structure of struct binarytreenode//two fork tree

{

T _data;

binarytreenode<t>* _left;

binarytreenode<t>* _right;

Binarytreenode (const t& x)

: _data (X._data)

, _left (NULL)

, _right (NULL)

{}

};

Template<class t>

Class BinaryTree

{

Public

BinaryTree ()

: _root (NULL)

{}

BinaryTree (const t* A, size_t size)//Build a tree

{

size_t index = 0;

_root = _createtree (A, size, index);

}

void Prevorder ()

{

_pervorder (_root);

cout << Endl;

}

void Inorder ()

{

_inorder (_root);

cout << Endl;

}

void Postorder ()

{

_postorder (_root);

cout << Endl;

}

void Levelorder ()

{

Queue<binarytreenode<t>*> Q;

if (_root)

{

Q.push (_root);

}

while (!q.empty ())

{

binarytreenode<t>* front = Q.front ();

Q.pop ();

cout << front._data << "";

if (front->_left)

{

Q.push (Front->_left);

}

if (front->_right)

{

Q.push (Front->_right);

}

}

cout << Endl;

}

int Size ()

{

Return _size (_root);

}

int Depth (binarytreenode<t>* root)

{

int ret = _depth (root);

return ret;

}

binarytreenode<t>* Find (binarytreenode<t>* root,t data)

{

if (root = = NULL)

Return

if (data = = Root->_data)

{

return root;

}

binarytreenode<t>* ret = Find (root->_left, data);

if (ret)

return ret;

Return Find (root->_right, data);;

}

Protected

binarytreenode<t>* _createtree (const t* A, size_t size, size_t& index)

{

binarytreenode* root = NULL;

if (Index < size && A[index]! = "#")

{

root = new binarytreenode<t> (A[index]);

Root->_left = _createtree (A, size, ++index);

Root->_right = _createtree (A, size, ++index);

}

return root;

}

void _prevorder (binarytreenode<t>* root)

{

if (root = = NULL)

{

Return

}

cout << root->_data << "";

_prevorder (Root->_left);

_prevorder (Root->_right);

}

void _inorder (binarytreenode<t>* root)

{

if (root = = NULL)

{

Return

}

_inorder (Root->_left);

cout << root->_data << "";

_inorder (Root->_right);

}

void _postorder (binarytreenode<t>* root)

{

if (root = = NULL)

{

Return

}

_postorder (Root->_left);

_postorder (Root->_right);

cout << root->_data << "";

}

int _size (binarytreenode<t>* root)

{

if (root = = NULL)

{

return 0;

}

Return _size (Root->_left) + _size (root->_right) + 1;

}

int _depth (binarytreenode<t>* root)

{

if (root = = NULL)

return 0;

int leftdepth = _depth (root->_left);

int rightdepth = _depth (root->_right);


return leftdepth > Rightdepth? Leftdepth + 1:rightdepth + 1;

}

void _getleafnum (binarytreenode<t>* root, int& num)

{

if (root = = NULL)

Return

if (Root->_left = = NULL && Root->_right = = null)

{

++num;

Return

}

_getleafnum (Root->_left);

_getleafnum (Root->_right);

}

Protected

binarytreenode<t>* _root;

};


This article is from the "end-of-the-guest" blog, please be sure to keep this source http://zheng2048.blog.51cto.com/10612048/1811631

Implementation of binary tree with C + +

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.