刷题笔记day5
二叉树的存储
struct node { typename data; node* lchild; node* rchild; }; node* root = NULL; node* newNode(int v) { node* Node = new node; Node->data = v; Node->lchild = Node->rchild = NULL; return Node; }
二叉树的查找、插入和创建
//查找所有数据域为给定数据域的节点并修改 void s(node* root, int x,int newdata) { if(root == NULL)return; if(root->data == x) { root->data = newdata; } s(root->lchild,x,newdata); s(root->rchild,x,newdata); } //插入 //插入位置是查找失败的位置 void ins(node* &root,int x)//root节点要引用 { if(root == NULL) { root = newNode(x); return; } //根据题目要求 ins(root->lchild,x); //ins(root->rchild,x); } //创建 node* create(int data[],int n) { node* root = NULL; for(int i=0;i<n;i++) { ins(root,data[i]); } return root; }
二叉树的遍历
//先序 void pre(node* root) { if(root == NULL) return; printf("%d\n",root->data); pre(root->lchild); pre(root->rchild); } //中序 void in(node* root) { if(root == NULL) return; pre(root->lchild); printf("%d\n",root->data); pre(root->rchild); } //后序 void post(node* tree) { if(root == NULL) return; pre(root->lchild); pre(root->rchild); printf("%d\n",root->data); }
层序遍历
//层序遍历 void layer(node* tree) { queue<node*>q; q.push(root); while(!q.empty()) { node* now = q.front(); q.pop(); printf("%d",now->data); if(now->lchild!=NULL) q.push(now->lchild); if(now->rchild!=NULL) q.push(now->lchild); } }
给定先序和中序求二叉树
先序、后序、层序,三者两两组合或者三个一起组合均无法确立唯一的一棵树。也就是说,中序遍历是构建树时必需的。
//给定先序和中序求二叉树 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(int k=inl;k<=inr;k++) { if(in[k] == pre[prel]) { 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); return root; }
循环队列(用于纯C实现层次遍历)
#include<stdio.h> int maxsize = 100; struct queue { struct node* t; int rear; int front; }; struct node { int data; struct node* lchild; struct node* rchild; }; void init(struct queue *q) { q->t = malloc(sizeof(struct node)*maxsize); q->rear = 0; q->front = 0; return ; } int isempty(struct queue *q) { if(q->front == q->rear) return 1; else return 0; } void enqueue(struct queue *q,struct node p)// { if((q->rear + 1)% maxsize == q->front)//满了失败 return; q->t[q->rear] = p; q->rear = (q->rear+1)%maxsize; return; } void dequeue(struct queue *q) { if(q->rear == q->front) return; q->front = (q->front+1)%maxsize; return; }
【PAT A1020】 Tree Traversals
后序中序建树+循环队列+层次遍历
#include<stdio.h> int maxsize = 100; struct queue { struct node* t; int rear; int front; }; struct node { int data; struct node* lchild; struct node* rchild; }node; void init(struct queue *q) { q->t = malloc(sizeof(struct node)*maxsize); q->rear = 0; q->front = 0; return ; } int isempty(struct queue *q) { if(q->front == q->rear) return 1; else return 0; } void enqueue(struct queue *q,struct node p)// { if((q->rear + 1)% maxsize == q->front)//满了失败 return; q->t[q->rear] = p; q->rear = (q->rear+1)%maxsize; return; } void dequeue(struct queue *q) { if(q->rear == q->front) return; q->front = (q->front+1)%maxsize; return; } int in[35]; int post[35]; struct node* create(int postl,int postr,int inl,int inr) { if(postl > postr) return NULL; struct node* root = malloc(sizeof(node)); root->data = post[postr]; 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-1,k+1,inr); return root; } //test void pre(struct node* root) { if(root == NULL) return; printf("%d\n",root->data); pre(root->lchild); pre(root->rchild); } int main() { int n; scanf("%d",&n); int i; for(i=0;i<n;i++) { scanf("%d",&post[i]); } for( i=0;i<n;i++) { scanf("%d",&in[i]); } struct node* ans = create(0,n-1,0,n-1); //pre(ans); struct queue q; init(&q); enqueue(&q,*ans); int kong = 1; while(!isempty(&q)) { if(kong == 1) { kong--; } else if(kong == 0) { printf(" "); } printf("%d",q.t[q.front].data); if(q.t[q.front].lchild) enqueue(&q,*q.t[q.front].lchild); if(q.t[q.front].rchild) enqueue(&q,*q.t[q.front].rchild); dequeue(&q); } return 0; }
codeup 区间贪心
#include<stdio.h> #include<stdlib.h> struct qu { int l; int r; }a[105]; int cmp(const void* a,const void* b) { struct qu* c = (qu*)a; struct qu* d = (qu*)b; if(c->l != d->l) return d->l-c->l; else return c->r-d->r; } int main() { int n; while(scanf("%d",&n)!=EOF) { if(n == 0) return 0; int i; for(i=0;i<n;i++) { scanf("%d%d",&a[i].l,&a[i].r); } qsort(a,n,sizeof(a[0]),cmp); int ans = 1; int nowl = a[0].l; for(i=1;i<n;i++) { if(a[i].r <= nowl) { nowl = a[i].l; ans++; } } printf("%d\n",ans); } }
链表基本操作
#include<stdio.h> #include<stdlib.h> struct node { int data; struct node* next; }; //创建链表 struct node* create(int array[]) { struct node* p; struct node* head; struct node* pre; head = (struct node* )malloc(sizeof(struct node)); head -> next = NULL; pre = head; int i; int n=5; for(i=0;i<n;i++) { p = (struct node* )malloc(sizeof(struct node)); p -> next = NULL; p -> data = array[i]; pre -> next = p; pre = p; } return head; } //查找 int search(struct node* head,int x) { int ccount = 0; struct node* p = head->next; while(p!=NULL) { if(p->data == x) { ccount++; } p = p->next; } return ccount; } //插入 void insert(struct node* head,int pos,int x) { struct node* p = head; while(pos--) p = p->next; struct node* q = (struct node*)malloc(sizeof(struct node*)); q->next = p->next; q->data = x; p->next = q; } //删除 void del(struct node* head,int x) { struct node* p = head->next; struct node* pre = head; while(p != NULL) { if(p->data == x) { pre->next = p->next; free(p); } else { p = p->next; pre = pre->next; } } }
【PAT A1032】Sharing
坑点在于最后节点的输出,如果不够5位,需要用05%d补齐。
#include<stdio.h> //静态链表 struct node { char data; int next; int flag; }a[100005]; int main() { int st1,st2; int n; scanf("%d%d%d",&st1,&st2,&n); int i; for(i=0;i<100005;i++) { a[i].flag = 0; } for(i=0;i<n;i++) { int num1; int tempc; int num2; scanf("%d %c %d",&num1,&tempc,&num2); a[num1].next = num2; a[num1].data = tempc; } int ans = -1; while(st1!=-1) { a[st1].flag = 1; st1 = a[st1].next; } while(st2!=-1) { if(a[st2].flag == 1) { ans = st2; break; } st2 = a[st2].next; } if(ans == -1) printf("-1\n"); else printf("%05d\n",ans); }
【PAT A1052】 Linked List Sorting
坑点在于可能存在所有节点都无效的情况。需要特判ccount是否等于0.
#include<stdio.h> #include<stdlib.h> struct node { int next; int key; int address; int flag; }a[100004]; int cmp(const void * a, const void *b) { struct node* c = (struct node*) a; struct node* d = (struct node*) b; if(c->flag != d->flag) return d->flag - c->flag; return c->key - d->key; } int main() { int n,st; scanf("%d%d",&n,&st); int i; for(i=0;i<n;i++) { int add; int kkey; int nnext; scanf("%d%d%d",&add,&kkey,&nnext); a[add].address = add; a[add].key = kkey; a[add].next = nnext; } for(i=0;i<100005;i++) a[i].flag = 0; int ccount = 0; int minadd = -1; int minkey = 200000; while(st != -1) { if(a[st].key < minkey) { minkey = a[st].key; minadd = st; } a[st].flag = 1; st = a[st].next; ccount++; } if(ccount == 0) { printf("0 -1\n"); return 0; } qsort(a,100005,sizeof(a[0]),cmp); /* for(i=0;i<n;i++) { printf("a.add:%d a.key:%d a.flag:%d\n",a[i].address,a[i].key,a[i].flag); } */ printf("%d %05d\n",ccount,minadd); for(i=0;i<ccount;i++) { if(i!=ccount-1) { printf("%05d %d %05d\n",a[i].address,a[i].key,a[i+1].address); } else { printf("%05d %d -1\n",a[i].address,a[i].key); } //printf("flag:%d\n",a[i].flag); } }