计算机考研复试上机07

news/2025/2/23 17:47:50

14、数据结构

1)二叉树

1.常用操作
struct TreeNode{
	int data;
	TreeNode *leftChild;
	TreeNode *rightChild;
}; 
//前序遍历
void PreOrder(TreeNode *root){
	if(root == NULL) return;
	visit(root->data);
	PreOrder(root->leftChild);
	PreOrder(root->rightChild);
	return;
} 
//层序遍历 
void levelOrder(TreeNode *root){
	queue<TreeNode*> myQueue;
	if(root != NULL) myQueue.push(root);
	while(!myQueue.empty()){
		TreeNode *current = myQueue.front();
		myQueue.pop();
		visit(current->data);
		if(current->leftChild != NULL)
			myQueue.push(current->leftChild);
		if(current->rightChild != NULL)
			myQueue.push(current->rightChild);
	}
}
2.二叉树遍历(清华大学复试上机题)

题目描述:

编一个程序,读入用户输入的一串先序遍历字符串,根据此字符串建立一棵二叉树(以指针方式存储)。例如,先序遍历字符串 ABC##DE#G##F###,其中“#”表示空格,空格字符代表空树。建立这棵二叉树后,再对二叉树进行中序遍历,输出遍历结果。

#include <bits/stdc++.h>
using namespace std;

struct TreeNode{
	char data;
	TreeNode *leftChild;
	TreeNode *rightChild;
	TreeNode(char c): data(c), leftChild(NULL), rightChild(NULL){}
}; 

TreeNode *Build(int &position, string str){
	char c = str[position++];
	if(c == '#') return NULL;
	TreeNode *root = new TreeNode(c);
	root->leftChild = Build(position,str);
	root->rightChild = Build(position, str);
	return root;
}

void InOrder(TreeNode *root){
	if(root == NULL) return;
	InOrder(root->leftChild);
	cout<<root->data<<" ";
	InOrder(root->rightChild);
	return;
}

int main(){
	string str;
	while(cin>>str){
		int position = 0;
		TreeNode *root = Build(position, str);
		InOrder(root);
	}
	cout<<endl;
	return 0; 
}

2)二叉排序树

1.二叉排序树(华中科技大学复试上机题)

题目描述:

二叉排序树也称二叉查找树。它可以是一棵空树,也可以是一棵具有如下特性的非空二叉树:1. 若左子树非空,则左子树上所有结点的关键字值均不大于根结点的关键字值。2. 若右子树非空,则右子树上所有结点的关键字值均不小于根结点的关键字值。3. 左、右子树本身也是一棵二叉排序树。 现在给你 N 个关键字值各不相同的结点,要求你按顺序将它们插入一个初始为空树的二叉排序树,每次插入成功后,求相应父结点的关键字值,若没有父结点,则输出-1。

#include <bits/stdc++.h>
using namespace std;

struct TreeNode{
	int data;
	TreeNode *leftChild;
	TreeNode *rightChild;
	TreeNode(int x): data(x),leftChild(NULL),rightChild(NULL){}
};

TreeNode *insert(TreeNode *root,int x,int father){
	if(root == NULL){
		root = new TreeNode(x);
		cout<<father<<endl;
	}else if(x < root->data){
		root->leftChild = insert(root->leftChild,x,root->data);
	}else{
		root->rightChild = insert(root->rightChild,x,root->data);
	}
	return root;
}

int main(){
	int n;
	while(cin>>n){
		TreeNode *root = NULL;
		for(int i = 0;i < n;i++){
			int x;
			cin>>x;
			root = insert(root,x,-1);
		}
	}
	
	return 0; 
}

3)优先队列

1.常用操作
#include <bits/stdc++.h>
using namespace std;

int main(){
	priority_queue<int> queue;
	cout<<queue.size()<<endl;
	queue.push(20);
	queue.push(100);
	queue.push(30);
	queue.push(50);
	cout&

http://www.niftyadmin.cn/n/5863634.html

相关文章

提效10倍:基于Paimon+Dolphin湖仓一体新架构在阿里妈妈品牌业务探索实践

1. 业务背景 阿里妈妈品牌广告数据包括投放引擎、下发、曝光、点击等日志&#xff0c;面向运筹调控、算法特征、分析报表、诊断监控等应用场景&#xff0c;进行了品牌数仓能力建设。随着业务发展&#xff0c;基于Lambda架构的数仓开发模式&#xff0c;缺陷日益突出&#xff1a;…

Windows 上编译 mebedtls 的鸿蒙库

mebedtls 地址&#xff1a;https://github.com/Mbed-TLS/mbedtls 准备工作&#xff1a; clone mebedtls 仓库到本地(tag: mbedtls-2.26.0)鸿蒙工具链(SDK version: v5.0.5) 编译文件修改&#xff1a; 对 CMakeLists.txt 进行修改&#xff0c;主要是关闭了以下几个选项 ENABLE_P…

Spring Boot Validation 接口校验:从零到掌握

在开发 Web 应用时&#xff0c;数据校验是不可忽视的一部分。无论是注册用户信息、提交表单数据&#xff0c;还是处理业务逻辑&#xff0c;数据的有效性和完整性都需要得到保证。Spring Boot 提供了强大的验证功能&#xff0c;基于 Hibernate Validator 框架&#xff0c;通过注…

11.Docker 之分布式仓库 Harbor

Docker 之分布式仓库 Harbor Docker 之分布式仓库 Harbor1. Harbor 组成2. 安装 Harbor Docker 之分布式仓库 Harbor Harbor 是一个用于存储和分发 Docker 镜像的企业级 Registry 服务器&#xff0c;由 VMware 开源&#xff0c;其通过添加一些企业必需的功能特性&#xff0c;例…

Spring Boot 集成 T-io 实现客户端服务器通信

Spring Boot 集成 T-io 实现客户端服务器通信 本文详细介绍如何在 Spring Boot 项目中集成 T-io 框架&#xff0c;实现客户端与服务器之间的通信&#xff0c;并支持组聊、群聊和私聊功能。通过本文&#xff0c;您能够全面了解 T-io core 的使用方法&#xff0c;以及如何正确启…

从零开始学 Rust:基本概念——变量、数据类型、函数、控制流

文章目录 Variables and MutabilityShadowing Data TypesScalar TypesCompound Types FunctionsFunction Parameters CommentsControl FlowRepetition with Loops Variables and Mutability fn main() {let mut x 5;println!("The value of x is: {}", x);x 6;pri…

蓝桥备赛(一)- C++入门(上)

一、工具安装 Dev-C安装&#xff1a;https://www.bilibili.com/video/BV1kC411G7CS 一般比赛会用到Dev-C, 但是Dev-C还是有自身的局限性 &#xff0c; 后续的博客学习中 &#xff0c; 必要的时候 &#xff0c; 会使用VS2022 &#xff0c; 下面是VS2022的安装和使用教程。 VS202…

Go语言中使用viper绑定结构体和yaml文件信息时,标签的使用

在Go中使用Viper将YAML配置绑定到结构体时&#xff0c;主要依赖 mapstructure 标签&#xff08;而非 json 或 yaml 标签&#xff09;实现字段名映射。 --- ### 1. **基础绑定方法** 使用 viper.Unmarshal(&config) 或 viper.UnmarshalKey("key", &subConfi…