Two-fork tree (i.)

Source: Internet
Author: User

A tree is a collection of elements of N (n>=0) finite data, shaped like an inverted tree.


650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/80/01/wKiom1cz_wejdM9BAAA8rH_zdpQ806.png "title=" QQ picture 20160512115506.png "alt=" Wkiom1cz_wejdm9baaa8rh_zdpq806.png "/>1 is a tree, no loop. 2 is not a tree, there is a loop.


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

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/7F/FE/wKioL1c0ARexYFhgAAA6C1j3UC8306.png "title=" QQ picture 20160512120147.png "alt=" Wkiol1c0arexyfhgaaa6c1j3uc8306.png "/>

The traversal of a tree

Example:

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/80/01/wKiom1c0ARXxZdJfAAAaHK-Vz9o319.png "title=" QQ picture 20160512120528.png "alt=" Wkiom1c0arxxzdjfaaaahk-vz9o319.png "/>

Pre-sequence traversal (first root traversal): 1, first access to the root node, 2, the pre-order access to the left subtree, 3, the pre-order access to the right sub-tree; "1 2 3 4 5 6"

Middle Sequence Traversal: 1, middle sequence access to the left subtree, 2, access to the root node, 3, middle order access to the right sub-tree; 3 2 4 1 6 5 "

post-sequential traversal (after root traversal), 1, After the visit to the left sub-tree, 2, after the visit to the right subtree, 3, access to the root node; "3 4 2 6 5 1"

Sequence traversal: A layer of nodes traverses sequentially. "1 2 5 3 4 6"


650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/> Tree's linked list structure

650 "this.width=650;" Src= "Http://s2.51cto.com/wyfs02/M02/80/01/wKiom1c0AVPSgdz5AABiBERIyKY015.png" title= "QQ picture 20160512115904.png" alt = "Wkiom1c0avpsgdz5aabiberiyky015.png"/> Tree construction and code implementation

