Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //---------------------------------------------------------------------------
- #include <vcl.h>
- #pragma hdrstop
- #include "Unit1.h"
- //---------------------------------------------------------------------------
- #pragma package(smart_init)
- #pragma resource "*.dfm"
- TForm1 *Form1;
- //---------------------------------------------------------------------------
- __fastcall TForm1::TForm1(TComponent* Owner)
- : TForm(Owner)
- {
- }
- struct Tree {
- int info;
- Tree *left, *right;
- }*root;
- void Add_List(Tree*, int);
- void View_Tree (Tree*, int);
- Tree* Del_Info(Tree*, int);
- void Del_Tree(Tree*);
- Tree* List(int);
- void del(int , Tree*);
- //---------------------------------------------------------------------------
- void __fastcall TForm1::Button1Click(TObject *Sender)
- {
- if(root != NULL) Del_Tree(root);
- root = List (StrToInt(Edit1->Text));
- }
- //---------------------------------------------------------------------------
- void __fastcall TForm1::Button3Click(TObject *Sender)
- {
- Memo1->Lines->Clear();
- if( root == NULL ) ShowMessage(" Create TREE !");
- else {
- Memo1->Lines->Add("---------- View -----------");
- View_Tree(root, 0);
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TForm1::Button2Click(TObject *Sender)
- {
- if(root == NULL) root = List (StrToInt(Edit1->Text));
- else Add_List (root, StrToInt(Edit1->Text));
- }
- //---------------------------------------------------------------------------
- void __fastcall TForm1::Button4Click(TObject *Sender)
- {
- int b = StrToInt(Form1->Edit1->Text);
- root = Del_Info(root, b);
- }
- //---------------------------------------------------------------------------
- void __fastcall TForm1::Button5Click(TObject *Sender)
- {
- Del_Tree(root);
- ShowMessage(" Tree Delete!");
- root = NULL;
- }
- //---------------------------------------------------------------------------
- void __fastcall TForm1::Button6Click(TObject *Sender)
- {
- if(root!=NULL){
- Del_Tree(root);
- ShowMessage(" Tree Delete!");
- }
- Close();
- }
- Tree* List(int inf) {
- Tree *t = new Tree;
- t -> info = inf;
- t -> left = t -> right = NULL;
- return t;
- }
- void Add_List(Tree *root, int key) {
- Tree *prev, *t;
- bool find = true;
- t = root;
- while ( t && find) {
- prev = t;
- if( key == t->info) {
- find = false;
- ShowMessage("Dublucate Key!");
- }
- else
- if ( key < t -> info ) t = t -> left;
- else t = t -> right;
- }
- if (find) {
- t = List(key);
- if ( key < prev -> info ) prev -> left = t;
- else prev -> right = t;
- }
- }
- void View_Tree(Tree *p, int level ) {
- String str;
- if ( p ) {
- View_Tree (p -> right , level+1);
- for ( int i=0; i<level; i++) str = str + " ";
- Form1->Memo1->Lines->Add(str + IntToStr(p->info));
- View_Tree(p -> left , level+1);
- }
- }
- void Del_Tree(Tree *t) {
- if ( t != NULL) {
- Del_Tree ( t -> left);
- Del_Tree ( t -> right);
- delete t;
- }
- }
- Tree* Del_Info(Tree *root, int key) {
- Tree *Del, *Prev_Del, *R, *Prev_R;
- Del = root;
- Prev_Del = NULL;
- while (Del != NULL && Del -> info != key) {
- Prev_Del = Del;
- if (Del->info > key) Del = Del->left;
- else Del = Del->right;
- }
- if (Del == NULL) {
- ShowMessage ( "NOT Key!");
- return root;
- }
- if (Del -> right == NULL) R = Del->left;
- else
- if (Del -> left == NULL) R = Del->right;
- else {
- Prev_R = Del;
- R = Del->left;
- while (R->right != NULL) {
- Prev_R = R;
- R = R->right;
- }
- if( Prev_R == Del) R->right = Del->right;
- else {
- R->right = Del->right;
- Prev_R->right = R->left;
- R->left = Prev_R;
- }
- }
- if (Del== root) root = R;
- else
- if (Del->info < Prev_Del->info)
- Prev_Del->left = R;
- else Prev_Del->right = R;
- delete Del;
- return root;
- }
- // Надо поправить
- void del(int x,Tree *Tree)
- {
- Tree *,*t;
- if (x<Tree->x)
- { p=Tree->l;
- if (p==NULL) return ;
- if (x!=p->x) return delete_Node(x, p);
- delete_p(&Tree->l);
- }
- if (x>=Tree->x)
- { p=Tree->r;
- if (p==NULL) return ;
- if (x!=p->x) return delete_Node(x, p);
- delete_p(&Tree->r);
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TForm1::Button7Click(TObject *Sender)
- {
- int key = StrToInt(Edit1->Text);
- del(key, Tree*);
- }
- //---------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement