The strongest data structure in history - the concept and structure of binary tree (with exercise analysis)

1. The concept and structure of the tree

1.1 The concept of tree

A tree is a nonlinear data structure, which consists of n (n>=0) finite nodes to form a set with hierarchical relationship. It is called a tree because it looks like an upside-down tree, which means it has the roots up and the leaves down .

  • There is a special node, called the root node, the root node has no predecessor nodes

  • Except for the root node, the remaining nodes are divided into M(M>0) sets T1, T2, ..., Tm that do not intersect with each other, each set Ti(1<= i <= m) is a structure with tree-like subtrees. The root node of each subtree has one and only one predecessor, and can have 0 or more successors

  • Trees are defined recursively. Each tree is composed of heels and subtrees, as shown in the following figure:

    image-20220331143458718

    The tree shown in the figure is composed of a root and three subtrees, and the same is true for subtrees. For example, the first subtree is composed of the root and two subtrees, and the second subtree is composed of the root and one subtree. , the third subtree is formed by the root and two subtrees, which is why the tree is defined recursively.

Note: In a tree structure, there can be no intersection between subtrees, otherwise it will not be a tree structure.

image-20220331144118690

In the above structure diagram, if the tree has a loop, it is a diagram.

1.2 Related concepts of trees

image-20220331144157472

Degree of a node: The number of subtrees contained in a node is called the degree of the node; as shown above: A is 6

Leaf node or terminal node (emphasis) : A node with a degree of 0 is called a leaf node; as shown in the figure above: B, C, H, I... and other nodes are leaf nodes

Non-terminal nodes or branch nodes : nodes whose degree is not 0; as shown in the figure above: D, E, F, G... and other nodes are branch nodes

Parent node or parent node (emphasis) : If a node contains child nodes, this 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 (emphasis) : 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

Sibling nodes : Nodes with the same parent node are called sibling nodes; as shown above: B and C are sibling nodes, but H and I are not sibling nodes

The degree of the tree: in a tree, the degree of the largest node is called the degree of the tree; as shown above: the degree of the tree is 6

The level of nodes : starting from the root, the root is the first layer, the child nodes of the root are the second layer, and so on; of course, there are also definitions starting from the root, and the root is the 0th layer, but this comparison few.

The height or depth of the tree (emphasis) : the maximum level of nodes in the tree; as shown above: the height of the tree is 4

Cousin node : nodes whose parents are in the same layer are cousins ​​of each other; as shown above: H and I are sibling nodes of each other

Ancestor of a node: all nodes on the branch from the root to the node; as shown above: A is the ancestor of all nodes, and the ancestor of Q is Q, A, E, J

Note: You can think of yourself as your ancestor.

Example: In the picture above. The most recent common ancestor of F and K is F, because F can be considered as its own ancestor, and F is also the ancestor of K.

Descendants : Any node in the subtree rooted at a node is called the descendant of the node. As shown above: all nodes are descendants of A

Note: You can consider yourself your own offspring.

Forest : A collection of m (m>0) disjoint trees is called a forest

1.3 Representation of trees

The tree structure is more complicated than the linear table, and it is more troublesome to store and represent. Since the value range is saved, the relationship between nodes and nodes must also be saved . In practice, there are many ways to represent trees, such as: parental representation , child representation, child parent representation, and child sibling representation, etc. Here we simply understand the most commonly used child brother notation.

typedef int DataType;
struct Node
{
	struct Node* _firstChild1; // 第一个孩子结点
	struct Node* _pNextBrother; // 指向其下一个兄弟结点
	DataType _data; // 结点中的数据域
};
//一个节点有多少个子节点都无所谓,,父亲指向第一个孩子,剩下的孩子,用孩子的兄弟指针链接起来。

image-20220401131451087

1.4 The use of trees in practice (representing the directory tree structure of the file system)

image-20220401133001448

2. Binary tree concept and structure

2.1 Concept

A binary tree is a finite set of nodes with a maximum degree of 2. The set:

  1. or empty
  2. Consists of a root node plus two binary trees called left and right subtrees

image-20220401133308326

As can be seen from the above figure:

  1. There is no node with degree greater than 2 in a binary tree
  2. The subtree of a binary tree is divided into left and right, and the order cannot be reversed, so the binary tree is an ordered tree

Note: For any binary tree, it is composed of the following situations:

image-20220401133357141

2.2 Realistic binary tree

image-20220401162420964

2.3 Special binary tree

  1. 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 the level of a binary tree is K, and the total number of nodes is , then it is a full binary tree.(For example, the first picture above is a full binary tree)
  2. Complete binary tree: A complete binary tree is a highly efficient data structure, and a complete binary tree is derived from a full binary tree. For a binary tree of depth K with n nodes, it is called a complete binary tree if and only if every node of the tree of depth K corresponds one-to-one with nodes numbered from 1 to n in the full binary tree of depth K. Note that a full binary tree is a special kind of complete binary tree.( The first k-1 layers are full, the last layer is full, but the last layer is continuous from left to right )

image-20220401162810285

2.4 Properties of Binary Trees and Related Exercises

