Advertisement
Guest User

Untitled

a guest
Oct 12th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. struct TREE{
  4. int num;
  5. int count;
  6. TREE *left, *right;
  7. };
  8.  
  9. TREE* root = NULL;
  10.  
  11. TREE* create(int n){
  12. TREE *t = new TREE();
  13. t->num = n;
  14. t->count = 0;
  15. t->left = t->right = NULL;
  16. return t;
  17. }
  18.  
  19. void addson(TREE* t, TREE* p){
  20. if(t->num == p->num){
  21. t->count++;
  22. return;
  23. }
  24. if(t->num > p->num){
  25. if(p->right == NULL){
  26. p->right = t;
  27. return;
  28. }
  29. else
  30. addson(t, p->right);
  31. }else{
  32. if(p->left == NULL){
  33. p->left = t;
  34. return;
  35. }
  36. else
  37. addson(t, p->left);
  38. }
  39. }
  40.  
  41. TREE* finder(TREE* node, int n){
  42. if(node == NULL)
  43. return NULL;
  44. if(n == node->num)
  45. return node;
  46. if(n > node->num)
  47. return finder(node->right, n);
  48. else
  49. return finder(node->left, n);
  50. }
  51.  
  52. void add(int n){
  53. TREE* index = create(n);
  54. if(root == NULL){
  55. root = index;
  56. return;
  57. }
  58. addson(index, root);
  59. }
  60.  
  61. void print(TREE* cur){
  62. if(cur != NULL){
  63. print(cur->left);
  64. printf("%d ", cur->num);
  65. print(cur->right);
  66. }
  67. }
  68.  
  69. int main(){
  70. int n, num;
  71. scanf("%d", &n);
  72. for(int i = 0; i < n; i++){
  73. scanf("%d", &num);
  74. add(num);
  75. }
  76. print(root);
  77. scanf("%d", &num);
  78. TREE* m = finder(root, num);
  79. printf("%d ", m->count);
  80. return 0;
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement