Binary tree (one)----to find the image of binary tree, recursive and non-recursive way

Source: Internet
Author: User

1, Binary tree definition:

typedef struct BTREENODEELEMENT_T_ {    void *data;} btreenodeelement_t;typedef struct Btreenode_t_ {    btreenodeelement_t     *m_pelemt;    struct Btreenode_t_    *m_pleft;    struct btreenode_t_    *m_pright;} btreenode_t;


2. Find binary Tree image

For example:

A A

b c ====> c b

D e E D


(1) Recursive method

If Proot is null, it is an empty tree and returns;

If the proot is not NULL, the proot of the left and right nodes are exchanged, and then the image of the left and right subtree is respectively obtained;


void Btreemirror (btreenode_t *proot) {

if (Proot = = NULL)

return;


btreenode_t *ptemp = proot->m_pleft;

Proot->m_pleft = proot->m_pright;

Proot->m_pleft = ptemp;


Btreemirror (Proot->m_pleft);

Btreemirror (Proot->m_pright);

Return

}


(2) Non-recursive mode

Step Description: With queue


First, proot the root node into the queue;

The first step: when the queue is not empty, gets the total number of nodes at the current level, that is, the length of the current queue; perform the second step;

The second step: according to the total number of nodes in the current layer, the team to traverse the node, in the traversal, the exchange of left and right nodes, if the left and right nodes exist, then the queue; when traversing all nodes in the current layer, traverse the next layer, performing the first step.

void   Btreemirror (btreenode_t *proot) {    if (proot = = null)        return null;    Queue <btreenode_t *> que;    Que.push (proot);    int curlevelnodestotal = 0;    while (!que.empty ()) {        curlevelnodestotal = Que.size ();        int cnt = 0;        while (CNT < curlevelnodestotal) {            ++cnt;            Proot = Que.front ();            Que.pop ();            btreenode_t *ptemp = proot->m_pleft:            proot->m_pleft = proot->m_pright;            Proot->m_pright = ptemp;            if (proot->m_pleft! = NULL)                que.push (proot->m_pleft);            if (proot->m_pright! = NULL)                que.push (proot->m_pright);}    }    return;}


Binary tree (one)----to find the image of binary tree, recursive and non-recursive way

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.