"Brick-to-Jade"-exploring the mutual Conversion Between Binary Trees and common Binary Trees

Source: Internet
Author: User

People who have been familiar with data structures will not be unfamiliar with terms related to trees, binary trees, and other trees. I will not go into details about some basic knowledge and concepts of the tree here. This article mainly describes the conversion of clue Binary Trees and common Binary Trees.


Before explaining this part, it is necessary to give a brief introduction to the clue binary tree.


First, why is the term "lead Binary Tree" appearing?


Let's take a look at the storage structure of a common Binary Tree:


The following figure shows its space utilization:

It can be seen that there are many null values in a common Binary Tree, resulting in a waste of space. There are n nodes in the ordinary binary tree, so there are a total of 2n pointers in this tree, but only N-1 are valid, and the remaining n + 1 are not used, is null. The clue binary tree uses N + 1 null pointer to store the frontend and backend relationships of nodes. At the same time, the chain table solves the problem of left and right children in the binary chain table.


Then how does the clue Binary Tree implement the functions mentioned above?


The representation of the clue Binary Tree:


On the surface, there are two more areas of the clue Binary Tree: lbit and rbit. It occupies more space than a common binary tree, but lbit and rbit are logical. Each of them only needs one "bit", which saves space and does not cause waste. So what are their functions? Its function is:


Well, I have introduced the ins and outs of the clue binary tree. Next I will explain how to convert a common binary tree into a clue binary tree.


The premise is that you need to know several concepts: pre-order traversal, post-order traversal, and Middle-order traversal of the binary tree. Here is just a brief memory method.

Pre-order traversal: Root-left-right subtree; post-order traversal: Left-right subtree-root; middle-order traversal: Left subtree-root-right subtree.


Faster first: Straight view of common binary tree and clue Binary Tree

STEP (we use the ordinal traversal as an example ):

First, traverse the following binary tree in the middle order and write the final result.


The result is: d B h e a f c g I

Then, follow the frontend and subsequent steps of the leaf node to establish a line (the red line indicates the successor) according to the sequence of the above results)


We will not repeat the other two types of traversal here. Here we will only show the results after the tracing. You can try it on your own. If you do not understand, contact me for a discussion.


The key to converting a common binary tree into a clue binary tree is to first write several traversal sequences of the common binary tree, and then complete the conversion between them after understanding the role of the clue binary tree.



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.