数据结构(10):二叉树基本功能的实现

/*    语言: C++              编译环境: Dev-C++5.4.0    */
#include <iostream>
using namespace std;

/* 二叉树的二叉链表结点结构定义 */
typedef char TElemType;  // 树结点的数据类型,目前暂定为整型
// 结点结构
typedef struct BiTNode
{
    TElemType data;                  // 结点数据
    struct BiTNode *lchild, *rchild;  // 左右孩子指针
} BiTNode, *BiTree;

/* 按前序输入二叉树中结点的值(一个字符) */
/* #表示空树,构造二叉链表表示二叉树T.  */
// 1
void CreateBiTree(BiTree &T)
{   
    TElemType ch;
    cin>>ch;
    if(ch == '#')
        T = NULL;           // 递归结束,建空树 
    else                    // 递归创建二叉树 
    {       
        T = new BiTNode;    // 生成根结点 
        T -> data = ch;     // 根结点数据域置为ch 
        CreateBiTree(T -> lchild); // 递归创建左子树 
        CreateBiTree(T -> rchild); // 递归创建右子树 
    } 
} 

/* 二叉树的前序遍历递归算法 */
// 2
void PreOrderTraverse(BiTree T)
{
    if(T == NULL)
        return;
    cout<<T->data;      // 显示结点数据,可以更改为其他对结点操作
    PreOrderTraverse(T->lchild);      // 再先序遍历左子树
    PreOrderTraverse(T->rchild);      // 最后先序遍历右子树
}

/* 二叉树的中序遍历递归算法 */
// 3
void InOrderTraverse(BiTree T)
{
    if(T == NULL)
        return;
    InOrderTraverse(T->lchild);      // 中序遍历左子树
    cout<<T->data;      // 显示结点数据,可以更改为其他对结点操作
    InOrderTraverse(T->rchild);      // 最后中序遍历右子树
}

/* 二叉树的后序遍历递归算法 */
// 4
void PosOrderTraverse(BiTree T)
{
    if(T == NULL)
        return;
    PosOrderTraverse(T->lchild);      // 先后序遍历左子树
    PosOrderTraverse(T->rchild);      // 再后序遍历右子树
    cout<<T->data;      // 显示结点数据,可以更改为其他对结点操作
}


/* 计算二叉树的深度 */
// 5
int Depth(BiTree T)
{
    int ldepth = 0;
    int rdepth = 0;

    if(!T) return 0;

    ldepth = Depth(T->lchild);  // 计算左子树深度 
    rdepth = Depth(T->rchild);  // 计算右子树深度 
    if(ldepth > rdepth)         // 选择较大者为树的深度 
        return (ldepth+1);
    else
        return (rdepth+1);
}

/* 计算总结点数 */
// 6
int NodeCount(BiTree T)
{
    if(!T) return 0;        // 后序 
    return (NodeCount(T->lchild) + NodeCount(T->rchild) + 1);   // 左子树结点数 + 右子树结点数 + 根结点 
}


/* 计算叶子结点数 */
// 7
void LeafNodeCount(BiTree T,int& leafnode)
{
    if(!T) return;

    if(!T->lchild && !T->rchild)        // 若为叶子结点  计数器加1 
        leafnode++;
    LeafNodeCount(T->lchild,leafnode);  // 向左遍历 
    LeafNodeCount(T->rchild,leafnode);  // 向右遍历 
}

/* 查找结点(返回地址) */
// 8
BiTree SearchNode(BiTree T,TElemType e)
{
    if(!T) return NULL;                 // 为空,返回NULL 

    if(T->data == e)                    // 最好情况 
        return T;

    BiTree Temp;
    Temp = SearchNode(T->lchild,e);     // 按左子树搜 
    if(!Temp)                           // 搜不到 
        Temp = SearchNode(T->rchild,e); // 按右子树搜 
    if(!Temp)                           // 还是搜不到,返回NULL 
        return NULL;
    else
        return Temp;                    // 否则返回地址 
}

