1. Complete binary tree
The complete binary tree is usually stored in sequential storage because of its structural characteristics. when all nodes of a complete binary tree with n nodes are numbered from 1 to N, a linear series of nodes is obtained.
such as: Complete binary tree In addition to the bottom layer, each layer is filled with nodes, each layer of nodes is exactly twice times the number of nodes in the previous layer, so through a node number 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 has no left child;
② when 2i+1≤n, the right child of node I is 2i+1, otherwise node i has no right child;
③ when I≠1, the parents of node I are node I/2;
650) this.width=650; "title=" 201405210954288838.gif "alt=" wkiol1vxfqexj6rqaadyooxy9yc156.gif "src=" http:// S3.51cto.com/wyfs02/m02/6e/00/wkiol1vxfqexj6rqaadyooxy9yc156.gif "/>
Note: Since the array subscript starts at 0 and uses an array to simulate the binary tree (including the heap, of course), if the root node is subscript 0, the left child is labeled 2*i+1 for each node I, and the right child is labeled 2*i+2.
2. General binary Tree
For a typical two-fork tree, the relationship between the subscript of an array element does not reflect the logical relationship between nodes in the binary tree if the node order in the tree is stored in a one-dimensional array, from left to right in order from top to bottom.
This assumes that the General binary tree is transformed , adding some nonexistent empty nodes, making it a complete binary tree form, and then storing it in one-dimensional array order. In the binary tree, assume that the added node corresponds to an element value of "null" in the array.
650) this.width=650; "title=" 34242423.gif "alt=" Wkiom1vxe9ay_vjbaadleasjrfa338.gif "src=" http://s3.51cto.com/ Wyfs02/m02/6e/04/wkiom1vxe9ay_vjbaadleasjrfa338.gif "/>
Reference blog:
Http://tech.ddvip.com/2014-05/1400680475210602.html
This article is from the "Nothing-skywalker" blog, please be sure to keep this source http://tianxingzhe.blog.51cto.com/3390077/1658816
Array Order store binary tree