二叉树操作,二叉搜索树操作,先序层序等遍历,树的旋转,并查集,最大堆建立,插入,删除,堆排序、哈弗曼树WPL(带权路径和最大)
程序来源于“算法笔记”,复习的时候手写了一遍,基础还是很有用的,这些常用的记一记感觉确实做题的时候顺畅一些
#include<iostream>
#include<vector>
#include<queue>
using namespace std;
struct node {
int data, layer;
node *lchild, *rchild;
};
node* Newnode(int x)
{
node* newnode = new node;
newnode->data = x;
newnode->lchild = newnode->rchild = NULL;
return newnode;
}
void insert(node*& root, int x)
{
if (root == NULL)
{
root = Newnode(x);
return;
}
if (左子树插入)insert(root->lchild, x);
else insert(root->rchild, x);
}
node* create(int data[], int n)
{
node* root = NULL;
for (int i = 0; i < n; i++)insert(root, data[i]);
}
void search(node* root, int x)
{
if (root == NULL)return;
if (root->data == x)
{
printf("%d", x);
return;
}
if (x > root->data)search(root->rchild, x);
else search(root->rchild, x);
}
void preorder(node* root)
{
if (root == NULL)return;
printf("%d", root->data);
preorder(root->lchild);
preorder(root->rchild);
}
void inorder(node* root)
{
if (root == NULL)return;
inorder(root->lchild);
printf("%d", root->data);
inorder(root->rchild);
}
void postorder(node* root)
{
if (root == NULL)return;
postorder(root->lchild);
postorder(root->rchild);
printf("%d", root->data);
}
void layerorder(node* root)
{
queue<node*>q;
root->layer = 1;
q.push(root);
while (!q.empty())
{
node* top = q.front();
q.pop();
printf("%d", top->data);
if (top->lchild)
{
top->lchild->layer = top->layer + 1;
q.push(top->lchild);
}
if (top->rchild)
{
top->rchild->layer = top->layer + 1;
q.push(top->rchild);
}
}
}
int pre[10000], in[100000];
node* create(int preL,int preR,int inL,int inR)
{
if (preL > preR)return NULL;
node* root = new node;
root->data = pre[preL];
int k;
for (k = inL; k <= inR; k++)
{
if (in[k] == root->data)break;
}
int numleft = k - inL;
root->lchild = create(preL + 1, preL + numleft, inL, k - 1);
root->rchild = create(preL + numleft + 1, preR, k + 1, inR);
}
int post[10000];
node* create(int postL,int postR,int inL,int inR)
{
node* root = NULL;
root->data = post[postL];
int k;
for (k = inL; k <= inR; k++)
{
if (in[k] == root->data)break;
}
int numleft = k - inL;
root->lchild = create(postL, postL + numleft - 1, inL, k - 1);
root->rchild = create(postL + numleft, postR, k + 1, inR);
}
const int maxn = 1e5 + 10;
struct node
{
int lchild, rchild, data;
}Node[maxn];
int index = 0;
int Newnode(int x)
{
Node[index].data = x;
Node[index].lchild = Node[index].rchild = -1;
return index++;
}
void insert(int& root, int x)
{
if (root == -1)
{
root = Newnode(x);
return;
}
if ()insert(Node[root].lchild, x);
else insert(Node[root].rchild, x);
}
int create(int data[], int n)
{
int root = -1;
for (int i = 0; i < n; i++)insert(root, data[i]);
}
void preorder(int root)
{
if (root == -1)return;
printf("%d", Node[root].data);
preorder(Node[root].lchild);
preorder(Node[root].rchild);
}
void inorder(int root)
{
if (root == -1)return;
inorder(Node[root].lchild);
printf("%d", Node[root].data);
inorder(Node[root].rchild);
}
void postorder(int root)
{
if (root == -1)return;
postorder(Node[root].lchild);
postorder(Node[root].rchild);
printf("%d", Node[root].data);
}
void Layerorder(int root)
{
queue<int>q;
q.push(root);
while (!q.empty())
{
int top = q.front();
q.pop();
printf("%d", Node[top].data);
if (Node[top].lchild != -1)q.push(Node[top].lchild);
if (Node[top].rchild != -1)q.push(Node[top].rchild);
}
}
const int maxn = 1e5 + 10;
struct node {
int data, layer;
vector<int>child;
}Node[maxn];
int index = 0;
int Newnode(int x)
{
Node[index].data = x;
Node[index].child.clear();
return index++;
}
void preorder(int root)
{
if (root == -1)return;
printf("%d", Node[root].data);
for (int i = 0; i < Node[root].child.size(); i++)
preorder(Node[root].child[i]);
}
void layerorder(int root)
{
queue<int>q;
Node[root].layer = 1;
q.push(root);
while (!q.empty())
{
int top = q.front();
q.pop();
for (int i = 0; i < Node[top].child.size(); i++)
{
int child = Node[top].child[i];
Node[child].layer = Node[top].layer + 1;
q.push(child);
}
}
}
struct node {
int v, height;
node *lchild, *rchild;
};
int getHeight(node* root)
{
if (root == NULL)return 0;
return root->height;
}
int getBalanceFactor(node* root)
{
return getHeight(root->lchild) - getHeight(root->rchild);
}
void updateHeight(node* root)
{
root->height = max(getHeight(root->lchild), getHeight(root->rchild)) + 1;
}
void R(node* root)
{
node* temp = root->lchild;
root->lchild = temp->rchild;
temp->rchild = root;
updateHeight(temp);
updateHeight(root);
root = temp;
}
void L(node* root)
{
node* temp = root->rchild;
root->rchild = temp->lchild;
temp->lchild = root;
updateHeight(temp);
updateHeight(root);
root = temp;
}
node* Newnode(int x)
{
node* newnode = new node;
newnode->v = x;
newnode->lchild = newnode->rchild = NULL;
return newnode;
}
void insert(node* root,int v)
{
if (root == NULL)
{
root = Newnode(v);
return;
}
if (v < root->v)
{
insert(root->lchild, v);
updateHeight(root);
if (getBalanceFactor(root) == 2)
{
if (getBalanceFactor(root->lchild) == 1)R(root);
else if (getBalanceFactor(root->lchild) == -1)
{
L(root->lchild);
R(root);
}
}
}
else
{
insert(root->rchild, v);
updateHeight(root);
if (getBalanceFactor(root) == 2)
{
if (getBalanceFactor(root->rchild) == -1)L(root);
else if (getBalanceFactor(root->rchild) == 1)
{
R(root->rchild);
L(root);
}
}
}
}
int father[10000], n;
void init()
{
for (int i = 0; i <= n; i++)father[i] = i;
}
int findfather(int x)
{
while (x != father[x])x = father[x];
return x;
}
void Union(int a, int b)
{
int fa = findfather(a);
int fb = findfather(b);
father[a] = b;
}
int findfather(int x)
{
int a = x;
while (x != father[x])x = father[x];
while (a != father[a])
{
int z = a;
a = father[a];
father[z] = x;
}
return x;
}
int heap[100000], n;
void downAdjust(int low, int high)
{
int i = low, int j = 2 * i;
while (j <= high)
{
if (j+1<=high&&heap[j + 1] > heap[j])j++;
if (heap[j] > heap[i])
{
swap(heap[j], heap[i]);
i = j;
j = i * 2;
}
else break;
}
}
void create()
{
for (int i = n / 2; i >= 1; i--)downAdjust(i, n);
}
void deleteTop()
{
heap[1] = heap[n--];
downAdjust(1, n);
}
void upAdjust(int low, int high)
{
int i = high, j = i / 2;
while (j >= low)
{
if (heap[i] > heap[j])
{
swap(heap[i], heap[j]);
i = j;
j = i / 2;
}
else break;
}
}
void insert(int x)
{
heap[n++] = x;
upAdjust(1, n);
}
void heapSort()
{
create();
for (int i = n; i >1; i--)
{
swap(heap[1], heap[i]);
downAdjust(1, i-1);
}
}
priority_queue<long long,vector<long long>,greater<long long>>
priority_queue<long long,vector<long long>,less<long long>>
queue<int>q;
while(q.size()>1)
{
int a = q.front();
q.pop();
int b = q.front();
q.pop();
q.push(a + b);
}