Advertisement
Guest User

Untitled

a guest
Feb 17th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.29 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. struct node
  5. {
  6. int data;
  7. node *left;
  8. node *right;
  9. };
  10.  
  11. node* temp1=NULL;
  12.  
  13. void printPre(node* root)
  14. {
  15.  
  16. if(root==NULL)
  17. return;
  18. cout<<root->data<<" ";
  19.  
  20. if(root->left !=NULL)
  21. {
  22. printPre(root->left);
  23. }
  24.  
  25. if(root->right !=NULL)
  26. {
  27. printPre(root->right);
  28. }
  29. }
  30.  
  31. void printIn(node* root)
  32. {
  33. if(root==NULL)
  34. return;
  35. if(root->left !=NULL)
  36. {
  37. printIn(root->left);
  38. }
  39. cout<<root->data<<" ";
  40. if(root->right !=NULL)
  41. {
  42. printIn(root->right);
  43. }
  44. }
  45.  
  46. void printPost(node* root)
  47. {
  48. if(root==NULL)
  49. return;
  50. if(root->left !=NULL)
  51. {
  52. printPost(root->left);
  53. }
  54.  
  55. if(root->right !=NULL)
  56. {
  57. printPost(root->right);
  58. }
  59. cout<<root->data<<" ";
  60. }
  61.  
  62.  
  63. node* bt(int y)
  64. {
  65. struct node *temp=new node();
  66. temp->data=y;
  67. temp->left=NULL;
  68. temp->right=NULL;
  69.  
  70. if(temp1==NULL)
  71. {
  72. temp1=temp;
  73. }
  74. else
  75. {
  76. struct node *temp2=new node();
  77. temp2=temp1;
  78.  
  79. while(true)
  80. {
  81. if(temp2->data>=y)
  82. {
  83. if(temp2->left==NULL)
  84. {
  85. temp2->left=temp;
  86. break;
  87. }
  88. else
  89. {
  90. temp2=temp2->left;
  91. }
  92. }
  93. else
  94. {
  95. if(temp2->right==NULL)
  96. {
  97. temp2->right=temp;
  98. break;
  99. }
  100. else
  101. {
  102. temp2=temp2->right;
  103. }
  104. }
  105. }
  106.  
  107. }
  108. }
  109.  
  110. int main()
  111. {
  112. int i,N,k,C,n=1;
  113. cin>>C;
  114. for(int f=0; f<C; ++f)
  115. {
  116. temp1=NULL;
  117. cin>>N;
  118. for(i=0; i<N; ++i)
  119. {
  120. cin>>k;
  121. bt(k);
  122. }
  123. printf("Case %d:\n",n);
  124. cout<<"Pre.:"<<" ";
  125. printPre(temp1);
  126. cout<<"\n";
  127. cout<<"In..:"<<" ";
  128. printIn(temp1);
  129. cout<<"\n";
  130. cout<<"Post:"<<" ";
  131. printPost(temp1);
  132. cout<<"\n";
  133. n++;
  134. cout<<"\n";
  135. }
  136. return 0;
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement