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

news/2025/3/21 2:19:46/
题目描述

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

输入

输入第一行包括一个整数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 

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

#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 testfreopen("in.txt","r",stdin);//freopen("out.txt","w",stdout);clock_t start=clock();#endif //testint 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 testclockid_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 //testreturn 0;
}


http://www.ppmy.cn/news/1580741.html

相关文章

主流区块链

文章目录 主流链1. Solana特点&#xff1a;适用场景&#xff1a;工具链&#xff1a; 2. Binance Smart Chain (BSC)特点&#xff1a;适用场景&#xff1a;工具链&#xff1a; 3. Avalanche特点&#xff1a;适用场景&#xff1a;工具链&#xff1a; 4. Polkadot特点&#xff1a;…

Error response from daemon: unknown or invalid runtime name: nvidia解决办法

编辑/etc/docker/daemon.json文件在json中添加内容 "runtimes": {"nvidia": {"path": "nvidia-container-runtime","runtimeArgs": []}}运行命令 sudo systemctl daemon-reload sudo systemctl restart docker参考连接 htt…

Leetcode Hot 100 79.单词搜索

1.题目 79. 单词搜索 给定一个 m x n 二维字符网格 board 和一个字符串单词 word 。如果 word 存在于网格中&#xff0c;返回 true &#xff1b;否则&#xff0c;返回 false 。 单词必须按照字母顺序&#xff0c;通过相邻的单元格内的字母构成&#xff0c;其中“相邻”单元格…

荣耀手机怎么录制屏幕?屏幕录制后为视频加水印更有“安全感”

在数字时代&#xff0c;屏幕录制已经成为记录和分享信息的重要方式之一。无论是记录游戏的高光时刻&#xff0c;还是制作教学视频&#xff0c;亦或是保存重要的线上会议内容&#xff0c;屏幕录制都能轻松搞定。 荣耀手机作为一款功能强大的设备&#xff0c;自然也提供了便捷的…

【华三】路由器交换机忘记登入密码或super密码的重启操作

【华三】路由器交换机忘记登入密码或super密码的重启操作 背景步骤跳过认证设备&#xff1a;路由器重启设备翻译说明具体操作 跳过当前系统配置重启设备具体操作 背景 当console口的密码忘记&#xff0c;或者说本地用户的密码忘记&#xff0c;其实这时候是登入不了路由器的&am…

【数据结构面试篇】

数据结构有哪些分类&#xff1f;它们各自的使用场景是什么&#xff1f; 数据结构是计算机存储和组织数据的方式&#xff0c;合理选择数据结构能显著提升程序效率。以下是主要分类、特点及使用场景的总结&#xff1a; 一、线性结构 1. 数组&#xff08;Array&#xff09; 特点…

教材征订管理系统基于Spring Boot-SSM

‌ 目录 ‌摘要‌&#xff1a; ‌一、绪论‌&#xff1a; ‌二、国内外研究现状‌&#xff1a; ‌三、研究目的与内容‌&#xff1a; ‌四、详细设计‌&#xff1a; ‌4.1系统架构设计‌&#xff1a; ‌4.2数据库设计‌&#xff1a; ‌五、功能模块设计‌&#xff1a;…

nacos安装,服务注册,服务发现,远程调用3个方法

安装 点版本下载页面 服务注册 每个微服务都配置nacos的地址&#xff0c;都要知道 服务发现 2个是知道了解 远程调用基本实现 远程调用方法2&#xff0c;负载均衡API测试 远程调用方法3&#xff0c;注解 负载均衡的远程调用&#xff0c; 总结 面试题