对应知识点 https://blog.csdn.net/qq_50882544/article/details/120765639
树
二叉树
最大(小)堆
- 修理牧场
1. 学到的知识点:用priority_queue优先级队列来构造最(大)小堆。
AC代码:
#include<iostream>
#include<queue>
#include<vector>
using namespace std;
int main() {
priority_queue<int, vector<int>, greater<int>> minHeap;//优先队列里面的最小堆
int N;
int weight, num1, num2;
cin >> N;//进行新一轮的输入
for (int i = 1; i <=N; ++i) {
cin >> num1;//逐个输入元素
minHeap.push(num1);//将元素压入最小堆
}
weight = 0;
while (minHeap.size() > 1) {//当最小堆的元素大于1
num1 = minHeap.top();//第一个最小的元素
minHeap.pop();//删除这个元素
num2 = minHeap.top();//第二个最小的元素
minHeap.pop();
weight += num1 + num2;//获取最小的权重加和
minHeap.push(num1 + num2);//将最小的权重加和放回到最小堆中
}
cout << weight << endl;
return 0;
}
关于STL中的priority_queue优先级队列的介绍https://blog.csdn.net/qq_50882544/article/details/120913552
二叉搜索树
- Height of BST
1. 学到的知识点:根据不同的序列构造BST
#include<iostream>
#include<queue>
#define MAXN 100//定义数组最大为100
using namespace std;
struct BiNode {
int data;
BiNode* left, * right;
};
typedef BiNode* BiTree;
void preOrder(BiTree T) {
if (T) {
cout << T->data << " ";
preOrder(T->left);
preOrder(T->right);
}
}
void inOrder(BiTree T) {
if (T) {
inOrder(T->left);
cout << T->data << " ";
inOrder(T->right);
}
}
void postOrder(BiTree T) {
if (T) {
postOrder(T->left);
postOrder(T->right);
cout << T->data << " ";
}
}
void levelOrder(BiTree T) {
if (T) {
queue<BiTree> p;
p.push(T);
while (!p.empty()) {
BiTree temp = p.front();
cout << p.front()->data<<" ";
p.pop();
if (temp->left) p.push(temp->left);
if (temp->right) p.push(temp->right);
}
}
else cout << "空树" << endl;
}
int getHeight(BiTree T) {
if (T) {
int hl = getHeight(T->left);
int hr = getHeight(T->right);
if (hl > hr) return++hl;
else return ++hr;
}
else return 0;
}
//根据前序序列建BST,返回树的根
BiTree creatTree(int preorder[], int start, int end) {
if (start > end) {//如果左边查找大于右边查找
return NULL;
}
if (start == end) {//有结点,直接构造结点,并返回
BiTree tempNode = (BiTree)malloc(sizeof(struct BiNode));
tempNode->data = preorder[start];
tempNode->left = tempNode->right = NULL;
return tempNode;
}
BiTree T = new BiNode();//用于存储根结点
T->left = T->right = NULL;
T->data = preorder[start];
int index = -1;//下标
for (int i = start + 1; i <= end; ++i) {//start值为子树的根,从其右边一个开始查找
if (preorder[i] > preorder[start]) {//和根植比较
index = i;//index最终停在不满足的位置
break;
}
}
//如果index为-1,证明后续值都比root的值要小,证明root只有左子树没有右子树
if (index == -1) {
index = end + 1;//这种情况index比所有元素都大,最后应该停在end的后一个位置
}
//如果根据index值,去构建当前root结点的左右子树
T->left = creatTree(preorder, start + 1, index - 1);//左边从当前查找的后一个结点,右边有恰好满足的结点
T->right = creatTree(preorder, index, end);//左边从正好满足的点,右边查找到末尾
return T;
}
int main()
{
int postorder[MAXN], N, i;
//preorder[MAXN]为前序序列数组,N为数组个数
cin >> N;
for (i = 0; i < N; i++) cin >> postorder[i];
BiTree BT = creatTree(postorder, 0,N-1);
cout << "前序序列:";
preOrder(BT);
cout << endl;
cout << "中序序列:";
inOrder(BT);
cout << endl;
cout << "后序序列:";
postOrder(BT);
cout << endl;
cout << "层序序列:";
levelOrder(BT);
cout << endl;
cout << "树的高度:"<< getHeight(BT);;
return 0;
}
}
//将preorder[MAXN]替换为postorder[MAXN],前序变后序建造BST
BiTree creatTree(int postorder[], int N) {
int i = 0, root = 0;
BiTree T = new BiNode();
if (N == 0) {
return NULL;
}
root = postorder[N - 1];//确定根值
T->data = root;
//确定
while (i < N) {
if (postorder[i] >= root) {
break;
}
i++;
}
T->left = creatTree(postorder, i);
T->right = creatTree(postorder + i, N - 1 - i);
return T;
}
AC代码:
#include <stdio.h>
#include <stdlib.h>
#define MAXN 10
int Height_of_BST(int preorder[], int N);
int main()
{
int preorder[MAXN], N, i;
scanf_s("%d", &N);
for (i = 0; i < N; i++) scanf_s("%d", &preorder[i]);
printf("%d\n", Height_of_BST(preorder, N));
return 0;
}
typedef struct BiNode{
int data;
struct BiNode* left, *right;
}*BiTree;
BiTree creatTree(int preorder[],int start,int end) {
if (start > end) {
return NULL;
}
if (start == end) {//有结点,直接构造结点,并返回
BiTree tempNode = (BiTree)malloc(sizeof(struct BiNode));
tempNode->data = preorder[start];
tempNode->left = tempNode->right = NULL;
return tempNode;
}
BiTree T = (BiTree)malloc(sizeof(struct BiNode));//用于存储根结点
T->left = T->right = NULL;
T->data = preorder[start];
int index = -1;
for (int i = start + 1; i <= end; ++i) {
if (preorder[i] > preorder[start]) {
index = i;
break;
}
}
//如果index为1,证明后续值都比root的值要小,证明root只有左子树没有右子树
if (index == -1) {
index = end + 1;
}
//如果根据index值,去构建当前root结点的左右子树
T->left = creatTree(preorder, start + 1, index - 1);
T->right = creatTree(preorder, index, end);
return T;
}
int getHeight(BiTree BT) {
int hl = 0, hr = 0, height = 0;
if (BT==NULL) {
return 0;
}
hl = getHeight(BT->left);
hr = getHeight(BT->right);
if (hl > hr) return ++hl;
else return ++hr;
}
int Height_of_BST(int preorder[], int N) {
if (N== 0) {//空树返回
return NULL;
}
int start = 0;
int end = N - 1;
BiTree BT = creatTree(preorder, start,end);
//如果根是0层,则减1;如果为1,则不减
return getHeight(BT)-1;
}
算法
1. 数组元素得存储位置问题
一维数组:
二维数组:
高维数组:
按行优先:
结合上边的高维数组存储图片便于理解
例如D[2][0][0],就是2(代表两个大方格)*3(代表三行)*4(代表4列)。
按列优先: