#include<iostream>
using namespace std;
#define MAX 1500
//二叉树定义
typedef struct BiTreeNode{
char data;
BiTreeNode *lChild;
BiTreeNode *rChild;
int lTag,rTag;
}BiTreeNode;
//全局变量
BiTreeNode *pre =NULL;
BiTreeNode *point[MAX+1]; //构建二叉树时辅助,用于定位子节点
//构建二叉树
BiTreeNode *CreateBiTree()
{
BiTreeNode *root=(BiTreeNode *)malloc(sizeof(BiTreeNode));
int i,j;char data;
cin>>i>>data;
while(i!=0&&data!='#')
{
BiTreeNode *newNode=(BiTreeNode *)malloc(sizeof(BiTreeNode));
newNode->data=data;
newNode->lTag=0;newNode->rTag=0;
newNode->lChild=NULL;newNode->rChild=NULL;
point[i]=newNode;
if(i==1) //为二叉树根节点
{
root=point[1];
}
else
{
j=i/2;
if(i%2==0) //为左孩子结点
{
point[j]->lChild=newNode;
}
else
{
point[j]->rChild=newNode;
}
}
cin>>i>>data;
}
return root;
}
//中序线索化二叉树,pre初始化为NULL
void Inthread(BiTreeNode *root)
{
if(root!=NULL)
{
Inthread(root->lChild);
if(root->lChild==NULL)
{
root->lTag=1;root->lChild=pre;
}
if(pre!=NULL&&pre->rChild==NULL)
{
pre->rTag=1;pre->rChild=root;
}
pre=root;
Inthread(root->rChild);
}
}
//查找结点p的前驱结点
BiTreeNode *InPre(BiTreeNode *p)
{
if(p->lTag==1)return p->lChild;
BiTreeNode *q=p->lChild;
if(q==NULL){
cout<<"无前驱结点\n";
return NULL;
}
while(q->rTag!=1)
{
q=q->rChild;
}
return q;
}
//查找结点p的后继结点
BiTreeNode *InSub(BiTreeNode *p)
{
if(p->rTag==1)return p->rChild;
BiTreeNode *q=p->rChild;
if(q==NULL){
cout<<"无后继结点\n";
return NULL;
}
while(q->lTag!=1)
{
q=q->lChild;
}
return q;
}
int main()
{
char cmd;int index;
do{
BiTreeNode *root=CreateBiTree();
Inthread(root);
cout<<"输入您要查找的结点,序号index\n";
cin>>index;
BiTreeNode *nodePre=InPre(point[index]);
BiTreeNode *nodeSub=InSub(point[index]);
if(nodePre!=NULL)
cout<<"结点"<<index<<"的前驱结点的值为"<<nodePre->data<<"\n";
if(nodeSub!=NULL)
cout<<"结点"<<index<<"的后继结点的值为"<<nodeSub->data<<"\n";
cout<<"继续吗Y/y?\n";
cin>>cmd;
}while(cmd=='Y'||cmd=='y');
return 0;
}
二叉树中序线索化及查找某一结点的前驱,后继结点
最新推荐文章于 2023-05-31 20:53:29 发布
本文介绍了一种使用C++实现二叉树线索化的方法,并提供了查找指定节点的前驱和后继节点的功能。通过输入节点的序号,程序能够输出该节点的前驱和后继节点的值。
5453

被折叠的 条评论
为什么被折叠?



