二叉树中序遍历线索完整实现

来源:互联网 发布:网络招聘时间安排 编辑:程序博客网 时间:2024/05/22 12:09
#ifndef RTAGTREE_H_INCLUDED
#define RTAGTREE_H_INCLUDED
typedef enum {Link,Thread}Tag; ///Link==0,Thread==1;
typedef char DataType;
typedef struct Node
{
    Tag Ltag,Rtag;        ///线索标致
    Node *lch,*rch;///二叉树的左右子树
    DataType data;        ///数据域
}Node,*BitreeNode;


void CreateTree(BitreeNode &bt);///前序构造二叉树


void PrintTree(BitreeNode bt,int depth);///输出一个二叉树


void PrintTree(BitreeNode bt);///输出一个二叉树(封装上面的输出函数)


void InThread(BitreeNode bt,BitreeNode &pre);///中序遍历线索二叉树


void CreateInThread(BitreeNode bt,BitreeNode &thrt);///中序建立线索二叉树


void PrintInThread(BitreeNode thrt);///输出中序线索遍历的二叉树




#endif // RTAGTREE_H_INCLUDED

#include<iostream>
#include<string>
#include<stdio.h>
//#include<windows.h>
#include<iomanip>
#include"Rtagtree.h"
using namespace std;


void CreateTree(BitreeNode &bt)///前序构造二叉树(前序)
{
   char ch;
   cin>>ch;
   if(ch=='#')///构造空树
   {
       bt=NULL;
       return;
   }
   bt=new Node;
   bt->data=ch;
   CreateTree( bt->lch);///递归遍历左子树
   CreateTree( bt->rch);///递归遍历右子树
}


const int row=5;///表示行的空格
const int col=35;///表示行的——这个对齐符号
///depth表示该子树当前的层次
void PrintTree(BitreeNode bt,int depth)///输出一个二叉树
{
    if(bt==NULL)///递归的出口
      return;
      PrintTree(bt->rch,depth+1);///遍历右子树
      int k;
      for(k=1;k<depth*row;k++)///对齐先输出空格
        cout<<" ";
        cout<<bt->data;
      for(int i=k;i<col;i++)///输出“-”
        cout<<"-";
        cout<<endl;///这里需要画行
      PrintTree(bt->lch,depth+1);///遍历左子树
}


void PrintTree(BitreeNode bt)///输出一个二叉树(封装上面的输出函数)
{
   PrintTree(bt,1);
   cout<<endl<<endl;
}


void InThread(BitreeNode bt,BitreeNode &pre)///中序遍历线索二叉树(进行中序线索化)
{
    if(bt)///如果根不为空,则进行线索化
    {
      InThread(bt->lch,pre);///对左子树进行线索化
      if(bt->lch==NULL)///如果遍历的结点为左子树的最左结点,则进行线索化
      {
          bt->Ltag=Thread;
          bt->lch=pre;///指向前驱
      }///建立前驱


      else
      bt->Ltag=Link;


      if(pre!=NULL&&pre->rch==NULL)///建立前驱的后继
      {
          pre->Rtag=Thread;
          pre->rch=bt;
      }


      else
      bt->Rtag=Link;
      pre=bt;///否则保持pre指向bt的前驱
      InThread(bt->rch,pre);
    }
}


void CreateInThread(BitreeNode bt,BitreeNode &thrt)///中序建立线索二叉树,thrt为头指针
{
     BitreeNode pre;
     thrt->Ltag=Link;///有左孩子
     thrt->Rtag=Thread;
     thrt->rch=thrt;


     if(!bt)///如果树为空
     thrt->lch=thrt;///左指针往回指


     else
     {
        thrt->lch=bt;///头结点左孩子指向根
        pre=thrt;
        InThread(bt,pre);///进行线索化
        pre->rch=thrt;///处理最后一个左结点
        pre->Rtag=Thread;
        thrt->rch=pre;///建立非空树的头结点的右线索
     }
}




void PrintInThread(BitreeNode thrt)///输出中序线索遍历的二叉树,bt为根结点,thrt为头结点
{
   BitreeNode bt;
   bt=thrt->lch;
   while(bt!=thrt)///不是最后一个结点
   {
    while(bt->Ltag==Link)///去寻找左子树的最左的左孩子时
    bt=bt->lch;
    cout<<bt->data<<setw(3);
    while(bt->Rtag==Thread&&bt->rch!=thrt)
    {
      bt=bt->rch;
      cout<<bt->data<<setw(3);
    }
    bt=bt->rch;
   }
}

#include <iostream>
#include<string>
#include<stdio.h>
#include<iomanip>
#include"Rtagtree.h"
using namespace std;


int main()
{
    freopen("data.in","r",stdin);
    BitreeNode bt,thrt;
    CreateTree(bt);///前序构造二叉树
    PrintTree(bt);
    cout<<"输出中序线索遍历的二叉树如下:"<<setw(4);
    CreateInThread(bt,thrt);///中序建立线索二叉树
    PrintInThread(thrt);///输出中序线索遍历的二叉树
    return 0;
}




原创粉丝点击