Binary Tree and forest transformation

Source: Internet
Author: User

For a tree, the order of children in the tree is not important, as long as the relationship between the two parents and the child is correct. However, in binary trees, the order of left and right children is strictly differentiated. Therefore, in order not to cause confusion when discussing the conversion between a binary tree and a general tree, it is agreed that the conversion should be performed based on the order of existing nodes on the tree. Here we study the Conversion Between Binary Trees and general trees, so that we can understand the essential links between them. At the same time, when studying and solving general tree problems, we can sometimes convert them into binary tree problems.

There is a natural one-to-one correspondence between trees or forests and Binary Trees. Any forest or tree can uniquely correspond to a binary tree. On the contrary, any binary tree can uniquely correspond to a forest or a tree.

(1) Conversion from trees and forests to Binary Trees

1) convert a tree to a binary tree

Each node in the tree can have at most one leftmost child (eldest son) and one right neighbor brother. According to this relationship, the tree can naturally be converted into a corresponding binary tree.

The idea of converting a tree into a binary tree is mainly based on the tree's child-brother storage method. The steps are as follows:

① Add a line: Use a dotted line to connect the brokers. It can be understood that the sibling pointer of each node points to one of its siblings.

② Strip: Keep the line between each node and Its leftmost child, and erase the line between the node and other children. It can be understood that each node has only one child pointer and points it to its own eldest son.

③ Rotation: Change the dotted line to a solid line to rotate 45 ℃ downward from the horizontal direction to the Right oblique bottom direction. The line in the original tree is left oblique and down. In this way, the tree is like a binary tree.

The tree shown in Figure 3-54a below can be converted to a binary tree shown in Figure 3-54d.

2) convert a forest into a binary tree

A forest is a finite set of trees, as shown in 3-55a. As we can see from the above section, a tree can be converted to a binary tree (without the right subtree), and a forest can be converted to a binary tree (without the right subtree) Forest. To convert a forest to a binary tree, follow these steps:

① Convert each sub-tree in the forest into a corresponding binary tree. A forest with several Binary Trees is formed, as shown in 3-55B.

② Based on the sequence of trees in the forest graph, the next binary tree is used as the right subtree of the root node of a binary tree on the front, so that the entire forest generates a binary tree, in fact, the root node of the first tree is the root node of the generated binary tree. Figure 3-55 shows an example of converting a forest into a binary tree. Figure 3-55d is a converted binary tree.

.

(2) binary tree-to-tree and forest conversion

1) convert a binary tree to a general tree

At this time, the binary tree must be a binary tree that has no right subtree converted from a certain tree (General tree. Not any binary tree can be restored to a general tree.

The restoration process is also divided into three steps:

① Add line: If a node I is the left child of a parent node, then, the right child of node I and when and only when continuously searching all right children along the right chain of the right child are connected to the parent node of node I with a dotted line respectively.

② Strip: erase the connections between all the parent nodes in the original binary tree and their right children. The right child is essentially the brother of the original node in the tree, and the erased link is the relationship between the brothers.

③ Organize: Change the dotted line to solid line and arrange nodes in layers.

Restore a binary tree to a general tree:

2) convert a binary tree to a forest

To convert a binary tree into a forest, follow these steps:

① Strip: erase the line between the root node of the binary tree and Its right child, and the line between the right child that is not searched continuously along the right link of the right child. In this way, a binary tree with multiple root nodes without the right subtree is obtained.

② Convert the obtained Binary Trees into general trees using the aforementioned methods.

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.