基础数据结构模板
mountain_ZGY · · 算法·理论
基础数据结构模板
::::::info[链表] :::::info[静态链表] ::::info[单向链表]
#include<bits/stdc++.h>
using namespace std;
struct Node{
int num,next;
};
struct node{
Node a[200010];
int head=0,tail=0,len=0;
queue<int> q;
void initList(){//初始化
for(int i=1;i<=200000;i++){
q.push(i);
}
len=0;
}
bool empty(){//判空
return len==0;
}
int size(){//长度
return len;
}
void push_front(int x){//在链表前面插入
int t=q.front();
q.pop();
len++;
a[t].num=x;
a[t].next=head;
head=t;
if(tail==0){
tail=t;
}
}
void push_back(int x){//在链表后面插入
int t=q.front();
q.pop();
a[t].num=x;
a[t].next=0;
if(len!=0){
a[tail].next=t;
}
len++;
tail=t;
if(head==0){
head=t;
}
}
void pop_front(){//删除链表头部
if(len==0){
return;
}
len--;
q.push(head);
if(a[head].next==0){
a[head].num=0;
head=0;
tail=0;
}else{
int t=a[head].next;
a[head].next=0;
a[head].num=0;
head=t;
}
}
void pop_back(){//删除链表尾部
if(len==0){
return;
}
len--;
if(head==tail){
q.push(head);
a[head].num=0;
head=0;
tail=0;
return;
}
int cur=head;
while(a[cur].next!=tail){
cur=a[cur].next;
}
q.push(tail);
a[tail].num=0;
a[cur].next=0;
tail=cur;
}
void insertAfter(int x,int y){//在指定元素后插入
if(head==0){
return;
}
if(a[tail].num==x){
push_back(y);
return;
}
int pos=head;
while(pos!=0&&a[pos].num!=x){
pos=a[pos].next;
}
if(pos==0){
return;
}
len++;
int t=q.front();
q.pop();
a[t].num=y;
a[t].next=a[pos].next;
a[pos].next=t;
if(a[t].next==0){
tail=t;
}
}
int front(){//返回头部元素
if(head==0){
return -1;
}
return a[head].num;
}
int back(){//返回尾部元素
if(tail==0){
return -1;
}
return a[tail].num;
}
void eraseValue(int pos){//按值删除
if(len==0){
return;
}
if(pos==a[head].num){
pop_front();
return;
}
int prev=head,cur=a[head].next;
while(cur!=0&&a[cur].num!=pos){
prev=cur;
cur=a[cur].next;
}
if(cur==0){
return;
}
len--;
a[prev].next=a[cur].next;
if(cur==tail){
tail=prev;
}
q.push(cur);
a[cur].num=0;
a[cur].next=0;
}
void eraseAt(int k){//按位置删除
if(k<=0||k>len){
return;
}
if(k==1){
pop_front();
return;
}
int cur=head;
for(int i=2;i<=k-1;i++){
cur=a[cur].next;
}
len--;
int target=a[cur].next;
a[cur].next=a[target].next;
if(target==tail){
tail=cur;
}
q.push(target);
a[target].num=0;
a[target].next=0;
}
void reverse(){//反转链表
if(head==0||a[head].next==0){
return;
}
int prev=0,cur=head,nextnode=0;
while(cur!=0){
nextnode=a[cur].next;
a[cur].next=prev;
prev=cur;
cur=nextnode;
}
int t=head;
head=tail;
tail=t;
}
void clear(){//清空链表
int cur=head;
while(cur!=0){
int nextnode=a[cur].next;
a[cur].num=0;
a[cur].next=0;
q.push(cur);
cur=nextnode;
}
len=0;
head=0;
tail=0;
}
void printList(){//按顺序输出链表
int cur=head;
while(cur!=0){
cout<<a[cur].num<<" -> ";
cur=a[cur].next;
}
cout<<"NULL\n";
}
};
node a;//定义链表
int main(){
a.initList();
if(a.empty()){
cout<<"No"<<endl;
}else{
cout<<"Yes"<<endl;
}
a.push_back(10);
a.push_back(20);
a.push_back(30);
a.printList();
cout<<a.front()<<endl;
cout<<a.back()<<endl;
cout<<a.size()<<endl;
a.pop_front();
a.pop_front();
a.printList();
cout<<a.front()<<endl;
cout<<a.back()<<endl;
cout<<a.size()<<endl;
a.push_front(40);
a.push_front(50);
a.push_front(60);
a.printList();
cout<<a.front()<<endl;
cout<<a.back()<<endl;
cout<<a.size()<<endl;
a.pop_back();
a.printList();
cout<<a.front()<<endl;
cout<<a.back()<<endl;
cout<<a.size()<<endl;
if(a.empty()){
cout<<"No"<<endl;
}else{
cout<<"Yes"<<endl;
}
a.reverse();
a.printList();
cout<<a.front()<<endl;
cout<<a.back()<<endl;
cout<<a.size()<<endl;
a.eraseAt(2);
a.printList();
cout<<a.front()<<endl;
cout<<a.back()<<endl;
cout<<a.size()<<endl;
a.eraseValue(40);
a.printList();
cout<<a.front()<<endl;
cout<<a.back()<<endl;
cout<<a.size()<<endl;
a.push_front(40);
a.eraseValue(60);
a.printList();
cout<<a.front()<<endl;
cout<<a.back()<<endl;
cout<<a.size()<<endl;
a.eraseValue(100);
a.printList();
cout<<a.front()<<endl;
cout<<a.back()<<endl;
cout<<a.size()<<endl;
a.clear();
a.printList();
cout<<a.size()<<endl;
a.push_back(10);
a.push_back(20);
a.push_back(30);
a.printList();
cout<<a.front()<<endl;
cout<<a.back()<<endl;
cout<<a.size()<<endl;
a.insertAfter(20,40);
a.printList();
cout<<a.front()<<endl;
cout<<a.back()<<endl;
cout<<a.size()<<endl;
return 0;
}
:::: ::::info[双向链表]
#include<bits/stdc++.h>
using namespace std;
struct Node{
int pre,nxt,num;
};
struct node{
Node a[100010];
int head=0,tail=0,len=0;
queue<int> q;
void initList(){//初始化
for(int i=1;i<=100000;i++){
q.push(i);
}
len=0;
}
bool empty(){//判空
return len==0;
}
int size(){//返回链表长度
return len;
}
void push_front(int x){//从头部插入元素
int t=q.front();
q.pop();
a[t].num=x;
a[t].nxt=head;
a[t].pre=0;
if(len!=0){
a[head].pre=t;
}
head=t;
len++;
if(tail==0){
tail=t;
}
}
void push_back(int x){//从尾部插入元素
int t=q.front();
q.pop();
a[t].num=x;
a[t].nxt=0;
a[t].pre=tail;
if(len!=0){
a[tail].nxt=t;
}
tail=t;
len++;
if(head==0){
head=t;
}
}
void pop_front(){//删除头部元素
if(len==0){
return;
}
len--;
q.push(head);
a[head].num=0;
if(a[head].nxt==0){
head=0;
tail=0;
}else{
int t=a[head].nxt;
a[head].nxt=0;
head=t;
}
}
void pop_back(){//删除尾部元素
if(len==0){
return;
}
len--;
q.push(tail);
a[tail].num=0;
if(a[tail].pre==0){
head=0;
tail=0;
}else{
int t=a[tail].pre;
a[tail].pre=0;
tail=t;
}
}
void insertBefore(int x,int y){//在元素前插入
if(len==0){
return;
}
if(a[head].num==x){
push_front(y);
return;
}
int pos=head;
while(pos!=0&&a[pos].num!=x){
pos=a[pos].nxt;
}
if(pos==0){
return;
}
len++;
int t=q.front();
q.pop();
a[t].num=y;
a[t].nxt=pos;
a[t].pre=a[pos].pre;
if(a[pos].pre!=0) a[a[pos].pre].nxt=t;
else head=t;
a[pos].pre=t;
}
void insertAfter(int x,int y){//在元素后插入
if(len==0){
return;
}
if(a[tail].num==x){
push_back(y);
return;
}
int pos=head;
while(pos!=0&&a[pos].num!=x){
pos=a[pos].nxt;
}
if(pos==0){
return;
}
len++;
int t=q.front();
q.pop();
a[t].num=y;
a[t].nxt=a[pos].nxt;
a[t].pre=pos;
if(a[t].nxt!=0){
a[a[t].nxt].pre=t;
}
a[pos].nxt=t;
if(a[t].nxt==0){
tail=t;
}
}
int front(){//返回头部元素
if(len==0){
return -1;
}
return a[head].num;
}
int back(){//返回尾部元素
if(len==0){
return -1;
}
return a[tail].num;
}
void eraseValue(int pos){//按值删除
if(len==0){
return;
}
if(pos==a[head].num){
pop_front();
return;
}
int prev=head,cur=a[head].nxt;
while(cur!=0&&a[cur].num!=pos){
prev=cur;
cur=a[cur].nxt;
}
if(cur==0){
return;
}
len--;
a[prev].nxt=a[cur].nxt;
if(a[cur].nxt!=0) a[a[cur].nxt].pre=prev;
if(cur==tail) tail=prev;
q.push(cur);
a[cur].num=0;
a[cur].nxt=0;
a[cur].pre=0;
}
void eraseAt(int k){//按位置删除
if(k<=0||k>len){
return;
}
if(k==1){
pop_front();
return;
}
int cur=head;
for(int i=2;i<=k-1;i++){
cur=a[cur].nxt;
}
len--;
int t=a[cur].nxt;
a[cur].nxt=a[t].nxt;
if(a[t].nxt!=0) a[a[t].nxt].pre=cur;
if(t==tail){
tail=cur;
}
q.push(t);
a[t].num=0;
a[t].nxt=0;
a[t].pre=0;
}
void reverse(){//反转链表
if(len<=1){
return;
}
int cur=head;
while(cur!=0){
int temp=a[cur].pre;
a[cur].pre=a[cur].nxt;
a[cur].nxt=temp;
cur=a[cur].pre;
}
int temp=head;
head=tail;
tail=temp;
}
void clear(){//清空链表
int cur=head;
while(cur!=0){
int nextnode=a[cur].nxt;
a[cur].num=0;
a[cur].nxt=0;
a[cur].pre=0;
q.push(cur);
cur=nextnode;
}
len=0;
head=0;
tail=0;
}
void printList(){//按顺序输出链表
int cur=head;
while(cur!=0){
cout<<a[cur].num<<" -> ";
cur=a[cur].nxt;
}
cout<<"NULL\n";
}
};
node L;//定义链表
int main(){
L.initList();
L.push_back(10);
L.push_back(20);
L.push_back(30);
L.printList();
L.push_front(5);
L.printList();
L.insertAfter(20,25);
L.printList();
L.insertAfter(30,35);
L.printList();
L.insertBefore(10,8);
L.printList();
L.eraseValue(25);
L.printList();
L.eraseAt(2);
L.printList();
L.pop_front();
L.pop_back();
L.printList();
L.reverse();
L.printList();
cout<<L.front()<<" "<<L.back()<<" "<<L.size()<<endl;
L.clear();
L.printList();
cout<<L.size()<<endl;
return 0;
}
| :::: |
|---|
| :::::info[动态链表] |
| ::::info[单向链表] |
| ```cpp |
| #include<bits/stdc++.h> |
| using namespace std; |
| struct Node{ |
| int data; |
| Node *next; |
| Node(int val=0, Node *nxt=NULL):data(val),next(nxt){} |
| }; |
| struct LinkedList{ |
| Node *head; |
| LinkedList():head(NULL){} |
| void push_front(int val){//从前面插入 |
| head=new Node(val,head); |
| } |
| void push_back(int val){//从后面插入 |
| Node *newnode=new Node(val); |
| if(head==NULL){ |
| head=newnode; |
| return; |
| } |
| Node *cur=head; |
| while(cur->next!=NULL){ |
| cur=cur->next; |
| } |
| cur->next=newnode; |
| } |
| void insertAfter(Node *cur,int val){//在指定节点后插入 |
| if(cur==NULL){ |
| return; |
| } |
| Node *newnode=new Node(val,cur->next); |
| cur->next=newnode; |
| } |
| void deletenode(int val){//按值删除节点 |
| if(head==NULL){ |
| return; |
| } |
| if(head->data==val){ |
| Node *temp=head; |
| head=head->next; |
| delete temp; |
| return; |
| } |
| Node *prev=head; |
| Node *cur=head->next; |
| while(cur!=NULL&&cur->data!=val){ |
| prev=cur; |
| cur=cur->next; |
| } |
| if(cur!=NULL){ |
| prev->next=cur->next; |
| delete cur; |
| } |
| } |
| Node *find(int val){//按值查找节点 |
| Node *cur=head; |
| while(cur!=NULL){ |
| if(cur->data==val){ |
| return cur; |
| } |
| cur=cur->next; |
| } |
| return NULL; |
| } |
| void printList(){//正向遍历打印 |
| Node *cur=head; |
| while(cur!=NULL){ |
| cout<<cur->data<<" -> "; |
| cur=cur->next; |
| } |
| cout<<"NULL\n"; |
| } |
| void freeList(){//释放整个链表 |
| while(head!=NULL){ |
| Node *temp=head; |
| head=head->next; |
| delete temp; |
| } |
| } |
| void reverseList(){//反转链表 |
| if(head==NULL |
| return; |
| } |
| Node *prev=NULL; |
| Node *cur=head; |
| Node *nextnode=NULL; |
| while(cur!=NULL){ |
| nextnode=cur->next; |
| cur->next=prev; |
| prev=cur; |
| cur=nextnode; |
| } |
| head=prev; |
| } |
| int get_front(){//获取头部元素 |
| if(head==NULL) return -1; |
| return head->data; |
| } |
| int get_back(){//获取尾部元素 |
| if(head==NULL) return -1; |
| Node *cur=head; |
| while(cur->next!=NULL){ |
| cur=cur->next; |
| } |
| return cur->data; |
| } |
| }; |
| int main(){ |
| LinkedList a; |
| a.push_back(10); |
| a.push_back(20); |
| a.push_back(30); |
| a.printList(); |
| cout<<a.get_front()<<endl; |
| cout<<a.get_back()<<endl; |
| a.deletenode(10); |
| a.printList(); |
| cout<<a.get_front()<<endl; |
| cout<<a.get_back()<<endl; |
| a.reverseList(); |
| a.printList(); |
| cout<<a.get_front()<<endl; |
| cout<<a.get_back()<<endl; |
| a.freeList(); |
| return 0; |
| } |
| ``` |
| :::: |
| ::::info[双向链表] |
| ```cpp |
| #include<bits/stdc++.h> |
| using namespace std; |
| struct Node{ |
| int data; |
| Node *prev; |
| Node *next; |
| Node(int val=0, Node p=NULL, Node n=NULL):data(val),prev(p),next(n){} |
| }; |
| struct LinkedList{ |
| Node *head; |
| LinkedList():head(NULL){} |
| void insertAfter(Node *cur,int val){//在指定节点后插入 |
| if(cur==NULL){ |
| return; |
| } |
| Node *newnode=new Node(val,cur,cur->next); |
| if(cur->next!=NULL){ |
| cur->next->prev=newnode; |
| } |
| cur->next=newnode; |
| } |
| void insertBefore(Node *cur,int val){//在指定节点前插入 |
| if(cur==NULL){ |
| return; |
| } |
| Node *newnode=new Node(val,cur->prev,cur); |
| if(cur->prev!=NULL){ |
| cur->prev->next=newnode; |
| }else{ |
| head=newnode; // cur 是头节点,更新 head |
| } |
| cur->prev=newnode; |
| } |
| void deletenode(Node *cur){//删除指定节点 |
| if(cur==NULL){ |
| return; |
| } |
| if(cur->prev!=NULL){ |
| cur->prev->next=cur->next; |
| }else{ |
| head=cur->next; // 删的是头节点,更新 head |
| } |
| if(cur->next!=NULL){ |
| cur->next->prev=cur->prev; |
| } |
| cur->prev=NULL; |
| cur->next=NULL; |
| delete cur; |
| } |
| void push_front(int val){//从前面插入 |
| if(head==NULL){ |
| head=new Node(val); |
| }else{ |
| insertBefore(head,val); |
| } |
| } |
| void push_back(int val){//从后面插入 |
| if(head==NULL){ |
| head=new Node(val); |
| return; |
| } |
| Node *cur=head; |
| while(cur->next!=NULL){ |
| cur=cur->next; |
| } |
| insertAfter(cur,val); |
| } |
| Node *find(int val){//按值查找节点 |
| Node *cur=head; |
| while(cur!=NULL){ |
| if(cur->data==val){ |
| return cur; |
| } |
| cur=cur->next; |
| } |
| return NULL; |
| } |
| void printList(){//正向遍历打印 |
| Node *cur=head; |
| while(cur!=NULL){ |
| cout<<cur->data<<" <-> "; |
| cur=cur->next; |
| } |
| cout<<"NULL\n"; |
| } |
| void freeList(){//释放整个链表 |
| while(head!=NULL){ |
| Node *temp=head; |
| head=head->next; |
| delete temp; |
| } |
| } |
| int get_front(){//获取头部元素 |
| if(head==NULL){ |
| return -1; |
| } |
| return head->data; |
| } |
| int get_back(){//获取尾部元素 |
| if(head==NULL){ |
| return -1; |
| } |
| Node *cur=head; |
| while(cur->next!=NULL){ |
| cur=cur->next; |
| } |
| return cur->data; |
| } |
| }; |
| int main(){ |
| LinkedList a; |
| a.push_front(10); |
| a.push_front(20); |
| a.push_front(30); |
| a.printList(); |
| cout<<a.get_front()<<endl; |
| cout<<a.get_back()<<endl; |
| a.push_back(40); |
| a.push_back(50); |
| a.printList(); |
| cout<<a.get_front()<<endl; |
| cout<<a.get_back()<<endl; |
| Node *pos=a.find(20); |
| if(pos!=NULL){ |
| a.insertAfter(pos,25); |
| } |
| a.printList(); |
| cout<<a.get_front()<<endl; |
| cout<<a.get_back()<<endl; |
| Node *pos1=a.find(40); |
| if(pos1!=NULL){ |
| a.insertBefore(pos1,35); |
| } |
| a.printList(); |
| cout<<a.get_front()<<endl; |
| cout<<a.get_back()<<endl; |
| Node *pos2=a.find(25); |
| if(pos2!=NULL){ |
| a.deletenode(pos2); |
| } |
| a.printList(); |
| cout<<a.get_front()<<endl; |
| cout<<a.get_back()<<endl; |
| if(a.head!=NULL){ |
| a.deletenode(a.head); |
| } |
| a.printList(); |
| cout<<a.get_front()<<endl; |
| cout<<a.get_back()<<endl; |
| Node *pos3=a.find(50); |
| if(pos3!=NULL){ |
| a.deletenode(pos3); |
| } |
| a.printList(); |
| cout<<a.get_front()<<endl; |
| cout<<a.get_back()<<endl; |
| a.freeList(); |
| return 0; |
| } |
| ``` |
| :::: |
| ::::: |
| :::::info[STL 链表] |
| ::::info[list双向循环链表] |
| ```cpp |
| #include<bits/stdc++.h> |
| using namespace std; |
| list<int> L;//定义list链表 |
| int main(){ |
| L.push_back(10);//尾部插入 |
| L.push_back(20);//尾部插入 |
| L.push_front(5);//头部插入 |
| L.pop_back();//尾部删除 |
| L.pop_front();//头部删除 |
| L.remove(10);//删除链表中所有的指定值 |
| cout<<L.size()<<endl;//输出链表长度 |
| L.push_back(10);//尾部插入 |
| L.push_front(20);//头部插入 |
| cout<<L.front()<<endl;//输出头部元素 |
| cout<<L.back()<<endl;//输出尾部元素 |
| cout<<L.size()<<endl;//输出链表长度 |
| for(auto it=L.begin();it!=L.end();it++){//逐个输出链表中元素 |
| cout<<*it<<" "; |
| } |
| cout<<endl; |
| L.push_back(30);//尾部插入 |
| L.push_front(40);//头部插入 |
| L.push_front(50);//头部插入 |
| L.push_back(60);//尾部插入 |
| cout<<L.front()<<endl;//输出头部元素 |
| cout<<L.back()<<endl;//输出尾部元素 |
| cout<<L.size()<<endl;//输出链表长度 |
| for(auto it=L.begin();it!=L.end();it++){//逐个输出链表中元素 |
| cout<<*it<<" "; |
| } |
| cout<<endl; |
| L.sort();//从小到大排序 |
| cout<<L.front()<<endl;//输出头部元素 |
| cout<<L.back()<<endl;//输出尾部元素 |
| cout<<L.size()<<endl;//输出链表长度 |
| for(auto it=L.begin();it!=L.end();it++){//逐个输出链表中元素 |
| cout<<*it<<" "; |
| } |
| cout<<endl; |
| L.sort(greater<int>());//从大到小排序 |
| cout<<L.front()<<endl;//输出头部元素 |
| cout<<L.back()<<endl;//输出尾部元素 |
| cout<<L.size()<<endl;//输出链表长度 |
| for(auto it=L.begin();it!=L.end();it++){//逐个输出链表中元素 |
| cout<<*it<<" "; |
| } |
| cout<<endl; |
| L.reverse();//反转链表 |
| cout<<L.front()<<endl;//输出头部元素 |
| cout<<L.back()<<endl;//输出尾部元素 |
| cout<<L.size()<<endl;//输出链表长度 |
| for(auto it=L.begin();it!=L.end();it++){//逐个输出链表中元素 |
| cout<<*it<<" "; |
| } |
| cout<<endl; |
| L.push_back(10);//尾部插入 |
| L.push_front(10);//头部插入 |
| L.push_back(20);//尾部插入 |
| L.push_back(40);//尾部插入 |
| L.push_front(50);//头部插入 |
| L.push_front(60);//头部插入 |
| cout<<L.front()<<endl;//输出头部元素 |
| cout<<L.back()<<endl;//输出尾部元素 |
| cout<<L.size()<<endl;//输出链表长度 |
| for(auto it=L.begin();it!=L.end();it++){//逐个输出链表中元素 |
| cout<<*it<<" "; |
| } |
| cout<<endl; |
| L.sort();//从小到大排序 |
| L.unique();//删除连续的重复数字 |
| cout<<L.front()<<endl;//输出头部元素 |
| cout<<L.back()<<endl;//输出尾部元素 |
| cout<<L.size()<<endl;//输出链表长度 |
| for(auto it=L.begin();it!=L.end();it++){//逐个输出链表中元素 |
| cout<<*it<<" "; |
| } |
| cout<<endl; |
| auto it=L.begin();//拿到指向第一个元素的迭代器 |
| advance(it,2);//让迭代器往后走指定步 |
| L.insert(it,99);//在迭代器it的前面插入指定元素 |
| cout<<L.front()<<endl;//输出头部元素 |
| cout<<L.back()<<endl;//输出尾部元素 |
| cout<<L.size()<<endl;//输出链表长度 |
| for(auto it=L.begin();it!=L.end();it++){//逐个输出链表中元素 |
| cout<<*it<<" "; |
| } |
| cout<<endl; |
| L.erase(it);//删掉迭代器指向的那个元素 |
| cout<<L.front()<<endl;//输出头部元素 |
| cout<<L.back()<<endl;//输出尾部元素 |
| cout<<L.size()<<endl;//输出链表长度 |
| for(auto it=L.begin();it!=L.end();it++){//逐个输出链表中元素 |
| cout<<*it<<" "; |
| } |
| cout<<endl; |
| list<int> L2={2,4,6}; |
| L.merge(L2);//转移元素到链表头部 |
| cout<<L.front()<<endl;//输出头部元素 |
| cout<<L.back()<<endl;//输出尾部元素 |
| cout<<L.size()<<endl;//输出链表长度 |
| for(auto it=L.begin();it!=L.end();it++){//逐个输出链表中元素 |
| cout<<*it<<" "; |
| } |
| cout<<endl; |
| list<int> L3={1,3,5}; |
| L.splice(L.end(),L3);//转移元素到链表尾部 |
| cout<<L.front()<<endl;//输出头部元素 |
| cout<<L.back()<<endl;//输出尾部元素 |
| cout<<L.size()<<endl;//输出链表长度 |
| for(auto it=L.begin();it!=L.end();it++){//逐个输出链表中元素 |
| cout<<*it<<" "; |
| } |
| cout<<endl; |
| return 0; |
| } |
| ``` |
| :::: |
| ::::: |
| :::::: |
| ::::::info[队列] |
| :::::info[普通队列] |
| ::::info[顺序队列] |
| :::info[手写队列] |
| ```cpp |
| #include<bits/stdc++.h> |
| using namespace std; |
| #define int long long |
| struct node{ |
| int a[1000010],head,tail; |
| void init(){//初始化 |
| head=1; |
| tail=0; |
| } |
| void clear(){//清空队列 |
| init(); |
| } |
| void push(int x){//从尾插入 |
| a[++tail]=x; |
| } |
| void pop(){//从头弹出 |
| if(head<=tail){ |
| head++; |
| } |
| } |
| int front(){//返回头部元素 |
| if(head>tail){ |
| return -1; |
| } |
| return a[head]; |
| } |
| int back(){//返回尾部元素 |
| if(head>tail){ |
| return -1; |
| } |
| return a[tail]; |
| } |
| int operator[](int x){//返回指定位置元素 |
| if(head+x-1>tail){ |
| return -1; |
| } |
| return a[head+x-1]; |
| } |
| bool empty(){//判空 |
| return head>tail; |
| } |
| int size(){//长度 |
| return tail-head+1; |
| } |
| void erase(int x){//删除所有指定值元素 |
| int tmp_head=head,tmp_tail=tail; |
| head=1,tail=0; |
| for(int i=tmp_head;i<=tmp_tail;i++){ |
| if(a[i]!=x){ |
| push(a[i]); |
| } |
| } |
| } |
| void reverse(){//反转队列 |
| int l=head,r=tail; |
| while(l<r){ |
| swap(a[l],a[r]); |
| l++,r--; |
| } |
| } |
| void print(){//按顺序输出队列元素 |
| for(int i=head;i<=tail;i++){ |
| cout<<a[i]<<" "; |
| } |
| cout<<"\n"; |
| } |
| }; |
| node q;//定义队列 |
| signed main(){ |
| q.init(); |
| q.push(10); |
| q.push(20); |
| q.push(30); |
| q.push(40); |
| q.push(40); |
| q.push(10); |
| q.push(10); |
| q.push(30); |
| cout<<q.front()<<" "<<q.back()<<" "<<q.size()<<endl; |
| if(q.empty()){ |
| cout<<"No"<<endl; |
| }else{ |
| cout<<"Yes"<<endl; |
| } |
| q.pop(); |
| cout<<q[2]<<endl; |
| cout<<q.front()<<" "<<q.back()<<" "<<q.size()<<endl; |
| q.print(); |
| q.erase(10); |
| q.print(); |
| q.reverse(); |
| q.print(); |
| return 0; |
| } |
| ``` |
| ::: |
| :::: |
| ::::info[循环队列] |
| :::info[手写队列] |
| ```cpp |
| #include<bits/stdc++.h> |
| using namespace std; |
| #define int long long |
| struct node{ |
| int a[100000],head,tail,cnt; |
| void init(){//初始化 |
| head=0; |
| tail=0; |
| cnt=0; |
| } |
| void clear(){//清空队列 |
| init(); |
| } |
| void push(int x){//从尾插入 |
| a[tail]=x; |
| tail=(tail+1)%100000; |
| cnt++; |
| } |
| void pop(){//从头弹出 |
| if(cnt>0){ |
| head=(head+1)%100000; |
| cnt--; |
| } |
| } |
| int front(){//返回头部元素 |
| if(cnt==0){ |
| return -1; |
| } |
| return a[head]; |
| } |
| int back(){//返回尾部元素 |
| if(cnt==0){ |
| return -1; |
| } |
| return a[(tail-1+100000)%100000]; |
| } |
| int operator[](int x){//返回指定位置元素 |
| if(x<1 |
| return -1; |
| } |
| return a[(head+x-1)%100000]; |
| } |
| bool empty(){//判空 |
| return cnt==0; |
| } |
| int size(){//长度 |
| return cnt; |
| } |
| void erase(int x){//删除所有指定值元素 |
| int sz=cnt; |
| int tmp_head=head; |
| head=0; |
| tail=0; |
| cnt=0; |
| for(int i=0;i<sz;i++){ |
| int val=a[(tmp_head+i)%100000]; |
| if(val!=x){ |
| push(val); |
| } |
| } |
| } |
| void reverse(){//反转队列 |
| int l=0,r=cnt-1; |
| while(l<r){ |
| swap(a[(head+l)%100000],a[(head+r)%100000]); |
| l++; |
| r--; |
| } |
| } |
| bool find(int x){//查找元素是否存在 |
| for(int i=0;i<cnt;i++){ |
| if(a[(head+i)%100000]==x){ |
| return true; |
| } |
| } |
| return false; |
| } |
| int count(int x){//统计元素出现次数 |
| int res=0; |
| for(int i=0;i<cnt;i++){ |
| if(a[(head+i)%100000]==x){ |
| res++; |
| } |
| } |
| return res; |
| } |
| int sum(){//求和 |
| int res=0; |
| for(int i=0;i<cnt;i++){ |
| res+=a[(head+i)%100000]; |
| } |
| return res; |
| } |
| void q_sort(){//排序 |
| int tmp[100000]; |
| int sz=cnt; |
| for(int i=0;i<sz;i++){ |
| tmp[i]=a[(head+i)%100000]; |
| } |
| sort(tmp,tmp+sz); |
| head=0; |
| tail=0; |
| cnt=0; |
| for(int i=0;i<sz;i++){ |
| push(tmp[i]); |
| } |
| } |
| void unique(){//去连续重复 |
| if(empty()){ |
| return; |
| } |
| int sz=cnt; |
| int tmp_head=head; |
| head=0; |
| tail=0; |
| cnt=0; |
| int last=a[tmp_head]; |
| push(last); |
| for(int i=1;i<sz;i++){ |
| int val=a[(tmp_head+i)%100000]; |
| if(val!=last){ |
| push(val); |
| last=val; |
| } |
| } |
| } |
| void print(){//按顺序输出队列元素 |
| for(int i=0;i<cnt;i++){ |
| cout<<a[(head+i)%100000]<<" "; |
| } |
| cout<<"\n"; |
| } |
| }; |
| node q;//定义队列 |
| signed main(){ |
| q.init(); |
| q.push(3); |
| q.push(1); |
| q.push(4); |
| q.push(1); |
| q.push(5); |
| cout<<q.find(4)<<" "<<q.find(9)<<endl; |
| cout<<q.count(1)<<" "<<q.count(9)<<endl; |
| cout<<q.sum()<<endl; |
| q.reverse(); |
| q.print(); |
| q.q_sort(); |
| q.print(); |
| q.unique(); |
| q.print(); |
| return 0; |
| } |
| ``` |
| ::: |
| :::info[queue循环队列] |
| ```cpp |
| #include<bits/stdc++.h> |
| using namespace std; |
| queue<int> q,q2;//定义队列 |
| int main(){ |
| q.push(10);//尾部插入 |
| q.push(20);//尾部插入 |
| q.push(30);//尾部插入 |
| cout<<q.front()<<endl;//输出队首元素 |
| cout<<q.back()<<endl;//输出队尾元素 |
| cout<<q.size()<<endl;//输出队列长度 |
| q.pop();//头部删除 |
| cout<<q.front()<<endl;//输出队首元素 |
| cout<<q.back()<<endl;//输出队尾元素 |
| cout<<q.size()<<endl;//输出队列长度 |
| if(q.empty()){//判空 |
| cout<<"Empty"<<endl; |
| }else{ |
| cout<<"Not Empty"<<endl; |
| } |
| q2.push(100);//尾部插入 |
| q2.push(200);//尾部插入 |
| q.swap(q2);//交换两个队列 |
| cout<<q.front()<<endl;//输出交换后q的队首元素 |
| cout<<q2.front()<<endl;//输出交换后q2的队首元素 |
| while(!q.empty()){//清空队列 |
| q.pop(); |
| } |
| cout<<q.size()<<endl;//输出清空后的队列长度 |
| return 0; |
| } |
| ``` |
| ::: |
| :::: |
| ::::: |
| :::::info[双端队列] |
| ::::info[手写双端队列] |
| ```cpp |
| #include<bits/stdc++.h> |
| using namespace std; |
| #define int long long |
| struct node{ |
| int a[1000010]; |
| int head,tail; |
| void init(){//初始化 |
| head=500000; |
| tail=500000; |
| } |
| void clear(){//清空队列 |
| init(); |
| } |
| void push_front(int x){//从头部插入 |
| a[--head]=x; |
| } |
| void push_back(int x){//从尾部插入 |
| a[tail++]=x; |
| } |
| void pop_front(){//从头部弹出 |
| if(head<tail){ |
| head++; |
| } |
| } |
| void pop_back(){//从尾部弹出 |
| if(head<tail){ |
| tail--; |
| } |
| } |
| int front(){//返回头部元素 |
| if(head>=tail){ |
| return -1; |
| } |
| return a[head]; |
| } |
| int back(){//返回尾部元素 |
| if(head>=tail){ |
| return -1; |
| } |
| return a[tail-1]; |
| } |
| int operator[](int x){//返回指定位置元素(从1开始) |
| if(head+x-1>=tail){ |
| return -1; |
| } |
| return a[head+x-1]; |
| } |
| bool empty(){//判空 |
| return head>=tail; |
| } |
| int size(){//返回长度 |
| return tail-head; |
| } |
| void print(){//按顺序输出元素 |
| for(int i=head;i<tail;i++){ |
| cout<<a[i]<<" "; |
| } |
| cout<<"\n"; |
| } |
| }; |
| node dq;//定义双端队列 |
| signed main(){ |
| dq.init(); |
| dq.push_back(10); |
| dq.push_back(20); |
| dq.push_front(5); |
| dq.push_front(1); |
| cout<<dq.front()<<endl; |
| cout<<dq.back()<<endl; |
| cout<<dq.size()<<endl; |
| dq.print(); |
| dq.pop_front(); |
| dq.pop_back(); |
| cout<<dq.front()<<endl; |
| cout<<dq.back()<<endl; |
| cout<<dq.size()<<endl; |
| dq.print(); |
| if(dq.empty()){ |
| cout<<"Empty"<<endl; |
| }else{ |
| cout<<"Not Empty"<<endl; |
| } |
| cout<<dq[1]<<endl; |
| dq.clear(); |
| cout<<dq.size()<<endl; |
| return 0; |
| } |
| ``` |
| :::: |
| ::::info[deque双端队列] |
| ``` |
| #include<bits/stdc++.h> |
| using namespace std; |
| deque<int> dq;//定义双端队列 |
| int main(){ |
| dq.push_back(10);//尾部插入 |
| dq.push_back(20);//尾部插入 |
| dq.push_front(5);//头部插入 |
| dq.push_front(1);//头部插入 |
| cout<<dq.front()<<endl;//输出队首元素 |
| cout<<dq.back()<<endl;//输出队尾元素 |
| cout<<dq.size()<<endl;//输出队列长度 |
| dq.pop_front();//头部删除 |
| dq.pop_back();//尾部删除 |
| cout<<dq.front()<<endl;//输出删除后的队首元素 |
| cout<<dq.back()<<endl;//输出删除后的队尾元素 |
| cout<<dq.size()<<endl;//输出队列长度 |
| if(dq.empty()){//判空 |
| cout<<"Empty"<<endl; |
| }else{ |
| cout<<"Not Empty"<<endl; |
| } |
| cout<<dq[0]<<endl;//按下标访问(从0开始) |
| dq.insert(dq.begin()+1,99);//在指定位置插入 |
| cout<<dq[1]<<endl;//输出插入后的元素 |
| dq.erase(dq.begin()+1);//删除指定位置元素 |
| cout<<dq[1]<<endl;//输出删除后的元素 |
| dq.clear();//清空队列 |
| cout<<dq.size()<<endl;//输出清空后的队列长度 |
| return 0; |
| } |
| ``` |
| :::: |
| ::::: |
| :::::info[单调队列] |
| ```cpp |
| #include<bits/stdc++.h> |
| using namespace std; |
| #define ll long long |
| int n,k,s[1000010]; |
| deque<int> a,b; |
| int main(){ |
| ios::sync_with_stdio(0); |
| cin>>n>>k; |
| for(int i=1;i<=n;i++){ |
| cin>>s[i]; |
| } |
| for(int i=1;i<=n;i++){ |
| while(!a.empty()&&s[a.back()]>s[i]){ |
| a.pop_back(); |
| } |
| a.push_back(i); |
| if(i>=k){ |
| while(!a.empty()&&a.front()<i-k+1){ |
| a.pop_front(); |
| } |
| cout<<s[a.front()]<<" "; |
| } |
| } |
| cout<<endl; |
| for(int i=1;i<=n;i++){ |
| while(!b.empty()&&s[b.back()]<s[i]){ |
| b.pop_back(); |
| } |
| b.push_back(i); |
| if(i>=k){ |
| while(!b.empty()&&b.front()<i-k+1){ |
| b.pop_front(); |
| } |
| cout<<s[b.front()]<<" "; |
| } |
| } |
| cout<<endl; |
| return 0; |
| } |
| ``` |
| ::::: |
| :::::: |
| :::::info[栈] |
| ::::info[普通栈] |
| :::info[手写栈] |
| ```cpp |
| #include<bits/stdc++.h> |
| using namespace std; |
| struct node{ |
| int a[100010],cnt=0; |
| void init(){//初始化 |
| cnt=0; |
| } |
| void clear(){//清空栈 |
| cnt=0; |
| } |
| void push(int x){//栈顶压入元素 |
| a[++cnt]=x; |
| } |
| void pop(){//栈顶弹出元素 |
| if(cnt!=0){ |
| cnt--; |
| } |
| } |
| int top(){//返回栈顶元素 |
| if(cnt==0){ |
| return -1; |
| } |
| return a[cnt]; |
| } |
| int operator[](int x){//返回指定位置元素 |
| if(cnt==0 |
| return -1; |
| } |
| return a[x]; |
| } |
| bool empty(){//判空 |
| return cnt==0; |
| } |
| int size(){//返回长度 |
| return cnt; |
| } |
| void erase(int x){//删除所有指定值元素 |
| int tmp=cnt; |
| cnt=0; |
| for(int i=1;i<=tmp;i++){ |
| if(a[i]!=x){ |
| push(a[i]); |
| } |
| } |
| } |
| void reverse(){//反转栈内元素 |
| int l=1,r=cnt; |
| while(l<r){ |
| swap(a[l],a[r]); |
| l++,r--; |
| } |
| } |
| void print(){//顺序输出栈内元素 |
| for(int i=1;i<=cnt;i++){ |
| cout<<a[i]<<" "; |
| } |
| cout<<"\n"; |
| } |
| }; |
| node st;//定义栈 |
| int main(){ |
| st.init(); |
| st.push(10); |
| st.push(20); |
| st.push(30); |
| st.push(40); |
| st.push(40); |
| st.push(10); |
| st.push(10); |
| st.push(30); |
| cout<<st.top()<<" "<<st.size()<<endl; |
| if(st.empty()){ |
| cout<<"No"<<endl; |
| }else{ |
| cout<<"Yes"<<endl; |
| } |
| st.pop(); |
| cout<<st[2]<<endl; |
| cout<<st.top()<<" "<<st.size()<<endl; |
| st.print(); |
| st.erase(10); |
| st.print(); |
| st.reverse(); |
| st.print(); |
| st.clear(); |
| if(st.empty()){ |
| cout<<"No"<<endl; |
| }else{ |
| cout<<"Yes"<<endl; |
| } |
| return 0; |
| } |
| ``` |
| ::: |
| :::info[stack栈] |
| ```cpp |
| #include<bits/stdc++.h> |
| using namespace std; |
| stack<int> st,st2;//定义栈 |
| int main(){ |
| st.push(10);//栈顶压入元素 |
| st.push(20);//栈顶压入元素 |
| st.push(30);//栈顶压入元素 |
| cout<<st.top()<<endl;//输出栈顶元素 |
| cout<<st.size()<<endl;//输出栈内元素个数 |
| st.pop();//弹出栈顶元素 |
| cout<<st.top()<<endl;//输出弹出后的栈顶元素 |
| cout<<st.size()<<endl;//输出栈内元素个数 |
| if(st.empty()){//判空 |
| cout<<"Empty"<<endl; |
| }else{ |
| cout<<"Not Empty"<<endl; |
| } |
| st2.push(100); |
| st2.push(200); |
| st.swap(st2);//交换两个栈 |
| cout<<st.top()<<endl;//输出交换后st的栈顶元素 |
| cout<<st2.top()<<endl;//输出交换后st2的栈顶元素 |
| while(!st.empty()){//清空栈 |
| st.pop(); |
| } |
| cout<<st.size()<<endl;//输出清空后的栈大小 |
| return 0; |
| } |
| ``` |
| ::: |
| :::: |
| ::::info[单调栈] |
| ```cpp |
| #include<bits/stdc++.h> |
| using namespace std; |
| #define ll long long |
| int n,s[100010],ans[100010]; |
| int main(){ |
| ios::sync_with_stdio(0); |
| stack<int> a; |
| cin>>n; |
| for(int i=1;i<=n;i++){ |
| cin>>s[i]; |
| } |
| for(int i=n;i>=1;i--){ |
| while(!a.empty()&&s[a.top()]<=s[i]){ |
| a.pop(); |
| } |
| if(a.empty()){ |
| ans[i]=0; |
| }else{ |
| ans[i]=a.top(); |
| } |
| a.push(i); |
| } |
| for(int i=1;i<=n;i++){ |
| cout<<ans[i]<<endl; |
| } |
| return 0; |
| } |
| ``` |
| :::: |
| ::::: |
| :::::::info[二叉树和哈夫曼树] |
| ::::::info[二叉树] |
| :::::info[二叉树的遍历] |
| ::::info[深度优先搜索] |
| :::info[先序遍历] |
| ```cpp |
| #include<bits/stdc++.h> |
| using namespace std; |
| struct into{ |
| int l,r; |
| }a[1030]; |
| int n,t; |
| void p1(int p){ |
| printf("%d ",p); |
| if(a[p].l!=0){ |
| p1(a[p].l); |
| } |
| if(a[p].r!=0){ |
| p1(a[p].r); |
| } |
| } |
| int main(){ |
| ios::sync_with_stdio(0); |
| scanf("%d",&n); |
| for(int i=1;i<=n;i++){ |
| scanf("%d%d",&a[i].l,&a[i].r); |
| } |
| t=0; |
| p1(1); |
| return 0; |
| } |
| ``` |
| ::: |
| :::info[中序遍历] |
| ```cpp |
| #include<bits/stdc++.h> |
| using namespace std; |
| struct into{ |
| int l,r; |
| }a[1030]; |
| int n,t; |
| void p2(int p){ |
| if(a[p].l!=0){ |
| p2(a[p].l); |
| } |
| printf("%d ",p); |
| if(a[p].r!=0){ |
| p2(a[p].r); |
| } |
| } |
| int main(){ |
| ios::sync_with_stdio(0); |
| scanf("%d",&n); |
| for(int i=1;i<=n;i++){ |
| scanf("%d%d",&a[i].l,&a[i].r); |
| } |
| t=0; |
| p2(1); |
| return 0; |
| } |
| ``` |
| ::: |
| :::info[后序遍历] |
| ```cpp |
| #include<bits/stdc++.h> |
| using namespace std; |
| struct into{ |
| int l,r; |
| }a[1030]; |
| int n,t; |
| void p3(int p){ |
| if(a[p].l!=0){ |
| p3(a[p].l); |
| } |
| if(a[p].r!=0){ |
| p3(a[p].r); |
| } |
| printf("%d ",p); |
| } |
| int main(){ |
| ios::sync_with_stdio(0); |
| scanf("%d",&n); |
| for(int i=1;i<=n;i++){ |
| scanf("%d%d",&a[i].l,&a[i].r); |
| } |
| t=0; |
| p3(1); |
| return 0; |
| } |
| ``` |
| ::: |
| :::info[先序中序转后序] |
| ```cpp |
| #include<bits/stdc++.h> |
| using namespace std; |
| int n,a[1030],b[1030]; |
| void dfs(int l1,int r1,int l2,int r2){ |
| if(l1>r1){ |
| return; |
| } |
| if(l1==r1){ |
| printf("%d ",a[l1]); |
| return; |
| } |
| int x; |
| for(int i=l2;i<=r2;i++){ |
| if(b[i]==a[l1]){ |
| x=i; |
| break; |
| } |
| } |
| dfs(l1+1,l1+x-l2,l2,x-1); |
| dfs(l1+x-l2+1,r1,x+1,r2); |
| printf("%d ",a[l1]); |
| } |
| int main(){ |
| ios::sync_with_stdio(0); |
| scanf("%d",&n); |
| for(int i=1;i<=n;i++){ |
| scanf("%d",&a[i]); |
| } |
| for(int i=1;i<=n;i++){ |
| scanf("%d",&b[i]); |
| } |
| dfs(1,n,1,n); |
| return 0; |
| } |
| ``` |
| ::: |
| :::info[中序后序转先序] |
| ```cpp |
| #include<bits/stdc++.h> |
| using namespace std; |
| int n,a[1030],b[1030]; |
| void dfs(int l1,int r1,int l2,int r2){ |
| if(l1>r1){ |
| return; |
| } |
| if(l1==r1){ |
| printf("%d ",a[l1]); |
| return; |
| } |
| printf("%d ",b[r2]); |
| int x=0; |
| for(int i=l1;i<=r1;i++){ |
| if(a[i]==b[r2]){ |
| x=i; |
| break; |
| } |
| } |
| dfs(l1,x-1,l2,l2+x-1-l1); |
| dfs(x+1,r1,l2+x-l1,r2-1); |
| } |
| int main(){ |
| cin>>n; |
| for(int i=1;i<=n;i++){ |
| cin>>a[i]; |
| } |
| for(int i=1;i<=n;i++){ |
| cin>>b[i]; |
| } |
| dfs(1,n,1,n); |
| return 0; |
| } |
| ``` |
| ::: |
| :::: |
| ::::info[广度优先搜索] |
| ```cpp |
| #include<bits/stdc++.h> |
| using namespace std; |
| struct node{ |
| int l,r; |
| }a[1025]; |
| int n; |
| void bfs(){ |
| queue<int> q; |
| q.push(1); |
| while(!q.empty()){ |
| int t=q.front(); |
| q.pop(); |
| cout<<t<<" "; |
| if(a[t].l!=0){ |
| q.push(a[t].l); |
| } |
| if(a[t].r!=0){ |
| q.push(a[t].r); |
| } |
| } |
| } |
| int main(){ |
| cin>>n; |
| for(int i=1;i<=n;i++){ |
| cin>>a[i].l>>a[i].r; |
| } |
| bfs(); |
| return 0; |
| } |
| ``` |
| :::: |
| ::::: |
| :::::info[二叉树的基本计数] |
| ::::info[求深度] |
| ```cpp |
| #include<bits/stdc++.h> |
| using namespace std; |
| struct node{ |
| int l,r; |
| }a[1030]; |
| int n; |
| int depth(int p){ |
| if(p==0){ |
| return 0; |
| } |
| return 1+max(depth(a[p].l),depth(a[p].r)); |
| } |
| int main(){ |
| cin>>n; |
| for(int i=1;i<=n;i++){ |
| cin>>a[i].l>>a[i].r; |
| } |
| cout<<depth(1)<<endl; |
| return 0; |
| } |
| ``` |
| :::: |
| ::::info[求节点数] |
| ```cpp |
| #include<bits/stdc++.h> |
| using namespace std; |
| struct node{ |
| int l,r; |
| }a[1030]; |
| int n; |
| int cntNode(int p){ |
| if(p==0){ |
| return 0; |
| } |
| return 1+cntNode(a[p].l)+cntNode(a[p].r); |
| } |
| int main(){ |
| cin>>n; |
| for(int i=1;i<=n;i++){ |
| cin>>a[i].l>>a[i].r; |
| } |
| cout<<cntNode(1)<<endl; |
| return 0; |
| } |
| ``` |
| :::: |
| ::::info[求叶子数] |
| ```cpp |
| #include<bits/stdc++.h> |
| using namespace std; |
| struct node{ |
| int l,r; |
| }a[1030]; |
| int n; |
| int cntLeaf(int p){ |
| if(p==0){ |
| return 0; |
| } |
| if(a[p].l==0&&a[p].r==0){ |
| return 1; |
| } |
| return cntLeaf(a[p].l)+cntLeaf(a[p].r); |
| } |
| int main(){ |
| cin>>n; |
| for(int i=1;i<=n;i++){ |
| cin>>a[i].l>>a[i].r; |
| } |
| cout<<cntLeaf(1)<<endl; |
| return 0; |
| } |
| ``` |
| :::: |
| ::::info[求第 k 层节点数] |
| ```cpp |
| #include<bits/stdc++.h> |
| using namespace std; |
| struct node{ |
| int l,r; |
| }a[1030]; |
| int n,k; |
| void dfs(int p,int dep,int &cnt){ |
| if(p==0){ |
| return; |
| } |
| if(dep==k){ |
| cnt++; |
| return; |
| } |
| dfs(a[p].l,dep+1,cnt); |
| dfs(a[p].r,dep+1,cnt); |
| } |
| int main(){ |
| cin>>n>>k; |
| for(int i=1;i<=n;i++){ |
| cin>>a[i].l>>a[i].r; |
| } |
| int cnt=0; |
| dfs(1,1,cnt); |
| cout<<cnt<<endl; |
| return 0; |
| } |
| ``` |
| :::: |
| ::::info[求最大宽度] |
| ```cpp |
| #include<bits/stdc++.h> |
| using namespace std; |
| struct node{ |
| int l,r; |
| }a[1030]; |
| int n; |
| int width[1030]; |
| void dfs(int p,int dep){ |
| if(p==0){ |
| return; |
| } |
| width[dep]++; |
| dfs(a[p].l,dep+1); |
| dfs(a[p].r,dep+1); |
| } |
| int main(){ |
| cin>>n; |
| for(int i=1;i<=n;i++){ |
| cin>>a[i].l>>a[i].r; |
| } |
| dfs(1,1); |
| int ans=0; |
| for(int i=1;i<=n;i++){ |
| ans=max(ans,width[i]); |
| } |
| cout<<ans<<endl; |
| return 0; |
| } |
| ``` |
| :::: |
| ::::: |
| :::::info[二叉树的应用] |
| ::::info[最近公共祖先] |
| ```cpp |
| #include<bits/stdc++.h> |
| using namespace std; |
| struct node{ |
| int l,r; |
| }a[1030]; |
| int n,u,v; |
| int fa[1030],dep[1030]; |
| void dfs(int p,int f,int d){ |
| if(p==0){ |
| return; |
| } |
| fa[p]=f; |
| dep[p]=d; |
| dfs(a[p].l,p,d+1); |
| dfs(a[p].r,p,d+1); |
| } |
| int lca(int u,int v){ |
| while(dep[u]>dep[v]){ |
| u=fa[u]; |
| } |
| while(dep[v]>dep[u]){ |
| v=fa[v]; |
| } |
| while(u!=v){ |
| u=fa[u]; |
| v=fa[v]; |
| } |
| return u; |
| } |
| int main(){ |
| cin>>n; |
| for(int i=1;i<=n;i++){ |
| cin>>a[i].l>>a[i].r; |
| } |
| dfs(1,0,1); |
| cin>>u>>v; |
| cout<<lca(u,v)<<endl; |
| return 0; |
| } |
| ``` |
| :::: |
| ::::info[节点到根的路径] |
| ```cpp |
| #include<bits/stdc++.h> |
| using namespace std; |
| struct node{ |
| int l,r; |
| }a[1030]; |
| int n,fa[1030]; |
| void dfs(int p,int f){ |
| if(p==0){ |
| return; |
| } |
| fa[p]=f; |
| dfs(a[p].l,p); |
| dfs(a[p].r,p); |
| } |
| int main(){ |
| cin>>n; |
| for(int i=1;i<=n;i++){ |
| cin>>a[i].l>>a[i].r; |
| } |
| dfs(1,0); |
| int x; |
| cin>>x; |
| vector<int> path; |
| while(x!=0){ |
| path.push_back(x); |
| x=fa[x]; |
| } |
| for(int i=path.size()-1;i>=0;i--){ |
| cout<<path[i]<<" "; |
| } |
| cout<<endl; |
| return 0; |
| } |
| ``` |
| :::: |
| ::::info[两节点距离] |
| ```cpp |
| #include<bits/stdc++.h> |
| using namespace std; |
| struct node{ |
| int l,r; |
| }a[1030]; |
| int n,u,v; |
| int fa[1030],dep[1030]; |
| void dfs(int p,int f,int d){ |
| if(p==0){ |
| return; |
| } |
| fa[p]=f; |
| dep[p]=d; |
| dfs(a[p].l,p,d+1); |
| dfs(a[p].r,p,d+1); |
| } |
| int lca(int u,int v){ |
| while(dep[u]>dep[v]){ |
| u=fa[u]; |
| } |
| while(dep[v]>dep[u]){ |
| v=fa[v]; |
| } |
| while(u!=v){ |
| u=fa[u]; |
| v=fa[v]; |
| } |
| return u; |
| } |
| int main(){ |
| cin>>n; |
| for(int i=1;i<=n;i++){ |
| cin>>a[i].l>>a[i].r; |
| } |
| dfs(1,0,1); |
| cin>>u>>v; |
| int w=lca(u,v); |
| cout<<dep[u]+dep[v]-2*dep[w]<<endl; |
| return 0; |
| } |
| ``` |
| :::: |
| ::::info[树的直径] |
| ```cpp |
| #include<bits/stdc++.h> |
| using namespace std; |
| struct node{ |
| int l,r; |
| }a[1030]; |
| int n,ans; |
| int dfs(int p){ |
| if(p==0){ |
| return 0; |
| } |
| int L=dfs(a[p].l); |
| int R=dfs(a[p].r); |
| ans=max(ans,L+R); |
| return 1+max(L,R); |
| } |
| int main(){ |
| cin>>n; |
| for(int i=1;i<=n;i++){ |
| cin>>a[i].l>>a[i].r; |
| } |
| ans=0; |
| dfs(1); |
| cout<<ans<<endl; |
| return 0; |
| } |
| ``` |
| :::: |
| ::::info[判断完全二叉树] |
| ```cpp |
| #include<bits/stdc++.h> |
| using namespace std; |
| struct node{ |
| int l,r; |
| }a[1030]; |
| int n; |
| bool isComplete(){ |
| queue<int> q; |
| q.push(1); |
| bool flag=false; |
| while(!q.empty()){ |
| int t=q.front(); |
| q.pop(); |
| if(t==0){ |
| flag=true; |
| continue; |
| } |
| if(flag){ |
| return false; |
| } |
| q.push(a[t].l); |
| q.push(a[t].r); |
| } |
| return true; |
| } |
| int main(){ |
| cin>>n; |
| for(int i=1;i<=n;i++){ |
| cin>>a[i].l>>a[i].r; |
| } |
| if(isComplete()){ |
| cout<<"Yes"<<endl; |
| }else{ |
| cout<<"No"<<endl; |
| } |
| return 0; |
| } |
| ``` |
| :::: |
| ::::info[判断二叉搜索树] |
| ```cpp |
| #include<bits/stdc++.h> |
| using namespace std; |
| struct node{ |
| int l,r,val; |
| }a[1030]; |
| int n; |
| bool check(int p,long long low,long long high){ |
| if(p==0){ |
| return true; |
| } |
| if(a[p].val<=low |
| return false; |
| } |
| return check(a[p].l,low,a[p].val)&&check(a[p].r,a[p].val,high); |
| } |
| int main(){ |
| cin>>n; |
| for(int i=1;i<=n;i++){ |
| cin>>a[i].l>>a[i].r>>a[i].val; |
| } |
| if(check(1,LLONG_MIN,LLONG_MAX)){ |
| cout<<"Yes"<<endl; |
| }else{ |
| cout<<"No"<<endl; |
| } |
| return 0; |
| } |
| ``` |
| :::: |
| ::::info[镜像翻转] |
| ```cpp |
| #include<bits/stdc++.h> |
| using namespace std; |
| struct node{ |
| int l,r; |
| }a[1030]; |
| int n; |
| void mirror(int p){ |
| if(p==0){ |
| return; |
| } |
| swap(a[p].l,a[p].r); |
| mirror(a[p].l); |
| mirror(a[p].r); |
| } |
| void p1(int p){ |
| if(p==0){ |
| return; |
| } |
| cout<<p<<" "; |
| p1(a[p].l); |
| p1(a[p].r); |
| } |
| int main(){ |
| cin>>n; |
| for(int i=1;i<=n;i++){ |
| cin>>a[i].l>>a[i].r; |
| } |
| mirror(1); |
| p1(1); |
| return 0; |
| } |
| ``` |
| :::: |
| ::::info[所有根到叶子的路径] |
| ``` |
| #include<bits/stdc++.h> |
| using namespace std; |
| struct node{ |
| int l,r; |
| }a[1030]; |
| int n; |
| vector<int> path; |
| void dfs(int p){ |
| if(p==0){ |
| return; |
| } |
| path.push_back(p); |
| if(a[p].l==0&&a[p].r==0){ |
| for(int i=0;i<path.size();i++){ |
| cout<<path[i]<<" "; |
| } |
| cout<<endl; |
| }else{ |
| dfs(a[p].l); |
| dfs(a[p].r); |
| } |
| path.pop_back(); |
| } |
| int main(){ |
| cin>>n; |
| for(int i=1;i<=n;i++){ |
| cin>>a[i].l>>a[i].r; |
| } |
| dfs(1); |
| return 0; |
| } |
| ``` |
| :::: |
| ::::info[所有叶子节点] |
| ```cpp |
| #include<bits/stdc++.h> |
| using namespace std; |
| struct node{ |
| int l,r; |
| }a[1030]; |
| int n; |
| void dfs(int p){ |
| if(p==0){ |
| return; |
| } |
| if(a[p].l==0&&a[p].r==0){ |
| cout<<p<<" "; |
| return; |
| } |
| dfs(a[p].l); |
| dfs(a[p].r); |
| } |
| int main(){ |
| cin>>n; |
| for(int i=1;i<=n;i++){ |
| cin>>a[i].l>>a[i].r; |
| } |
| dfs(1); |
| return 0; |
| } |
| ``` |
| :::: |
| ::::: |
| :::::: |
| ::::::info[哈夫曼树] |
| :::::info[建树 + WPL] |
| ```cpp |
| #include<bits/stdc++.h> |
| using namespace std; |
| #define ll long long |
| struct node{ |
| ll w; |
| int l,r; |
| node(ll w=0,int l=0,int r=0):w(w),l(l),r(r){} |
| }; |
| struct cmp{ |
| bool operator()(int x,int y){ |
| return node[x].w>node[y].w; |
| } |
| }; |
| node a[200010]; |
| int n,cnt; |
| priority_queue<int,vector<int>,cmp> pq; |
| int build(){ |
| cnt=n; |
| while(pq.size()>1){ |
| int x=pq.top(); |
| pq.pop(); |
| int y=pq.top(); |
| pq.pop(); |
| cnt++; |
| a[cnt]=node(a[x].w+a[y].w,x,y); |
| pq.push(cnt); |
| } |
| return pq.top(); |
| } |
| ll wpl(int p,int dep){ |
| if(a[p].l==0&&a[p].r==0){ |
| return a[p].w*dep; |
| } |
| return wpl(a[p].l,dep+1)+wpl(a[p].r,dep+1); |
| } |
| int main(){ |
| ios::sync_with_stdio(0); |
| cin>>n; |
| for(int i=1;i<=n;i++){ |
| ll x; |
| cin>>x; |
| a[i]=node(x); |
| pq.push(i); |
| } |
| int root=build(); |
| cout<<wpl(root,0)<<endl; |
| return 0; |
| } |
| ``` |
| ::::: |
| :::::info[哈夫曼编码] |
| ```cpp |
| #include<bits/stdc++.h> |
| using namespace std; |
| #define ll long long |
| struct node{ |
| ll w; |
| int l,r; |
| node(ll w=0,int l=0,int r=0):w(w),l(l),r(r){} |
| }; |
| struct cmp{ |
| bool operator()(int x,int y){ |
| return node[x].w>node[y].w; |
| } |
| }; |
| node a[200010]; |
| int n,cnt; |
| string code[200010]; |
| priority_queue<int,vector<int>,cmp> pq; |
| int build(){ |
| cnt=n; |
| while(pq.size()>1){ |
| int x=pq.top(); |
| pq.pop(); |
| int y=pq.top(); |
| pq.pop(); |
| cnt++; |
| a[cnt]=node(a[x].w+a[y].w,x,y); |
| pq.push(cnt); |
| } |
| return pq.top(); |
| } |
| void dfs(int p,string s){ |
| if(a[p].l==0&&a[p].r==0){ |
| code[p]=s; |
| return; |
| } |
| dfs(a[p].l,s+"0"); |
| dfs(a[p].r,s+"1"); |
| } |
| int main(){ |
| ios::sync_with_stdio(0); |
| cin>>n; |
| for(int i=1;i<=n;i++){ |
| ll x; |
| cin>>x; |
| a[i]=node(x); |
| pq.push(i); |
| } |
| int root=build(); |
| dfs(root,""); |
| for(int i=1;i<=n;i++){ |
| cout<<i<<" "<<a[i].w<<" "<<code[i]<<endl; |
| } |
| return 0; |
| } |
| ``` |
| ::::: |
| :::::info[哈夫曼解码] |
| ```cpp |
| #include<bits/stdc++.h> |
| using namespace std; |
| #define ll long long |
| struct node{ |
| ll w; |
| int l,r; |
| node(ll w=0,int l=0,int r=0):w(w),l(l),r(r){} |
| }; |
| struct cmp{ |
| bool operator()(int x,int y){ |
| return node[x].w>node[y].w; |
| } |
| }; |
| node a[200010]; |
| int n,cnt; |
| string code[200010]; |
| priority_queue<int,vector<int>,cmp> pq; |
| int build(){ |
| cnt=n; |
| while(pq.size()>1){ |
| int x=pq.top(); |
| pq.pop(); |
| int y=pq.top(); |
| pq.pop(); |
| cnt++; |
| a[cnt]=node(a[x].w+a[y].w,x,y); |
| pq.push(cnt); |
| } |
| return pq.top(); |
| } |
| void dfs(int p,string s){ |
| if(a[p].l==0&&a[p].r==0){ |
| code[p]=s; |
| return; |
| } |
| dfs(a[p].l,s+"0"); |
| dfs(a[p].r,s+"1"); |
| } |
| void decode(int root,string s){ |
| int p=root; |
| for(char c:s){ |
| if(c=='0'){ |
| p=a[p].l; |
| }else{ |
| p=a[p].r; |
| } |
| if(a[p].l==0&&a[p].r==0){ |
| cout<<p<<" "; |
| p=root; |
| } |
| } |
| cout<<endl; |
| } |
| int main(){ |
| ios::sync_with_stdio(0); |
| cin>>n; |
| for(int i=1;i<=n;i++){ |
| ll x; |
| cin>>x; |
| a[i]=node(x); |
| pq.push(i); |
| } |
| int root=build(); |
| dfs(root,""); |
| for(int i=1;i<=n;i++){ |
| cout<<i<<" "<<a[i].w<<" "<<code[i]<<endl; |
| } |
| string s; |
| cin>>s; |
| decode(root,s); |
| return 0; |
| } |
| ``` |
| ::::: |
| :::::: |
| ::::::: |
| ::::::info[堆] |
| :::::info[手写堆] |
| ::::info[大根堆] |
| ```cpp |
| #include<bits/stdc++.h> |
| using namespace std; |
| struct node{ |
| int a[100010],cnt=0; |
| void init(){//初始化 |
| cnt=0; |
| } |
| void clear(){//清空堆 |
| cnt=0; |
| } |
| void push(int x){//插入元素 |
| a[++cnt]=x; |
| int i=cnt; |
| while(i>1&&a[i]>a[i/2]){ |
| swap(a[i],a[i/2]); |
| i/=2; |
| } |
| } |
| void pop(){//弹出堆顶 |
| if(cnt==0){ |
| return; |
| } |
| a[1]=a[cnt--]; |
| int i=1; |
| while(i*2<=cnt){ |
| int j=i*2; |
| if(j+1<=cnt&&a[j+1]>a[j]){ |
| j++; |
| } |
| if(a[i]>=a[j]){ |
| break; |
| } |
| swap(a[i],a[j]); |
| i=j; |
| } |
| } |
| int top(){//返回堆顶元素 |
| if(cnt==0){ |
| return -1; |
| } |
| return a[1]; |
| } |
| bool empty(){//判空 |
| return cnt==0; |
| } |
| int size(){//返回长度 |
| return cnt; |
| } |
| void erase(int x){//删除所有指定值元素 |
| int tmp[100010],tot=0; |
| for(int i=1;i<=cnt;i++){ |
| if(a[i]!=x){ |
| tmp[++tot]=a[i]; |
| } |
| } |
| cnt=0; |
| for(int i=1;i<=tot;i++){ |
| push(tmp[i]); |
| } |
| } |
| void build(int n,int b[]){//建堆 |
| cnt=n; |
| for(int i=1;i<=n;i++){ |
| a[i]=b[i]; |
| } |
| for(int i=cnt/2;i>=1;i--){ |
| int j=i; |
| while(j*2<=cnt){ |
| int k=j*2; |
| if(k+1<=cnt&&a[k+1]>a[k]){ |
| k++; |
| } |
| if(a[j]>=a[k]){ |
| break; |
| } |
| swap(a[j],a[k]); |
| j=k; |
| } |
| } |
| } |
| void print(){//按顺序输出堆内元素 |
| for(int i=1;i<=cnt;i++){ |
| cout<<a[i]<<" "; |
| } |
| cout<<"\n"; |
| } |
| }; |
| node heap;//定义堆 |
| int main(){ |
| heap.init(); |
| heap.push(10); |
| heap.push(20); |
| heap.push(30); |
| heap.push(5); |
| heap.push(40); |
| heap.print(); |
| cout<<heap.top()<<" "<<heap.size()<<endl; |
| if(heap.empty()){ |
| cout<<"No"<<endl; |
| }else{ |
| cout<<"Yes"<<endl; |
| } |
| heap.pop(); |
| heap.print(); |
| cout<<heap.top()<<" "<<heap.size()<<endl; |
| heap.erase(10); |
| heap.print(); |
| heap.clear(); |
| cout<<heap.size()<<endl; |
| int b[9]={0,3,1,4,1,5,9,2,6}; |
| heap.build(8,b); |
| heap.print(); |
| cout<<heap.top()<<endl; |
| return 0; |
| } |
| ``` |
| :::: |
| ::::info[小根堆] |
| ```cpp |
| #include<bits/stdc++.h> |
| using namespace std; |
| struct node{ |
| int a[100010],cnt=0; |
| void init(){//初始化 |
| cnt=0; |
| } |
| void clear(){//清空堆 |
| cnt=0; |
| } |
| void push(int x){//插入元素 |
| a[++cnt]=x; |
| int i=cnt; |
| while(i>1&&a[i]<a[i/2]){ |
| swap(a[i],a[i/2]); |
| i/=2; |
| } |
| } |
| void pop(){//弹出堆顶 |
| if(cnt==0){ |
| return; |
| } |
| a[1]=a[cnt--]; |
| int i=1; |
| while(i*2<=cnt){ |
| int j=i*2; |
| if(j+1<=cnt&&a[j+1]<a[j]){ |
| j++; |
| } |
| if(a[i]<=a[j]){ |
| break; |
| } |
| swap(a[i],a[j]); |
| i=j; |
| } |
| } |
| int top(){//返回堆顶元素 |
| if(cnt==0){ |
| return -1; |
| } |
| return a[1]; |
| } |
| bool empty(){//判空 |
| return cnt==0; |
| } |
| int size(){//返回长度 |
| return cnt; |
| } |
| void erase(int x){//删除所有指定值元素 |
| int tmp[100010],tot=0; |
| for(int i=1;i<=cnt;i++){ |
| if(a[i]!=x){ |
| tmp[++tot]=a[i]; |
| } |
| } |
| cnt=0; |
| for(int i=1;i<=tot;i++){ |
| push(tmp[i]); |
| } |
| } |
| void build(int n,int b[]){//建堆 |
| cnt=n; |
| for(int i=1;i<=n;i++){ |
| a[i]=b[i]; |
| } |
| for(int i=cnt/2;i>=1;i--){ |
| int j=i; |
| while(j*2<=cnt){ |
| int k=j*2; |
| if(k+1<=cnt&&a[k+1]<a[k]){ |
| k++; |
| } |
| if(a[j]<=a[k]){ |
| break; |
| } |
| swap(a[j],a[k]); |
| j=k; |
| } |
| } |
| } |
| void print(){//按顺序输出堆内元素 |
| for(int i=1;i<=cnt;i++){ |
| cout<<a[i]<<" "; |
| } |
| cout<<"\n"; |
| } |
| }; |
| node heap;//定义堆 |
| int main(){ |
| heap.init(); |
| heap.push(10); |
| heap.push(20); |
| heap.push(30); |
| heap.push(5); |
| heap.push(40); |
| heap.print(); |
| cout<<heap.top()<<" "<<heap.size()<<endl; |
| if(heap.empty()){ |
| cout<<"No"<<endl; |
| }else{ |
| cout<<"Yes"<<endl; |
| } |
| heap.pop(); |
| heap.print(); |
| cout<<heap.top()<<" "<<heap.size()<<endl; |
| heap.erase(10); |
| heap.print(); |
| heap.clear(); |
| cout<<heap.size()<<endl; |
| int b[9]={0,3,1,4,1,5,9,2,6}; |
| heap.build(8,b); |
| heap.print(); |
| cout<<heap.top()<<endl; |
| return 0; |
| } |
| ``` |
| :::: |
| ::::: |
| :::::info[priority_queue优先队列] |
| ::::info[大根堆] |
| ```cpp |
| #include<bits/stdc++.h> |
| using namespace std; |
| priority_queue<int> pq;//定义大根堆 |
| int main(){ |
| pq.push(10);//插入元素 |
| pq.push(20);//插入元素 |
| pq.push(30);//插入元素 |
| pq.push(5);//插入元素 |
| pq.push(40);//插入元素 |
| cout<<pq.top()<<endl;//输出堆顶元素 |
| cout<<pq.size()<<endl;//输出堆内元素个数 |
| if(pq.empty()){//判空 |
| cout<<"Empty"<<endl; |
| }else{ |
| cout<<"Not Empty"<<endl; |
| } |
| pq.pop();//弹出堆顶元素 |
| cout<<pq.top()<<endl;//输出弹出后的堆顶元素 |
| cout<<pq.size()<<endl;//输出堆内元素个数 |
| priority_queue<int> pq2; |
| pq2.push(100); |
| pq2.push(200); |
| pq.swap(pq2);//交换两个堆 |
| cout<<pq.top()<<endl;//输出交换后pq的堆顶元素 |
| cout<<pq2.top()<<endl;//输出交换后pq2的堆顶元素 |
| while(!pq.empty()){//清空堆 |
| pq.pop(); |
| } |
| cout<<pq.size()<<endl;//输出清空后的堆大小 |
| return 0; |
| } |
| ``` |
| :::: |
| ::::info[小根堆] |
| ```cpp |
| #include<bits/stdc++.h> |
| using namespace std; |
| priority_queue<int,vector<int>,greater<int>> pq;//定义小根堆 |
| int main(){ |
| pq.push(10);//插入元素 |
| pq.push(20);//插入元素 |
| pq.push(30);//插入元素 |
| pq.push(5);//插入元素 |
| pq.push(40);//插入元素 |
| cout<<pq.top()<<endl;//输出堆顶元素 |
| cout<<pq.size()<<endl;//输出堆内元素个数 |
| if(pq.empty()){//判空 |
| cout<<"Empty"<<endl; |
| }else{ |
| cout<<"Not Empty"<<endl; |
| } |
| pq.pop();//弹出堆顶元素 |
| cout<<pq.top()<<endl;//输出弹出后的堆顶元素 |
| cout<<pq.size()<<endl;//输出堆内元素个数 |
| priority_queue<int,vector<int>,greater<int>> pq2; |
| pq2.push(100); |
| pq2.push(200); |
| pq.swap(pq2);//交换两个堆 |
| cout<<pq.top()<<endl;//输出交换后pq的堆顶元素 |
| cout<<pq2.top()<<endl;//输出交换后pq2的堆顶元素 |
| while(!pq.empty()){//清空堆 |
| pq.pop(); |
| } |
| cout<<pq.size()<<endl;//输出清空后的堆大小 |
| return 0; |
| } |
| ``` |
| :::: |
| ::::: |
| :::::: |