/* 二叉树表达式 */
// 9
int CalculateTree(BiTree T)
{
    int lvalue = 0,rvalue = 0,value = 0;
    if(T) {
        if(!T->lchild && !T->rchild)
            return ((int)T->data-'0');
        else
        {
            lvalue = CalculateTree(T->lchild);
            rvalue = CalculateTree(T->rchild);
            switch(T->data)
            {
                case '+':
                    value = lvalue + rvalue; break;
                case '-':
                    value = lvalue - rvalue; break;
                case '*':
                    value = lvalue * rvalue; break;
                case '/':
                    value = lvalue / rvalue; break;
            }
            return value;
        }
    }   
    return 0;
}

/*   先序凹入法显示二叉树 */
// 10
void ShowBiTree(BiTree T,int cc)        // cc的初值为 当前二叉树的结点数count 
{
    int i;
    if(T)
    {   
//      右对齐输出:
//      int j;
//      for(i=0;i<count-cc;i++) cout<<" ";    
//      for(j=0;j<cc;j++)       cout<<"#";
//      cout<<":"<<T->data<<endl; 
        cout<<T->data<<":";
        for(i=0;i<cc;i++) cout<<"#";
        cout<<endl;
        cc--;
        ShowBiTree(T->lchild,cc);
        ShowBiTree(T->rchild,cc);
    }
}

int main()
{
    cout<<"*******************************************"<<endl;
    cout<<"*                  二叉树                 *"<<endl;
    cout<<"*******************************************"<<endl;
    cout<<"*   1、按先序创建二叉树                   *"<<endl;
    cout<<"*   2、先序遍历                           *"<<endl;
    cout<<"*   3、中序遍历                           *"<<endl;
    cout<<"*   4、后序遍历                           *"<<endl;
    cout<<"*   5、树的深度                           *"<<endl;
    cout<<"*   6、总结点数                           *"<<endl;
    cout<<"*   7、叶子结点数                         *"<<endl;
    cout<<"*   8、查找结点                           *"<<endl;
    cout<<"*   9、二叉树表达式计算(仅限于个位整数)   *"<<endl;
    cout<<"*   10、先序凹入法显示二叉树              *"<<endl;
    cout<<"*   0、退出程序                           *"<<endl;
    cout<<"*******************************************"<<endl;

    int node = 0;
    int leafnode = 0;
    int choose = 0;
    char searchnode;
    BiTree Temp;
    BiTree Tree;
    while(1)
    {
        cout<<"请输入选择:";
        cin>>choose; 
        switch(choose)
        {
            case 1:
                CreateBiTree(Tree);
                cout<<"创建完毕."<<endl<<endl;
                continue;
            case 2:
                cout<<"先序遍历:"<<endl;
                PreOrderTraverse(Tree);
                cout<<endl<<endl;
                continue;
            case 3:
                cout<<"中序遍历:"<<endl;
                InOrderTraverse(Tree);
                cout<<endl<<endl;
                continue;
            case 4:
                cout<<"后序遍历:"<<endl;
                PosOrderTraverse(Tree);
                cout<<endl<<endl;
                continue;
            case 5:
                cout<<"二叉树的深度为: "<<Depth(Tree)<<endl<<endl; 
                continue;
            case 6:
                node = NodeCount(Tree);
                cout<<"二叉树的总结点数为: "<<node<<endl<<endl;
                continue;
            case 7:
                LeafNodeCount(Tree,leafnode);
                cout<<"二叉树的叶子结点数为: "<<leafnode<<endl<<endl;
                continue;
            case 8:
                cout<<"请输入要查找的信息:";
                cin>>searchnode;
                Temp = SearchNode(Tree,searchnode);
                if(Temp)
                    cout<<"地址"<<searchnode<<":"<<Temp<<endl<<endl;
                else
                    cout<<"不存在"<<endl<<endl;
                continue;
            case 9:
                cout<<"表达式二叉树的值为: "<<CalculateTree(Tree)<<endl<<endl;
                continue;
            case 10:
                cout<<"先序凹入法:"<<endl;
                ShowBiTree(Tree,node);
                cout<<endl<<endl;
                continue;
            case 0:
                cout<<"程序正常结束."<<endl<<endl;
                break;
            default:
                cout<<"程序异常结束."<<endl<<endl;
                break;
        }
        break;
    }

    return 0;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值