当前位置: 首页 > article >正文

计算机考研复试上机07

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.kler.cn/a/557716.html

相关文章:

  • EVM系区块链开发网节点搭建及测试详细文档
  • unordered_map和 unordered_set
  • 20250221 NLP
  • 基于C++ Qt的图形绘制与XML序列化系统
  • HW面试经验分享 | 北京蓝中研判岗
  • 【Java】File 类
  • 水果生鲜农产品推荐系统 协同过滤余弦函数推荐水果生鲜农产品 Springboot Vue Element-UI前后端分离 代码+开发文档+视频教程
  • WPF实现打印机控制及打印
  • ACWing蓝桥杯集训·每日一题2025-6122. 农夫约翰的奶酪块-Java
  • malloc如何分配内存
  • 区块链相关方法-SWOT分析
  • DeepSeek底层揭秘——微调
  • Linux基本指令(三)+ 权限
  • w223信息技术知识赛系统设计与实现
  • 【Python爬虫(47)】探秘分布式爬虫性能:从测试到优化之路
  • 清华大学第五弹:《DeepSeek与AI幻觉》
  • Python strip() 方法详解:用途、应用场景及示例解析(中英双语)
  • 【多模态处理篇三】【DeepSeek语音合成:TTS音色克隆技术揭秘】
  • [VSCode]彻底卸载和重装,并搭建Java开发环境
  • 内容中台重构企业内容管理的价值维度与实施路径