Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct node {
- int n; // liczba kluczy zawarta w węźle
- int* key; // tablica kluczy w węźle
- node** child; // wskaźniki do synów węzła
- bool leaf; // czy węzeł jest liściem
- };
- bool is_sorted(int t[], int n)
- {
- if(n==1) return true;
- for(int i=1; i<n; i++)
- {
- if(t[i]<t[i-1]) return false;
- }
- return true;
- }
- int leaf_height(node*p)
- {
- int height=1;
- while(p->leaf==false)
- {
- height++;
- p=p->child[0];
- }
- return height;
- }
- bool is_proper_height(node*p, int height, int i)
- {
- if(p==NULL)
- {
- height=0;
- return false;
- }
- if(p->leaf)
- {
- if(i==height)
- return true;
- else return false;
- }
- for(int j=0; j<=p->n; j++)
- {
- if(is_proper_height(p->child[j], height, i+1)==false) return false;
- }
- return true;
- }
- bool is_good_node(node*p, int a, int b)
- {
- if(p==NULL) return false;
- if(p->n<t-1 || p->n>2*t-1) return false;
- if(!is_sorted(p->key, p->n)) return false;
- if(p->key[0]<a || p->key[p->n-1]>b) return false;
- if(p->leaf) return true;
- for(int i=0; i<=p->n; i++)
- {
- if(i>0 && i<p->n)
- {
- if(!is_good_node(p->child[i], p->key[i-1], p->key[i])) return false;
- }
- else if(i==0)
- {
- if(!is_good_node(p->child[i], INT_MIN, p->key[i])) return false;
- }
- else
- {
- if(!is_good_node(p->child[i], p->key[i-1], INT_MAX)) return false;
- }
- }
- return true;
- }
- bool is_b_tree(node*p)
- {
- if(p->n>2*t-1) return false;
- if(!is_sorted(p->key, p->n)) return false;
- int height=leaf_height(p);
- if(!is_proper_height(p, height, 1)) return false;
- for(int i=0; i<=p->n; i++)
- {
- if(i>0 && i<p->n)
- {
- if(!is_good_node(p->child[i], p->key[i-1], p->key[i])) return false;
- }
- else if(i==0)
- {
- if(!is_good_node(p->child[i], INT_MIN, p->key[i])) return false;
- }
- else
- {
- if(!is_good_node(p->child[i], p->key[i-1], INT_MAX)) return false;
- }
- }
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement