Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2020
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.59 KB | None | 0 0
  1. // Lab6.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5.  
  6. //arbore
  7. typedef struct node {
  8. int key;
  9. int size;
  10. int niv;
  11. struct node *left;
  12. struct node *right;
  13. } NodeT;
  14.  
  15. NodeT* createNode(int key) {
  16. NodeT *nod = (NodeT*)malloc(sizeof(NodeT));
  17. nod->left = NULL;
  18. nod->right = NULL;
  19. nod->niv = 0;
  20. nod->size = 1;
  21. nod->key = key;
  22. return nod;
  23. }
  24. NodeT* buildPBT(int *a,int first,int last,int niv) {
  25. if (first > last) return NULL;
  26. else {
  27. int mij = (first + last) / 2;
  28. NodeT *node = createNode(a[mij]);
  29. node->niv = niv;
  30. node->left = buildPBT(a, first, mij - 1,niv+1);
  31. node->right = buildPBT(a, mij + 1, last,niv+1);
  32. if (node->left != NULL && node->right != NULL)
  33. node->size = node->left->size + node->right->size + 1;
  34. if (node->left == NULL && node->right == NULL)
  35. node->size = 1;
  36. if (node->left == NULL && node->right != NULL)
  37. node->size = node->right->size + 1;
  38. if (node->left != NULL && node->right == NULL)
  39. node->size = node->left->size + 1;
  40. return node;
  41. }
  42.  
  43. }
  44. NodeT* OSSelect(NodeT *T, int i) {
  45. int r;
  46. if (T->left == NULL)
  47. r = 1;
  48. else r = T->left->size + 1;
  49. if (i == r)
  50. return T;
  51. else if (i < r) return OSSelect(T->left, i);
  52. else return OSSelect(T->right, i - r);
  53. }
  54.  
  55.  
  56. //pretty-print
  57. typedef struct nou {
  58. struct nou *next;
  59. NodeT *ptr;
  60. }nodPretty;
  61. nodPretty *prim, *ultim;
  62. int niv;
  63. int adaugareCoada(NodeT *p)
  64. {
  65. nodPretty *q;
  66. q = (nodPretty*)malloc(sizeof(nodPretty));
  67. q->ptr = p;
  68. q->next = 0;
  69. if (!prim)
  70. {
  71. prim = q;
  72. ultim = q;
  73. }
  74. else
  75. {
  76. ultim->next = q;
  77. ultim = q;
  78. }
  79. return 1;
  80. }
  81. NodeT *extragereCoada()
  82. {
  83. nodPretty *p;
  84. NodeT *q;
  85. if (!prim)
  86. return 0;
  87. p = prim;
  88. prim = prim->next;
  89. if (!prim)
  90. ultim = 0;
  91. q = p->ptr;
  92. free(p);
  93. return q;
  94. }
  95. int traversare(NodeT *rad)
  96. {
  97. NodeT *p;
  98. int i;
  99. prim = NULL;
  100. ultim = NULL;
  101. adaugareCoada(rad);
  102. do
  103. {
  104. p = extragereCoada();
  105. if (p!=NULL)
  106. {
  107. if (niv != p->niv)
  108. {
  109. printf("\n");
  110. niv = p->niv;
  111. }
  112. for (i = 0; i<6 / niv; i++)
  113. printf(" ");
  114. printf(" %d: size %d", p->key,p->size);
  115. adaugareCoada(p->left);
  116. adaugareCoada(p->right);
  117. }
  118. } while (p!=NULL || prim!=NULL);
  119. return 1;
  120. }
  121. void preorder(NodeT *p)
  122. {
  123. if (p != NULL)
  124. {
  125. printf("%d ", p->key);
  126. preorder(p->left);
  127. preorder(p->right);
  128. }
  129. }
  130.  
  131.  
  132. int main()
  133. {
  134. int a[13] = { 1,2,3,4,5,6,7,8,9,10,11,12,13};
  135. NodeT* T = buildPBT(a, 0, 12, 1);
  136. niv = T->niv;
  137. preorder(T);
  138. printf("\n");
  139. traversare(T);
  140. printf("\nSelectie: %d\n", OSSelect(T, 1)->key);
  141. return 0;
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement