The storage structure of binary tree

Source: Internet
Author: User

Binary tree is a nonlinear structure, that is, each data node has at most one precursor, but can have multiple successors. It can take a sequential storage structure and a chained storage structure.

1. sequential storage structure

Binary tree sequential storage, is to use a continuous set of storage units to store the nodes in the two-fork tree. Therefore, all the nodes of the two-fork tree must be arranged into a proper sequence, where the nodes in the sequence can reflect the logical relationship between nodes, the numbering method from the tree root, from the upper to the lower, each layer from left to right to all nodes numbered, the disadvantage is that the storage space can cause great waste, In the worst case, a right single tree with a depth of k and only K nodes needs to 2k-1 a node storage space. According to the nature of binary tree, the complete binary tree and full two-fork tree are suitable for sequential storage, and the sequence number of nodes in the tree can uniquely reflect the logical relationship between nodes, so as to save storage space as much as possible, and to use the subscript value of array elements to determine the position of nodes in the binary tree and the relationship between nodes. Figure 5-5 (a) is a complete binary tree, figure 5-5 (b) gives the graph 5-5 (a) as shown in the sequential storage structure of the complete binary tree.

(a) A complete binary tree (b) Sequential storage structure

Figure 5-5 Sequential storage of a complete binary tree

For a typical two-fork tree, if the order of the nodes in the tree is still stored in a one-dimensional array in order from top to bottom and left to right, the relationship between the subscript of the array element does not reflect the logical relationship between the nodes in the binary tree, and only adds some non-existent empty nodes, making it a complete binary tree form. It is then stored in one-dimensional array order. 5-6 The complete binary tree morphology and its sequential storage state of a general binary tree are given. Obviously, this kind of storage for the need to add a lot of empty nodes in order to transform a binary tree into a complete binary tree storage, will cause a lot of space waste, it is not appropriate to use sequential storage structure. The worst case is the right single tree, 5-7, a right single tree with a depth of k, only K nodes, but the allocation of 2k-1 storage units.

(a) a binary tree (b) complete binary tree after transformation

(c) Complete binary tree sequential storage state after transformation

Fig. 5-6 General binary tree and its sequential storage

(a) A single right Two-fork tree (b) a fully binary tree corresponding to the right single-branch tree modified

(c) Sequential storage state of complete binary tree after single-branch tree transformation

Fig. 5-7 Right Single-branch two-fork tree and its sequential storage

Structure 5-1 two binary tree sequential storage

#define MAXSIZE     // Suppose that a one-dimensional array holds a maximum of 100 elements char Datatype;  // Suppose the data type of a binary tree element is a character typedef struct{Datatype bt[maxsize];     int btnum;  } btseq; Copy Code

2. chained storage structure

The chain storage structure of binary tree means that a binary tree is represented by a chain list, that is, the logical relation of the element is indicated by the chain.

The usual method is that each node in the list consists of three fields, the data field and the left and right pointer fields, and the left hand pointer is used to give the storage address of the node where the child and the child are located. Its node structure is:

Where the data domain holds information about a node, lchild and Rchild each hold a pointer to the left child and right child, and when the left child or right child does not exist, the corresponding pointer field value is empty (denoted by the symbol ∧ or null). The chain storage structure of a two-fork tree, represented by such a node structure, is called a binary list, as shown in 5-8.

(a) a binary tree (b) Binary linked list storage structure

Fig. 5-8 representation of a two-fork tree in a two-fork list

To facilitate access to the parents of a node, you can also add a parent field to the linked list node, which is used to point to its parent node. Each node consists of four fields with a node structure of:

This storage structure makes it easy to find the children's nodes and find the parents ' nodes, but it increases the space overhead relative to the binary list storage structure. The chain storage structure of a two-fork tree, represented by such a node structure, is called a three-pronged list.

Figure 5-9 shows the three-prong list representation of a binary tree as shown in Figure 5-8 (a).

Fig. 5-9 representation of a cross-linked table with a two fork tree

Although it is not possible to find the parents directly from the node in the binary list, the two-fork linked list is flexible and easy to operate, and it can save space for the general situation of the two-fork tree, even more than the sequential storage structure. Therefore, the binary linked list is the most commonly used two-fork tree storage method.

Structure 5-2 Two fork-tree chained storage

Char  // defines the data type of a binary tree element as a character typedef struct  node   // defines the node by data field, left and right pointers composed of {Datatype data;   *lchild,*Rchild;} Bitree;

The storage structure of binary tree

Related Article

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.