Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.17 KB | None | 0 0
  1. struct node {
  2. int n; // liczba kluczy zawarta w węźle
  3. int* key; // tablica kluczy w węźle
  4. node** child; // wskaźniki do synów węzła
  5. bool leaf; // czy węzeł jest liściem
  6. };
  7.  
  8. bool is_sorted(int t[], int n)
  9. {
  10. if(n==1) return true;
  11. for(int i=1; i<n; i++)
  12. {
  13. if(t[i]<t[i-1]) return false;
  14. }
  15. return true;
  16. }
  17.  
  18. int leaf_height(node*p)
  19. {
  20. int height=1;
  21. while(p->leaf==false)
  22. {
  23. height++;
  24. p=p->child[0];
  25. }
  26. return height;
  27. }
  28.  
  29. bool is_proper_height(node*p, int height, int i)
  30. {
  31. if(p==NULL)
  32. {
  33. height=0;
  34. return false;
  35. }
  36.  
  37. if(p->leaf)
  38. {
  39. if(i==height)
  40. return true;
  41. else return false;
  42. }
  43.  
  44. for(int j=0; j<=p->n; j++)
  45. {
  46. if(is_proper_height(p->child[j], height, i+1)==false) return false;
  47. }
  48. return true;
  49.  
  50. }
  51.  
  52. bool is_good_node(node*p, int a, int b)
  53. {
  54. if(p==NULL) return false;
  55. if(p->n<t-1 || p->n>2*t-1) return false;
  56. if(!is_sorted(p->key, p->n)) return false;
  57. if(p->key[0]<a || p->key[p->n-1]>b) return false;
  58. if(p->leaf) return true;
  59.  
  60. for(int i=0; i<=p->n; i++)
  61. {
  62. if(i>0 && i<p->n)
  63. {
  64. if(!is_good_node(p->child[i], p->key[i-1], p->key[i])) return false;
  65. }
  66. else if(i==0)
  67. {
  68. if(!is_good_node(p->child[i], INT_MIN, p->key[i])) return false;
  69. }
  70. else
  71. {
  72. if(!is_good_node(p->child[i], p->key[i-1], INT_MAX)) return false;
  73. }
  74. }
  75. return true;
  76. }
  77.  
  78. bool is_b_tree(node*p)
  79. {
  80. if(p->n>2*t-1) return false;
  81. if(!is_sorted(p->key, p->n)) return false;
  82. int height=leaf_height(p);
  83. if(!is_proper_height(p, height, 1)) return false;
  84. for(int i=0; i<=p->n; i++)
  85. {
  86. if(i>0 && i<p->n)
  87. {
  88. if(!is_good_node(p->child[i], p->key[i-1], p->key[i])) return false;
  89. }
  90. else if(i==0)
  91. {
  92. if(!is_good_node(p->child[i], INT_MIN, p->key[i])) return false;
  93. }
  94. else
  95. {
  96. if(!is_good_node(p->child[i], p->key[i-1], INT_MAX)) return false;
  97. }
  98. }
  99. return true;
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement