-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 74af9d8
Showing
7 changed files
with
261 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#include <cstring> | ||
#define CMAX 128 | ||
#define N 1005 | ||
struct Node{ | ||
Node* child[CMAX], fa; | ||
int count, no; | ||
char ch; | ||
Node(){ | ||
memset(child, NULL, sizeof(child)); | ||
count=no=_no; | ||
} | ||
}; | ||
struct DictTree{ | ||
Node* root; | ||
int size; | ||
Node* idx2node[N]; | ||
Node* alloc(){ Node* tmp = new Node(); return tmp; } | ||
int char2c(char c){ return c;} | ||
DictTree(){size = 0; root = alloc();} | ||
void modify(char* target, int& no, int& count){ | ||
Node* iter = root; | ||
while(target != '\0'){ | ||
int cn = char2c(*target); | ||
if(iter->child[cn] == NULL){ | ||
Node* child = alloc(++size); | ||
iter->child[cn] = child; | ||
child->fa = iter; | ||
} | ||
target++; | ||
if(target == '\0') {iter->count++; no = iter->no = size; idx2node[size++] = iter; count = iter->count; break;} | ||
iter = iter->child[cn]; | ||
} | ||
} | ||
void query(int idx, char* res){ | ||
Node* iter = idx2node[idx]; | ||
while(iter->fa){ | ||
*res = iter->ch; | ||
iter = iter->fa; | ||
} | ||
} | ||
}; |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
#include <stdio.h> | ||
#define ll long long | ||
#define N 1005 | ||
#define TOP 1000 | ||
struct Node{ | ||
ll val; | ||
void init(int v){val = v;} | ||
}node[N]; | ||
void swap(Node& a, Node& b){ | ||
ll tmp = a.val; | ||
a.val = b.val; | ||
b.val = tmp; | ||
} | ||
struct Heap{ | ||
int size; | ||
Heap(){ size = 1; } | ||
Node* upon(int idx){ | ||
for(int i = idx; i > 1; i >>= 1){ | ||
if(node[i].val < node[i/2].val){ | ||
swap(node[i], node[i/2]); | ||
} | ||
else return &node[i]; | ||
} | ||
return &node[1]; | ||
} | ||
Node* down(int idx){ | ||
int rev = idx; | ||
while(2*idx < size){ | ||
if(2*idx + 1 >= size){ | ||
if(node[2*idx].val < node[idx].val){ | ||
swap(node[idx], node[2*idx]); | ||
} | ||
return &node[2*idx]; | ||
} | ||
if(node[2*idx+1].val > node[2*idx].val){ | ||
if(node[2*idx].val < node[idx].val){ | ||
swap(node[idx], node[2*idx]); | ||
idx *= 2; | ||
} | ||
else return &node[idx]; | ||
} | ||
else{ | ||
if(node[2*idx+1].val < node[idx].val){ | ||
swap(node[idx], node[2*idx+1]); | ||
idx = idx*2 + 1; | ||
} | ||
else return &node[idx]; | ||
} | ||
} | ||
return &node[idx]; | ||
} | ||
Node* insert(int v){ | ||
node[size].init(v); | ||
if(size >= TOP){ | ||
if(node[1].val < node[size].val){ | ||
swap(node[1], node[size]); | ||
return down(1); | ||
} | ||
} | ||
size++; | ||
return upon(size-1); | ||
} | ||
void display(){ | ||
while(size > 1){ | ||
printf("\n"); | ||
printf("TOP %lld\n", node[1].val); | ||
for(int i = 1; i < size; i++){ | ||
printf("%lld\n", node[i].val); | ||
} | ||
size--; | ||
printf("\n"); | ||
swap(node[size], node[1]); | ||
down(1); | ||
} | ||
} | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
#include <stdio.h> | ||
#define TOP 1000 | ||
#define ll long long | ||
struct Node{ | ||
Node *child[2], *fa; | ||
ll val; | ||
Node(ll v=0){ | ||
val = v; child[0] = child[1] = fa = NULL; | ||
} | ||
}; | ||
void swap(Node* fa, Node* ch){ | ||
Node* pnt; | ||
pnt[0] = fa->fa; pnt[1] = fa->child[0]; pnt[2] = fa->child[1]; | ||
fa->fa = ch->fa; fa->child[0] = ch->child[0]; fa->child[1] = ch->child[1]; | ||
ch->fa = pnt[0]; ch->child[0] = pnt[1]; ch->child[1] = pnt[2]; | ||
} | ||
struct Heap{ | ||
Node* root; | ||
int size; | ||
Heap(){ size = 0; root = new Node();} | ||
void down(Node *ptr){ | ||
while(ptr){ | ||
ll v = ptr->child[1]->val; | ||
if(ptr->child[0] && ptr->child[1]){ | ||
ll x = ptr->child[0]->val, y = ptr->child[1]->val; | ||
if(x > y){ | ||
if(y < v){ | ||
swap(ptr, ptr->child[1]); | ||
} | ||
else return; | ||
} | ||
else{ | ||
if(x < v){ | ||
swap(ptr, ptr->child[0]); | ||
} | ||
else return; | ||
} | ||
} | ||
if(ptr->child[0]){ | ||
ll x = ptr->child[0]->val; | ||
if(x < v){ | ||
swap(ptr, ptr->child[0]); | ||
} | ||
else return; | ||
} | ||
return; | ||
} | ||
} | ||
void upon(Node *ptr){ | ||
while(ptr->fa != root){ | ||
Node* fa = ptr->fa; | ||
if(fa->val > ptr->val){ | ||
printf("swap %lld %lld\n", fa->val, ptr->val); | ||
swap(fa, ptr); | ||
printf("swap %lld %lld\n", fa->val, ptr->val); | ||
} | ||
else return; | ||
} | ||
} | ||
Node* bfs(Node *rt, int id){ | ||
puts("BFS"); | ||
printf("%d\n", id); | ||
Node* queue[TOP]; | ||
int head = 0, tail = 0; | ||
if(tail == id) return rt; | ||
queue[tail++] = rt; | ||
while(head < tail){ | ||
Node* top = queue[head]; head++; | ||
printf("%d %lld\n", tail, top->val); | ||
// if(id == -1){ | ||
// printf("%lld\n", top->val); | ||
// } | ||
if(top->child[0]) { | ||
if(tail == id) return top->child[0]; | ||
queue[tail++] = top->child[0]; | ||
} | ||
if(top->child[1]) { | ||
if(tail == id) return top->child[0]; | ||
queue[tail++] = top->child[1]; | ||
} | ||
} | ||
return NULL; | ||
} | ||
Node* insert(int x){ | ||
if(size >= TOP){ | ||
if(root->child[0]->val < x){ | ||
root->child[0]->val = x; // Mark | ||
down(root->child[0]); | ||
return root->child[0]; | ||
} | ||
return NULL; | ||
} | ||
Node* newptr = new Node(x); size++; | ||
if(size == 1){ | ||
newptr->fa = root; root->child[0] = newptr; return newptr; | ||
} | ||
puts("INSERT"); | ||
Node* fa = bfs(root->child[0], size/2-1); | ||
newptr->fa = fa; | ||
fa->child[size&1] = newptr; | ||
upon(newptr); | ||
return newptr; | ||
} | ||
void display(){ | ||
printf("##################\n"); | ||
bfs(root->child[0], -1); | ||
} | ||
}; |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#include "heap_list.h" | ||
ll a[10005]; | ||
int len; | ||
int main(){ | ||
Heap minheap = Heap(); | ||
minheap.insert(3); | ||
minheap.display(); | ||
minheap.insert(5); | ||
minheap.display(); | ||
minheap.insert(4); | ||
minheap.display(); | ||
minheap.insert(7); | ||
minheap.display(); | ||
minheap.insert(1); | ||
minheap.display(); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#include <iostream> | ||
using namespace std; | ||
struct node{ | ||
int val; | ||
int b; | ||
node(int v = 0){ val = v; b = 1;} | ||
}; | ||
int main(){ | ||
node a = node(1); | ||
node b = node(2); | ||
cout << a.val << " " << b.val << endl; | ||
cout << a.b << " " << b.b << endl; | ||
cout << &a << " " << &b << endl; | ||
swap(a, b); | ||
cout << a.val << " " << b.val << endl; | ||
cout << a.b << " " << b.b << endl; | ||
cout << &a << " " << &b << endl; | ||
} |