复试机试算法总结#9:数据结构二

1. 二叉树

二叉树遍历:前序PreOrder,中序InOrder,后续PostOrder。遍历函数中要首先确定根节点是否为空,空则直接返回。

//结点定义
struct TreeNode
{
	ElementType data;
	TreeNode *leftChild;
	TreeNode *rightRight;
	//TreeNode(ElementType c):data(c),left(NULL),right(NULL) {}//方便根结点赋值
};


//新建根结点
ElementType c;
TreeNode* root=new TreeNode(c);

1.1 递归建树+中序遍历

https://www.nowcoder.com/practice/4b91205483694f449f94c179883c1fef?tpId=40&tqId=21342&tPage=1&rp=1&ru=/ta/kaoyan&qru=/ta/kaoyan/question-ranking

#include<iostream>
#include<cstdio>
#include<string>

using namespace std;

struct TreeNode
{
	char data;
	TreeNode *left;
	TreeNode *right;
	TreeNode(char c):data(c),left(NULL),right(NULL) {}
};

TreeNode* Build(string str,int& position)//递归的方式建树 
{
	char c=str[position++];
	if(c=='#')//空树 
		return NULL;
	TreeNode* root=new TreeNode(c);
	root->left=Build(str,position);
	root->right=Build(str,position);
	return root;
}

void InOrder(TreeNode* root)//中序遍历 
{
	if(root==NULL)
		return;
	InOrder(root->left);
	cout<<root->data<<" ";
	InOrder(root->right);
	return;
}

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

1.2 前序+中序=>后序

能够确定一棵唯一的树的组合:前序+中序、中序+后序

https://www.nowcoder.com/practice/6e732a9632bc4d12b442469aed7fe9ce?tpId=40&tqId=21544&tPage=1&rp=1&ru=/ta/kaoyan&qru=/ta/kaoyan/question-ranking

#include<iostream>
#include<cstdio>
#include<string>

using namespace std;

struct TreeNode
{
	char data;
	TreeNode* left;
	TreeNode* right;
	TreeNode(char c):data(c),left(NULL),right(NULL) {}
}; 

TreeNode* Build(string str1,string str2)//递归建树 
{
	if(str1.size()==0)
		return NULL;
	char c=str1[0];
	TreeNode* root=new TreeNode(c);//建立根结点 
	int pos=str2.find(c);//寻找切分点 
	root->left=Build(str1.substr(1,pos),str2.substr(0,pos));//左子树从左侧截取 
	root->right=Build(str1.substr(pos+1),str2.substr(pos+1));//右子树从右侧截取 
	return root;
}

void PostOrder(TreeNode* root)//后序遍历 
{
	if(root==NULL)
		return;	
	PostOrder(root->left);
	PostOrder(root->right);
	cout<<root->data;
	return;
}

int main()
{
	string str1,str2;
	while(cin>>str1>>str2)
	{
		TreeNode* root=Build(str1,str2);
		PostOrder(root);
		cout<<endl;
	}	
	return 0;
} 

2. 二叉排序树(二叉搜索树)

对二叉排序树进行中序遍历,结果必然是升序序列。可以对本来无序的序列进行排序,并实现动态维护

例题:二叉排序树

https://www.nowcoder.com/practice/b42cfd38923c4b72bde19b795e78bcb3?tpId=40&tqId=21555&tPage=1&rp=1&ru=/ta/kaoyan&qru=/ta/kaoyan/question-ranking

#include<iostream>
#include<cstdio>

using namespace std;

struct TreeNode
{
	int data;
	TreeNode* left;
	TreeNode* right;
	TreeNode(int x):data(x),left(NULL),right(NULL) {}
};

TreeNode* Insert(TreeNode* root,int x)//建立二叉排序树 
{
	if(root==NULL)//建立根结点 
		root=new TreeNode(x);
	else if(x<root->data)//插入左子树 
		root->left=Insert(root->left,x);
	else if(x>root->data)//插入右子树 
		root->right=Insert(root->right,x);
	return root;
}

void PreOrder(TreeNode* root)//先序遍历 
{
	if(root==NULL)
		return;
	cout<<root->data<<" ";
	PreOrder(root->left);
	PreOrder(root->right);
	return;
}

void InOrder(TreeNode* root)//中序 
{
	if(root==NULL)
		return;
	InOrder(root->left);
	cout<<root->data<<" ";
	InOrder(root->right);
	return;
}

void PostOrder(TreeNode* root)//后序 
{
	if(root==NULL)
		return;
	PostOrder(root->left);
	PostOrder(root->right);
	cout<<root->data<<" ";
	return;
}

int main()
{
	int n;
	while(cin>>n)
	{
		TreeNode* root=NULL;
		for(int i=0;i<n;++i)
		{
			int tmp;
			cin>>tmp;
			root=Insert(root,tmp); 
		}
		PreOrder(root);
		cout<<endl;
		InOrder(root);
		cout<<endl;
		PostOrder(root);
		cout<<endl;
	}
	
	
	return 0;
}

3. 优先队列

优先队列:将数据按照事先规定的优先级次序进行动态组织维护的数据结构。它类似队列,只能访问当前队列中优先级最高的元素,但不遵循先进先出的原则,而是遵循“最高级先出”规律。优先队列默认使用大顶堆,若要采用小顶堆(优先级低的先输出),应按例题2重新定义。往往用于求解与顺序或最值有关的问题。

#include<queue> //注意添加头文件

