Advertisement
Guest User

Untitled

a guest
May 24th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.71 KB | None | 0 0
  1. //---------------------------------------------------------------------------
  2.  
  3. #include <vcl.h>
  4. #pragma hdrstop
  5.  
  6. #include "Unit1.h"
  7. //---------------------------------------------------------------------------
  8. #pragma package(smart_init)
  9. #pragma resource "*.dfm"
  10. TForm1 *Form1;
  11. //---------------------------------------------------------------------------
  12. __fastcall TForm1::TForm1(TComponent* Owner)
  13. : TForm(Owner)
  14. {
  15. }
  16. struct Tree {
  17. int info;
  18. Tree *left, *right;
  19. }*root;
  20. void Add_List(Tree*, int);
  21. void View_Tree (Tree*, int);
  22. Tree* Del_Info(Tree*, int);
  23. void Del_Tree(Tree*);
  24. Tree* List(int);
  25. void del(int , Tree*);
  26.  
  27. //---------------------------------------------------------------------------
  28.  
  29. void __fastcall TForm1::Button1Click(TObject *Sender)
  30. {
  31. if(root != NULL) Del_Tree(root);
  32. root = List (StrToInt(Edit1->Text));
  33.  
  34. }
  35. //---------------------------------------------------------------------------
  36. void __fastcall TForm1::Button3Click(TObject *Sender)
  37. {
  38. Memo1->Lines->Clear();
  39. if( root == NULL ) ShowMessage(" Create TREE !");
  40. else {
  41. Memo1->Lines->Add("---------- View -----------");
  42. View_Tree(root, 0);
  43. }
  44.  
  45. }
  46. //---------------------------------------------------------------------------
  47. void __fastcall TForm1::Button2Click(TObject *Sender)
  48. {
  49. if(root == NULL) root = List (StrToInt(Edit1->Text));
  50. else Add_List (root, StrToInt(Edit1->Text));
  51.  
  52. }
  53. //---------------------------------------------------------------------------
  54. void __fastcall TForm1::Button4Click(TObject *Sender)
  55. {
  56. int b = StrToInt(Form1->Edit1->Text);
  57. root = Del_Info(root, b);
  58.  
  59. }
  60. //---------------------------------------------------------------------------
  61. void __fastcall TForm1::Button5Click(TObject *Sender)
  62. {
  63. Del_Tree(root);
  64. ShowMessage(" Tree Delete!");
  65. root = NULL;
  66.  
  67. }
  68. //---------------------------------------------------------------------------
  69. void __fastcall TForm1::Button6Click(TObject *Sender)
  70. {
  71. if(root!=NULL){
  72. Del_Tree(root);
  73. ShowMessage(" Tree Delete!");
  74. }
  75. Close();
  76.  
  77. }
  78. Tree* List(int inf) {
  79. Tree *t = new Tree;
  80. t -> info = inf;
  81. t -> left = t -> right = NULL;
  82. return t;
  83. }
  84.  
  85. void Add_List(Tree *root, int key) {
  86. Tree *prev, *t;
  87. bool find = true;
  88. t = root;
  89. while ( t && find) {
  90. prev = t;
  91. if( key == t->info) {
  92. find = false;
  93. ShowMessage("Dublucate Key!");
  94. }
  95. else
  96. if ( key < t -> info ) t = t -> left;
  97. else t = t -> right;
  98. }
  99. if (find) {
  100. t = List(key);
  101. if ( key < prev -> info ) prev -> left = t;
  102. else prev -> right = t;
  103. }
  104. }
  105. void View_Tree(Tree *p, int level ) {
  106. String str;
  107. if ( p ) {
  108. View_Tree (p -> right , level+1);
  109. for ( int i=0; i<level; i++) str = str + " ";
  110. Form1->Memo1->Lines->Add(str + IntToStr(p->info));
  111. View_Tree(p -> left , level+1);
  112. }
  113. }
  114. void Del_Tree(Tree *t) {
  115. if ( t != NULL) {
  116. Del_Tree ( t -> left);
  117. Del_Tree ( t -> right);
  118. delete t;
  119. }
  120. }
  121. Tree* Del_Info(Tree *root, int key) {
  122. Tree *Del, *Prev_Del, *R, *Prev_R;
  123.  
  124. Del = root;
  125. Prev_Del = NULL;
  126.  
  127. while (Del != NULL && Del -> info != key) {
  128. Prev_Del = Del;
  129. if (Del->info > key) Del = Del->left;
  130. else Del = Del->right;
  131. }
  132. if (Del == NULL) {
  133. ShowMessage ( "NOT Key!");
  134. return root;
  135. }
  136.  
  137. if (Del -> right == NULL) R = Del->left;
  138. else
  139. if (Del -> left == NULL) R = Del->right;
  140. else {
  141.  
  142. Prev_R = Del;
  143. R = Del->left;
  144. while (R->right != NULL) {
  145. Prev_R = R;
  146. R = R->right;
  147. }
  148.  
  149. if( Prev_R == Del) R->right = Del->right;
  150. else {
  151. R->right = Del->right;
  152. Prev_R->right = R->left;
  153. R->left = Prev_R;
  154. }
  155. }
  156. if (Del== root) root = R;
  157. else
  158. if (Del->info < Prev_Del->info)
  159. Prev_Del->left = R;
  160. else Prev_Del->right = R;
  161. delete Del;
  162. return root;
  163. }
  164.  
  165. // Надо поправить
  166. void del(int x,Tree *Tree)
  167. {
  168. Tree *,*t;
  169. if (x<Tree->x)
  170. { p=Tree->l;
  171. if (p==NULL) return ;
  172. if (x!=p->x) return delete_Node(x, p);
  173. delete_p(&Tree->l);
  174. }
  175. if (x>=Tree->x)
  176. { p=Tree->r;
  177. if (p==NULL) return ;
  178. if (x!=p->x) return delete_Node(x, p);
  179. delete_p(&Tree->r);
  180. }
  181. }
  182. //---------------------------------------------------------------------------
  183.  
  184. void __fastcall TForm1::Button7Click(TObject *Sender)
  185. {
  186. int key = StrToInt(Edit1->Text);
  187. del(key, Tree*);
  188. }
  189. //---------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement