Big talk. The sequential storage structure of 14:2-fork Tree (array implementation)

Source: Internet
Author: User
Tags empty

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/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.