Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- typedef struct user {
- char *name;
- struct user *left, *right;
- int height, leftTree, rightTree;
- }User;
- typedef struct page {
- char *name;
- User *root;
- struct page *next, *prev;
- }Page;
- Page *pages[1000];
- User *newUser(char *newName) {
- User *user =(User *)malloc(sizeof(User));
- user->name =(char *)malloc(sizeof(newName));
- user->left = NULL;
- user->right = NULL;
- user->height = 1;
- user->leftTree = 0;
- user->rightTree = 0;
- sprintf(user->name, "%s", newName);
- return user;
- }
- int height(User *user) {
- int hei;
- if (user == NULL)
- hei = 0;
- else
- hei = user->height;
- return hei;
- }
- int nameCmp(User *user, char *name) {
- if (user == NULL)
- return 0;
- else {
- int i = strcmp(user->name, name);
- return i;
- }
- }
- int balanceNum(User *user) {
- int balance;
- if (user == NULL)
- return 0;
- balance = (height(user->left)) - (height(user->right));
- return balance;
- }
- int more(int x, int y) {
- if (x > y)
- return x;
- else
- return y;
- }
- User *right(User *root) {
- User *newRoot = root->left;
- root->leftTree = newRoot->rightTree;
- root->left = newRoot->right;
- newRoot->right = root;
- root->height = (more(height(root->left), height(root->right))) + 1;
- newRoot->height = (more(height(newRoot->left), height(newRoot->right))) + 1;
- return newRoot;
- }
- User *left(User *root) {
- User *newRoot = root->right;
- newRoot->leftTree = root->leftTree + newRoot->leftTree + 1;
- root->rightTree = newRoot->leftTree;
- root->right = newRoot->left;
- newRoot->left = root;
- root->height = (more(height(root->left), height(root->right))) + 1;
- newRoot->height = (more(height(newRoot->left), height(newRoot->right))) + 1;
- return newRoot;
- }
- User *insert(User *root, char *name) {
- int balance;
- if (root == NULL) {
- root = (User*)malloc(sizeof(User));
- root = newUser(name);
- return root;
- }
- if (strcmp(root->name, name) > 0) {
- root->left = insert(root->left, name);
- root->leftTree++;
- }else if (strcmp(root->name, name) < 0) {
- root->right = insert(root->right, name);
- root->rightTree++;
- }
- root->height = 1 + (more(height(root->left), height(root->right)));
- balance = balanceNum(root);
- if (balance > 1 || balance < -1) {
- //ll
- if (balance > 1 && nameCmp(root->left, name) > 0) {
- root = right(root);
- return root;
- }
- //lr
- if (balance > 1 && nameCmp(root->left, name) < 0) {
- User *child = root->left;
- root->left = left(child);
- root = right(root);
- return root;
- }
- //rr
- if (balance < -1 && nameCmp(root->right, name) < 0) {
- root = left(root);
- return root;
- }
- //rl
- if (balance < -1 && nameCmp(root->right, name) > 0) {
- User *child = root->right;
- root->right = right(child);
- root = left(root);
- return root;
- }
- }
- return root;
- }
- User *LR(User *root) {
- root = root->left;
- while (root->right != NULL) {
- root = root->right;
- }
- return root;
- }
- User *RL(User *root) {
- root = root->right;
- while (root->left != NULL) {
- root = root->left;
- }
- return root;
- }
- User *Delete(char *name, User *root) {
- if (root== NULL)
- return root;
- if (strcmp(root->name, name) > 0) {
- root->left = Delete(name, root->left);
- root->leftTree--;
- }
- else if (strcmp(root->name, name) < 0) {
- root->right = Delete(name, root->right);
- root->rightTree--;
- }
- else {
- //nema deti
- if (root->left == NULL && root->right == NULL) {
- root = NULL;
- return root;
- }
- //nema lave dieta
- if (root->left == NULL && root->right != NULL) {
- User *newRoot = root->right;
- root = NULL;
- root = newRoot;
- newRoot = NULL;
- }
- //nema prave dieta
- if (root->right == NULL && root->left != NULL) {
- User *newRoot = root->left;
- root = NULL;
- root = newRoot;
- newRoot = NULL;
- }
- //ma 2 deti
- if (root->left != NULL && root->right != NULL) {
- User *substitute = RL(root);
- root->name = substitute->name;
- root->right = Delete(root->name, root->right);
- }
- }
- root->height = 1 + (more(height(root->left), height(root->right)));
- int balance = balanceNum(root);
- //ll
- if (balance > 1 && balanceNum(root->left) > 0) {
- root = right(root);
- return root;
- }
- //lr
- if (balance > 1 && balanceNum(root->left) < 0) {
- User *child = root->left;
- root->left = left(child);
- root = right(root);
- return root;
- }
- //rr
- if (balance < -1 && balanceNum(root->right) < 0) {
- root = left(root);
- return root;
- }
- //rl
- if (balance < -1 && balanceNum(root->right) > 0) {
- User *child = root->right;
- root->right = right(child);
- root = left(root);
- return root;
- }
- return root;
- }
- int find(char *name, User *root) {
- if (root == NULL) {
- return 0;
- }
- if (strcmp(root->name, name) > 0) {
- return find(name, root->left);
- }
- if (strcmp(root->name, name) < 0) {
- return find(name, root->right);
- }
- if (strcmp(root->name, name) == 0)
- return 1;
- else
- return 0;
- }
- int hash(char *str){
- unsigned long hash = 5381;
- int c;
- while (c = *str++)
- hash = ((hash << 5) + hash) + c;
- hash %= 1000;
- return hash;
- }
- Page * (char *name, char *user) {
- Page *readPage;
- readPage = (Page *)malloc(sizeof(Page));
- readPage->name = (char *)malloc(50 * sizeof(char));
- sprintf(readPage->name, "%s", name);
- readPage->root = insert(NULL, user);
- readPage->prev = NULL;
- readPage->next = NULL;
- return readPage;
- }
- void like(char *page, char *user) {
- int index = hash(page), move = 0, i;
- Page *readPage = pages[index];
- if (readPage != NULL) {
- Page *prev = NULL;
- while (strcmp(readPage->name, page) != 0) {
- prev = readPage;
- readPage = readPage->next;
- move++;
- if (readPage == NULL)
- break;
- }
- if (readPage == NULL) {
- readPage = newPage(page, user);
- readPage->prev = prev;
- prev->next = readPage;
- return;
- }
- else
- {
- readPage->root = insert(readPage->root, user);
- }
- }
- else {
- readPage = newPage(page, user);
- }
- for (i = 0; i < move; i++)
- pages[index] = pages[index]->next;
- pages[index] = readPage;
- for (i = 0; i < move; i++)
- pages[index] = pages[index]->prev;
- return;
- ;
- }
- void unlike(char *page, char *user) {
- int index = hash(page), move = 0;
- Page *readPage = pages[index];
- if (readPage == NULL) {
- return;
- }
- else {
- while (strcmp(readPage->name, page) != 0) {
- readPage = readPage->next;
- move++;
- if (readPage == NULL)
- return;
- }
- readPage->root = Delete(user, readPage->root);
- }
- ;
- }
- User *getUser(int k, User *root) {
- if (root == NULL)
- return NULL;
- else if (root->leftTree == k)
- return root;
- else if (k <= root->leftTree)
- getUser(k, root->left);
- else if (k > root->leftTree)
- getUser((k - root->leftTree - 1), root->right);
- }
- char *getuser(char *page, int k) {
- int index = hash(page);
- char *user = NULL;
- Page *readPage = pages[index];
- if (readPage == NULL) {
- return NULL;
- }else{
- while (strcmp(readPage->name, page) != 0) {
- readPage = readPage->next;
- if (readPage == NULL)
- return NULL;
- }
- if (readPage->root == NULL) {
- return NULL;
- }
- }
- User *root = getUser(k-1,readPage->root);
- if (root == NULL)
- return NULL;
- else
- user = root->name;
- return user;
- }
- void init()
- {
- int i;
- for (i = 0; i < 1000; i++) {
- pages[i] = NULL;
- }
- ;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement