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