Introduction
In a linear table, the logical relationship between elements is equal (peer), and there is no rank, only a succession of points. But in the tree, the elements are composed of complex genealogy relationships.
the definition of a tree
n elements are a collection of inverted tree shapes. Each element corresponds to a node in the tree
(1) Root: In a tree, there is only one specific node called root (Root);
(2) The remaining nodes can be divided into m (m≥0) disjoint subsets of Tl,t2,...,tm, each of which is itself a tree, and is called the root
Sub-tree(Subree). As can be seen, the definition of a tree is a recursive definition.explanation of the noun related to the treeThere are a lot of concepts associated with trees, but it seems to make sense to understand them. Write down or good, or look at other documents, you do not know what others are saying, and the exams like this very much--. Empty tree : A tree with no nodes. Depth of tree : Hierarchy of trees. , the depth of the tree is 3 degrees : The maximum value of all node degrees in the tree. , the degree of a 2,b is also 2, the other is 1,so, the degree of the tree is 2. Ordered Tree : The sub-tree is orderly, is concerned about the order. Unordered tree : The reciprocal positions of the children are not affected, not in the order of meaning. leaf nodes : terminal nodes, nodes with no sub-nodes. , d,e,f are leaf nodes. Branch node : Non-terminal node. Nodes that have sub-nodes. , a,b,c are branch nodes. Node degree : number of child nodes owned by a node (number of children). , B has 2 sub-nodes, then it has a degree of 2. Child node : A node's direct sub-node, called the node of the child node. , A's child is b,c. F is the child paper of C. Parents (parent node) : Parent node of a node. In addition to the root node, a node has only one parent. , A is a common parent node of B and C. Brother : The nodes that have the same parent node are brothers to each other. , b and C are brothers, and D and E are brothers. Cousins : The nodes of the same layer are cousins to each other. , D,e and F are cousins of each other. Descendants : All nodes derived from a node, It is called the offspring of this knot, including its children. , B,c,e,d,f are descendants of a. Forest: M a tree that is unrelated to each other and constructs a forest. Two fork TreeThe degree of the binary tree is 2, that is, each node has a maximum of 2 children, and the 2 child paper is about the point. Even if there are only 1 children, it is divided into left child, or right child. Properties of binary trees 1,
Two the number of nodes on the first layer of a fork tree is 2i-1 (i>=1)When the maximum is reached, it is considered according to the two-fork tree. 2.
A two-fork tree with a depth of K has at most 2k-1 nodes (k≥1). According to the nature of 1, it can be concluded that: Total_node = + + + + ... + 2k-1 can be found, this is a male ratio of 2 K-term geometric series, by the equal formula sum to get Total_node = 2k-1
3.
in any binary tree, if the terminal (leaf) node number is n0, the degree of 2 node is N2, then there is: n0=n2+1. To prove this nature, it is necessary to analyze the point and the line. Set: The number of nodes with a degree of 2 has N2, the degree of 1 of the nodes have N1, the degree of 0 leaf node has n0, the sum of the tree points for total. The total number of connections for the tree is L. ①total = n2 + n1 + n0②l = total-1 In addition to the root node, the other nodes are led by a line overhead. ③l = 2*n2 + N1 degree 2 node, the top will be 2 lines downward, the same, the degree of 1 nodes will lead to 1. Combined with 3 sub-extinction: N0 = n2 + 1
full two fork tree
In addition to the leaf nodes, each node has a maximum value of 2.
Complete binary TreeIf a binary tree at most only the bottom of the two layers on the node can be less than 2 degrees, and the next layer of the nodes are concentrated in the leftmost position of the layer, then this binary tree is called a complete binary tree. For example:On the basis of the K-layer full two-tree, by deleting the lowest-right continuous n (n>=0) leaf nodes, a K-layer complete binary tree can be obtained. Features: Full two fork tree must be a complete binary tree, in turn not necessarily. Properties:
1. The depth of a complete binary tree with n nodes is: Int (log2n) + 1Int (log2n) represents the largest integer that is not greater than log2n.chain-type binary tree
As with linear tables, binary trees have sequential implementations and chain implementations, and for the sake of simplicity, we focus on 3 traversal algorithms, so we chose the chain implementation. Sequential implementations are later mentioned. First we build the data type, assuming the stored target data type is char. Here is an example of a two-tree with 5 elements. The data for the nodes are the characters A, B, C, E, F, respectively. Each node, in addition to the data domain, has 2 pointer fields that hold the memory address of its 2 children, respectively. The address of the node is what I assume, the purpose of which is to explain the relationship between them.
Char DataType; // Target Storage Object type struct node // node { DataType data; struct node* left; struct node* right;} Node;typedef Node* linkedbinarytree;
It's not a good way to create this tree by manually hard-coding it. But here we just want to focus on understanding the 3 traversal algorithm, so, here to endure it. Believe in understanding the traversal, other problems will be solved.
Full code
#include <stdio.h>typedefCharDatatype;typedefstructnode{DataType data; structnode*Left ; structnode*Right ;} Node;typedef Node*Linkedbinarytree;/***************function declare*******************/voidLinkedbinarytree_init (Linkedbinarytree *ptree);intMain () {Linkedbinarytree tree; Linkedbinarytree_init (&tree); return 0;}voidLinkedbinarytree_init (Linkedbinarytree *Ptree) { //Root*ptree =NewNode; (*ptree)->data ='A'; //bNODE*PB =NewNode; PB->data ='B'; //CNODE*PC =NewNode; PC->data ='C'; //DNODE*PD =NewNode; PD->data ='D'; //eNode*pe =NewNode; PE->data ='E'; //links(*ptree)->left = PB; (*ptree)->right =pc; PB->left = PD; Pb->right = PE; Pc->left = NULL; Pc->right =NULL; PD->left = NULL; Pd->right = NULL; Pe->left = NULL; Pe->right =NULL;}View Code
So far, we have created the tree nearly. All right, let's take a rest. Next stop: Two traversal of the tree
The first experience of the "Data structure" tree