《算法笔记》9.4小节——数据结构专题(2)->二叉查找树(BST) 问题 A: 二叉排序树

题目描述

输入一系列整数,建立二叉排序数,并进行前序,中序,后序遍历。

输入

输入第一行包括一个整数n(1<=n<=100)。接下来的一行包括n个整数。

输出

可能有多组测试数据,对于每组数据,将题目所给数据建立一个二叉排序树,并对二叉排序树进行前序、中序和后序遍历。每种遍历结果输出一行。每行最后一个数据之后有一个空格。

样例输入
复制代码
1
2 
2
8 15 
4
21 10 5 39 
样例输出
复制代码
2 
2 
2 
8 15 
8 15 
15 8 
21 10 5 39 
5 10 21 39 
5 10 39 21 

分析:按照题意建立二叉排序树即可,注意二叉排序树中没有相等的元素。

cpp 复制代码
#include    <algorithm>
#include     <iostream>
#include      <cstdlib>
#include      <cstring>
#include       <string>
#include       <vector>
#include       <cstdio>
#include        <queue>
#include        <stack>
#include        <ctime>
#include        <cmath>
#include          <map>
#include          <set>
#include<unordered_map>
#define INF 0x3f3f3f3f
#define db1(x) cout<<#x<<"="<<(x)<<endl
#define db2(x,y) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<endl
#define db3(x,y,z) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<", "<<#z<<"="<<(z)<<endl
#define db4(x,y,z,a) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<", "<<#z<<"="<<(z)<<", "<<#a<<"="<<(a)<<endl
#define db5(x,y,z,a,r) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<", "<<#z<<"="<<(z)<<", "<<#a<<"="<<(a)<<", "<<#r<<"="<<(r)<<endl
using namespace std;

typedef struct node
{
    int val;
    struct node *left,*right;
}node;

void Free(node *p)
{
    if(p==NULL)return;
    Free(p->left);
    Free(p->right);
    free(p);
    return;
}

void pre_order(node *p)
{
    if(p==NULL)return;
    printf("%d ",p->val);
    pre_order(p->left);
    pre_order(p->right);
}

void in_order(node *p)
{
    if(p==NULL)return;
    in_order(p->left);
    printf("%d ",p->val);
    in_order(p->right);
}

void post_order(node *p)
{
    if(p==NULL)return;
    post_order(p->left);
    post_order(p->right);
    printf("%d ",p->val);
}

int main(void)
{
    #ifdef test
    freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    clock_t start=clock();
    #endif //test

    int n;
    while(~scanf("%d",&n))
    {
        node *root=NULL;
        for(int i=0;i<n;++i)
        {
            int a;scanf("%d",&a);
            node *p=(node*)malloc(sizeof(node));
            p->val=a;p->left=p->right=NULL;
            if(!i)root=p;
            else
            {
                node *temp=root;
                while(1)
                {
                    if(a>temp->val)
                    {
                        if(temp->right==NULL)
                        {
                            temp->right=p;break;
                        }
                        else temp=temp->right;
                    }
                    else if(a<temp->val)
                    {
                        if(temp->left==NULL)
                        {
                            temp->left=p;break;
                        }
                        else temp=temp->left;
                    }
                    else break;
                }
            }
        }
        pre_order(root);printf("\n");
        in_order(root);printf("\n");
        post_order(root);printf("\n");
        Free(root);
    }

    #ifdef test
    clockid_t end=clock();
    double endtime=(double)(end-start)/CLOCKS_PER_SEC;
    printf("\n\n\n\n\n");
    cout<<"Total time:"<<endtime<<"s"<<endl;        //s为单位
    cout<<"Total time:"<<endtime*1000<<"ms"<<endl;    //ms为单位
    #endif //test
    return 0;
}
相关推荐
知来者逆2 小时前
计算机视觉——速度与精度的完美结合的实时目标检测算法RF-DETR详解
图像处理·人工智能·深度学习·算法·目标检测·计算机视觉·rf-detr
阿让啊2 小时前
C语言中操作字节的某一位
c语言·开发语言·数据结构·单片机·算法
এ᭄画画的北北2 小时前
力扣-160.相交链表
算法·leetcode·链表
爱研究的小陈3 小时前
Day 3:数学基础回顾——线性代数与概率论在AI中的核心作用
算法
渭雨轻尘_学习计算机ing3 小时前
二叉树的最大宽度计算
算法·面试
BB_CC_DD3 小时前
四. 以Annoy算法建树的方式聚类清洗图像数据集,一次建树,无限次聚类搜索,提升聚类搜索效率。(附完整代码)
深度学习·算法·聚类
梁下轻语的秋缘5 小时前
每日c/c++题 备战蓝桥杯 ([洛谷 P1226] 快速幂求模题解)
c++·算法·蓝桥杯
CODE_RabbitV5 小时前
【深度强化学习 DRL 快速实践】逆向强化学习算法 (IRL)
算法
mit6.8245 小时前
[贪心_7] 最优除法 | 跳跃游戏 II | 加油站
数据结构·算法·leetcode
keep intensify5 小时前
通讯录完善版本(详细讲解+源码)
c语言·开发语言·数据结构·算法