2.4.1 Properties of Binary Trees

  1. If the specified level of the root node is 1, then a non-empty binary tree has at most 2 (i-1) nodes on the i-th level .
  2. If the number of levels of the root node is specified to be 1, the maximum number of nodes of a binary tree with a depth of h is 2 h -1.
  3. For any binary tree, if the number of leaf nodes of degree 0 is n 0 , and the number of branch nodes of degree 2 is n 2 , then there is n 0 = n 2 +1 . (As long as a degree of 2 is added , it must add two degrees of 0)
  4. If the level of the root node is 1, the depth of a full binary tree with n nodes, h= log 2 (n+1). (ps: log 2 (n+1) is log base 2, n+ 1 is logarithm)
  5. For a complete binary tree with n nodes, if all nodes are numbered from 0 in the array order from top to bottom and left to right, then for the node with ordinal i:
    1. If i>0, the parent sequence number of the node at i position: (i-1)/2 ; i=0, i is the root node number, no parent node
    2. If 2i+1<n, the left child sequence number: 2i+1, 2i+1>=n otherwise there is no left child
    3. If 2i+2<n, the right child sequence number: 2i+2, 2i+2>=n otherwise there is no right child

2.4.2 Related Exercises

  1. A binary tree has a total of 399 nodes, of which there are 199 nodes with degree 2, then the number of leaf nodes in the binary tree is (B)

    A does not exist such a binary tree

    B 200

    C 198

    D 199

    Analysis: Using the above property 3, the number of degree 0 is 1 more than the number of degree 2, that is, 199+1 or 200 can get the result we want .

  2. Among the following data structures, which is not suitable for sequential storage structure (A)

    A non-complete binary tree

    B heap

    C queue

    D stack

  3. In a complete binary tree with 2N nodes, the number of leaf nodes is (A)

    A N

    B N+1

    C N-1

    DN/2

    Parse:

    Assuming that the number of nodes with degree 2 is N 2 , then the number of nodes with degree 1 is N 1 , and the number of nodes with degree 1 is N 0 . From the above properties, the relationship between N 2 and N 0 can be obtained , that is, N 0 = N 2 + 1

    N 0 + N 1 + N 2 = 2N

    Using the above relationship, the following formula can be obtained:

    N 0 + N 1 + N 0 - 1 = 2N

    2N 0 - 1 + N 1 = 2N

    Complete binary tree N 1 is 1 or 0

    If the above formula is to be satisfied, then N 1 can only be 1 (must satisfy 2N 0 - 1 + N 1 = 2N), so the following formula is obtained

    2N 0 = 2N

    i.e. N 0 = N

    So the number of leaf nodes is N

  4. A complete binary tree has 531 nodes, so the height of the tree is (B)

    A 11

    B 10

    C 8

    D 12

    Parse:

    The number of nodes of a full binary tree with h layers is: 2 h -1

    Assuming that the number of nodes in a full binary tree is N, the depth of the binary tree is: h = log 2 (N+1)

    For this problem, assuming the height is h, then the number of nodes in the binary tree is:

    Up to: 2 h -1

    Minimum: 2 h-1 (that is, the number of nodes in the first h-1 layer plus 1, the formula is N = 2 h-1 -1+1 ( add 1 because there is another node in the last layer ))

    Assuming that the number of nodes in a binary tree is N, then N must satisfy the following inequality:

    2^h-1 < N < 2h-1

    After the given conditions are brought into the calculation, h is 10.

  5. A complete binary tree with 767 nodes, the number of leaf nodes is ()

    A 383

    B 384

    C 385

    D 386

    Assuming that the number of nodes with degree 2 is N 2 , then the number of nodes with degree 1 is N 1 , and the number of nodes with degree 1 is N 0 . From the above properties, the relationship between N 2 and N 0 can be obtained , that is, N 0 = N 2 + 1

    N 0 + N 1 + N 2 = 767

    N 0 + N 1 + N 0 -1 = 767

    2 * N 0 - 1 + N 1 = 767

    At this time, N 1 can only be 0, why? Because when N1 is 1, the number of nodes should be an even number at this time, but the title given is an odd number, so N1 can only be 0.

    So the number of leaf nodes after calculation is 384.

2.5 Storage structure of binary tree

Binary trees can generally be stored in two structures, a sequential structure and a chain structure.

  1. sequential storage

    Sequential structure storage is to use arrays for storage . Generally, arrays are only suitable for representing complete binary trees , because not complete binary trees will waste space. In reality, only the heap will use the array to store it, and we will specifically explain the heap in the following chapters. Binary tree order storage is physically an array, and logically is a binary tree.

    image-20220403104455348

  2. chain storage

    The chain storage structure of a binary tree means that 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. The chain structure is further divided into two-fork chain and three-fork chain. At present, we generally use two-fork chain in our study. Later, when we learn high-level data structures such as red-black tree, we will use three-fork chain.

    image-20220403104625988

    typedef int BTDataType;
    // 二叉链
    struct BinaryTreeNode
    {
    	struct BinTreeNode* _pLeft; // 指向当前节点左孩子
    	struct BinTreeNode* _pRight; // 指向当前节点右孩子
    	BTDataType _data; // 当前节点值域
    }
    // 三叉链
    struct BinaryTreeNode
    {
    	struct BinTreeNode* _pParent; // 指向当前节点的双亲
    	struct BinTreeNode* _pLeft; // 指向当前节点左孩子
    	struct BinTreeNode* _pRight; // 指向当前节点右孩子
    	BTDataType _data; // 当前节点值域
    };
    

おすすめ

転載: blog.csdn.net/m0_57304511/article/details/124227733