Tree - binary tree and binary tree traversal (17)

Here Insert Picture Description


A. Binary tree

1. The definition of binary tree

The following conditions are met called binary tree structure:

1. Each of the nodes is not greater than 2.

Child node can not be arbitrarily reversed the order of 2 per node.


2. The nature of binary tree

1 Properties: k-th layer on the binary tree, up to 2 ^ (k-1) (k≥1) nodes.

Property 2: depth m of the binary tree has at most 2m-1 (m≥1) nodes.

Nature 3: In any one binary tree, if it is the terminal number of nodes n0, the number of nodes of degree 2 is n2, then n0 = n2 + 1.

Property 4: binary tree has n nodes and a depth of at least [log2n] +1, where [] denotes the integer part log2n log2n is taken.

Nature 5: If there is a complete binary tree of n nodes to all nodes will be from top to bottom and left to right order of a sequence number starting from 1, then for any number of node i are:

① As i = 1, i is the root node of the binary tree, no parents, as i> 1, the serial number of the parent node of node i i / 2.

② If 2i> n, then the number is no left child node i, such 2i ≤ n, to the left of the serial number of the child nodes of node i is 2i.

③ As 2i + 1> n, the number of node i no right child, such 2i + 1 ≤ n, then the number is a serial number of the right child node of the node i is 2i + 1.


3. binary storage structure

Binary tree structure is non-linear, each node can have up to two successor. Stores binary tree structure divided into sequential storage structure and chain storage structure.


1). Sequential storage structure

Sequential storage structure may be implemented using a one-dimensional array, as shown below:
Here Insert Picture Description


As can be seen, when the binary tree is a complete binary tree, this storage configuration is very convenient, but space is not wasted, but for the average of a binary tree must be "virtual node" to make into a complete binary tree is stored, which resulting in a waste of space, as shown below:
Here Insert Picture Description
Therefore, the order is stored generally applicable to a complete binary tree, since it can not be stored in order to meet the storage needs of the binary tree, you can use a chain store, as shown below.


2). Storage Structure

Here Insert Picture Description
For an arbitrary binary tree, each node has only one parent node (except the root), up to two children, it may be designed on the structure shown in FIG: [lchind data rchild]. The binary tree is also called binary list.


3). Junction code implementation

Binary Code linked list node structure to achieve the following:

typedef struct Node
{
    DataType data;
    struct Node * LChild;
    struct Node * RChild;
}BiTNode,*BiTree;

II. Binary tree traversal (focus)

1. ergodic theory course

Here Insert Picture Description
If left subtree represents the traversal, the root, the right subtree with LDR, the following six kinds of traversal order:

① access to the root, traverse the right subtree, traverse the left subtree. Referred to as DRL

② access to the root, traverse left subtree, traverse the right subtree. Denoted by DLR

③ Traverse the left subtree, root access, traversing the right subtree. Referred to as LDR

④ Traverse the right subtree, root access, traversing the left subtree. Referred to as RDL

⑤ Traverse the left subtree, traverse the right subtree, root access. Denoted by LRD

⑥ Traverse the right subtree traverse left subtree, root access. Recorded as RLD

If the order specified by the first left and right traversal, only the three remaining: DLR, LDR, LRD.

Then depending on the access order of the root, respectively, said front DLR order traversal, in order traversal of the LDR, LRD is postorder traversal.


Here Insert Picture Description
For the above-described binary tree:
preorder traversal sequence: ABDECFG
preorder sequence: DBEAFCG
postorder sequence: DEBFGCA

Written this, I think it will be more helpful Meng new understanding:

Preorder traversal sequence: (A) (B (DE )) (C (FG))
inorder traversal order: ((D) B (E )) (A) ((F) C (G))
postorder traversal order :( (DE) B) (( FG) C) (A)

先将大头写下来然后进行填写,类似于填空,但是仅限于帮助你理解遍历顺序,而不是让你使用这个方法记忆遍历顺序,我们应该通过辅助工具来帮助我们真正了解。


Here again a hand and thought:
Here Insert Picture Description

Try to write about and the following is not the same as it answers, as if to show that you have learned!
Preorder traversal: ABDFGCEH
preorder: BFDGACEH
postorder traversal: FGDBHECA

The following code is used to implement various traversal.


2. preorder traversal

void PreOrder(BiTree root)
{ // root为指向二叉树或某一树的根结点
    if(root!=NULL)
    {
        Visit(root->data); // 访问根结点,显示数据结点
        PreOrder(root->LChild);// 遍历左子树
        PreOrder(root->RChild);// 遍历右子树
    }
}

3. preorder

void PreOrder(BiTree root)
{
    if(root!=NULL)
    {
        PreOrder(root->LChild);
        Visit(root->data)
        PreOrder(root->RChild);
    }
}

4. postorder

void PreOrder(BiTree root)
{
    if(root!=NULL)
    {        
        PreOrder(root->LChild);
        PreOrder(root->RChild);
        Visit(root->data);
    }
}

Although the theory of knowledge is very complicated for the binary tree traversal, but due to the binary tree is a recursive definition of the structure, the first order, in sequence, after, it is recursively defined. Therefore, a recursive binary tree traversal mode code amount small.


If wrong, please correct me criticism, comments are welcome.
Each text sentence: No one will be responsible for your poor, there are rich people you applaud! So do not live in someone else's mouth, do yourself! There is a road, boldly go away; you have a dream, boldly fly; the way forward, not afraid of people stop, I'm afraid surrender themselves!

Published 74 original articles · won praise 180 · views 30000 +

Guess you like

Origin blog.csdn.net/Fdog_/article/details/104782153