Two-fork Tree

Source: Internet
Author: User

two fork tree: Two The fork tree is a special tree, with a maximum of two child nodes per node, called left child and right child, respectively.

full two fork tree: a two-fork tree with a height of N is a two-tree with 2^n-1 nodes.

Complete binary tree: if the depth of the two-fork tree is H, except for the H layer, the nodes of each layer (1~H-1) reach the maximum number, and all nodes of the H layer are continuously concentrated on the leftmost side, which is the complete binary tree

4 ways to traverse a binary tree

Pre-sequence traversal (first root traversal): (1): First access to the root node, (2): Pre-order access to the left subtree, (3): Pre-order access to the right subtree;

The Middle Sequence Traversal: (1): The middle sequence accesses the left subtree, (2): Accesses the root node, (3): The middle sequence accesses the right subtree;

Post-sequential traversal (after the root traversal): (1): After the subsequent access to the left subtree, (2): The subsequent access to the right subtree; (3): Access to the root node;

Sequence traversal: (1): A layer of nodes is traversed sequentially.



#pragma  once#include<iostream> #include <queue>using namespace std;template<class  T>struct BinaryTreeNode{T _data; binarytreenode* _left; binarytreenode* _right; Binarytreenode (const t& x): _data (x),  _left (null),  _right (null) {}};template<class  t>class binarytree{protected:binarytreenode<t>* _root;protected:// The first sequence traversal produces a two-fork tree Binarytreenode<t>* _createbinarytree (t* arr, size_t &index, size_t  size) {binarytreenode<t>* root = null;if  (index < size&& arr[index] !=  ' # ') {root = new binarytreenode<t> (Arr[index]); root->_left  = _createbinarytree (arr, ++index, size); Root->_right = _createbinarytree (arr ,  ++index, size);} Return root;} Void _preorder (Binarytreenode<t>* root) {if  (root != null) {cout << root->_data <<  " "; _preorder (Root->_left); _preorder (root->_right);} return;} Void _inorder (Binarytreenode<t>* root) {if  (root != null) {_InOrder (root->_ left);cout << root->_data<< " "; _inorder (root->_right);} return;} Void _postorder (Binarytreenode<t>* root) {if  (root != null) {_PostOrder (root-> _left) _postorder (root->_right);cout << root->_data <<  " ";} return;} Void _clear (Binarytreenode<t>* root) {if  (root) {_clear (root->_left); _Clear (root->_ right);d elete root;}} Int  _size (Binarytreenode<t>* root) {int size = 0;if  (root) {size  +=  (_size (root->_left) +1); Size += _size (root->_right);} Return  size;} Int _depth (Binarytreenode<t>* root) {int depth = 0;if  (root) {int  leftdepth =&Nbsp;_depth (Root->_left); Int rightdepth = _depth (root->_right);d epth =  leftdepth > rightdepth ? leftdepth + 1 : rightdepth + 1;} Return depth;} Public:binarytree (): _root (NULL) {}binarytree (t* a, size_t size) {size_t index = 0; _root = _createbinarytree (a, index, size);} ~binarytree () {_clear (_root);} Void  preorder () {_preorder (_root); Cout << endl;} Void  inorder () {_inorder (_root); Cout << endl;} Void  postorder () {_postorder (_root); Cout << endl;} Void levelorder () {Queue<binarytreenode<t>*> q;q.push (_root);while  (!q.empty ()) {if   (Q.front ()) {Cout << q.front ()->_data <<  "  ";if  ( Q.front ()->_left) Q.push (Q.front ()->_left), if (Q.front ()->_right)     q.push (Q.front ()->_right); Q.pop ();}} Cout << endl;} Int size () {return  _size (_root);} Int depth () {return _depth (_root);}}; #include "BinaryTree.h" Void test1 () {binarytree<int> b1;int array[10] = { 1,  2, 3,  ' # ',  ' # ', 4,  ' # ',  ' # ', 5, 6 }; BINARYTREE&LT;INT&GT;&NBSP;B2 (array, 10); B1. Preorder (); B2. Preorder (); B2. Inorder (); B2. Postorder (); B2. Levelorder (); cout << b1. Size ()  <<  "  "  << b2. Size ()  << endl;cout << b1. Depth ()  <<  "  "  << b2. Depth ()  << endl;} Int main () {Test1 (); System ("pause"); return 0;}


This article is from the "Small Stop" blog, please be sure to keep this source http://10541556.blog.51cto.com/10531556/1748098

Two-fork 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.