priority_queue<数据类型> PQ;//定义优先队列 

//优先队列的两种状态 
PQ.empty() //是否为空 
PQ.size()  //元素个数 

//元素增删
PQ.push(元素)
PQ.pop()

//元素访问
PQ.top() 

3.1 顺序问题

例题1:复数集合

https://www.nowcoder.com/practice/abdd24fa839c414a9b83aa9c4ecd05cc?tpId=40&tqId=21528&tPage=1&rp=1&ru=/ta/kaoyan&qru=/ta/kaoyan/question-ranking

#include<iostream>
#include<cstdio>
#include<queue>
#include<string>

using namespace std;

struct complex//定义复试结构体 
{
	int real;//实部 
	int imag;//虚部 
	complex(int a,int b):real(a),imag(b) {}
	bool operator< (complex c) const//重载小于号 
	{
		return (real*real+imag*imag) < (c.real*c.real+c.imag*c.imag);
	}
};

int main()
{
	int n;
	while(cin>>n)
	{
		priority_queue<complex> PQ;
		while(n--)
		{
			string str;
			cin>>str;
			if(str=="Pop")//Pop指令 
			{
				if(!PQ.empty())//非空 
				{
					complex tmp=PQ.top();
					cout<<tmp.real<<"+i"<<tmp.imag<<endl;
					PQ.pop();
					cout<<"SIZE = "<<PQ.size()<<endl;
				}
				else
					cout<<"empty"<<endl;
			}
			else//Insert指令
			{
				int a,b;
				scanf("%d+i%d",&a,&b);//直接从scanf获取输入的两个元素
				PQ.push(complex(a,b));
				cout<<"SIZE = "<<PQ.size()<<endl;
			} 
		}
	}	
	return 0;
}

3.2 哈夫曼树

例题2:哈夫曼树

哈夫曼树:给定n个带有权值的结点,以它们为叶子构造的一棵带权路径长度和最小的二叉树。求法:每次从节点集合中取两个权值最小的点作为叶子,它们父节点的权值设为两者之和,并将父节点加入剩余结点集合,重复上述步骤。

求长度和有两种方法:

  1. 各层叶结点x层数求和。
  2. 建树过程中构造的父节点权值求和(不涉及层数的计算)。

https://www.nowcoder.com/practice/162753046d5f47c7aac01a5b2fcda155?tpId=40&tqId=21520&tPage=1&rp=1&ru=/ta/kaoyan&qru=/ta/kaoyan/question-ranking

#include<iostream>
#include<cstdio>
#include<queue>

using namespace std;

int main()
{
	int n;
	while(cin>>n)
	{
		priority_queue<int,vector<int>,greater<int> > PQ;//重新定义优先队列 
		while(n--)
		{
			int x;
			cin>>x;
			PQ.push(x);
		} 
		int answer=0;
		while(1<PQ.size())//求带权路径长度 
		{
			int a=PQ.top();
			PQ.pop();
			int b=PQ.top();
			PQ.pop();
			answer+=(a+b);//因为每次循环都会加一次,所以不用设计层数 
			PQ.push(a+b);
		}
		cout<<answer<<endl;
	}
	
	
	
	return 0;
}

4. 散列表

一种根据关键字直接进行访问的数据结构,通过建立关键字和存储位置的直接映射关系,便可利用关键码直接访问元素。

映射模板map:将关键字key和映射值value形成一个键值对,并进行存储的容器。其底层使用红黑树实现,内部仍然有序(对关键字按字典排序),所以查找效率为O(logn)。标准库中还有一种无序映射:unordered_map,查找效率为O(1),两者的操作几乎相同。

#include<map> //注意头文件

//map的定义
map<key数据类型,value数据类型> myMap;

//map的状态
myMap.empty()
myMap.size()

//元素的增删
myMap.insert(pair<key数据类型,value数据类型>(key,value));
myMap.erase(key);

//元素的访问,三种方法 
//方法一 
myMap[key] //map内重载了[],返回值为value 
//方法二 
myMap.at(key)//返回值为value
//方法三,利用迭代器 
map<key数据类型,value数据类型>::iterator it;	
for(it=myMap.begin();it!=myMap.end();it++)
{
	cout<<it->first;//输出key值 
	cout<<it->escond;//输出value值 
} 

//map元素操作
it=myMap.find(key);//查找元素,返回迭代器
myMap.clear();//清空

//迭代器操作
myMap.begin()
myMap.end() 

例题:魔咒词典

https://www.nowcoder.com/practice/c6ca566fa3984fae916e6d7beae8ea7f?tpId=40&tqId=21477&tPage=1&rp=1&ru=/ta/kaoyan&qru=/ta/kaoyan/question-ranking

#include<iostream>
#include<cstdio>
#include<map>

using namespace std;

map<string,string> dic;

int main()
{
	string str;
	while(getline(cin,str))//避免因为空格丢失信息 
	{
		if(str=="@END@")
			break;
		int pos=str.find(']');
		string key=str.substr(0,pos+1);
		string value=str.substr(pos+2);
		//建立双向映射
		dic[key]=value;
		dic[value]=key; 
	}
	int n;
	cin>>n;
	getchar();//吃掉回车
	while(n--)
	{
		string ask;
		getline(cin,ask);
		string answer=dic[ask];
		if(answer=="")
			answer="what?";
		else if(answer[0]=='[')//输出魔咒名字不带括号
			answer=answer.substr(1,answer.size()-2); 
		cout<<answer<<endl;
	}
	
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值