SCAU 数据结构实验5 8608 实现二叉排序树的各种算法(2)

SCAU 数据结构实验5 8608 实现二叉排序树的各种算法(2)

/*Description
用函数实现如下二叉排序树算法: 
(1) 插入新结点 
(2) 前序、中序、后序遍历二叉树 
(3) 中序遍历的非递归算法 
(4) 层次遍历二叉树 
(5) 在二叉树中查找给定关键字(函数返回值为成功1,失败0) 
(6) 交换各结点的左右子树 
(7) 求二叉树的深度 
(8) 叶子结点数



输入格式
第一行:准备建树的结点个数n 
第二行:输入n个整数,用空格分隔 
第三行:输入待查找的关键字 
第四行:输入待查找的关键字 
第五行:输入待插入的关键字


输出格式
第一行:二叉树的先序遍历序列 
第二行:二叉树的中序遍历序列 
第三行:二叉树的后序遍历序列 
第四行:查找结果 
第五行:查找结果 
第六行~第八行:插入新结点后的二叉树的先、中、序遍历序列 
第九行:插入新结点后的二叉树的中序遍历序列(非递归算法) 
第十行:插入新结点后的二叉树的层次遍历序列 
第十一行~第十三行:第一次交换各结点的左右子树后的先、中、后序遍历序列 
第十四行~第十六行:第二次交换各结点的左右子树后的先、中、后序遍历序列 
第十七行:二叉树的深度 
第十八行:叶子结点数


输入样例
7
40 20 60 18 50 56 90
18
35
30


输出样例
40 20 18 60 50 56 90
18 20 40 50 56 60 90
18 20 56 50 90 60 40
1
0
40 20 18 30 60 50 56 90
18 20 30 40 50 56 60 90
18 30 20 56 50 90 60 40
18 20 30 40 50 56 60 90
40 20 60 18 30 50 90 56
40 60 90 50 56 20 30 18
90 60 56 50 40 30 20 18
90 56 50 60 30 18 20 40
40 20 18 30 60 50 56 90
18 20 30 40 50 56 60 90
18 30 20 56 50 90 60 40
4
4
*/
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <cstdio>
#include <stack>
#include <queue>
using namespace std;

typedef struct node
{
    int key;
    struct node *lChild, *rChild;
} Node, *BST;

void creatBST(BST &T,int ele) //创建二叉树
{

    if(T==NULL)
    {
        T=new Node;
        T->key=ele;
        T->lChild=NULL;
        T->rChild=NULL;
        return;
    }
    else
    {
        if(ele==T->key) return;
        if(ele<T->key) creatBST(T->lChild,ele);
        else creatBST(T->rChild,ele);

    }


}

void findBST(BST T,int ele)//在二叉树中查找元素
{


    if(!T)
    {
        cout << 0 << endl;
        return;
    }
    if(T->key==ele)
    {
        cout << 1 << endl;
        return;
    }

    else if(T->key > ele)
        findBST(T->lChild,ele);
    else if(T->key < ele)
        findBST(T->rChild,ele);

}
void firsttravel(BST T) //先序遍历
{

    if(T)
    {
        cout << T->key << " ";
        firsttravel(T->lChild);
        firsttravel(T->rChild);
    }
}
void midtravel(BST T) // 中序遍历
{

    if(T)
    {

        midtravel(T->lChild);
        cout << T->key << " ";
        midtravel(T->rChild);
    }
}

void lasttravel(BST T) // 后序遍历
{

    if(T)
    {
        lasttravel(T->lChild);
        lasttravel(T->rChild);
        cout << T->key << " ";
    }
}
void insertBST(BST T,int ele) //在二叉树中插入元素
{


    if(T->key==ele) return;
    Node *p=new Node;
    p->key=ele;
    p->lChild=NULL;
    p->rChild=NULL;
    if(T->key > ele)
    {
        if(T->lChild==NULL)
        {
            T->lChild=p;
            return;
        }
        else
            insertBST(T->lChild,ele);
    }
    if(T->key < ele)
    {
        if(T->rChild==NULL)
        {
            T->rChild=p;
            return;
        }
        else
            insertBST(T->rChild,ele);
    }

}
void InOrderNor(BST T) //中序遍历的非递归算法
{
    stack<BST> s;
    Node *cur = T;
    while (cur != NULL || !s.empty())
    {
        while (cur != NULL)
        {
            s.push(cur);
            cur = cur->lChild;
        }
        Node *top = s.top();
        s.pop();
        cout << top->key << " ";
        cur = top->rChild;
    }
}
void LevelOrder(BST T)//层次遍历
{
    queue<Node *> q;
    if(!T) return;
    q.push(T);
    BST S;
    while(!q.empty())
    {
        S=q.front();
        q.pop();
        cout << S->key <<" " ;
        if(S->lChild!=NULL)
            q.push(S->lChild);
        if(S->rChild!=NULL)
            q.push(S->rChild);
    }
    return;
}

void swapBST(BST T ) //交换左右子树
{

    if(!T) return;
    Node *p=new Node;
    p=T->lChild;
    T->lChild=T->rChild;
    T->rChild=p;
    swapBST(T->lChild);
    swapBST(T->rChild);

}
int height(BST T) //求二叉树的深度
{

    if(!T) return 0;
    else
    {
        int l=height(T->lChild);
        int r=height(T->rChild);
        return 1+max(l,r);
    }
}
int getCount(BST T) // 求叶子节点数
{
    if (T == NULL)
    {
        return 0;
    }
    if (T->lChild == NULL && T->rChild == NULL)
    {
        return 1;
    }
    return getCount(T->lChild) + getCount(T->rChild);
}

int main()
{
    int n,m;
    cin >> n;
    int k;
    BST T=NULL;
    for(int i=0; i<n; i++)
    {
        cin  >>  k;
        creatBST(T,k);
    }
    //先序遍历
    firsttravel(T);
    cout << endl;
    //中序遍历
    midtravel(T);
    cout << endl;
    //后序遍历
    lasttravel(T);
    cout << endl;
    int ele;
    cin >> ele;
    //在二叉树中查找
    findBST(T,ele);
    cin >> ele;
    //在二叉树中查找
    findBST(T,ele);
    cin >> ele;
    //插入
    insertBST(T,ele);
    //先序遍历
    firsttravel(T);
    cout << endl;
    //中序遍历
    midtravel(T);
    cout << endl;
    //后序遍历
    lasttravel(T);
    cout << endl;
    InOrderNor(T);
    cout << endl;
    LevelOrder(T);
    cout << endl;
    //第一次交换左右子树
    swapBST(T);
    //先序遍历
    firsttravel(T);
    cout << endl;
    //中序遍历
    midtravel(T);
    cout << endl;
    //后序遍历
    lasttravel(T);
    cout << endl;
    //第一次交换左右子树
    swapBST(T);
    //先序遍历
    firsttravel(T);
    cout << endl;
    //中序遍历
    midtravel(T);
    cout << endl;
    //后序遍历
    lasttravel(T);
    cout << endl;
    //二叉树的深度
    cout << height(T) << endl;
    //二叉树的叶子节点数
    cout << getCount(T);
    return 0;
}

  • 3
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值