content
1. The concept and structure of trees
1.2 Related important concepts
the height or depth of the tree
1.3 The tree's child sibling notation
The depth h of a full binary tree of n nodes
Specify the number of layer nodes:
Special relationship between leaf nodes and degree 2 nodes
A complete binary tree with n total nodes
2.4 Storage structure of binary tree
1. The concept and structure of trees
1.1 Concept
A tree is a nonlinear data structure, which consists of n (n>=0) finite nodes to form a set with hierarchical relationship. Looks like an upside-down tree with its roots pointing up.
Subtrees (branches) cannot be linked, otherwise it will not be a tree structure.
1.2 Related important concepts
The concept of memory tree using blood relationship
parent node or parent node
If a node has child nodes (with branches), the node is called the parent node of its child nodes;
As shown above: A is the parent node of B
child node or child node
The root node of the subtree contained in a node is called the child node of the node;
As shown above: B is the child node of A
degree of node
The number of subtrees contained in a node is called the degree of the node (the number of children of a node);
As shown above: A is 6, D is 1, E is 2
degree of tree
In a tree, the degree of the largest node is called the degree of the tree (the number of children of the node with the most children);
As shown above: the degree of the tree is 6
leaf node or terminal node
Nodes with degree 0 are called leaf nodes (nodes without branches at the end of the tree);
As shown above: B, C, H, I, K, L, M, N, P, Q nodes are leaf nodes
sibling node
Nodes with the same parent node are called sibling nodes;
As shown above: B and C are sibling nodes
node level
Starting from the definition of the root, the root is level 1, the child nodes of the root are level 2, and so on;
the height or depth of the tree
The maximum level of nodes in the tree; as shown above: the height of the tree is 4
ancestor of the node
All nodes on the branch from the root to the node; as shown above: A is the ancestor of all nodes
descendants
Any node in a subtree rooted at a node is called a descendant of that node. As shown above: all nodes are descendants of A
forest
The set of m (m>0) disjoint trees is called a forest;
1.3 The tree's child sibling notation
The chain storage structure is used, the node that saves the child points to the child, the node of the brother points to the brother, and saves the element
typedef int DataType;
struct Node
{
struct Node* _firstChild1; // 第一个孩子结点
struct Node* _pNextBrother; // 指向其下一个兄弟结点
DataType _data; // 结点中的数据域
};
2. Binary tree
2.1 Concept
A binary tree is a set of n finite elements, which is either empty or consists of an element called the root and two disjoint binary trees called the left subtree and the right subtree, respectively. It is an ordered tree . When the set is empty, the binary tree is called an empty binary tree. In a binary tree, an element is also called a node.
Features
1. There is no node with a degree greater than 2 in a
binary tree 2. The subtrees of a binary tree are divided into left and right, and the order cannot be reversed, so the binary tree is an ordered tree
2.2 Special binary tree
full binary tree
A binary tree, if the number of nodes in each layer reaches the maximum value, the binary tree is a full binary tree. That is, if a binary tree has K levels and the total number of nodes is 2^k-1, then it is a full binary tree.
complete binary tree
For a binary tree of depth K with n nodes , it is called a complete binary tree if and only if each node corresponds to a node numbered from 1 to n in a full binary tree of depth K one-to-one . Note that a full binary tree is a special kind of complete binary tree .
2.3 Important properties
It is stipulated that the number of layers of the root node is 1, the number of layers is represented by k, and the number of layers K = the depth h of the tree
The total number of nodes n:
n=2^k-1 or n=2^h-1
The depth h of a full binary tree of n nodes
Formula: h =
It can be deduced from the formula of summed points
Specify the number of layer nodes:
A non-empty binary tree has at most 2^(i-1) nodes at level i.
Special relationship between leaf nodes and degree 2 nodes
n0=n2+1 n0 (number of leaf nodes) n2 (degree is 2 nodes)
A complete binary tree with n total nodes
Note: The serial number starts from 0, the serial number in the following figure is marked on it, n is the total number of nodes, not the maximum serial number
For the node with serial number i
: 1. If i>0, the parental serial number of the node at i position: (i-1)/2 ; i=0, i is the root node number, and there is no parent nodeThe child node number cannot be greater than the summary point number
2. If 2i+1<n, left child number: 2i+1 , 2i+1>=n, then no left child
3. If 2i+2<n, right child number: 2i+2 , 2i+2>=n no right child
2.4 Storage structure of binary tree
sequential storage
Ordinary binary trees are not suitable for storing in arrays, because there may be a lot of wasted space . A complete binary tree is more suitable for storage in a sequential structure. As shown below
chain storage
A binary tree is represented by a linked list, that is, a chain is used to indicate the logical relationship of elements. The usual method is that each node in the linked list consists of three fields, the data field and the left and right pointer fields . The left and right pointers are respectively used to give the storage addresses of the link nodes where the left and right children of the node are located.
This is the end of the basic concept of binary tree. The next article introduces the concept and implementation of heap: implementation of heap + heap sorting - Programmer Sought