满二叉排序树任意三个节点最近公共父节点

#include <iostream>

using namespace std;
int tree[1024*1024];
int k;
int a,b,c;
int power2(int n)
{
    int i=1;
    int j;
    for(j=0; j<n; j++)
        i=i*2;
    return i;
}
int get_xiabiao(int n)
{
    int i,j;
    int xiabiao=-1;
    for(i=0; i<power2(k)-1; i++)
    {
        if(tree[i]==n)
        {
            xiabiao=i;
            break;

        }
    }
    return xiabiao;

}
int get_shendu(int n)
{
    int i=0;
    for(i=0; n; i++)
    {
        n=(n-1)/2;
    }
    return i;

}
int main()
{
    cin>>k>>a>>b>>c;
    int i,j;
    int m;
    for(i=0; i<k; i++)
    {
        int start=power2(k-1-i);
        for(j=power2(i)-1,m=0; j<power2(i+1)-1; j++,m++)
        {
            tree[j]=start+start*2*m;
        }
    }

    int a_xiabiao=get_xiabiao(a);
    int b_xiabiao=get_xiabiao(b);
    int c_xiabiao=get_xiabiao(c);
    int a_shendu=get_shendu(a_xiabiao);
    int b_shendu=get_shendu(b_xiabiao);
    int c_shendu=get_shendu(c_xiabiao);


    int Min=1<<30;
    if(Min>a_shendu)Min=a_shendu;
    if(Min>b_shendu)Min=b_shendu;
    if(Min>c_shendu)Min=c_shendu;

    for(i=0; i<a_shendu-Min; i++)
        a_xiabiao=(a_xiabiao-1)/2;
    for(i=0; i<b_shendu-Min; i++)
        b_xiabiao=(b_xiabiao-1)/2;
    for(i=0; i<c_shendu-Min; i++)
        c_xiabiao=(c_xiabiao-1)/2;

    for(i=0; i<Min; i++)
    {
        if(a_xiabiao==b_xiabiao&&b_xiabiao==c_xiabiao)
        {
            break;
        }
        else
        {
            a_xiabiao=(a_xiabiao-1)/2;
            b_xiabiao=(b_xiabiao-1)/2;
            c_xiabiao=(c_xiabiao-1)/2;
        }
    }
    cout<<tree[a_xiabiao]<<endl;
    return 0;
}

#include <iostream>

using namespace std;
int k,a,b,c;
struct tree
{
    int data;
    tree *left;
    tree *right;
};
int power2(int n)
{
    int i=1;
    for(int j=0; j<n; j++)
        i=i*2;
    return i;

}
void create(tree *root,int n)
{
    if(n==k)
    {
        root->data=power2(n-1);
    }
    if(n==k-2)
    {
        tree *tree_left=new tree;
        tree *tree_right=new tree;

        root->left=tree_left;
        root->right=tree_right;

        tree_left->data=root->data-power2(n-2);
        tree_right->data=root->data+power2(n-2);

        return ;
    }
    tree *tree_left=new tree;
    tree *tree_right=new tree;
    root->left=tree_left;
    root->right=tree_right;

    tree_left->data=root->data-power2(n-2);
    create(root->left,n-1);
    tree_right->data=root->data+power2(n-2);
    create(root->right,n-1);
    return ;
}
tree *FindParent2(tree*root,tree*a,tree*b)
{
  if(root==NULL)
  return NULL;
  if(root==a||root==b)
  return root;
  tree*left=FindParent2(root->left,a,b);
  tree*right=FindParent2(root->right,a,b);
  if(left&&right)
  return root;
  return left?left:right;
}
tree *FindParent(tree*root,tree*a,tree*b)
{
    int min,max;
    if(a->data<b->data)
    {
        min=a->data,max=b->data;
    }
    else
    {
        min=b->data,max=a->data;
    }
    while(root)
    {
        if(root->data>=min&&root->data<=max)
            return root;
        else    if(root->data<min&&root->data<max)
        {
            root=root->right;
        }
        else root=root->left;

    }
    return NULL;

}
tree *Find(tree*root,int n)
{
    if(root->data==n)
    {
      return root;

    }
    else if(root->data<n)
    {
        return Find(root->right,n);
    }
    return Find(root->left,n);
}
int main()
{
    cin>>k>>a>>b>>c;
    tree *root=new tree;
    create(root,k);

    tree *atree=new tree;
    tree *btree=new tree;
    tree *ctree=new tree;
    atree=Find(root,a);
    btree=Find(root,b);
    ctree=Find(root,c);
    tree *abparent=new tree;
    abparent=FindParent(root,atree,btree);

    tree *abcparent=new tree;
    abcparent=FindParent(root,abparent,ctree);
    cout<<abcparent->data<<endl;

/*
    tree *abparent2=new tree;
    abparent2=FindParent2(root,atree,btree);

    tree *abcparent2=new tree;
    abcparent2=FindParent2(root,abparent2,ctree);
    cout<<abcparent2->data<<endl;
*/
    return 0;
}

    原文作者:满二叉树
    原文地址: https://blog.csdn.net/u012067392/article/details/73467499
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