#include <stdio.h>
#include <stdlib.h>
#include <math.h>
typedef struct Node
{
char value;
struct Node *leftChild;
struct Node *rightChild;
struct Node *parent;
int depth;
}TreeNode;
//根据中序和前序序列构造二叉树
TreeNode *CreateFromMidPre(char mid[],int midLeft,int midRight,char pre[],int preLeft,int preRight,TreeNode *parent)
{
TreeNode *node = (TreeNode*)malloc(sizeof(TreeNode));
int midLeft1,midRight1,preLeft1,preRight1,rootPos=0;
if(midRight-midLeft != preRight-preLeft)
{
printf("错误:输入的中序和前序序列结点个数不一样!\n");
return node;
}
node->value = pre[preLeft];
node->leftChild = NULL;
node->rightChild = NULL;
node->parent=parent;
if(parent==NULL)
node->depth=0;
else
node->depth=parent->depth+1;
for(;mid[rootPos]!=pre[preLeft];rootPos++);
if(rootPos!=midLeft)
{
midRight1 = rootPos-1;
preRight1 = rootPos-midLeft+preLeft;
node->leftChild = CreateFromMidPre(mid,midLeft,midRight1,pre,preLeft+1,preRight1,node);
}
if(rootPos!=midRight)
{
midLeft1 = rootPos+1;
preLeft1 = rootPos-midLeft+preLeft+1;
node->rightChild = CreateFromMidPre(mid,midLeft1,midRight,pre,preLeft1,preRight,node);
}
return node;
}
{
if(root==NULL)
return NULL;
if(root->value==data)
return root;
else if(Find(root->leftChild,data)!=NULL)
return Find(root->leftChild,data);
else if(Find(root->rightChild,data)!=NULL)
return Find(root->rightChild,data);
else
return NULL;
}
{
TreeNode *p1,*p2;
int differ,i;
p1=Find(root,data1);
p2=Find(root,data2);
if(p1==NULL||p2==NULL)
{
printf("输入的节点不都在树中\n");
return;
}
if(p1->depth>p2->depth)
{
differ = p1->depth-p2->depth;
for(i=0;i<differ;i++)
{
p1=p1->parent;
}
}
else if(p1->depth<p2->depth)
{
differ = p2->depth-p1->depth;
for(i=0;i<differ;i++)
{
p2=p2->parent;
}
}
while(p1!=p2)
{
p1=p1->parent;
p2=p2->parent;
}
printf("最近公共节点:%c\n",p1->value);
}
int main()
{
TreeNode *root1;
char preSeq[20],midSeq[20],data1,data2;
int i=0,numNode;
printf("请输入节点总数:\n");
scanf("%d",&numNode);
fflush(stdin);
printf("请输入先序序列:\n");
for(i=0;i<numNode;i++)
scanf("%c",preSeq+i);
fflush(stdin);
printf("请输入中序序列:\n");
for(i=0;i<numNode;i++)
scanf("%c",midSeq+i);
fflush(stdin);
root1=CreateFromMidPre(midSeq,0,i-1,preSeq,0,i-1,NULL);
while(1)
{
printf("请输入要找的两个节点:\n");
scanf("%c%c",&data1,&data2);
fflush(stdin);
SamePreNode(root1,data1,data2);
if(data1==0)
break;
}
return 1;
}
#include <stdlib.h>
#include <math.h>
typedef struct Node
{
char value;
struct Node *leftChild;
struct Node *rightChild;
struct Node *parent;
int depth;
}TreeNode;
//根据中序和前序序列构造二叉树
TreeNode *CreateFromMidPre(char mid[],int midLeft,int midRight,char pre[],int preLeft,int preRight,TreeNode *parent)
{
TreeNode *node = (TreeNode*)malloc(sizeof(TreeNode));
int midLeft1,midRight1,preLeft1,preRight1,rootPos=0;
if(midRight-midLeft != preRight-preLeft)
{
printf("错误:输入的中序和前序序列结点个数不一样!\n");
return node;
}
node->value = pre[preLeft];
node->leftChild = NULL;
node->rightChild = NULL;
node->parent=parent;
if(parent==NULL)
node->depth=0;
else
node->depth=parent->depth+1;
for(;mid[rootPos]!=pre[preLeft];rootPos++);
if(rootPos!=midLeft)
{
midRight1 = rootPos-1;
preRight1 = rootPos-midLeft+preLeft;
node->leftChild = CreateFromMidPre(mid,midLeft,midRight1,pre,preLeft+1,preRight1,node);
}
if(rootPos!=midRight)
{
midLeft1 = rootPos+1;
preLeft1 = rootPos-midLeft+preLeft+1;
node->rightChild = CreateFromMidPre(mid,midLeft1,midRight,pre,preLeft1,preRight,node);
}
return node;
}
//给出节点的值,得到指向节点的指针
{
if(root==NULL)
return NULL;
if(root->value==data)
return root;
else if(Find(root->leftChild,data)!=NULL)
return Find(root->leftChild,data);
else if(Find(root->rightChild,data)!=NULL)
return Find(root->rightChild,data);
else
return NULL;
}
//主要思想是先得到的两个节点的深度,较深节点指针的循环指向父节点,直到同两个节点同层,
//然后两个指针同时循环指向父节点,知道两个指针碰头
{
TreeNode *p1,*p2;
int differ,i;
p1=Find(root,data1);
p2=Find(root,data2);
if(p1==NULL||p2==NULL)
{
printf("输入的节点不都在树中\n");
return;
}
if(p1->depth>p2->depth)
{
differ = p1->depth-p2->depth;
for(i=0;i<differ;i++)
{
p1=p1->parent;
}
}
else if(p1->depth<p2->depth)
{
differ = p2->depth-p1->depth;
for(i=0;i<differ;i++)
{
p2=p2->parent;
}
}
while(p1!=p2)
{
p1=p1->parent;
p2=p2->parent;
}
printf("最近公共节点:%c\n",p1->value);
}
int main()
{
TreeNode *root1;
char preSeq[20],midSeq[20],data1,data2;
int i=0,numNode;
printf("请输入节点总数:\n");
scanf("%d",&numNode);
fflush(stdin);
printf("请输入先序序列:\n");
for(i=0;i<numNode;i++)
scanf("%c",preSeq+i);
fflush(stdin);
printf("请输入中序序列:\n");
for(i=0;i<numNode;i++)
scanf("%c",midSeq+i);
fflush(stdin);
root1=CreateFromMidPre(midSeq,0,i-1,preSeq,0,i-1,NULL);
while(1)
{
printf("请输入要找的两个节点:\n");
scanf("%c%c",&data1,&data2);
fflush(stdin);
SamePreNode(root1,data1,data2);
if(data1==0)
break;
}
return 1;
}