1. Sequential Storage structure:
The sequential storage structure of binary tree is to store nodes in binary tree with one-dimensional array.
2. Complete two-fork tree:
Because of its structural characteristics, the complete binary tree is usually stored in sequential storage mode. A linear series of nodes is obtained when all nodes of a complete binary tree with n nodes are numbered from 1 to N.
The following figure: Complete binary tree except the bottom layer, each layer is filled with nodes, the number of each layer is exactly twice times the number of the previous layer of nodes, so the number of a node can infer its parents node and left, right child node number:
① when 2i≤n, the left child of node i is 2i, otherwise node I have no left child;
② when 2i+1≤n, the right child of node I is 2i+1, otherwise node I have no right child;
③ when I≠1, the parents of node I are node I/2;
Note: Because the array subscript starts at 0, the subscript of the array element equals the ordinal of the node in the complete binary tree minus 1.
3. General two-fork tree:
For a general two-fork tree, if the node order in the tree is stored in a one-dimensional array in order from top to bottom, left to right, the relationship between the subscript of the array elements cannot reflect the logical relationship between the nodes in the binary tree.
It is assumed that the general binary tree will be modified to add some empty nodes that do not exist, so that it becomes a complete binary tree form, and then stored in the order of one-dimensional array. The element value in the binary tree that is assumed to be added in the array is "empty" with the ^ representation.
More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/