Advertisement
Guest User

Untitled

a guest
May 27th, 2015
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.93 KB | None | 0 0
  1. #include <stdio.h>
  2. struct Node
  3. {
  4. int Key;
  5. Node* Left;
  6. Node* Right;
  7. };
  8. typedef Node* Tree;
  9. Node* TaoNode(int X)
  10. {
  11. Node* p= new Node;
  12. if(p==NULL) return NULL;
  13. p->Left = p->Right=NULL;
  14. p->Key= X;
  15. return p;
  16. }
  17. void ThemNodeVaoCay( Node* p,Tree &c)
  18. {
  19. if(c==NULL) c=p;
  20. else
  21. {
  22. if(p->Key< c->Key)
  23. ThemNodeVaoCay(p,c->Left);
  24. else if(p->Key > c->Key) ThemNodeVaoCay(p,c->Right);
  25. else return;
  26. }}
  27. void Nhap(Tree &c)
  28. { int chon = 0;
  29. do
  30. { int x;
  31. printf("\n Nhap x: ");
  32. scanf("%d",&x);
  33. Node* p= TaoNode(x);
  34. ThemNodeVaoCay(p,c);
  35. printf("Bam 1 de tiep tuc nhap, 0 de out");
  36. scanf("%d",&chon);
  37. }while(chon);
  38. }
  39. void Xuat(Tree c)
  40. {if (c!=NULL)
  41. { if(c->Left!=NULL)
  42. Xuat(c->Left);
  43. if(c->Right!=NULL)
  44. Xuat(c->Right);
  45. }
  46. }
  47. void LNR(Tree c)
  48. { if(c!=NULL)
  49. { LNR(c->Left);
  50. LNR(c->Right);
  51. printf(" %d " , c->Key);
  52. }
  53. }
  54. void main()
  55. {
  56. Tree c = NULL;
  57. Nhap(c);
  58. printf(" Xuat theo LNR : " )
  59. Xuat(c);
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement