当前位置:网站首页>AcWing 3540. 二叉搜索树 二叉排序树 BST
AcWing 3540. 二叉搜索树 二叉排序树 BST
2022-07-16 06:18:00 【Alkali!】
题目
https://www.acwing.com/problem/content/description/3543/
纯写一个二叉排序树
思路
二叉排序树其实非常的简单,是二叉树与结点大小关系的结合。
结点值的大小:左儿子<父结点<右儿子
代码
#include<iostream>
#include<unordered_map>
using namespace std;
struct TreeNode //树结构体
{
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x):val(x),left(NULL),right(NULL)
{
}
};
TreeNode *root,*fa; //根节点、待插入位置结点的父亲结点
unordered_map<int,bool> M; //判重的哈希表,防止插入重复元素值
int n,x; //插入数的总数,待插入值
void search(TreeNode *t) //返回插入位置的父亲结点
{
if(!t) return; //如果搜索到了空结点,则返回空,结束
if(x<t->val) //如果待插入值比当前结点小
{
fa=t; //当前结点为父亲节点
search(t->left); //递归去查它的左儿子
}
else if(x>t->val) //如果待插入值比当前结点大
{
fa=t; //当前结点为父亲节点
search(t->right); //递归去查它的右儿子
}
}
void PrePrint(TreeNode *t) //前序遍历
{
if(!t) return;
printf("%d ",t->val);
PrePrint(t->left);
PrePrint(t->right);
}
void inPrint(TreeNode *t) //中序遍历
{
if(!t) return;
inPrint(t->left);
printf("%d ",t->val);
inPrint(t->right);
}
void succPrint(TreeNode *t) //后序遍历
{
if(!t) return;
succPrint(t->left);
succPrint(t->right);
printf("%d ",t->val);
}
int main()
{
for(int i=1;i<=1000;i++) //哈希表,1-1000内的数都没有被访问过
M[i]=false;
scanf("%d",&n);
scanf("%d",&x);
root=new TreeNode(x); //建立根节点
n--;
while(n--)
{
fa=NULL;
scanf("%d",&x);
if(!M[x]) //已经在树里面的就别放进来了
{
search(root); //找到当前要插入的结点该插入位置的父结点指针
if(fa) //如果找到了
{
TreeNode *temp=new TreeNode(x); //建立节点
if(x<(fa->val)) //值比父结点小,作为左儿子
fa->left=temp;
else if(x>(fa->val)) //值比父节点大,作为右儿子
fa->right=temp;
M[x]=true; //设置访问标记,表示被访问过了
}
}
}
//输出
PrePrint(root);
printf("\n");
inPrint(root);
printf("\n");
succPrint(root);
return 0;
}
边栏推荐
- Redis is configured to save RDB snapshots, but it is currently not able to persist
- 如何用常数时间插入、删除和获取随机元素
- 启动失败 Failed to determine a suitable driver class 问题解决方案
- 出海已成大势,技术如何赋能?| ArchSummit
- Design of hydrogen monitoring and alarm system based on single chip microcomputer (0492)
- 分层自动化测试模型变与不变
- STM32通用定时器
- Binary build kubernetes
- Design of Bluetooth electronic scale system based on MCU (0493)
- 小程序毕设作品之微信评选投票小程序毕业设计(6)开题答辩PPT
猜你喜欢

Hal firmware library

家族树问题

Network security experiment: firewall technology
![[每周一更]-(第3期):Web开发安全注意事项](/img/2e/64e2f7aca24abd6b68d844e0b78a3e.jpg)
[每周一更]-(第3期):Web开发安全注意事项

Will the arrears of Alibaba cloud international ECS be automatically released?

Design of Bluetooth electronic scale system based on MCU (0493)

C#窗体应用程序常用控件介绍

微机原理与技术接口 实验三 循环结构

线上MySQL的自增id用尽怎么办?

ViewGroup event distribution sorting
随机推荐
Desai wisdom number - discount (gradient stacking chart): per capita disposable income of national residents
小程序毕设作品之微信评选投票小程序毕业设计(5)任务书
Codeforces Round #803 (Div. 2) C. 3SUM Closure
NC16857 [NOI1999]生日蛋糕
压力测试工具(常用)和sendfile的过程
小程序畢設作品之微信評選投票小程序畢業設計(5)任務書
How to insert, delete and obtain random elements with constant time
STM32 application development practice tutorial: multi computer communication application development based on CAN bus
Is it safe for Huatai Securities to open an account? Is it a regular securities company?
00 后博士获聘南大特任副研究员,曾 4 岁上小学,14 岁考入南大!
Reconstructing the geometric form of weight space with training set
What if the self incrementing ID of online MySQL is exhausted?
I "bring salt" for tdengine! "High price" to recruit photographed developers
Wechat Evaluation of applet design works Voting applet Graduation Design (5) Task Book
前 K 个高频元素问题
Stonedb announces open source, why is the integrated real-time HTAP architecture the current best solution
Design of hydrogen monitoring and alarm system based on single chip microcomputer (0492)
The degradation mechanism is not designed properly, and the online system crashes instantly
二进制搭建 Kubernetes
数百亿数据压缩至 600GB,TDengine 落地协鑫能科移动能源平台