Sanath12345

Untitled

May 21st, 2021
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.77 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3. struct bnode{
  4. int data;
  5. struct bnode *l,*r;
  6. bnode(int data){
  7. this->data=data;
  8. l=NULL;
  9. r=NULL;
  10. };
  11. };
  12. struct queue{
  13. int size;
  14. int front;
  15. int rear;
  16. struct bnode *elements[50];
  17. };
  18. void enqueue(struct queue *Q,struct bnode *T){
  19. if((Q->rear+1)%Q->size==Q->front) cout<<"queue is full ";
  20. else{
  21. if(Q->rear==-1){
  22. Q->rear=0;Q->front=0;
  23. }
  24. else Q->rear=(Q->rear+1)%Q->size;
  25. Q->elements[Q->rear]=T;
  26. }
  27. }
  28. struct bnode *dequeue(struct queue *Q){
  29. if(Q->front==-1) cout<<"queue is empty ";
  30. else{
  31. struct bnode *t=Q->elements[Q->front];
  32. if(Q->front==Q->rear){
  33. Q->front=-1;Q->rear=-1;
  34. }
  35. else Q->front++;
  36. return t;
  37. }
  38. }
  39. void getSibling(struct bnode *T,int x){
  40. struct queue *Q=new queue();
  41. Q->front=-1;
  42. Q->rear=-1;
  43. Q->size=10;
  44. enqueue(Q,T);
  45. struct bnode *e=new bnode(-1);
  46. enqueue(Q,e);
  47. struct queue *ans=new queue();
  48. ans->front=-1;
  49. ans->rear=-1;
  50. ans->size=10;
  51. while(Q->rear!=-1){
  52. struct bnode *p=dequeue(Q);
  53. enqueue(ans,p);
  54. if(p->data==-1){
  55. if(Q->rear==-1) return ;
  56. p=dequeue(Q);
  57. enqueue(Q,e);
  58. int tag=0;
  59. while(ans->rear!=-1){
  60. struct bnode *c=dequeue(ans);
  61. if(c->data==-1){
  62. enqueue(ans,c);
  63. break;
  64. }
  65. if(c->data==x){
  66. tag=1;
  67. }
  68. enqueue(ans,c);
  69. }
  70. if(tag==1){
  71. int flag=0;
  72. while(ans->rear!=-1){
  73. struct bnode *c=dequeue(ans);
  74. if(c->data==-1){
  75. if(flag==0) cout<<-1<<endl;
  76. return ;
  77. }
  78. if(c->data!=x){
  79. flag=1;
  80. cout<<c->data<<" ";
  81. }
  82. }
  83. }
  84. if(tag==0){
  85. while(ans->rear!=-1){
  86. dequeue(ans);
  87. }
  88. }
  89. enqueue(ans,p);
  90. }
  91. if(p->l!=NULL) enqueue(Q,p->l);
  92. if(p->r!=NULL) enqueue(Q,p->r);
  93. }
  94. }
  95. void getparent(struct bnode *T,int x,int &p){
  96. if(T==NULL) return;
  97. if(T->l!=NULL and T->l->data==x){
  98. p=T->data;
  99. return ;
  100. }
  101. if(T->r!=NULL and T->r->data==x){
  102. p=T->data;
  103. return ;
  104. }
  105. getparent(T->l,x,p);
  106. getparent(T->r,x,p);
  107. }
  108. int main(){
  109. // creating given binary tree
  110. struct bnode *T=new bnode(4);
  111. T->l=new bnode(3);
  112. T->r=new bnode(9);
  113. T->l->l=new bnode(5);
  114. T->l->r=new bnode(6);
  115. T->r->l=new bnode(1);
  116. T->r->r=new bnode(8);
  117. // tree created
  118. int x;
  119. cout<<"enter node value : "<<endl;
  120. cin>>x;
  121. int p=-1;
  122. getparent(T,x,p);
  123. if(p!=-1){
  124. cout<<"parent is "<<p<<endl;
  125. cout<<"uncle of "<<x<<" is : ";
  126. getSibling(T,p);
  127. }
  128. else{
  129. cout<<-1<<endl;
  130. cout<<"no parent therefore no uncle"<<endl;
  131. }
  132.  
  133. }
Advertisement
Add Comment
Please, Sign In to add comment