Recursive implementation of binary tree

Source: Internet
Author: User

Binary tree is a very useful structure, and a binary tree is a tree structure with a maximum of two subtrees per node. The subtree is often referred to as the left subtree and the right subtree (subtree). Binary trees are often used to implement a two-fork search tree and a two-fork heap.

Each node of a binary tree has a maximum of two subtrees trees (no nodes with a degree greater than 2), and the subtree of the binary tree has left and right points, and the order cannot be reversed. The first layer of the binary tree has a maximum of 2^{i-1} nodes, and a two-tree with a depth of K has a maximum of 2^k-1 nodes; for any binary tree T, if its terminal node number is N_0, and the degree of 2 is n_2, then n_0=n_2+1.

A depth of k, and there are 2^k-1 nodes called full two-tree, the depth of k, there are N nodes of two-tree, and only if each of its nodes with a depth of k in the full two-tree, the sequence number of 1 to n corresponds to a node, called a complete binary tree. Detailed definition see Baidu Encyclopedia

Two the structure of the fork tree makes it very useful in the sorting algorithm, the most useful when it is a balanced binary tree, the balanced binary tree will be discussed in my blog.

Talking about the two-fork tree, we have to discuss the traversal of the binary tree, in general, there are 4 ways to traverse the binary tree:

Suppose our tree is like this:

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/7F/22/wKioL1cUxBGDysZRAAATh5980GE537.png "title=" Capture 3. PNG "alt=" Wkiol1cuxbgdyszraaath5980ge537.png "/>


(i) Pre-sequence traversal

First we have to analyze the sequence of the first order traversal: A,b,d,e,c,f,g.

the traversal of a tree is simply a matter of recursive implementation, and we will traverse a whole tree to decompose into sub-problems that traverse the Saozi right sub-tree.

void Prevorder ()//pre-order Traversal {_prevorder (_root);} void _prevorder (binarytreenode<t>* root) {if (root = NULL) {return;} cout << root->_data << "";//First output root node _prevorder (root->_left);//In the output left subtree _prevorder (root->_right);// Last right sub-tree}

(ii) Middle sequence traversal

Order of Traversal: D,b,e,a,f,c,g

void Midorder ()//middle order Traversal {_midorder (_root);} void _midorder (binarytreenode<t>* root) {if (root = NULL) {return;} _midorder (root->_left), cout << root->_data << ""; _midorder (root->_right);}

(iii) post-sequential traversal

Traversal Order: D,e,b,f,g,c,a

void Rearorder ()//post-traversal {_rearorder (_root);} void _rearorder (binarytreenode<t>* root) {if (root = NULL) {return;} _rearorder (Root->_left); _rearorder (root->_right); cout << root->_data << "";}

(iv) Sequence traversal

Traversal Order: A,b,c,d,e,f,g

sequence traversal can take advantage of the queue FIFO features, each layer of the node into the queue, as long as the queue is not empty, a queue.

void Sequenceorder ()//sequence traversal {queue<binarytreenode<t>*> q;if (_root) Q.push (_root), while (!q.empty ()) {if ( Q.front ()->_left) {Q.push (Q.front ()->_left);} if (Q.front ()->_right) {Q.push (Q.front ()->_right);} cout << Q.front ()->_data<< ""; Q.pop ();}}

Tree traversal is relatively simple, below we look at a bit difficult:

(a) The number of leaf nodes to be asked for tree:

The number of leaf nodes is always on the deepest layer. , every time when the left and right subtree of the root node of a sub-problem is null, we add one to the number of ring nodes, of course, the leaf node can be defined as a static variable, so that each addition is the same variable.

You can also not define static variables, because static variables can be a security issue for threads.

size_t Leafcount () {return _leafcount (_root);} size_t _leafcount (binarytreenode<t>* root) {if (root = NULL) {return 0;} if (root->_left==null && root->_right ==null) {return 1;} Return (_leafcount (root->_left) +_leafcount (Root->_right));}

(ii) Finding the depth of the tree

Finding the depth of the tree is a difficult problem, because we have to compare the depth of the different subtrees and then take the largest one, but in a recursive program it is difficult to guarantee that a variable will not change. Here we just compare the depth of the left and right words in each sub-problem, each return the depth of the maximum value plus one, the last value is the depth of the tree.

size_t deepth () {return _deepth (_root);} size_t _deepth (binarytreenode<t>* root) {if (root = NULL) {return 0;} size_t leftdeep = _deepth (root->_left) +1;size_t rightdeep = _deepth (root->_right) +1;return leftDeep > Rightdeep? Leftdeep:rightdeep;}

(c) The number of nodes seeking trees

This problem is relatively easy, we can use any kind of traversal way to traverse the tree, each traversal to a node, the number is to be.

size_t Size () {return _size (_root);} size_t _size (binarytreenode<t>* root) {if (root = NULL) {return 0;} Return _size (Root->_left) + _size (root->_right) + 1;}


Recursive implementation 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.