Binary tree (two)----Find the lowest ancestor node (or the nearest public parent node, etc.) for a node, 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. Find the lowest ancestor node (or the nearest public parent node) of the two nodes in a binary tree

The lowest ancestor node is the last same node that is traversed from the root node to the given node

For example:

A

B C

                                             d    & nbsp                   e       & nbsp;             F                                G

H I J K L M N O

For example, the lowest ancestor node for H and J is B.

Because the link from the root node A to H is: a B D H

Links from the root node A to J are: a B E J

To view the link nodes, B is the last of the same nodes, the so-called nearest public parent node or the lowest ancestor node.


(1) Recursive method

If the given proot is null, which is an empty tree, the returned public node is naturally null;

If the given Proot is the same as any of the two nodes, it indicates that Proot is one of the two nodes to be searched, then returns proot directly, indicating that at least one node is found in the current link;

If the given proot is not any of the two nodes, then it is necessary to re-look in the left and right sub-tree of Proot, there are three cases: two nodes are on the left subtree, two nodes are on the right subtree, one in the left subtree and one in the subtree; specifically:

Case one: If the left dial hand tree finds the public node is null, it indicates that from the Zogen node to all the leaf nodes of the left subtree, no one of the two nodes is found, which means that the two nodes are not on the left subtree, not the left subtree, then must be on the right subtree;

Scenario Two: If the right subtree finds a public node that is null, indicating that no node can be found in the right subtree, then two nodes must be on the left subtree;

Case three: If the left and right sub-tree lookup of the public node is not NULL, indicating that the left and right subtree contains one node, the current node proot is the lowest public node, return to it.

Three cases are mutually exclusive and can only be one of them.

btreenode_t *getlastcommonparent (btreenode_t *proot, btreenode_t *pnode1, btreenode_t *pNode2) {    if (pRoot = = NULL)/ /description is an empty tree, no lookup, no corresponding node is found, return null return  null;    if (proot = = PNode1 | | proot = = PNODE2)//Description Find one of the two nodes on the root node of the current subtree        return proot;    btreenode_t   *pleft = getlastcommonparent (Proot->m_pleft, PNode1, pNode2);  Find two nodes in the left dial hand tree and return find results    btreenode_t   *pright = getlastcommonparent (Proot->m_pright, PNode1, pNode2);// Find two nodes in the right subtree and return the Find results    if (pleft = = NULL)//If not found in the left subtree, it is concluded that two nodes are in the right subtree and can return the query results in the right subtree;        Pright;    if (pright = = NULL)//If not found in the right subtree, it is determined that two nodes are in the left subtree, you can return the query results in the left subtree, otherwise, it is necessary to combine the results of the left subtree query to determine the        return pleft;        Return proot;//If one of the two nodes is found in the left and right subtree, the proot is the lowest common ancestor node and returns. }


(2) Non-recursive mode:




























Binary tree (two)----Find the lowest ancestor node (or the nearest public parent node, etc.) for a node, 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.