Advertisement
Guest User

cyka

a guest
May 25th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.08 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #define nmax 1234
  4. using namespace std;
  5. ifstream in("date.txt");
  6.  
  7. struct nod{
  8. int inf;
  9. nod *st,*dr;
  10.  
  11. };
  12.  
  13. nod *rad;
  14.  
  15. void creare (nod *&p,int x)
  16. {
  17. if(p!=NULL)
  18. {
  19. if(x<p->inf)
  20. creare(p->st,x);
  21. else
  22. {
  23. if(x>p->inf)
  24. creare(p->dr,x);
  25. else
  26. cout<<"Valoare existenta"<<endl;
  27. }
  28. }
  29. else
  30. {
  31. p=new nod;
  32. p->inf=x;
  33. p->st=p->dr=NULL;
  34. }
  35. }
  36. void SRD(nod *rad)
  37. {
  38. if(rad !=NULL)
  39. {
  40. SRD(rad->st);
  41. cout<<rad->inf<<" ";
  42. SRD(rad->dr);
  43. }
  44. }
  45.  
  46. void cmd(nod* &p, nod* &f)
  47. {
  48. nod *aux;
  49. if(f->dr)
  50. cmd(p,f->dr);
  51. else
  52. {
  53. p->inf=f->inf;
  54. aux=f;
  55. f=f->st;
  56. delete aux;
  57. }
  58. }
  59.  
  60. void sterg(nod *&p, int k)
  61. {
  62. nod *aux;
  63.  
  64. if(p!=NULL)
  65. if(p->inf==k)
  66. if(p->st==0&&p->dr==0) ///daca e nod terminal
  67. {
  68. delete p;
  69. p=0;
  70. }
  71. else
  72. if(p->st==0) ///are numai subordonat drept
  73. {
  74. aux=p->dr;
  75. delete p;
  76. p=aux;
  77. }
  78. else
  79. if(p->dr==0) ///are numai subordonat drept
  80. {
  81. aux=p->st;
  82. delete p;
  83. p=aux;
  84. }
  85. else
  86. cmd(p,p->st); ///are ambii subordonati
  87. else
  88. if(p->inf!=k)
  89. sterg(p->dr,k);
  90. else
  91. sterg(p->st,k);
  92. else
  93. cout<<"valoarea de sters nu se gaseste in arbore ";
  94.  
  95. }
  96.  
  97.  
  98. int main()
  99. {
  100. rad=NULL;
  101. while(!in.eof())
  102. {int info;
  103. in>>info;
  104. creare(rad,info);}
  105. SRD(rad);
  106. int x;
  107. cout<<endl<<"valoarea de sters ";
  108. cin>>x;
  109. cout<<endl;
  110. sterg(rad,x);
  111. cout<<endl<<"arborele contine dupa stergere urmatoarele noduri "<<endl;
  112.  
  113. SRD(rad);
  114.  
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement