Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define cmm_len 7
- char ins[cmm_len] = "INSERT";
- char sch[cmm_len] = "SEARCH";
- char lkp[cmm_len] = "LOOKUP";
- char del[cmm_len] = "DELETE";
- char cmm[6] = { 0 };
- int n;
- struct node {
- int k, count;
- struct node *parent, *left, *right;
- char v[10];
- } *tree;
- struct node *y_ins, *y_rot, *y_del, *y_scc, *p, *x_ins, *x_seq, *x_rep, *x_rot, *x_rea, *x_del, *x_dsc, *x_lkp, *x_min, *x_sch;
- void inittree (struct node *s)
- {
- s = NULL;
- }
- void insert(void)
- {
- int key;
- struct node *x = tree, *y_ins = (struct node*)malloc(sizeof(struct node));
- scanf("%d %s", &y_ins->k, y_ins->v);
- key = y_ins->k;
- y_ins->count = 1;
- y_ins->parent = y_ins->left = y_ins->right = NULL;
- if (tree == NULL)
- tree = y_ins;
- else
- for (;;) {
- if (key < x->k) {
- if (x->left == NULL) {
- x->left = y_ins;
- y_ins->parent = x;
- x->count++;
- break;
- }
- x->count++;
- x = x->left;
- }
- else {
- if (x->right == NULL) {
- x->right = y_ins;
- y_ins->parent = x;
- x->count++;
- break;
- }
- x->count++;
- x = x->right;
- }
- }
- }
- void replacenode(struct node *x, struct node *y)
- {
- struct node *p = x->parent;
- if (x == tree) tree = y;
- else {
- if (y != NULL)
- y->parent = p;
- if (p->left == x) p->left = y;
- else p->right = y;
- }
- }
- void search(void)
- {
- int n;
- scanf ("%d", &n);
- int num = n + 1;
- struct node *elem = tree;
- for (;;) {
- if (elem->left != NULL) {
- if ((elem->left)->count + 1 == num) {
- printf("%s\n", elem->v);
- break;
- }
- else
- if ((elem->left)->count >= num) {
- elem = elem->left;
- }
- else {
- num = num - (elem->left)->count - 1;
- elem = elem->right;
- }
- }
- else {
- if (num == 1) {
- printf("%s\n", elem->v);
- break;
- }
- else {
- elem = elem->right;
- num = num - 1;
- }
- }
- }
- }
- struct node *descend( int key)
- {
- struct node *x_dsc = tree;
- while (x_dsc != NULL && x_dsc->k != key) {
- if (key < x_dsc->k) x_dsc = x_dsc->left;
- else x_dsc = x_dsc->right;
- }
- return x_dsc;
- }
- struct node *minimum(struct node *t)
- {
- struct node turn;
- struct node *x_min;
- if (t == NULL) {
- x_min = NULL;
- return x_min;
- }
- else {
- //struct node *x;
- x_min = t;
- while (x_min->left != NULL)
- x_min = x_min->left;
- }
- return x_min;
- }
- struct node *succ(struct node *elem)
- {
- struct node turn;
- struct node *y_scc;
- if (elem->right != NULL)
- y_scc = minimum(elem->right);
- else {
- y_scc = elem->parent;
- while (y_scc != NULL && elem == y_scc->right) {
- elem = y_scc;
- y_scc = y_scc->parent;
- }
- }
- return y_scc;
- }
- void delete (void)
- {
- int k;
- scanf ("%d", &k);
- struct node turn;
- struct node *x_del = descend( k), *y_del;
- if (x_del == NULL) printf ("panic");
- if (x_del->left == NULL && x_del->right == NULL) {
- edt(x_del);
- replacenode(x_del, NULL);
- }
- else {
- if (x_del->left == NULL) {
- edt(x_del);
- replacenode(x_del, x_del->right);
- }
- else {
- if (x_del->right == NULL) {
- edt(x_del);
- replacenode(x_del, x_del->left);
- }
- else {
- y_del = succ(x_del);
- edt(y_del);
- replacenode(y_del, y_del->right);
- (x_del->left)->parent = y_del;
- y_del->left = x_del->left;
- if (x_del->right != NULL) (x_del->right)->parent = y_del;
- y_del->right = x_del->right;
- y_del->count = x_del->count;
- replacenode(x_del, y_del);
- }
- }
- }
- free(x_del);
- }
- void lookup()
- {
- int key;
- scanf ("%d", &key);
- struct node turn;
- struct node *elem = descend(key);
- printf("%s\n", elem->v);
- }
- void replace(struct node *x_rep)
- {
- if (x_rep != NULL) {
- if (x_rep->left != NULL)
- replace(x_rep->left);
- if (x_rep->right != NULL)
- replace(x_rep->right);
- free(x_rep);
- }
- }
- int main()
- {
- struct node turn;
- int i;
- scanf("%d", &n);
- inittree (&turn);
- for (i = 0; i < n; i++) {
- scanf("%s", cmm);
- if (0 == strcmp(cmm, ins)) insert();
- if (0 == strcmp(cmm, sch)) search();
- if (0 == strcmp(cmm, del)) delete();
- if (0 == strcmp(cmm, lkp)) lookup();
- }
- replace(tree);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement