天梯赛&&HBU训练营——7-2树的遍历

给定一棵二叉树的后序遍历和中序遍历,请你输出其层序遍历的序列。这里假设键值都是互不相等的正整数。

输入格式:
输入第一行给出一个正整数N(≤30),是二叉树中结点的个数。第二行给出其后序遍历序列。第三行给出其中序遍历序列。数字间以空格分隔。

输出格式:
在一行中输出该树的层序遍历的序列。数字间以1个空格分隔,行首尾不得有多余空格。

输入样例:
7
2 3 1 5 7 6 4
1 2 3 4 5 6 7
输出样例:
4 1 6 3 5 7 2
知识点
前序遍历:根左右
中序遍历:左根右
后序遍历:左右根

思路:
root:当前树在后序遍历中根的位置
start:当前树在中序遍历中开始的位置
end:当前树在中序遍历中结束的位置
index:当前子树根所在位置数

则可知后序遍历中2 3 1 5 7 6 4末尾4为根,想知道“左右根”中左右子树分界,则可求出中序遍历中根的位置i,求出右子树的数目end-i
左子树:
根在后序遍历中位置root-1-(end-i)
在中序遍历中从start到i-1
右子树:
根位于前序遍历中位置root+(i-start+1)
在中序遍历中从i+1到end
运用递归,求出二叉树

二叉树
在这里插入图片描述
代码:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
using namespace std;
vector<int> last,mid,level(100000,-1);
void fun(int root,int start,int end,int index){
	//后序:左右根,根为后续遍历最后一个数root
	//中序:左根右, 在中序遍历中从start到end寻找root,将root左右两边分为左子树,右子树 
		int i=start;
		if(start>end) 
			return;
	while(i<end&&mid[i]!=last[root]) i++;//i为中序遍历中根的位置 
	level[index]=last[root];;
	fun(root-1-end+i,start,i-1,2*index+1);//2*index+1,index的左子树
	//end-i为中序遍历右子树大小 
	//root-1-(end-i)为后序遍历:左右根-根-有=左子树 
	fun(root-1,i+1,end,2*index+2);//2*index+2,index的右子树
	//根为右子树后续遍历root左边的数,求左子树 
} 
int main(){
	int n;
	cin>>n;
	last.resize(n);
	mid.resize(n);
	for(int i=0;i<n;i++)
		cin>>last[i];
	for(int i=0;i<n;i++)
		cin>>mid[i];
		fun(n-1,0,n-1,0);
		for(int i=0,cnt=0;i<level.size();i++){
			if(level[i]!=-1){
				cout<<level[i];
				if(cnt!=n-1) cout<<" ";
				cnt++;
			}
		}
		return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SVM算法通过将数据映射到高维空间,将数据分为两个类别。SVM算法的目标是找到一个超平面,可以将数据分为两个类别。SMO算法是一种优化算法,用于求解SVM中的二次规划问题。下面介绍如何使用SMO算法编写SVM对CIFAR-10数据进行分类。 首先,我们需要加载CIFAR-10数据集。CIFAR-10数据集包含10个类别的60000个32x32彩色图像。每个类别包含6000个图像。我们将使用Python中的pickle模块来加载数据集。以下是加载数据集的代码: ```python import pickle import numpy as np def unpickle(file): with open(file, 'rb') as fo: dict = pickle.load(fo, encoding='bytes') return dict def load_cifar10_data(): xs = [] ys = [] for j in range(5): d = unpickle('cifar-10-batches-py/data_batch_%d' % (j + 1)) x = d[b'data'] y = d[b'labels'] xs.append(x) ys.append(y) d = unpickle('cifar-10-batches-py/test_batch') xs.append(d[b'data']) ys.append(d[b'labels']) x = np.concatenate(xs) / np.float32(255) y = np.concatenate(ys) return x.reshape((len(x), -1)), np.array(y) ``` 接下来,我们将使用SMO算法来训练SVM模型。以下是使用SMO算法训练SVM模型的代码: ```python class SVM: def __init__(self, C, toler, kernel_opt=('linear', 0)): self.C = C self.toler = toler self.kernel_opt = kernel_opt def fit(self, X, y): n_samples, n_features = X.shape alpha = np.zeros(n_samples) b = 0 kernel = kernel_set[self.kernel_opt[0]] K = np.zeros((n_samples, n_samples)) for i in range(n_samples): K[:, i] = kernel(X, X[i], self.kernel_opt[1]) iter = 0 while iter < max_iter: num_changed_alphas = 0 for i in range(n_samples): Ei = np.dot(alpha * y, K[:, i]) + b - y[i] if (y[i] * Ei < -self.toler and alpha[i] < self.C) or \ (y[i] * Ei > self.toler and alpha[i] > 0): j = np.random.choice([x for x in range(n_samples) if x != i]) Ej = np.dot(alpha * y, K[:, j]) + b - y[j] alpha_i_old, alpha_j_old = alpha[i], alpha[j] if y[i] != y[j]: L = max(0, alpha[j] - alpha[i]) H = min(self.C, self.C + alpha[j] - alpha[i]) else: L = max(0, alpha[i] + alpha[j] - self.C) H = min(self.C, alpha[i] + alpha[j]) if L == H: continue eta = 2.0 * K[i, j] - K[i, i] - K[j, j] if eta >= 0: continue alpha[j] -= y[j] * (Ei - Ej) / eta alpha[j] = min(alpha[j], H) alpha[j] = max(alpha[j], L) if abs(alpha[j] - alpha_j_old) < 1e-5: continue alpha[i] += y[i] * y[j] * (alpha_j_old - alpha[j]) b1 = b - Ei - y[i] * (alpha[i] - alpha_i_old) * K[i, i] - \ y[j] * (alpha[j] - alpha_j_old) * K[i, j] b2 = b - Ej - y[i] * (alpha[i] - alpha_i_old) * K[i, j] - \ y[j] * (alpha[j] - alpha_j_old) * K[j, j] if 0 < alpha[i] < self.C: b = b1 elif 0 < alpha[j] < self.C: b = b2 else: b = (b1 + b2) / 2 num_changed_alphas += 1 if num_changed_alphas == 0: iter += 1 else: iter = 0 self.X = X self.y = y self.kernel = kernel self.alpha = alpha self.b = b def predict(self, X): n_samples, n_features = X.shape K = np.zeros((n_samples, len(self.X))) for i in range(n_samples): K[i, :] = self.kernel(self.X, X[i], self.kernel_opt[1]) y_pred = np.dot(self.alpha * self.y, K) + self.b return np.sign(y_pred) ``` 最后,我们使用以下代码来加载数据集并使用SMO算法训练SVM模型: ```python X, y = load_cifar10_data() y[y == 0] = -1 X_train, X_test = X[:50000], X[50000:] y_train, y_test = y[:50000], y[50000:] svm = SVM(C=1.0, toler=0.001, kernel_opt=('rbf', 1)) svm.fit(X_train, y_train) y_pred_train = svm.predict(X_train) y_pred_test = svm.predict(X_test) train_acc = np.mean(y_train == y_pred_train) test_acc = np.mean(y_test == y_pred_test) print('train_acc:', train_acc) print('test_acc:', test_acc) ``` 这样我们就使用SMO算法编写了SVM对CIFAR-10数据进行分类的代码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值