您的位置:首页 > 其它

二叉树(二叉链式存储)8种基本操作的实现

2015-04-15 07:52 330 查看
/*  数据结构分析与学习专栏
*   Copyright (c) 2015, 山东大学 计算机科学与技术专业 学生
*   All rights reserved.
*   作    者:   高祥
*   完成日期:  2015 年 4 月 13 日
*   版 本 号:019

*任务描述:针对二叉树遍历,实现8种方法
*   1:建立二叉树 ;
*   2:层序输出二叉树 ;
*   3:判断二叉树是否为空 ;
*   4:求二叉树的高度 ;
*   5:求二叉树的总结点数;
*   6:求二叉树的度为1的结点数 ;
*   7:求二叉树的叶子结点数;
*   8:销毁二叉树;

*主要函数:
*   1.void CreateByPreOrder(BinaryTree &T);//给定完整的先序遍历建树
*   2.void LevelTraverse(BinaryTree T);//层次遍历
*   3.int IsEmpty(BinaryTree T);//判断二叉树是否为空
*   4.int BinaryTreeHeight(BinaryTree T);//求树的高度
*   5.int NodeCount(BinaryTree T);//求二叉树结点的总数
*   6.int DegreeOneCount(BinaryTree T);//求度为1的结点的总数
*   7.int LeafNodeCount(BinaryTree T);//求叶子结点总数
*   8.void DestroyBinaryTree(BinaryTree &T);//销毁二叉树

*/
#include<iostream>
#include<queue>
#include<algorithm>
#include<cstdlib>
using namespace std;

typedef char ElemType;

typedef struct BTNode//二叉树结点类型
{
    ElemType data;
    struct BTNode *leftchild;
    struct BTNode *rightchild;
} BTNode,*BinaryTree;

void CreateByPreOrder(BinaryTree &T);//给定完整的先序遍历建树
void LevelTraverse(BinaryTree T);//层次遍历
int IsEmpty(BinaryTree T);//判断二叉树是否为空
int BinaryTreeHeight(BinaryTree T);//求树的高度
int NodeCount(BinaryTree T);//求二叉树结点的总数
int DegreeOneCount(BinaryTree T);//求度为1的结点的总数
int LeafNodeCount(BinaryTree T);//求叶子结点总数
void DestroyBinaryTree(BinaryTree &T);//销毁二叉树
void Interaction();

int main()
{
    Interaction();

    BinaryTree T;
    int operate;

    while(cin>>operate)
    {
        switch(operate)
        {
        case 0:
            return 0;

        case 1:
            cout<<"请输入某棵二叉树的先序遍历(若某结点的左/右子结点不存在,用‘#’表示):\n";
            CreateByPreOrder(T);
            LevelTraverse(T);
            break;

        case 2:
            LevelTraverse(T);
            break;

        case 3:
            if(IsEmpty(T))
            {
                cout<<"二叉树为空。\n";
            }
            else
            {
                cout<<"二叉树不为空。\n";
            }
            break;

        case 4:
            cout<<"二叉树的高度为:"<<BinaryTreeHeight(T)<<endl;
            break;

        case 5:
            cout<<"二叉树的总结点数为:"<<NodeCount(T)<<endl;
            break;

        case 6:
            cout<<"二叉树度为1的结点数为:"<<DegreeOneCount(T)<<endl;
            break;

        case 7:
            cout<<"二叉树的叶子结点数为:"<<LeafNodeCount(T)<<endl;
            break;

        case 8:
            DestroyBinaryTree(T);
            cout<<"二叉树销毁成功,进行其他操作请先创建新的二叉树。\n";
            break;

        default:
            cout<<"请输入正确的操作数字!\n";
            break;
        }
    }

    return 0;
}

void CreateByPreOrder(BinaryTree &T)//给定完整的先序遍历建树
{
    ElemType ch;
    cin>>ch;

    if(ch=='#')
    {
        T=NULL;//空结点
    }
    else
    {
        T=(BinaryTree)malloc(sizeof(BTNode));//建立根结点
        T->data=ch;
        CreateByPreOrder(T->leftchild);//建立当前根结点的左子结点
        CreateByPreOrder(T->rightchild);//建立当前根结点的右子结点
    }
}

void LevelTraverse(BinaryTree T)//层次遍历
{
    if(!IsEmpty(T))
    {
        //利用数据结构队列实现:首先将根结点入队。在队列不为空的前提下:
        //取出队列首元素T,输出其值并删除队列首元素,若T的左子结点存在,将其入队;
        //若T的右子结点存在,将其入队。重复该过程直至队列为空。
        cout<<"二叉树的层序遍历为:\n";
        queue<BinaryTree> Q;
        Q.push(T);//根结点入队

        while(!Q.empty())//在队列不为空的前提下
        {
            BinaryTree now=Q.front();//取出队列首元素T
            cout<<now->data<<" ";//输出其值
            Q.pop();//删除队列首元素

            if(now->leftchild)//若T的左子结点存在,将其入队
            {
                Q.push(now->leftchild);
            }

            if(now->rightchild)//若T的右子结点存在,将其入队
            {
                Q.push(now->rightchild);
            }
        }
        cout<<endl;
    }
    else
    {
        cout<<"二叉树为空。\n";
    }
}

int IsEmpty(BinaryTree T)//判断二叉树是否为空
{
    if(T)
    {
        return 0;
    }
    return 1;
}

int BinaryTreeHeight(BinaryTree T)//求树的高度
{
    int leftheight,rightheight;
    if(!IsEmpty(T))
    {
        leftheight=BinaryTreeHeight(T->leftchild);
        rightheight=BinaryTreeHeight(T->rightchild);
        return max(leftheight,rightheight)+1;
    }

    return 0;
}

int NodeCount(BinaryTree T)//求二叉树结点的总数
{
    if(IsEmpty(T))
    {
        return 0;
    }

    return 1+NodeCount(T->leftchild)+NodeCount(T->rightchild);
}

int DegreeOneCount(BinaryTree T)//求度为1的结点的总数
{
    int sum=0,leftcount,rightcount;
    if(!IsEmpty(T))
    {
        if((T->leftchild&&!T->rightchild)||(!T->leftchild&&T->rightchild))
        {
            sum++;
        }

        leftcount=DegreeOneCount(T->leftchild);
        rightcount=DegreeOneCount(T->rightchild);

        sum=sum+leftcount+rightcount;
    }
    return sum;
}

int LeafNodeCount(BinaryTree T)//求叶子结点总数
{
    if(IsEmpty(T))
    {
        return 0;
    }

    if(!T->leftchild&&!T->rightchild)
    {
        return 1;
    }

    return LeafNodeCount(T->leftchild)+LeafNodeCount(T->rightchild);
}

void DestroyBinaryTree(BinaryTree &T)//销毁二叉树
{
    if(!IsEmpty(T))
    {
        DestroyBinaryTree(T->leftchild);
        DestroyBinaryTree(T->rightchild);
        free(T);
        T=NULL;
    }
}

void Interaction()
{
    cout<<"请输入对应操作的序号:\n";
    cout<<"0:退出程序 ;\n";
    cout<<"1:建立二叉树 ;\n";
    cout<<"2:层序输出二叉树 ;\n";
    cout<<"3:判断二叉树是否为空 ;\n";
    cout<<"4:求二叉树的高度 ;\n";
    cout<<"5:求二叉树的总结点数;\n";
    cout<<"6:求二叉树的度为1的结点数 ;\n";
    cout<<"7:求二叉树的叶子结点数;\n";
    cout<<"8:销毁二叉树;\n";
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: