Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.55 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <stdint.h>
  5. #include <math.h>
  6. #include <string.h>
  7. #include <SDL2/SDL.h>
  8. #include <SDL2/SDL_image.h>
  9. #include <SDL2/SDL_ttf.h>
  10.  
  11. SDL_Window* gWin;
  12. SDL_Renderer* gRdr;
  13. SDL_Surface* gSfc;
  14. TTF_Font* gFnt;
  15.  
  16. typedef struct node node;
  17. struct node {
  18. node* l;
  19. node* r;
  20. node* p;
  21. int d;
  22. char c;
  23. };
  24.  
  25. const int rect_sz = 30;
  26. const int mgn_ln = 30;
  27. const int wWidth = 1920;
  28. const int wHeight = 1080;
  29.  
  30. int max(int a,int b) {
  31. if(a > b) return a;
  32. return b;
  33. }
  34.  
  35. void draw_graph_rec(int x,int y,int w,int h,node* root) {
  36. SDL_Rect r;
  37. r.x = x+w/2-rect_sz/2;
  38. r.y = y;
  39. r.w = rect_sz;
  40. r.h = rect_sz;
  41. if(root == NULL) SDL_RenderDrawRect(gRdr,&r);
  42. else {
  43. SDL_RenderFillRect(gRdr,&r);
  44. char b[16];
  45. sprintf(b,"%d",root->d);
  46. SDL_Color c = {0xff,0,0};
  47. SDL_Surface* s =TTF_RenderText_Solid(gFnt,b,c);
  48. SDL_Texture* t = SDL_CreateTextureFromSurface(gRdr,s);
  49.  
  50. SDL_RenderCopy(gRdr,t,NULL,&r);
  51.  
  52. SDL_DestroyTexture(t);
  53. SDL_FreeSurface(s);
  54. }
  55. if(root != NULL) {
  56. SDL_RenderDrawLine(gRdr,x+w/2,y+rect_sz,x+w/4,y+rect_sz+mgn_ln);
  57. SDL_RenderDrawLine(gRdr,x+w/2,y+rect_sz,x+w*3/4,y+rect_sz+mgn_ln);
  58. draw_graph_rec(x,y+rect_sz+mgn_ln,w/2,0,root->l);
  59. draw_graph_rec(x+w/2,y+rect_sz+mgn_ln,w/2,0,root->r);
  60. }
  61. }
  62.  
  63. void draw_graph(node* root) {
  64. SDL_SetRenderDrawColor(gRdr,0xff,0xff,0xff,0xff);
  65. SDL_RenderClear(gRdr);
  66. SDL_SetRenderDrawColor(gRdr,0,0,0xff,0xff);
  67.  
  68. int depth = get_depth(root);
  69. draw_graph_rec(0,0,wWidth,0,root);
  70.  
  71. SDL_RenderPresent(gRdr);
  72. }
  73.  
  74. node* mknode(node* l,node* r,node* p,int d,char c) {
  75. node* ret = malloc(sizeof(node));
  76. ret->l = l;
  77. ret->r = r;
  78. ret->p = p;
  79. ret->d = d;
  80. ret->c = c;
  81. return ret;
  82. }
  83.  
  84. void insBST(node* n,node* root) {
  85. node* c = root;
  86. while(1) {
  87. if(c->d > n->d) {
  88. if(c->r == NULL) {
  89. c->r = n;
  90. n->p = c;
  91. break;
  92. } else c = c->r;
  93. } else if(c->d < n->d) {
  94. if(c->l == NULL) {
  95. c->l = n;
  96. n->p = c;
  97. break;
  98. } else c = c->l;
  99. } else break;
  100. }
  101. }
  102.  
  103. int main() {
  104. SDL_Init(SDL_INIT_VIDEO);
  105. gWin = SDL_CreateWindow("Binary Search Tree",SDL_WINDOWPOS_UNDEFINED,SDL_WINDOWPOS_UNDEFINED,wWidth,wHeight,SDL_WINDOW_SHOWN);
  106. gSfc = SDL_GetWindowSurface(gWin);
  107. gRdr = SDL_CreateRenderer(gWin,-1,SDL_RENDERER_ACCELERATED);
  108. TTF_Init();
  109. gFnt = TTF_OpenFont("ipag.ttf",30);
  110. if(gFnt == NULL) return -1;
  111. node* root = mknode(NULL,NULL,NULL,1000,0);
  112.  
  113. time_t t;
  114. srand((unsigned) time(&t));
  115. for(int i = 0;i < 100;i++) {
  116. insBST(mknode(NULL,NULL,NULL,rand()%2000,0),root);
  117. }
  118.  
  119. draw_graph(root);
  120. SDL_Delay(5000);
  121.  
  122. TTF_CloseFont(gFnt);
  123. TTF_Quit();
  124. SDL_DestroyWindow(gWin);
  125. SDL_Quit();
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement