Some of the rules in this blog (such as the root node hierarchy of 1) are all from the data structure (C language Edition) tree
In the data structure of the tree is a very important point of knowledge, in the learning process of trees we can know is that many of its songs key nouns, these basic concepts can sometimes make us very clear about the nature of the choice to fill in the blanks:
Node: A node consists of a data element and several branches (pointers) pointing to other subtrees.
the degree of the node: the number of subtrees owned by the node is called the degree of the node.
leaf node: the node of degree 0 is called the leaf node, the leaf node is called the break node, the
branch node: the node of the degree complement 0 is called the Branch node, and the branch node leaf is known as the Non terminal node. All nodes except the leaf node in a number are branch nodes;
ancestor node: All nodes from the root node to the branch of the node;
descendants node: All nodes in a subtree with a node as its root;
parent node: A node in the tree has a child node. Then the node is called the parent node of the child node, and the parent node leaf is called the precursor node;
child node: The root node of the subtree of a node in the tree is called the child node of the node, and the child node Leaf is called the successor node;
sibling nodes: nodes with identical parent nodes are called sibling nodes;
Degree of a tree: the maximum value of the degrees of all nodes in a tree is called the degree of the tree; the level of the node
: from the root node in the tree, the number of branches on the path is called the level of the node, the level of root node is 1, the other nodes are the level of the parents and 1;
The depth of the tree: the maximum value of the hierarchy of all nodes in the tree becomes the depth of the tree;
Forest: A set of tree m trees (m greater than or equal to 0). In nature trees and Senlin are two completely different concepts, but in the data structure of China, the difference between them is very small. By deleting the root node of a non-empty number, the tree becomes a forest; conversely, if a node is added, the forest is aggregated and the root node of each tree becomes his child, and the forest becomes a tree;
Two fork Tree
A binary tree (Binary) is another tree-shaped structure, it is characterized by at most only two subtrees per node (that is, the node with no degree greater than 2 in the binary tree), and the subtree of the binary tree has the left and right points, the order can not be arbitrarily reversed;
Two similar but distinct concepts in binary trees: full two fork trees/ complete binary trees ;
Full two fork tree: In a binary tree, if all branches exist Saozi right subtree, and all leaf nodes are on the same layer.
Complete binary tree: If a two-node tree with n nodes has the same structure as the first n nodes of the two-forked tree. called Complete binary tree;
The nature of the binary tree:
1. There are at most 2^ (i-1) nodes on the first layer of the binary tree (I >= 1);
2, the depth of K two-tree at most (2^k)-1 nodes;
3, for any binary tree T, if its terminal node number is n0, the degree of 2 of the knot is N2, then no = n2 + 1;
4, the depth of the complete binary tree with n nodes is Llog2 (n) "+1; (log takes the logarithm of 2 as the base N in addition to 1)
5, if the node of a complete binary tree with n nodes is numbered in sequence (from 1 to LLOG2 (n) to the"+1 layer, each layer is left-to-right), Then to any node I (1<=i<=n), there are:
(1), if the I=1, then the node i is the root of the two fork tree, no shuangqin, if i>1, then its parents paren (i) is the node li/2";
(2) If 2i>n, then the node I have no left child, otherwise its left child lchild (i) is node 2i;
(3) If 2i+1>n, then the node I have no right child, otherwise its right child rchild (i) is the node 2i+1;
Based on these 5-point properties, we can look at the following questions:
1: We can full two fork tree is no degree of 1 node, so the branch node has 31, leaf node has 32;
2: Using the formula in the Nature 4 can be very simple to get a depth of 9;
3: In the complete binary tree, its node is odd to indicate that it does not have a degree of 1 nodes, so its leaf node is: 36;
4: Its knot number is even, so it has a degree of 1 nodes, so there are 500 leaf nodes, 2 of the node is 499, only 1 non-null left subtree, no non-empty right subtree;
See some of the above questions, our nature is still very important, so we have to bear in mind these several properties;
After understanding the basic concept and nature of the binary tree, we have to traverse the two-fork tree, in which the traversal of the binary tree is divided into 4,
First-order traversal of the binary tree:
If the binary tree is empty, then null operation; otherwise:
(1) access to the root node,
(2) first-order traversal of the left subtree,
(3) first-order traversal of the right subtree;
In order to traverse the binary tree:
If the binary tree is empty, then null operation; otherwise:
(1) The sequence traverses the left subtree,
(2) accesses the root node,
(3) The middle sequence traverses the right subtree;
After the second traversal of the binary tree:
If the binary tree is empty, then empty operation; otherwise: (1) After the second step
traversal left subtree, (2) after the subsequent traversal of the
right subtree,
(3) Access to root node;
In fact, their three methods are very similar, that is, the order of access is not the same, they are in the realization of the time is the use of recursion;
There is also a sequence of sequential traversal, which is different from the above three:
According to the sequence order of the binary tree (from the root node to the leaf node layer), the same layer traverses the binary tree in the order of the first left and right,
Sequence Traversal algorithm:
A, initialization of a queue;
B. The pointer to the root node is like a queue;
C, when the queue is not empty, loop through the following steps
>> out the queue to get a node
>> If the left subtree of the node is not empty, the left subtree of the nodes will be changed into the queue
>> if the right subtree is not empty, the right subtree of the junction will be changed into queue
D and end
In this blog's code, I construct a binary tree from the first order to its construction, copy construction, overloading of assignment operators, destructor, four ways of traversal, complete
#include <iostream> using namespace std; #include <queue> template<class t> struct Binarytreenode {binarytreenode (const t& data): _data (d
ATA), _pleft (null), _pright (null) {} T _data; binarytreenode<t>* _pleft; The left child binarytreenode<t>* _pright;
Right child}; Template<class T> class BinaryTree {typedef binarytreenode<t> Node; Public:binarytree (): _p Root (NULL) {} BinaryTree (const T array[], size_t size, const t& Invalid) {//Create tree size_t
index = 0;
_createtree (_proot, array, size, index, invalid);
} BinaryTree (const binarytree<t>& T) {_proot = _copybinarytree (t._proot);
} binarytree<t>& operator= (const binarytree<t>& T) {if (_proot) {
_destroytree (_proot);
_proot = _copybinarytree (t._proot); else _proot = _copybInarytree (T._proot);
return *this;
}//first-order traversal void preorder () {_preorder (_proot);
cout << Endl;
///In-order traversal: Access left subtree--> root--> right subtree void inorder () {_inorder (_proot);
cout << Endl;
//Subsequent traversal: access to the left subtree--> access to the right subtree--> access to the root node void Postorder () {_postorder (_proot);
cout << Endl;
}//sequence traversal void Levelorder () {queue<node*> q;
if (_proot) Q.push (_proot);
while (!q.empty ()) {node* pcur = Q.front ();
cout << pcur->_data << "";
Q.pop ();
if (pcur->_pleft) {Q.push (pcur->_pleft);
} if (pcur->_pright) {Q.push (pcur->_pright);
}} ~binarytree () {_destroytree (_proot); } private:void _createtree (node*& proot,Const T array[], size_t size, size_t& index, const t& Invalid)//When recursion is implemented we must refer to its variables, otherwise the final return cannot be followed by the string's traversal order;
if (Index < size && Array[index]!= invalid) {proot = new Node (array[index));
_createtree (proot->_pleft, array, size, ++index, invalid);
_createtree (proot->_pright, array, size, ++index, invalid);
} node* _copybinarytree (node* proot) {node* tmp = NULL;
if (NULL!= proot) {tmp = new Node (proot->_data);
Tmp->_pleft = _copybinarytree (proot->_pleft);
Tmp->_pright = _copybinarytree (proot->_pright);
return TMP;
} void _destroytree (node* &proot) {if (proot) {_destroytree (proot->_pleft);
_destroytree (Proot->_pright);
Delete Proot;
Proot = NULL;
} void _preorder (node* proot) { if (proot) {cout << proot->_data << "";
_preorder (Proot->_pleft);
_preorder (Proot->_pright);
} void _inorder (node* proot) {if (proot) {_inorder (proot->_pleft);
cout << proot->_data << "";
_inorder (Proot->_pright);
} void _postorder (node* proot) {if (proot) {_postorder (proot->_pleft);
_postorder (Proot->_pright);
cout << proot->_data << "";
}} private:binarytreenode<t>* _proot;
};
void Test () {char array[] = "124## #35 # #6";
binarytree<char> s (array, strlen (array), ' # ');
binarytree<char> S1 (s);
S.preorder (); S1.
Preorder ();
S.inorder (); S1.
Inorder ();
S.postorder (); S1.
Postorder ();
Binarytree<char> S2;
S2 = s;
S.levelorder (); } int MAin () {Test ();
return 0; }