Lehr [] [] data structures and algorithms C language [] consisting of two binary tree traversing the entire tree Release

Lehr [] [] data structures and algorithms C language [] consisting of two binary tree traversing the entire tree Release

Binary tree preorder, inorder, postorder traversal, you can launch any two known the whole tree looks like. The idea is the same, here to preorder and inorder example.

This process involves the following reverse derivation, the reader is preparing a draft paper XD

For example: a known sequence preorder traversal and, whole tree shape seeking?
Preorder: ABDEHCFIJG
in order: DBHEAFJICG

Solution:
First-order traversal of the first element is the root node, so A is the root node, by location preorder in A, we can in order to traverse the left subtree and right subtree points out:
D B H EA F J I C G
left sub tree ... ... right child
so you can draw such a sketch:

Here Insert Picture Description

Next we left subtreeBDEHFor analysis:
first order:A B D E H C F I J G
中序:D B H E A F J I C G
So the left subtree, the first order and order are:
preorder: BDEH
in sequence: DBHE
it is a root node B Similarly, separated left and right subtrees:
in order: DB H E
its left subtree is the right sub-D HE tree
further refine this FIG obtained:
Here Insert Picture Description
and then continues above:
first order:B DEH
in order:D BHE
so the right subtree, the first order and order are:
first order: the EH
in order: HE
so Similarly as a root node E, H of the left subtree.
This figure further refinement get:
Here Insert Picture Description
Now, the entire left have all been clearly defined, and now look at the right part:
Total Sort:
preorder:A B D E HCFIJG
in order:D B H E AFJICG
so the right subtree, the first order and order are:
preorder: CFIJG
in sequence: FJICG
so Similarly C is a root node, separated left and right subtrees:
in the order: F J IC G
to give the right and left subtree FJI subtree G, after subdivision drawing:
Here Insert Picture Description
Turning now FJI analysis tree:
preorder: FIJ
in sequence: FJI
so naturally, the root node F. But here note the following, in order also F in the first place, so the left subtree is empty right subtree JI, it is also the right subtree as above, and then divided again, and finally get the full Tree:
Here Insert Picture Description

success!

Published 33 original articles · won praise 26 · views 2609

Guess you like

Origin blog.csdn.net/qq_43948583/article/details/91357376