The construction of binary tree should pay attention to the difference with the chain table, the binary tree here is very low-level, each tree just constructs a single two-fork tree node, the overall view is the next upward build. Users need to manually build their own tree, rather than directly to insert data into the two fork tree, because it is not a single chain structure, the binary tree is very rich.
Mention the traversal:
Iterating through the way is straightforward. The first order, the middle order, and the sequential traversal are all only for the root node. For example, the middle order is to first traverse the left subtree---------right subtree. (root node in the middle)
/*1) The basic concept of the tree: the degree: Plainly is the node has a number of sub-branches of the leaf node: White is the degree of 0 of the node parents: plainly is the Father node level: The contract root node is 1, the subsequent child nodes increment the height: plainly is the highest number of levels 2) binary tree type 1> two tree: The leaf must be on the last layer, and the other node degree is 2 2> expansion of the binary tree: In addition to the leaf node, the other node degree is 2 1, the largest feature of the extended binary tree is his outer path length = length of the inner path +2* number of non-leaf nodes 2, typical applications is Havermann encoded 3> complete binary tree: Only the last two levels of the node can be less than 2, and the last 1 layers of leaf nodes must be left. 1, the entire complete binary tree according to from left to right, from top to bottom of the 0-->n to be numbered, if the child node ordinal is I, the parent node is (i-1)/2. 2, the implementation of the typical application size heap. 4> Other types of binary tree 3) The mutual transfer between the forest and the two-fork tree*/Template<classT>structbtnode{Btnode<T> *_lchild, *_rchild; T element; Btnode (ConstT &e) {element=e; _lchild= _rchild =NULL; }};template<classT>classjbbinarytree{ Public: Jbbinarytree (); ~Jbbinarytree (); Btnode<T> *_root; //Empty the current tree voidClear (); //gets the current elementT getelement ()Const; //determine if the current binary tree is not an empty two-pronged tree BOOLIsEmpty ()Const; //Build a tree number voidMaketree (ConstT &x,JBBinaryTree<T> *left,jbbinarytree<t> *Right ); //Remove a tree number voidBreaktree (T &x,jbbinarytree*left,jbbinarytree*Right ); //recursive traversal of pre-sequence traverse unification voidPreorder (btnode<t> *t); //Middle Sequence Traversal voidInorder (btnode<t> *t); //Post-post traversal voidPostorder (btnode<t> *t);}; Template<classT>Jbbinarytree<T>:: Jbbinarytree () {_root=NULL;} Template<classT>Jbbinarytree<t>::~Jbbinarytree () {Delete_root;} Template<classT>voidJbbinarytree<t>:: Clear () {Delete_root; _root=NULL;} Template<classT>T Jbbinarytree<t>::getelement ()Const { if(IsEmpty ()) {Reurn NULL; } return_root->element;} Template<classT>BOOLJbbinarytree<t>::isempty ()Const { return_root = =NULL;} Template<classT>voidJbbinarytree<t>::maketree (ConstT &x, jbbinarytree<t> *left, jbbinarytree<t> *Right ) { if(_root)return;//If the root node is not empty, return directly this actually makes it very inconvenient for the user to work. You must re-create a node_root =NewBtnode<t> (x);//instantiate a binary root node_root->_lchild = left->_root;//let the left pointer point to the root node of the Zuozi_root->_rchild = right->_root;//let the right pointer point to the root node of the right subtreeLeft->_root = Right->_root =NULL;} Template<classT>voidJbbinarytree<t>::breaktree (T &x, Jbbinarytree*left, jbbinarytree*Right ) { if(!_root | | left = right | | left->_root | | right->_root) { //if the tree itself is empty, or used to undertake the same as the left and right tree itself, or to undertake the Zuo subtree is not empty directly exit return; } x= _root->element;//Move the element domain of the root node of this tree awayLeft->_root = _root->_lchild;//separate the Zuozi into a treeRight->_root = _root->_rchild;//split the right subtree into a single tree Delete_root; Clear ();} Template<classT>voidJbbinarytree<t>::p Reorder (btnode<t> *t) {if(t) {printf ("%d",t->element); Preorder (t-_lchild); Preorder (t-_rchild); }}template<classT>voidJbbinarytree<t>::inorder (btnode<t> *t) {if(t) {inorder (t-_lchild); printf ("%d",t->element); Inorder (t-_rchild); }}template<classT>voidJbbinarytree<t>::p ostorder (btnode<t> *t) {if(t) {postorder (t-_lchild); Postorder (t-_rchild); printf ("%d",t->element); }}
Let's take a look at the main program
#include"stdafx.h"#include"stdlib.h"#include"JBQueue.h"#include"JBStack.h"#include"JBBinaryTree.h"intMain () {{Jbbinarytree<int>l,r,a,b,c,d,e; A.maketree (0,&l,&R); B.maketree (1,&l,&R); C.maketree (2,&a,&b); D.maketree (3,&a,&b); E.maketree (4,&c,&d); printf ("Pre-order traversal:"); E.preorder (E._root); printf ("\ n Sequence traversal:"); E.inorder (E._root); printf ("\ nthe post-traversal:"); E.postorder (E._root); printf ("\ n"); } System ("Pause"); return 0;}
Results:
Construction of binary tree of data structure C + + version