Advertisement
Guest User

cancer tree

a guest
Nov 19th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.40 KB | None | 0 0
  1. typedef struct user {
  2. char *name;
  3. struct user *left, *right;
  4. int height, leftTree, rightTree;
  5. }User;
  6.  
  7. typedef struct page {
  8.  
  9. char *name;
  10. User *root;
  11. struct page *next, *prev;
  12.  
  13. }Page;
  14.  
  15. Page *pages[1000];
  16.  
  17. User *newUser(char *newName) {
  18. User *user =(User *)malloc(sizeof(User));
  19. user->name =(char *)malloc(sizeof(newName));
  20. user->left = NULL;
  21. user->right = NULL;
  22. user->height = 1;
  23. user->leftTree = 0;
  24. user->rightTree = 0;
  25. sprintf(user->name, "%s", newName);
  26.  
  27.  
  28. return user;
  29. }
  30.  
  31. int height(User *user) {
  32. int hei;
  33. if (user == NULL)
  34. hei = 0;
  35. else
  36. hei = user->height;
  37.  
  38. return hei;
  39.  
  40. }
  41.  
  42. int nameCmp(User *user, char *name) {
  43. if (user == NULL)
  44. return 0;
  45. else {
  46. int i = strcmp(user->name, name);
  47. return i;
  48. }
  49.  
  50. }
  51.  
  52. int balanceNum(User *user) {
  53. int balance;
  54.  
  55. if (user == NULL)
  56. return 0;
  57.  
  58. balance = (height(user->left)) - (height(user->right));
  59.  
  60. return balance;
  61. }
  62.  
  63. int more(int x, int y) {
  64. if (x > y)
  65. return x;
  66. else
  67. return y;
  68.  
  69. }
  70.  
  71. User *right(User *root) {
  72. User *newRoot = root->left;
  73. root->leftTree = newRoot->rightTree;
  74.  
  75.  
  76. root->left = newRoot->right;
  77. newRoot->right = root;
  78.  
  79. root->height = (more(height(root->left), height(root->right))) + 1;
  80. newRoot->height = (more(height(newRoot->left), height(newRoot->right))) + 1;
  81.  
  82.  
  83. return newRoot;
  84. }
  85.  
  86. User *left(User *root) {
  87. User *newRoot = root->right;
  88. newRoot->leftTree = root->leftTree + newRoot->leftTree + 1;
  89. root->rightTree = newRoot->leftTree;
  90.  
  91. root->right = newRoot->left;
  92. newRoot->left = root;
  93.  
  94. root->height = (more(height(root->left), height(root->right))) + 1;
  95. newRoot->height = (more(height(newRoot->left), height(newRoot->right))) + 1;
  96.  
  97.  
  98. return newRoot;
  99. }
  100.  
  101. User *insert(User *root, char *name) {
  102. int balance;
  103.  
  104. if (root == NULL) {
  105. root = (User*)malloc(sizeof(User));
  106. root = newUser(name);
  107. return root;
  108. }
  109.  
  110.  
  111.  
  112. if (strcmp(root->name, name) > 0) {
  113. root->left = insert(root->left, name);
  114. root->leftTree++;
  115. }else if (strcmp(root->name, name) < 0) {
  116. root->right = insert(root->right, name);
  117. root->rightTree++;
  118. }
  119.  
  120.  
  121.  
  122. root->height = 1 + (more(height(root->left), height(root->right)));
  123.  
  124. balance = balanceNum(root);
  125.  
  126. if (balance > 1 || balance < -1) {
  127.  
  128. //ll
  129. if (balance > 1 && nameCmp(root->left, name) > 0) {
  130. root = right(root);
  131. return root;
  132. }
  133. //lr
  134. if (balance > 1 && nameCmp(root->left, name) < 0) {
  135. User *child = root->left;
  136. root->left = left(child);
  137. root = right(root);
  138. return root;
  139. }
  140. //rr
  141. if (balance < -1 && nameCmp(root->right, name) < 0) {
  142. root = left(root);
  143. return root;
  144. }
  145. //rl
  146. if (balance < -1 && nameCmp(root->right, name) > 0) {
  147. User *child = root->right;
  148. root->right = right(child);
  149. root = left(root);
  150. return root;
  151. }
  152.  
  153. }
  154. return root;
  155. }
  156.  
  157. User *LR(User *root) {
  158.  
  159. root = root->left;
  160.  
  161. while (root->right != NULL) {
  162. root = root->right;
  163. }
  164.  
  165. return root;
  166. }
  167.  
  168. User *RL(User *root) {
  169.  
  170. root = root->right;
  171.  
  172. while (root->left != NULL) {
  173. root = root->left;
  174. }
  175.  
  176.  
  177. return root;
  178. }
  179.  
  180. User *Delete(char *name, User *root) {
  181.  
  182. if (root== NULL)
  183. return root;
  184.  
  185. if (strcmp(root->name, name) > 0) {
  186. root->left = Delete(name, root->left);
  187. root->leftTree--;
  188. }
  189. else if (strcmp(root->name, name) < 0) {
  190. root->right = Delete(name, root->right);
  191. root->rightTree--;
  192. }
  193. else {
  194. //nema deti
  195. if (root->left == NULL && root->right == NULL) {
  196. root = NULL;
  197.  
  198. return root;
  199. }
  200.  
  201. //nema lave dieta
  202. if (root->left == NULL && root->right != NULL) {
  203. User *newRoot = root->right;
  204. root = NULL;
  205. root = newRoot;
  206. newRoot = NULL;
  207. }
  208.  
  209. //nema prave dieta
  210. if (root->right == NULL && root->left != NULL) {
  211. User *newRoot = root->left;
  212. root = NULL;
  213. root = newRoot;
  214. newRoot = NULL;
  215. }
  216.  
  217. //ma 2 deti
  218. if (root->left != NULL && root->right != NULL) {
  219. User *substitute = RL(root);
  220. root->name = substitute->name;
  221. root->right = Delete(root->name, root->right);
  222. }
  223. }
  224.  
  225. root->height = 1 + (more(height(root->left), height(root->right)));
  226.  
  227. int balance = balanceNum(root);
  228.  
  229. //ll
  230. if (balance > 1 && balanceNum(root->left) > 0) {
  231. root = right(root);
  232. return root;
  233. }
  234. //lr
  235. if (balance > 1 && balanceNum(root->left) < 0) {
  236. User *child = root->left;
  237. root->left = left(child);
  238. root = right(root);
  239. return root;
  240. }
  241. //rr
  242. if (balance < -1 && balanceNum(root->right) < 0) {
  243. root = left(root);
  244. return root;
  245. }
  246. //rl
  247. if (balance < -1 && balanceNum(root->right) > 0) {
  248. User *child = root->right;
  249. root->right = right(child);
  250. root = left(root);
  251. return root;
  252. }
  253.  
  254.  
  255. return root;
  256.  
  257.  
  258. }
  259.  
  260.  
  261. int find(char *name, User *root) {
  262.  
  263. if (root == NULL) {
  264. return 0;
  265. }
  266.  
  267. if (strcmp(root->name, name) > 0) {
  268. return find(name, root->left);
  269. }
  270. if (strcmp(root->name, name) < 0) {
  271. return find(name, root->right);
  272. }
  273. if (strcmp(root->name, name) == 0)
  274. return 1;
  275. else
  276. return 0;
  277.  
  278. }
  279.  
  280.  
  281. int hash(char *str){
  282. unsigned long hash = 5381;
  283. int c;
  284.  
  285. while (c = *str++)
  286. hash = ((hash << 5) + hash) + c;
  287.  
  288. hash %= 1000;
  289. return hash;
  290.  
  291. }
  292.  
  293. Page * (char *name, char *user) {
  294. Page *readPage;
  295. readPage = (Page *)malloc(sizeof(Page));
  296. readPage->name = (char *)malloc(50 * sizeof(char));
  297. sprintf(readPage->name, "%s", name);
  298. readPage->root = insert(NULL, user);
  299. readPage->prev = NULL;
  300. readPage->next = NULL;
  301. return readPage;
  302.  
  303. }
  304.  
  305. void like(char *page, char *user) {
  306. int index = hash(page), move = 0, i;
  307. Page *readPage = pages[index];
  308.  
  309. if (readPage != NULL) {
  310. Page *prev = NULL;
  311. while (strcmp(readPage->name, page) != 0) {
  312. prev = readPage;
  313. readPage = readPage->next;
  314. move++;
  315. if (readPage == NULL)
  316. break;
  317. }
  318.  
  319. if (readPage == NULL) {
  320. readPage = newPage(page, user);
  321. readPage->prev = prev;
  322. prev->next = readPage;
  323. return;
  324. }
  325. else
  326. {
  327. readPage->root = insert(readPage->root, user);
  328. }
  329.  
  330. }
  331. else {
  332. readPage = newPage(page, user);
  333. }
  334.  
  335. for (i = 0; i < move; i++)
  336. pages[index] = pages[index]->next;
  337.  
  338. pages[index] = readPage;
  339.  
  340. for (i = 0; i < move; i++)
  341. pages[index] = pages[index]->prev;
  342.  
  343. return;
  344. ;
  345. }
  346.  
  347. void unlike(char *page, char *user) {
  348. int index = hash(page), move = 0;
  349. Page *readPage = pages[index];
  350.  
  351. if (readPage == NULL) {
  352. return;
  353. }
  354. else {
  355. while (strcmp(readPage->name, page) != 0) {
  356. readPage = readPage->next;
  357. move++;
  358. if (readPage == NULL)
  359. return;
  360. }
  361. readPage->root = Delete(user, readPage->root);
  362. }
  363.  
  364.  
  365.  
  366.  
  367. ;
  368. }
  369.  
  370. User *getUser(int k, User *root) {
  371. if (root == NULL)
  372. return NULL;
  373. else if (root->leftTree == k)
  374. return root;
  375. else if (k <= root->leftTree)
  376. getUser(k, root->left);
  377. else if (k > root->leftTree)
  378. getUser((k - root->leftTree - 1), root->right);
  379.  
  380.  
  381.  
  382. }
  383.  
  384. char *getuser(char *page, int k) {
  385. int index = hash(page);
  386. char *user = NULL;
  387. Page *readPage = pages[index];
  388. if (readPage == NULL) {
  389. return NULL;
  390. }else{
  391.  
  392. while (strcmp(readPage->name, page) != 0) {
  393. readPage = readPage->next;
  394. if (readPage == NULL)
  395. return NULL;
  396. }
  397.  
  398. if (readPage->root == NULL) {
  399. return NULL;
  400. }
  401. }
  402.  
  403. User *root = getUser(k-1,readPage->root);
  404. if (root == NULL)
  405. return NULL;
  406. else
  407. user = root->name;
  408.  
  409. return user;
  410. }
  411.  
  412. void init()
  413. {
  414. int i;
  415.  
  416. for (i = 0; i < 1000; i++) {
  417. pages[i] = NULL;
  418.  
  419. }
  420. ;
  421. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement