Binary search tree (6)-Recent public ancestor LCA

Source: Internet
Author: User

given the two nodes in a binary lookup tree, find their nearest public ancestor (Lowest Common Ancestor-lca).
before detailed introduction, you can refer to the following article "Binary tree (70)-Recent public ancestor [1]"

The function prototype is defined as follows:

    Node *getlca (node* root, int n1, int n2)    where N1 and N2 are the specified two node values.

For example, in the above BST, the LCA of 10 and 14 is 12, while the 8 and 14 LCA are 8.

The following is a definition of LCA from Wikipedia:
Suppose there is a binary tree T, where the minimum common ancestor of nodes N1 and N2 is defined as the smallest node in T, which contains N1 and N2 as descendants (while a node itself can be a descendant of itself).

The LCA of nodes N1 and N2 is a common ancestor, farthest from the root root node. It is useful for calculations of LCA, for example, to calculate the distance between a pair of nodes in a tree: the distance from N1 to N2 is N1 to root, plus the distance of N2 root, minus their LCA to root distance of twice times.


workaround:

C + + program, to find the BST two nodes lca#include <iostream>struct node{int key;     Node *left; Node *right;};/ /Seek N1 and N2 's LCA. Assume that both N1 and N2 are in BST.     Node *getlca (node* root, int n1, int n2) {if (root = null) return null; If N1 and N2 are less than root, then LCA is located in record if (Root->key > N1 && root->key > N2) return Getlca (root->     Left, N1, N2); If N1 and N2 are larger than root, the LCA is located in the right subtree if (Root->key < n1 && Root->key < N2) return Getlca (root->     Right, N1, N2); return root;}     Create a new BST node *createnewnode (int item) {Node *temp = new node;     Temp->key = Item;     Temp->left = Temp->right = NULL; return temp;} int main () {/* 20/8 \ \ 4 12/10 */Node *root = CRE     Atenewnode (20);     Root->left = Createnewnode (8);     Root->right = Createnewnode (22);     Root->left->left = Createnewnode (4);     Root->left->right = Createnewnode (12); root-&Gt;left->right->left = Createnewnode (10);     Root->left->right->right = Createnewnode (14);     int N1 = ten, N2 = 14;     Node *t = Getlca (Root, N1, N2);     printf ("LCA of%d and%d is%d \ n", N1, N2, T->key);     N1 = +, N2 = 8;     T = Getlca (root, N1, N2);     printf ("LCA of%d and%d is%d \ n", N1, N2, T->key);     N1 = ten, n2 = 22;     T = Getlca (root, N1, N2);     printf ("LCA of%d and%d is%d \ n", N1, N2, T->key); return 0;}
Output:
LCA of ten and 12
LCA of and 8 is 8
LCA of Ten and 20

Time complexity: O (h), where H is the height of the tree.

In addition, the above program requires additional memory for recursive function call stacks. You can use the following traversal method to avoid additional memory space.
Find nodes N1 and N2 Lcanode *getlca (node* root, int n1, int n2) {while    (root! = NULL)    {         //If N1 and N2 are less than root, then LCA is located in the left subtree C4/>if (Root->data > N1 && root->data > N2)           root = root->left;        If N1 and N2 are greater than root, then LCA is in the right subtree        else if (Root->data < n1 && Root->data < n2)           root = Root->righ t;         else break;    }    return root;}

Binary search tree (6)-Recent public ancestor LCA

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.