Tree and binary tree Conversion

Source: Internet
Author: User

1. convert a tree to a binary tree

1. Add a line between all the sibling nodes with the same parent nodes in the tree;

2. Keep the line between the newly added node and the left sibling node and delete the line between the node and the parent node instead of the first child node in the tree.

3. Sort all the reserved and added connections so that the first child node of each node is located at the left child pointer, so that the right brother node of each node is located at the right child pointer.



2. Restore a binary tree to a tree

How to restore a binary tree to a tree:

1. if a node is the left child of its parent node, the right child of the node and the right child of the right child ...... are connected to the parent node of the node with a line

2. Delete connections between all parent nodes in the original binary tree and the right child

3. Sort all reserved and added connections so that all child nodes of each node are located at the same tree level.


3. tree traversal

First root traversal, then root Traversal

First root traversal:

1. Access the root node; 2. Traverse each subtree of the root node in the order from left to right


Root traversal:

1. In the left-to-right order, each subtree of the root node is traversed by the root node.

2. Access the root node


Note:

1. The first root traversal sequence of the tree must be the same as the first traversal sequence of the Binary Tree converted by the tree.

2. The post-root traversal sequence of the tree must be the same as the central traversal sequence of the Binary Tree converted by the tree.











Tree and binary tree Conversion

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.