Template<class t>class binarytree{public:binarytree (): _root (NULL) {}binarytree (const T*  str, size_t size,const t& invalid) {size_t index = 0;_root =  _createtree (str, size,index, invalid);} BinaryTree (const binarytree<t>& b) {_root = _copy (b._root);} Binarytree<t>& operator= (binarytree<t> b) {swap (_root, b._root); return * this;} ~binarytree () {_destory (_root);} Protected:binarytreenode<t>* _createtree (const t* str, size_t size, size_t & index, const t& invalid) {        // The reference is to prevent fallback binarytreenode<t>* root = null;if  (index < size)  & &  (Str[index] != invalid)) {root = new binarytreenode<t> (Str[index]);// First construct the left subtree Root->_left = _createtree (STR,&NBSP;SIZE,&NBsp;++index, invalid);//Build Right subtree Root->_right = _createtree (str, size, ++index,  Invalid);} Return root;} Binarytreenode<t>* _copy (Const binarytreenode<t>* root) {if  (root) {       BinaryTreeNode<T>* newRoot = new BinaryTreeNode<T> ( Root->_data);   newroot->_left = _copy (root->_left);   newRoot->_right  = _copy (root->_right);   return newroot;} Return null;} Void _destory (binarytreenode<t>* root) {binarytreenode<t>* cur = root;if   (Root) {_destory (root->_left); _destory (root->_right);d Elete cur;cur = null;root  = null;} return;} protected:binarytreenode<t>* _root;};


Recursive traversal of a tree

Public:void prevorder ()   //pre-order recursive {_prevorder (_root); Cout << endl;} Void inorder ()     //in order recursive {_inorder (_root); Cout << endl;} Void postorder ()    //post-shipment recursion {_postorder (_root); Cout << endl;} Void levelorder ()    //sequence recursion {_levelorder (_root);} Protected:void _prevorder (Const binarytreenode<t>* root) {if  (Root == NULL) {return;} cout << root->_data <<  " "; _prevorder (Root->_left); _PrevOrder (root- >_right);} Void _inorder (Const binarytreenode<t>* root) {if  (root == null) {return;     }_inorder (root->_left);cout << root->_data <<  "   "; _inorder (root->_right); return;} Void _postorder (Const binarytreenode<t>* root) {if  (root == null) {return;} _postorder (Root->_left); _postorder (roOt->_right);cout << root->_data <<  " ";} Void _levelorder (binarytreenode<t>* root) {Queue<binarytreenode<t>*> q;q.push ( Root);while  (0 < q.size ()) {Binarytreenode<t>* tmp = q.front (); cout  << tmp->_data <<  " "; Q.pop ();if  (tmp->_left != null ) {Q.push (tmp->_left);} if  (tmp->_right != null) {Q.push (tmp->_right);}}}

VOID&NBSP;PREVORDER_NONR ()    //non-recursive pre-order {stack<binarytreenode<t>* > v1; V1.push (_root); Binarytreenode<t>* cur = _root;cout << v1.top ()->_data <<   " ";while  (!v1.empty ()) {V1.pop ();if  (cur->_right) {V1.push (cur->_right);} if  (cur->_left) {V1.push (cur->_left);} if  (V1.size ()  == 0) {return;} Cout << v1.top ()->_data <<  " "; Cur = v1.top ();} VOID&NBSP;INORDER_NONR ()    //non-recursive middle order {stack<binarytreenode<t>* > v2; binarytreenode<t>* cur = _root;while  (cur| |! V2.empty ()) {while  (cur) {v2.push (cur); cur = cur->_left;} Cout << v2.top ()->_data <<  " "; Binarytreenode<t>* top = v2.top (); V2.pop (); cur = top->_right;}} VOID&NBSP;POSTORDER_NONR ()      //non-recursive post-shipment {BINARYTReenode<t>* cur = _root; Binarytreenode<t>* prev = null;stack<binarytreenode<t>* > s;while   (cur | |  !s.empty ()) {while  (cur) {s.push (cur); cur = cur->_left;} Binarytreenode<t>* top = s.top ();if  (top->_right == null | |  top->_right == prev) {cout << top->_data <<  " "; S.pop ();p rev = top;} Else{cur = top->_right;}}}

tree size, depth ....

       size_t size () Size of    //tree {size_t size =  _size (_root); return size;} Size_t depth () Depth of    //tree {size_t dep = _depth (_root); return dep;} Size_t leafsize () Number of     //leaf nodes {size_t count = _leafsize (_root); return  count;} Binarytreenode<t>* find (const t& d)     //find Node {return _Find (_ ROOT,&NBSP;D);} How many nodes/*size_t getklevel (const size_t& k) {return _getklevel (_root, k) are there on level k?} */size_t getklevel (const size_t& k) {Size_t level = 1;_getklevel (_root,  1evel, k, size); return size;} Size_t _size (Const binarytreenode<t>* root) {size_t size = 1;if  (root= =null) {return 0;} Size += _size (Root->_left); size += _size (root->_right); return size;} Size_t _dePTH (const binarytreenode<t>* root) {size_t dep1 = 1;size_t dep2 =  1;if  (root == null) {return 0;} Dep1 += _depth (Root->_left);d ep2 += _depth (root->_right);if  (dep1 > &NBSP;DEP2) {return dep1;} ELSE{RETURN&NBSP;DEP2;}} Size_t _leafsize (const binarytreenode<t>* root) {size_t count = 0;if  ( Root == null) {return 0;} if  ((root->_left == null)  &&  (root->_right == null)) {return  1;} Count += _leafsize (Root->_left); count += _leafsize (root->_right); return count;} Binarytreenode<t>* _find (binarytreenode<t>* root, const t& d) {if   (root == null) {return null;} if  (root->_data == d) {return root;} Binarytreenode<t>* ret = _find (root->_left, d);if  (ret) {REturn ret;} Return _find (root->_right, d);} /*size_t _getklevel (binarytreenode<t>* root, size_t k) {if  (root ==  NULL) {return 0;} if  (k == 1) {return 1;} Return _getklevel (root->_left, k - 1)  + _getklevel (root->_right, k &NBSP;-&NBSP;1);} */void _getklevel (binarytreenode<t>* root, size_t level, size_t k,  size_t& size) {if  (root == null) {return;} if  (k == level) {Size++;return;} _getklevel (root->_left, level + 1, k, size); _getklevel (root->_right, level  + 1, k, size);}

The next article will introduce you to the clues of the tree, write bad please teach Oh oh oh oh ~650) this.width=650; "src=" Http://img.baidu.com/hi/jx2/j_0002.gif "alt=" J_0002.gif " />

Two-fork tree (i.)

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.