C + + algorithm to find the node number, depth and four traversal methods of binary tree

Source: Internet
Author: User

The data structure of the node class Btree{public:int       M_nvalue; btree*    M_nleft; btree*    m_nright;public:btree (int value) {  m_nvalue = value;}};


One: The number of nodes to find the binary tree:

/* To find the number of nodes in the binary number recursive solution: 1: If the binary tree is empty, the number of nodes is 02: If the binary tree is not empty, the number of binary tree nodes = number of left subtree nodes + right subtree node number +1;*/int getnodecount (btree* proot) {if ( Proot = = NULL) return 0;int leftnum  =  getnodecount (proot->m_nleft); int rightnum =  Getnodecount (proot- >m_nright); int ret = Leftnum+rightnum+1;return ret;}


Two: To find the depth of the binary tree:

/* Depth recursive solution for binary tree: 1: If the binary tree is empty, then the depth of the two fork tree is 02: If the binary tree is not empty, the binary tree depth = MAX (left child depth, right subtree depth) +1;*/int gettreedepth (btree* proot) {if (Proot = = NULL) return 0;int leftdepth  = gettreedepth (proot->m_nleft); int rightdepth = Gettreedepth (proot->m_nright) ; int ret = max (leftdepth,rightdepth) +1;return ret;}


Three: Four kinds of traversal methods:

/* Pre-sequence traversal: 1: If the binary tree is empty, empty operation 2: If the binary tree is not empty, access the root node, the pre-sequence traverses the left subtree, the pre-sequence traverses the right subtree */void preordertraverse (btree* proot) {if (Proot = = NULL) return; cout<<proot->m_nvalue<<endl; Preordertraverse (Proot->m_nleft); Preordertraverse (proot->m_nright);} /* Sequence Traversal: 1: If the binary tree is empty, empty operation 2: If the binary tree is not empty, the first step is to traverse the left word tree, the second step is to follow the node, the third step is to traverse the right subtree */void inordertraverse (btree* proot) {if (Proot = = NULL ) Return;inordertraverse (Proot->m_nleft); Cout<<proot->m_nvalue<<endl;inordertraverse (pRoot- >m_nright);} /* Post-Traversal: 1: If the binary tree is empty, empty operation 2: If the binary tree is not empty, the first step is to traverse the left subtree, the second step is to traverse the right subtree, the third step is to access the node; */void backordertraverse (btree* proot) {if (Proot = NULL) return; Backordertraverse (Proot->m_nleft); Backordertraverse (proot->m_nright); Cout<<proot->m_nvalue<<endl;} /* Layered traversal of the binary tree (from top to bottom, left to right) is equivalent to the breadth-first search, which is implemented using queues. The queue is initialized, and the node is pressed into the queue. When the queue is not empty: Popup a node, access, if the left subtree node or right subtree node is not empty, press it into the queue! */void Leveltraverse (btree* proot) {if (Proot = = NULL) return;queue<btree*> q;q.push (Proot); while (!q.empty ()) { btree* Pnode = Q.front (); Q.pop (); cout<<pnode->m_nvalue<<endl;//Access node if (pnode->m_nleft! = null) Q.push (pnode->m_nleft); if (pnode->m_nright! = null) Q.push (pnode->m_ nright);}}


C + + algorithm to find the node number, depth and four traversal methods 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.