Binary Tree is one of the most important aspects of data structure. The introduction of binary tree is because a good data structure is very helpful for data sorting, query, and other operations. It is also a balance between space and efficiency !!
Binary Tree definition: each node has at most two Subtrees (that is, nodes with a degree greater than 2 in a binary tree), and there are left and right Subtrees in a binary tree, and their order cannot be reversed. (From the "Data Structure C Language" Yan Weimin version, if there is rights, can be sent to: shenganbeiyang@163.com, I will immediately delete)
Shape:
Excerpt from: "Data Structure" Yan Weimin Figure 6.4
Binary Tree Classification: Full Binary Tree, full Binary Tree, general binary tree (simple for the time being ).
(A) If the binary tree is full, the degree of the root node is 1, the degree of the leaf node is 0, and the degree of the remaining nodes is 2.
(B) Complete Binary Tree: it is easy to understand. On the basis of the full Binary Tree, remove the node with the serial number back. Note that it must be counted from the last vertex. B is, and C and D are not. Therefore, C and D are only common Binary Trees.
Several important terms of Binary Tree: Degree, depth, root node, parent, leaf node, and subtree.
Degree: the number of subnodes that each node can have. Depth: layers; root node: the top-level vertex; parent: Actually a node. For example, in (a), the parent nodes of 4 and 5 are 2; leaf nodes: A node with a degree of 0; subtree: for example (a), the marked part is a subtree of 1.
Related Properties of the tree: 1, layer I has at most 2 (I-1) power nodes;
2, K depth of the binary tree at most 2 ^ K-1 nodes;
3. If the number of nodes whose degree is 0 is N0 and the number of nodes whose degree is 2 is N2, N0 = n2 + 1;
4. It is mainly about the positional relationship between nodes. It is actually very simple and will not be pasted out.
Tree creation and traversal:
Including the forward and backward order, which is actually the order of element access, such as (d): The first order 124536, the middle order: 425136, and the back order: 452631. Simple painting is enough.
The Code is as follows:
/* Email: shengbaiyang@163.comqq: 501968942 */# include <iostream> using namespace STD; struct tnode {char value; struct tnode * lchild; struct tnode * rchild ;}; typedef tnode * bitree; void initbitree (bitree & T) {char inchar = getchar (); // # indicates an empty node, but you must also enter it, to ensure tree integrity, if (inchar = '#') t = 0; else {T = (bitree) malloc (sizeof (tnode); If (! T) throw "Invalid Address"; else {T-> value = inchar; initbitree (t-> lchild); initbitree (t-> rchild );}}} // access void preorder (bitree & T) {If (t) {cout <"node is:" <t-> value <Endl; preorder (t-> lchild); preorder (t-> rchild) ;}/// void inorder (bitree & T) {If (t) {inorder (t-> lchild); cout <"node is:" <t-> value <Endl; inorder (t-> rchild );}} // post-Order Access to void postorder (bitree & T) {If (t) {postorder (t-> lchild); postorder (t-> rchild ); cout <"node is:" <t-> value <Endl ;}int main () {bitree t; cout <"Enter the node value :"; initbitree (t); cout <"pre-ordered access:" <Endl; preorder (t); cout <"middle-Order Access:" <Endl; inorder (t ); cout <"sequential access:" <Endl; postorder (t );}
Input: 124 #5 #3 #6 ##
It can be seen that the result is consistent with the previous manual derivation. Be sure not to miss #. Otherwise, not only the output result is incorrect, but it cannot run properly on schedule.
The recursive version has been written above. Next we will talk about the recursive version:
Http://www.cppblog.com/ngaut/archive/2006/01/01/2351.aspx.
Code for sequential traversal on March 13
Typedef int valuety; typedef struct node {valuety data; node * lchild; node * rchild;} node, * pnode; // initialize the binary tree void initbitree (pnode & PN) {valuety input; cin> input; If (input =-1) Pn = 0; else {Pn = (pnode) malloc (sizeof (node); If (Pn = 0) return; PN-> DATA = input; initbitree (PN-> lchild); initbitree (PN-> rchild) ;}} void stack_inorder (pnode PN) {stack <pnode> stack; while (! Stack. Empty () | PN! = NULL) {If (PN! = NULL) {stack. push (PN); Pn = Pn-> lchild;} else {pnode TMP = stack. top (); stack. pop (); cout <TMP-> data <Endl; Pn = TMP-> rchild ;}}}
Void stack_postorder (pnode PN) {stack <pnode *> S; pnode pre = NULL; pnode Top = NULL;
While (null! = Pn) |! S. Empty () {If (null! = Pn) {S. Push (PN); Pn = Pn-> left;} else {Top = S. Top (); If (top-> right! = Pre) {Pn = Top-> right; Pre = NULL; // pre is a one-time task. After the comparison, it becomes invalid.} else {visit (top ); pre = top; S. pop ();}}}