交换左右子树

交换左右子树

//算法5.5 计算二叉树的深度,增加左右子数交换等功能
#include<iostream>
using namespace std;

//二叉树的二叉链表存储表示
typedef struct BiNode
{               
    char data;                      //结点数据域
    struct BiNode *lchild,*rchild;  //左右孩子指针
}BiTNode,*BiTree;

//用算法5.3建立二叉链表
void CreateBiTree(BiTree &T)
{   
    //按先序次序输入二叉树中结点的值(一个字符),创建二叉链表表示的二叉树T
    char ch;
    cin >> ch;
    if(ch=='#')  T=NULL;            //递归结束,建空树
    else{                           
        T=new BiTNode;
        T->data=ch;                 //生成根结点
        CreateBiTree(T->lchild);    //递归创建左子树
        CreateBiTree(T->rchild);    //递归创建右子树
    }                               //else
}                                   //CreateBiTree

int Depth(BiTree T)
{ 
    int m,n;
    if(T == NULL ) return 0;        //如果是空树,深度为0,递归结束
    else 
    {                           
        m=Depth(T->lchild);         //递归计算左子树的深度记为m
        n=Depth(T->rchild);         //递归计算右子树的深度记为n
        if(m>n) return(m+1);        //二叉树的深度为m 与n的较大者加1
        else return (n+1);
    }
}
void InOrderTraverse(BiTree T){  
    //中序遍历二叉树T的递归算法
    if(T){
        InOrderTraverse(T->lchild);
        cout << T->data;
        InOrderTraverse(T->rchild);
    }
}
void inChangeLR(BiTree &T)
{
    BiTree temp;
    if(T){
        if(T->lchild==NULL&&T->rchild==NULL){
        return;
    } else{
        temp=T->lchild;
        T->lchild = T->rchild;
        T->rchild=temp;
    }
    inChangeLR(T->lchild);
    inChangeLR(T->rchild);
    }
}
void preChangeLR(BiTree &T)
{
BiTree temp;
    if(T){
        inChangeLR(T->lchild);
        if(T->lchild==NULL&&T->rchild==NULL){
        return;
    } else{
        temp=T->lchild;
        T->lchild = T->rchild;
        T->rchild=temp;
    }
    inChangeLR(T->rchild);
    }
}
void postChangeLR(BiTree &T)
{
BiTree temp;
    if(T){
        inChangeLR(T->lchild);
        inChangeLR(T->rchild);
        if(T->lchild==NULL&&T->rchild==NULL){
        return;
    } else{
        temp=T->lchild;
        T->lchild = T->rchild;
        T->rchild=temp;
    }
    
    }
}

int main()
{
    BiTree tree;
    cout<<"请输入建立二叉链表的序列:\n";
    CreateBiTree(tree);

    InOrderTraverse(tree);
    cout<<"数的深度为:"<<Depth(tree)<<endl;
    cout<<"中序遍历的结果为:\n";
    InOrderTraverse(tree);
    cout<<"\n";
    //以下三种执行其中一种
    cout<<"交换后中序遍历的结果为:\n";
    inChangeLR(tree);
    InOrderTraverse(tree);
    cout<<"\n";
    cout<<"交换后前序序遍历的结果为:\n";
    preChangeLR(tree);
    InOrderTraverse(tree);
    cout<<"\n";
    cout<<"交换后后序遍历的结果为:\n";
    postChangeLR(tree);
    InOrderTraverse(tree);

    return 0; 
}

猜你喜欢

转载自www.cnblogs.com/ygjzs/p/11874619.html