Binary Tree--find the nearest ancestor of two random nodes

Source: Internet
Author: User

Very practical too long not a binary tree, because need to use recently, we feel very necessary to consolidate knowledge. The middle involves a random algorithm that is looking for the direct ancestor of two nodes.

I remember and calculus through, put forward the following method, there are many other ways on the net to achieve, once again only for their own hours of work to record and accumulate it!

The program is written in C, and the assumption is that it will be more convenient to implement it in C #.

The first is the data structure definition:

typedef char TELEMTYPE;TYPEDEF bool status;typedef struct Bitnode{telemtype data;struct bitnode * lchild, * rchild;} Bitnode, * bitree;

The second is achievement. The definition of a tree is established by the recursive sequence of sequential sequences.

Bitnode * Createbitree () {char ch;    Bitnode * T;    scanf ("%c", &ch);    if (ch== ' # ') T = 0;    else{        T = (bitree) malloc (sizeof (Bitnode));        T->data = ch;        T->lchild = Createbitree ();        T->rchild = Createbitree ();    }    return T;}

The delimiter for empty nodes is used for "#" and can be substituted with other characters.

The basic algorithm for finding recent ancestors is recursion. For each node, first infer whether there is a direct association. Do not have their own direct parent nodes, and recursive calls require a depth of two nodes to infer which parent node to use with the next call.

Detailed implementations such as the following:

Find recent common ancestor nodes for two nodes Bitnode * Findnearestancestor (Bitnode * root, bitnode* p1, bitnode* p2, int h1, int h2) {if (!p1 | |!p2) Return 0;if (P1 = = p2) {if (P1 = = root) return root;return p1;} if (p1 = = P2->lchild | | p1 = = p2->rchild) return p2;if (P2 = P1->lchild | | p2 = p1->rchild) return p1;if (H1 = = H2) return Findnearestancestor (Root, GetParent (root, p1), GetParent (Root, p2), h1-1, h2-1); Elsereturn findnearestances Tor (Root, H1 > h2?) GetParent (Root, p1): P1,h1 < H2?

GetParent (Root, p2): p2,h1 > H2?

H1-1: H1, H1 < H2? h2-1: H2);}


The GetParent is the direct parent node of the P node in the tree rooted in root, defined as the following:

Bitnode * GetParent (bitnode* root, Bitnode * p) {if (!root | | p = = root) return 0;if (p = = Root->lchild | | p = = Root->r Child) {return root;} Else{return GetParent (Root->lchild, p) = = 0?

GetParent (Root->rchild, p): GetParent (Root->lchild, p);}}

In the main function, call for example the following:

int main () {//Test sequence: abc## #de # #fg # # printf ("Please enter the pre-order sequence. Empty nodes replaced with ' # ': \ n '); Bitree tree = Createbitree ();        Bitnode * node = findnearestancestor (   tree,       tree->rchild->lchild,               tree->rchild->rchild- >lchild,      getheight (tree,tree->rchild->lchild),      getheight (tree,tree->rchild->rchild-> lchild));p rintf (the recent parent of node%c and node%c is:%c\n), tree->rchild->lchild->data,tree->rchild->rchild-> Lchild->data,node->data); return 0;}

the GetHeight function is used above. Used to get the height of the node p in a given tree, the implementation of this function takes a lot of time. The main is to get the height of the tree, very little to get the height of the specified node, in fact, such as the following:

Finds the height of the node p. Note with only the height of the tree alone is different int getheight (bitnode* root, Bitnode * p, int h = 1) {if (!root) return 0;if (p = = Root->lchild | | p = = Root->rchild) return H + 1;return  getheight (Root->lchild, p, h+1) = = 0? GetHeight (Root->rchild, P, h+1): GetHeight (Root->lchild, p, h+1);}

The first sequence of tests used is listed as

abc## #de # #fg # # #

The corresponding two-fork tree is as follows:

A

/      \

b d
/           /    \

C E F

/

G

The results are as follows:


Only this record. Now, after forgetting. Also want to share with you.





Binary Tree--find the nearest ancestor of two random nodes

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.