A tree structure is an important non-linear data structure. It can well describe objects with Branch structures or hierarchies in the objective world, such as file management of operating systems, compile the syntax structure in the program and organize the database system information.
The tree is defined as follows:
The tree is a finite set t of n (n> = 0) nodes. If t is null, it is an empty tree. Otherwise, the following two conditions are met:
(1) There is only one specific node called the root node;
(2) The remaining nodes can be divided into M subsets that do not conflict with each other. Each subset is a tree called the subtree of the root node.
Tree Representation Methods include tree structure, nested structure, and concave representation.
Binary Tree is an important form of tree structure. many practical problems abstract the data structure into binary trees. Even common trees can be converted into binary trees.
A binary tree is a finite set of n (n> = 0) nodes, it is either an empty set or composed of a root node and two left and right subtree that are mutually exclusive.
Note:
(1) A binary tree is different from a disordered tree. Each node of a binary tree has at most two Subtrees, which have left and right sides,
(2) The Binary Tree and the ordered tree with a degree of 2 are different. In the ordered tree, although the children of a node have left and right points, if there is only one child, there is no need to distinguish between the left and right sides, for binary trees, even if there is only one child, there are left and right points.
Binary Tree storage generally has two types: sequential Storage Structure and chained storage structure.
The sequential storage structure stores the nodes in a binary tree in a certain order in a continuous storage space. The node position in this sequence reflects the positional relationship between nodes.
For a complete binary tree, we numbered all nodes in the tree from the root, from left to right to obtain a linear sequence that reflects the binary tree structure.
Except the bottom layer, all layers of the Complete Binary Tree are full of nodes. The number of nodes in each layer is exactly twice the number of nodes in the previous layer. The number of a node can be pushed to its parent, left, right, brother, and other nodes. Assume that the node number I is Ki (1 ≤ I ≤ n), the following are available:
① If I> 1, the number of the ki's parent is. If I = 1, The Ki is the root node without the parent.
② If 2I is less than or equal to N, the number of the left child of Ki is 2I; otherwise, ki has no left child, that is, Ki must be a leaf. Therefore, the number in the Complete Binary Tree is
The node must be a leaf node.
③ If 2I + 1 ≤ n, the number of the right child of Ki is 2I + 1; otherwise, ki has no right child.
④ If I is odd and not 1, the number of the Left brother of Ki is I-1; otherwise, ki has no left brother.
⑤ If I is an even number and less than N, the right Brother Number of Ki is I + 1; otherwise, ki has no right brother.
Store all nodes in the Complete Binary Tree in the order of numbers in a vector BT [0 .. n. Where: Bt [1. N] is used to store nodes, and Bt [0] is not used or used to store the number of nodes.
The above is for a full binary tree, but we know that in general, our binary tree is not always a full binary tree. For a general binary tree, we need to add some "virtual nodes" to the tree ", turn it into a Complete Binary Tree.
The chain storage structure, as its name implies, stores references to its subnodes in each node. If the left child node of a node is empty, the reference pointing to the left child tree is left empty. If the right child node is empty, the reference pointing to the right child node is left empty. A binary tree linked list is determined by a reference pointing to the root node. If the reference is empty, the binary tree is empty.
For a Complete Binary Tree, the ordered storage structure is simple and saves storage space. However, for a general binary tree, the ordered storage structure is prone to waste of storage space, at the same time, the sequential storage structure for adding and deleting a binary tree will also cause a large amount of movement. Therefore, we prefer to use a chain storage structure to store binary trees.