Sequential storage Binary Tree Compression Algorithm

Source: Internet
Author: User

Abstract: Ordered Binary Tree occupies a large space and cannot be inserted or deleted. It is rare to discuss Binary Tree compression and storage. However, the search for Ordered Binary Trees is very convenient. This article provides a compression storage method for Binary Trees Based on the leaf node location, called the leaf node location method.
This algorithm is based on the following basic properties of Binary Trees:

1: In any binary tree, if the number of terminal nodes is N0 and the number of nodes with the degree of 2 is N2, N0 = n2 + 1.
2. If the nodes of a Complete Binary Tree with N knots are arranged in sequence and numbered from 1, if the child node is left or right, their numbers are 2I, 2I + 1, respectively.

The basic idea of this method is: Any node of a binary tree is the parent node/grandfather node of the leaf node or leaf node. After adding any binary tree as a Complete Binary Tree and numbering from 1 according to the sequence, the numbers of any node in the original binary tree can be calculated based on the number of the leaf node.
For example, it is a simple binary tree.

The result of compression using the leaf node location method is:
A [6] = [a, B, c, d, e, f];
B [3] = [2, 6, 15]
A [] is the data information of the node. 2, 6, and 14 in B [] are the hierarchical traversal numbers of leaf node B, D, and F when the binary tree is supplemented as a Complete Binary Tree. The numbers of nodes A, C, and E can be calculated by the numbers of their leaf nodes. For example, node e is numbered int (15/2) = 7.
The following algorithm restores the compressed binary tree to the serial number of a non-empty node. Based on the location information of the leaf node A [], the location information of non-null nodes in the binary tree is calculated and stored in array B [] in reverse order.
The following code is passed in turboc2.0.
# Include <stdio. h>
Void main ()
{
Int A [6] = {0, 4, 5, 13, 24, 60 };
Int B [15] = {0 };
Int J = 0;
Int left, middle, right, K;
Int n = 5;
 
Clrscr ();
B [0] = A [n];
J ++;
While (n> 1)
{
A [n] = A [n]/2;
A [0] = A [n];
Left = 1; Right = n-1;
While (left <= right)
{
Middle = (left + right)/2;
If (A [0] <A [Middle])
Right = middle-1;
Else
Left = middle + 1;
}
If (A [n] = A [left-1] & left! = 1)
{
N --;
}
Else
{
For (k = n-1; k> = left; k --)
A [k + 1] = A [k];
A [left] = A [0];
}
B [J] = A [n];
J ++;

}
For (j = 0; j <15; J ++)
Printf ("% d \ n", B [J]);
}

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.