线索二叉树中插入结点

#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;
   cout<<"输入结点索引值i,和结点值data,以0 #结束\n";
   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==0)
  {
    q=q->lChild;
  }
  return q;
}

//插入结点到线索二叉树
bool InsertNode(BiTreeNode *p,int index,char method)
{
  if(method!='l'&&method!='L'&&method!='r'&&method!='R')
  {
     cout<<"命令有误,插入方式为l,L,r,R.\n";
     return false;
  }
  if(point[index]==NULL)     //不存在该结点
  {   
     cout<<"插入的位置无效\n";
     return false;
  }
  if(method=='l'||method=='L')   //插入为poinit[index]所指向结点的左孩子结点O(∩_∩)O~有点拗口
 {
     if(point[index]->lTag==1)   //插入位置的结点无左孩子结点
    {
      p->lChild=point[index]->lChild;
      point[index]->lChild=p;
      p->lTag=1;p->rTag=1;
      point[index]->lTag=0;
     }
     else        //有左孩子结点,需找到point[index]左子树最右下角的结点
     {
       BiTreeNode *s=point[index]->lChild;
       while(s->rTag==0)s=s->rChild;
       p->lChild=point[index]->lChild;
       point[index]->lChild=p;
       s->rChild=p;
       p->lTag=0;p->rTag=1;
     }
     point[index*2]=p;
  }
  if(method=='r'||method=='R')
  {
      if(point[index]->rTag==1)   //无右孩子结点
     {
        p->rChild=point[index]->rChild;
        point[index]->rChild=p;
        p->lChild=point[index];
        point[index]->rTag=0;
        p->rTag=1;p->lTag=1;
      }
     else        //存在有孩子结点,需找到右子树最左下角的结点
     {
        BiTreeNode *s=point[index]->rChild;
        while(s->lChild==0)s=s->lChild;
        p->rChild=point[index]->rChild;
        point[index]->rChild=p;
        p->lChild=point[index];
        p->rTag=0;p->lTag=1;
        s->lChild=p;
     }
     point[index*2+1]=p;
  }
  return true;
}


int main()
{
  char cmd,data;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<<"\n"<<"输入插入结点的值,及插入的位置,插入的方式(l,r or L,R)\n";
     cin>>data>>index>>cmd;
     BiTreeNode *p=(BiTreeNode *)malloc(sizeof(BiTreeNode));
     p->data=data;
     InsertNode(p,index,cmd);
     cout<<"继续吗Y/y?\n";
     cin>>cmd;
  }while(cmd=='Y'||cmd=='y');
  return 0;
}
 
  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值