Binary tree (x)----compare two binary trees (structure and data), recursive and non-recursive

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, compare two binary tree structure is the same, do not involve the stored data

(1) Recursive method

If two of the two-tree proot are empty trees, the same is true;

If two two-tree proot one is an empty tree, the other is not an empty tree, it is not the same, and returns false;

If two of the two-fork trees are not empty, then you need to compare the left and right subtrees separately, and then, judging by the comparison, return False if one is false.


BOOL  btreecompare (btreenode_t *proot1, btreenode_t *proot2) {    //If both are empty trees, then the same    if (pRoot1 = = NULL && PRoot2 = = NULL)        return true;    If one is empty, one is not empty, then the    if (pRoot1! = null && PRoot2 = = null)  | |        (PROOT1 = = NULL && PRoot2! = null))        return false;    If all is not empty, then you need to compare the left and right subtree, then according to the comparison of the results of the predicate    bool  leftcmp = Btreecompare (Proot1->m_pleft, proot2->m_pleft);    BOOL  rightcmp = Btreecompare (Proot1->m_pright, proot2->m_pright);    Return (leftcmp && rightcmp);}

(2) Non-recursive mode

With queue implementation

Implementation algorithm:

First, the given root node pRoot1 and PRoot2 are enqueued.

The first step: When two queues are not empty, the total number of nodes in the current hierarchy of two trees (that is, the number of nodes in the current queue), the first comparison of the number of nodes is the same, if not the same, two trees naturally different, if the number of nodes is the same, the team needs to be compared. If there is a queue that is not empty, the comparison exits.

Step Two: If there is a queue that is not empty, empty the queue and return the difference.


BOOL Btreecompare (btreenode_t *proot1, btreenode_t *proot2) {if (pRoot1 = = NULL && PROOT2 = NULL) ret    Urn false;    Queue <btreenode_t *> que1;    Queue <btreenode_t *> que2;    Que1.push (PROOT1);    Que2.push (PROOT2);    int curLevelNodeTotal1 = 0;    int curLevelNodeTotal2 = 0; BOOL flag = TRUE;  Step out of identity as a comparison inconsistency while ((!que1.empty ()) && (!que2.empty ()))//When two queues are not empty, compare {CurLevelNodeTotal1 =  Que1.size (); Gets the total number of current layer nodes in tree 1 curLevelNodeTotal2 = Que2.size ();            Gets the total number of current layer nodes in Tree 2 if (curLevelNodeTotal1! = curLevelNodeTotal2) {flag = false;//The total number of nodes in the current layer is inconsistent and does not need to be compared, jump directly        Break        } int cnt1 = 0;//the counter int cnt2 = 0 when traversing this layer node;            while (Cnt1 < CurLevelNodeTotal1 && Cnt2 < curLevelNodeTotal2) {++cnt1;            ++cnt2;            PROOT1 = Que1.front ();            Que1.pop ();            PRoot2 = Que2.front ();            Que2.pop (); Judging the structure of the left and right nodes of PROOT1 and PRoot2Whether the same if ((proot1->m_pleft! = null && Proot2->m_pleft = = null) | |                (Proot1->m_pleft = = NULL && proot2->m_pleft! = null) | |                (Proot1->m_pright! = null && Proot2->m_pright = = null) | | (Proot1->m_pright = = NULL && proot2->m_pright! = null))                {flag = false;            Break            }//Will enqueue the left and right nodes if (proot1->m_pleft! = NULL) Que1.push (proot1->m_pleft);            if (proot1->m_pright! = NULL) Que1.push (proot1->m_pright);            if (proot2->m_pleft! = NULL) Que2.push (proot2->m_pleft);        if (proot2->m_pright! = NULL) Que2.push (proot2->m_pright);    } if (flag = = false) break;        }//If the comparison flag is false, then the IF (flag = = False) {while (!que1.empty ()) Que1.pop (); WhiLe (!que2.empty ()) Que2.pop ();    return false; } return true;


3, compare the two binary tree structure and data is the same, that is, two identical trees

The difference from the above is that after the comparison structure is the same, you need to compare the data of the current node to be consistent.

The algorithm is consistent and requires only one line of code to be added.


(1) Recursive mode:

BOOL  btreecompare (btreenode_t *proot1, btreenode_t *proot2) {    //If both are empty trees, then the same    if (pRoot1 = = NULL && PRoot2 = = NULL)        return true;    If one is empty, one is not empty, then the    if (pRoot1! = null && PRoot2 = = null)  | |        (PROOT1 = = NULL && PRoot2! = null))        return false;    Compares the data in the current node    if (proot1->m_pelemt! = proot2->m_pelemt)        return false;    If all is not empty, then you need to compare the left and right subtree, then according to the comparison of the results of the predicate    bool  leftcmp = Btreecompare (Proot1->m_pleft, proot2->m_pleft);    BOOL  rightcmp = Btreecompare (Proot1->m_pright, proot2->m_pright);    Return (leftcmp && rightcmp);}


(2) Non-recursive mode

BOOL Btreecompare (btreenode_t *proot1, btreenode_t *proot2) {if (pRoot1 = = NULL && PROOT2 = NULL) ret    Urn false;    Queue <btreenode_t *> que1;    Queue <btreenode_t *> que2;    Que1.push (PROOT1);    Que2.push (PROOT2);    int curLevelNodeTotal1 = 0;    int curLevelNodeTotal2 = 0; BOOL flag = TRUE;  Step out of identity as a comparison inconsistency while ((!que1.empty ()) && (!que2.empty ()))//When two queues are not empty, compare {CurLevelNodeTotal1 =  Que1.size (); Gets the total number of current layer nodes in tree 1 curLevelNodeTotal2 = Que2.size ();            Gets the total number of current layer nodes in Tree 2 if (curLevelNodeTotal1! = curLevelNodeTotal2) {flag = false;//The total number of nodes in the current layer is inconsistent and does not need to be compared, jump directly        Break        } int cnt1 = 0;//the counter int cnt2 = 0 when traversing this layer node;            while (Cnt1 < CurLevelNodeTotal1 && Cnt2 < curLevelNodeTotal2) {++cnt1;            ++cnt2;            PROOT1 = Que1.front ();            Que1.pop ();            PRoot2 = Que2.front ();            Que2.pop ();        Compare data consistency in the current node    if (proot1->m_pelemt! = proot2->m_pelemt) {flag = false;            Break  }//Determine if PROOT1 and PRoot2 are identical if the structure is the same if (proot1->m_pleft! = null && Proot2->m_pleft = = NULL                )    ||                (Proot1->m_pleft = = NULL && proot2->m_pleft! = null) | |                (Proot1->m_pright! = null && Proot2->m_pright = = null) | | (Proot1->m_pright = = NULL && proot2->m_pright! = null))                {flag = false;            Break            }//Will enqueue the left and right nodes if (proot1->m_pleft! = NULL) Que1.push (proot1->m_pleft);            if (proot1->m_pright! = NULL) Que1.push (proot1->m_pright);            if (proot2->m_pleft! = NULL) Que2.push (proot2->m_pleft);        if (proot2->m_pright! = NULL) Que2.push (proot2->m_pright); } IF (flag = = false) break;        }//If the comparison flag is false, then the IF (flag = = False) {while (!que1.empty ()) Que1.pop ();        while (!que2.empty ()) Que2.pop ();    return false; } return true;




Binary tree (x)----compare two binary trees (structure and data), recursive and non-recursive

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.