Advertisement
Guest User

Untitled

a guest
May 19th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.48 KB | None | 0 0
  1. #include <stdio.h>
  2. #include<stdlib.h>
  3. typedef struct Node//khoi tao nut
  4. {
  5. Node* left;
  6. Node* right;
  7. int data;
  8. }Node;
  9. typedef Node* Tree;//tao cay
  10. Node* TaoNode(int x)//tao 1 nut
  11. {
  12. Node* p = (Node*)malloc(sizeof(Node));
  13. p->left = NULL;
  14. p->right = NULL;
  15. p->data = x;
  16. return p;
  17. }
  18. void ThemNodeVaoCay(Tree &T, int x)//them nut vao cay
  19. {
  20. Node* p = TaoNode(x);
  21. if (T == NULL)
  22. T = p;
  23. else
  24. {
  25. if (T->data>x) ThemNodeVaoCay(T->left, x);
  26. else ThemNodeVaoCay(T->right, x);
  27. }
  28. }
  29. void Nhap(Tree &T)//nhap phan tu vao cay
  30. {
  31. int x;
  32. while (1)
  33. {
  34. printf("Nhap vao Node: ");
  35. scanf("%d", &x);
  36. if (x == 0) break;
  37. ThemNodeVaoCay(T, x);
  38. }
  39. }
  40. void Xuat(Tree T)
  41. {
  42. if (T != NULL)
  43. {
  44. printf("%d ", T->data);
  45. if (T->left != NULL)
  46. Xuat(T->left);
  47. if (T->right != NULL)
  48. Xuat(T->right);
  49. }
  50. }
  51.  
  52. int SoLa(Tree T){
  53. if (T == NULL) return 0;
  54. else
  55. if (T->left == NULL && T->right == NULL) return 1;
  56. else
  57. {
  58. int d1 = SoLa(T->left);
  59. int d2 = SoLa(T->right);
  60. return d1 + d2;
  61. }
  62. }
  63. int SoNode(Tree T){
  64. if (T == NULL) return 0;
  65. else
  66. if (T->left == NULL && T->right == NULL) return 1;
  67. else{
  68. int d1 = SoNode(T->left);
  69. int d2 = SoNode(T->right);
  70. return d1 + d2 + 1;
  71. }
  72. }
  73. int Height(Tree T){
  74. if (T == NULL) return 0;
  75. else if (T->left == NULL && T->right == NULL) return 1;
  76. else {
  77. int d1 = Height(T->left);
  78. int d2 = Height(T->right);
  79. if (d1>d2)
  80. return d1 + 1;
  81. else return d2 + 1;
  82. }
  83. }
  84. /*int Search(Tree T,int x){
  85. if(T==NULL) return 0;
  86. if(T->data==x) return x;
  87. else
  88. if(T->data>x)
  89. Search(T->left,x);
  90. else
  91. Search(T->right,x);
  92.  
  93. }*/
  94. /*void GiaTriLonHon_x(Tree T,int x){
  95. if(T!=NULL){
  96. if(T->data>x)
  97. printf("%d\t",T->data);
  98. if (T->left != NULL){
  99. if(T->data>x)
  100. GiaTriLonHon_x(T->left,x);
  101. }
  102. if (T->right != NULL){
  103. if(T->data>x)
  104. GiaTriLonHon_x(T->right,x);
  105. }
  106. }
  107. }*/
  108. int Search(Tree T, int x){
  109. if (!T)
  110. return 0;
  111. if (T->data == x)
  112. return 1 + Search(T->left, x) + Search(T->right, x);
  113.  
  114. return 0 + Search(T->left, x) + Search(T->right, x);
  115. }
  116. int main()
  117. {
  118. Tree T = NULL;
  119. Nhap(T);
  120. printf("\nXuat cay nhi phan tim kiem theo thu tu truoc NLR: ");
  121. Xuat(T);
  122. printf("\n Tong cac nut la trong cay: %d", SoLa(T));
  123. printf("\n Tong cac so nut trong cay: %d", SoNode(T));
  124. printf("\n Chieu cao cua cay la: %d ", Height(T));
  125. int x;
  126. printf("\nNhap vao gia tri de tim: ");
  127. scanf("%d", &x);
  128. int s = Search(T, x);
  129. if (s != 0)
  130. {
  131. printf("\n%d xuat hien %d lan trong cay ", x, s);
  132. }
  133.  
  134. return 0;
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement