Форум программистов, компьютерный форум, киберфорум
C++ Builder
Войти
Регистрация
Восстановить пароль
Карта форума Темы раздела Блоги Сообщество Поиск Заказать работу  
 
Рейтинг 4.71/7: Рейтинг темы: голосов - 7, средняя оценка - 4.71
0 / 0 / 0
Регистрация: 25.10.2015
Сообщений: 26
1

Программирование с использованием деревьев на основе рекурсивных типов данных

28.10.2015, 23:56. Показов 1291. Ответов 6
Метки нет (Все метки)

Author24 — интернет-сервис помощи студентам
Хочу к своей программе дописать еще одну функцию (она на картинке ) .
Но после вбивания кода выдает ошибку : E2451 Undefined symbol 'Res'
Плюс к этому я не знаю как правильно вызвать эту функцию (функция на картинке )
Программирование с использованием деревьев на основе рекурсивных типов данных


Выложил рабочий код без этой функции

Это "LR8.cpp"
C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
//---------------------------------------------------------------------------
 
#include <vcl.h>
#pragma hdrstop
 
#include "LR8.h"
#include "lr8u.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
int n=9;
TTree *proot;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------
 
void __fastcall TForm1::FormCreate(TObject *Sender)
{
Edit1->Text="9";
StringGrid1->Cells[0][0] = "ф.В.О.";
StringGrid1->Cells[1][0] = "Ключ";
StringGrid1->Cells[0][1] = "A00"; StringGrid1->Cells[1][1] = "5";
StringGrid1->Cells[0][2] = "A01"; StringGrid1->Cells[1][2] = "2";
StringGrid1->Cells[0][3] = "A02"; StringGrid1->Cells[1][3] = "4";
StringGrid1->Cells[0][4] = "A03"; StringGrid1->Cells[1][4] = "1";
StringGrid1->Cells[0][5] = "A04"; StringGrid1->Cells[1][5] = "7";
StringGrid1->Cells[0][6] = "A05"; StringGrid1->Cells[1][6] = "6";
StringGrid1->Cells[0][7] = "A06"; StringGrid1->Cells[1][7] = "8";
StringGrid1->Cells[0][8] = "A07"; StringGrid1->Cells[1][8] = "0";
StringGrid1->Cells[0][9] = "A08"; StringGrid1->Cells[1][9] = "9";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BitBtn2Click(TObject *Sender)
{
 TInf A;
  A.fio = StringGrid1->Cells[0][1];
  A.key = StrToInt(StringGrid1->Cells[1][1]);
   MakeList(&proot,A);
  for (int i=2;i<=n;i++){
    A.fio = StringGrid1->Cells[0][i];
    A.key = StrToInt(StringGrid1->Cells[1][i]);
     DobTree(proot,A);
       }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BitBtn3Click(TObject *Sender)
{
 int kl=-1;
 TreeView1->Items->Clear();
  ViewTree(proot,kl);
  TreeView1->FullExpand();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BitBtn4Click(TObject *Sender)
{
 DeleteTree(&proot);
 Memo1->Clear();
 TreeView1->Items->Clear();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BitBtn5Click(TObject *Sender)
{
 Memo1->Clear();
 WrtTree(&proot);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BitBtn1Click(TObject *Sender)
{
 n=StrToInt(Edit1->Text);
  StringGrid1->RowCount = n+1;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BitBtn6Click(TObject *Sender)
{
 Memo1->Clear();
 WrtTreee(&proot);
}

Это "lr8u.h"

C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#pragma hdrstop
#include "lr8u.h"
#include "lr8.h"
#include <alloc.h>
 
//---------------------------------------------------------------------------
 
#pragma package(smart_init)
 
//---------------------------------------------------------------------------
 
void MakeList(TTree **p,TInf Inf)
{
 *p = new TTree;
 (*p)->Inf = Inf;
 (*p)->A1=NULL;
 (*p)->A2=NULL;
}
 
void DobTree(TTree *proot, TInf Inf )
{
 TTree *p = proot, *q;
 bool bl;
    while (p != NULL) {
     q = p ;
      bl = (Inf.key < p->Inf.key);
       if(bl) p = p->A1;
        else p = p->A2;
              }
    MakeList(&p,Inf);
     if (bl) q->A1=p; else q->A2=p;
}
 
void ViewTree(TTree *p, int kl)
{
 if (p == NULL) return;
  if (kl == -1)
    Form1->TreeView1->Items->AddFirst(NULL,p->Inf.fio+" = "+IntToStr(p->Inf.key));
      else
    Form1->TreeView1->Items->AddChildFirst(Form1->TreeView1->Items->Item[kl], p->Inf.fio+" = "+IntToStr(p->Inf.key));
       kl++;
      ViewTree(p->A1,kl);
      ViewTree(p->A2,kl);
       kl--;
}
 
void DeleteTree(TTree **p)
{
 if (*p == NULL)return;
  DeleteTree(&(*p)->A1);
  DeleteTree(&(*p)->A2);
   delete(*p);
   *p=NULL;
}
 
void WrtTree(TTree **p)
{
 if (*p == NULL )return;
   Form1->Memo1->Lines->Add((*p)->Inf.fio+" "+(*p)->Inf.key);
   WrtTree((&(*p)->A1));
   WrtTree((&(*p)->A2));
}
 
void WrtTreee(TTree **p)
{
 if (*p == NULL )return;
   WrtTree((&(*p)->A1));
   WrtTree((&(*p)->A2));
   Form1->Memo1->Lines->Add((*p)->Inf.fio+" "+(*p)->Inf.key);
}

Это текст заголовочного файла "lr8u.h"

C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#ifndef lr8uH
#define lr8uH
#include <vcl.h>
 
struct TInf{
  int key;
  AnsiString fio;
  };
 
 
struct TTree {
   TInf Inf;
   TInf *Res;
   TTree *A1;
   TTree *A2;
    };
 
#endif
 
//---------------------------------------------------------------------------
void MakeList(TTree**,TInf);
void DobTree(TTree*,TInf);
void ViewTree(TTree*, int);
void DeleteTree(TTree**);
void WrtTree(TTree**);
void WrtTreee(TTree**);
Может кто знает как правильно вбить эту функцию ?
0
Programming
Эксперт
94731 / 64177 / 26122
Регистрация: 12.04.2006
Сообщений: 116,782
28.10.2015, 23:56
Ответы с готовыми решениями:

Программирование с использованием типов данных, создаваемых пользователем
Получить список кинотеатров, в которых идут фильмы кинокомпа- нии Warner Bros. Type kinoafisha...

Программирование с использованием составных пользовательских типов данных
Получить список театров и количество мест в залах, в которых идут спектакли А.П. Чехова Выдает...

Программирование задач с использованием файловых типов данных
Дан текстовый файл f. Переписать компоненты файла f в файл g ,вставляя в начало каждой строки...

Организация списков в виде очереди на основе рекурсивных данных
Создать список из случайных чисел в виде очереди и отсортировать рекурсивным методом слияния, после...

6
управление сложностью
1687 / 1300 / 259
Регистрация: 22.03.2015
Сообщений: 7,545
Записей в блоге: 5
29.10.2015, 08:43 2
Начнем с того, что это процедура, а не функция.
1
0 / 0 / 0
Регистрация: 25.10.2015
Сообщений: 26
29.10.2015, 14:28  [ТС] 3
Замечание принял , продолжение будет ?
0
Супер-модератор
Эксперт Pascal/DelphiАвтор FAQ
32835 / 21172 / 8148
Регистрация: 22.10.2011
Сообщений: 36,432
Записей в блоге: 8
29.10.2015, 14:38 4
Лучший ответ Сообщение было отмечено Paradontoks как решение

Решение

Цитата Сообщение от Paradontoks Посмотреть сообщение
после вбивания кода выдает ошибку : E2451 Undefined symbol 'Res'
Значит, неправильно вбил. Res - это параметр, как он может быть не определен? Почему функция - в виде картинки, а не в виде текста? Чтобы мы вбили тоже ее от руки? Так оно не нам надо, вообще-то.
Цитата Сообщение от Paradontoks Посмотреть сообщение
Плюс к этому я не знаю как правильно вызвать эту функцию
C++
1
2
3
4
5
TInf res;
bool b;
int search_for = 1; // чего будем искать?
 
PoiskTree(root, search_for, &b, &res); // вызов функции (в С/С++ нет понятия процедура, вообще-то)
0
0 / 0 / 0
Регистрация: 25.10.2015
Сообщений: 26
30.10.2015, 21:20  [ТС] 5
Выдает ошибку :
[C++ Error] lr8u.cpp(87): E2171 Body has already been defined for function 'PoiskTree(TTree *,int,bool *,TInf *)'


Проблема как я понял с TInf *Res

C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
//---------------------------------------------------------------------------
 
#include <vcl.h>
#pragma hdrstop
 
#include "LR8.h"
#include "lr8u.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
int n=9;
TTree *proot;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------
 
void __fastcall TForm1::FormCreate(TObject *Sender)
{
Edit1->Text="9";
StringGrid1->Cells[0][0] = "ô.Â.Î.";
StringGrid1->Cells[1][0] = "Êëþ÷";
StringGrid1->Cells[0][1] = "A00"; StringGrid1->Cells[1][1] = "5";
StringGrid1->Cells[0][2] = "A01"; StringGrid1->Cells[1][2] = "2";
StringGrid1->Cells[0][3] = "A02"; StringGrid1->Cells[1][3] = "4";
StringGrid1->Cells[0][4] = "A03"; StringGrid1->Cells[1][4] = "1";
StringGrid1->Cells[0][5] = "A04"; StringGrid1->Cells[1][5] = "7";
StringGrid1->Cells[0][6] = "A05"; StringGrid1->Cells[1][6] = "6";
StringGrid1->Cells[0][7] = "A06"; StringGrid1->Cells[1][7] = "8";
StringGrid1->Cells[0][8] = "A07"; StringGrid1->Cells[1][8] = "0";
StringGrid1->Cells[0][9] = "A08"; StringGrid1->Cells[1][9] = "9";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BitBtn2Click(TObject *Sender)
{
 TInf A;
  A.fio = StringGrid1->Cells[0][1];
  A.key = StrToInt(StringGrid1->Cells[1][1]);
   MakeList(&proot,A);
  for (int i=2;i<=n;i++){
    A.fio = StringGrid1->Cells[0][i];
    A.key = StrToInt(StringGrid1->Cells[1][i]);
     DobTree(proot,A);
       }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BitBtn3Click(TObject *Sender)
{
 int kl=-1;
 TreeView1->Items->Clear();
  ViewTree(proot,kl);
  TreeView1->FullExpand();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BitBtn4Click(TObject *Sender)
{
 DeleteTree(&proot);
 Memo1->Clear();
 TreeView1->Items->Clear();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BitBtn5Click(TObject *Sender)
{
 Memo1->Clear();
 WrtTree(&proot);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BitBtn1Click(TObject *Sender)
{
 n=StrToInt(Edit1->Text);
  StringGrid1->RowCount = n+1;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BitBtn6Click(TObject *Sender)
{
 Memo1->Clear();
 WrtTreee(&proot);
}
//---------------------------------------------------------------------------
 
 
void __fastcall TForm1::BitBtn7Click(TObject *Sender)
{
 TInf Res;
 int k=StrToInt(Edit2->Text);
 bool b = false ;
 PoiskTree(proot, k, &b, &Res);
  if (b) Memo1->Lines->Add("  ");
}
C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#pragma hdrstop
#include "lr8u.h"
#include "lr8.h"
#include <alloc.h>
 
//---------------------------------------------------------------------------
 
#pragma package(smart_init)
 
//---------------------------------------------------------------------------
 
void MakeList(TTree **p,TInf Inf)
{
 *p = new TTree;
 (*p)->Inf = Inf;
 (*p)->A1=NULL;
 (*p)->A2=NULL;
}
 
void DobTree(TTree *proot, TInf Inf )
{
 TTree *p = proot, *q;
 bool bl;
    while (p != NULL) {
     q = p ;
      bl = (Inf.key < p->Inf.key);
       if(bl) p = p->A1;
        else p = p->A2;
              }
    MakeList(&p,Inf);
     if (bl) q->A1=p; else q->A2=p;
}
 
void ViewTree(TTree *p, int kl)
{
 if (p == NULL) return;
  if (kl == -1)
    Form1->TreeView1->Items->AddFirst(NULL,p->Inf.fio+" = "+IntToStr(p->Inf.key));
      else
    Form1->TreeView1->Items->AddChildFirst(Form1->TreeView1->Items->Item[kl], p->Inf.fio+" = "+IntToStr(p->Inf.key));
       kl++;
      ViewTree(p->A1,kl);
      ViewTree(p->A2,kl);
       kl--;
}
 
void DeleteTree(TTree **p)
{
 if (*p == NULL)return;
  DeleteTree(&(*p)->A1);
  DeleteTree(&(*p)->A2);
   delete(*p);
   *p=NULL;
}
 
void WrtTree(TTree **p)
{
 if (*p == NULL )return;
   Form1->Memo1->Lines->Add((*p)->Inf.fio+" "+(*p)->Inf.key);
   WrtTree((&(*p)->A1));
   WrtTree((&(*p)->A2));
}
 
void WrtTreee(TTree **p)
{
 if (*p == NULL )return;
   WrtTree((&(*p)->A1));
   WrtTree((&(*p)->A2));
   Form1->Memo1->Lines->Add((*p)->Inf.fio+" "+(*p)->Inf.key);
}
 
void PoiskTree(TTree *p,int k,bool *bl,TInf *Res)
{
 if ((p != NULL ) && (*bl != true )) {
  if (p->Inf.key != k ) {
  PoiskTree(p->A1,k,bl,Res);
  PoiskTree(p->A2,k,bl,Res);
                }
          else {
        *bl=true;
        *Res = p->Inf;
                }
                       }
}
 
void PoiskTree(TTree *p, int k ,bool *bl ,TInf *Res)
{
 if ((p != NULL ) && (*bl !=true )){
  if (p->Inf.key != k ) {
    PoiskTree(p->A1,k,bl,Res);
    PoiskTree(p->A2,k,bl,Res);
               }
               else {
               *bl=true;
               *Res = p->Inf;
                }
   }
}
C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#ifndef lr8uH
#define lr8uH
#include <vcl.h>
 
struct TInf{
  int key;
  AnsiString fio;
  };
 
 
struct TTree {
   TInf Inf;
   TTree *A1;
   TTree *A2;
    };
 
#endif
 
//---------------------------------------------------------------------------
void MakeList(TTree**,TInf);
void DobTree(TTree*,TInf);
void ViewTree(TTree*, int);
void DeleteTree(TTree**);
void WrtTree(TTree**);
void WrtTreee(TTree**);
void PoiskTree(TTree* , int ,bool *bl ,TInf *Res);
0
Супер-модератор
Эксперт Pascal/DelphiАвтор FAQ
32835 / 21172 / 8148
Регистрация: 22.10.2011
Сообщений: 36,432
Записей в блоге: 8
30.10.2015, 21:54 6
Лучший ответ Сообщение было отмечено Paradontoks как решение

Решение

Цитата Сообщение от Paradontoks Посмотреть сообщение
Проблема как я понял с TInf *Res
Проблема - в том, что реализация PoiskTree скопирована дважды зачем-то.
1
0 / 0 / 0
Регистрация: 25.10.2015
Сообщений: 26
31.10.2015, 19:28  [ТС] 7
На самом деле реализация PoiskTree была написана 1 раз , это я такой криворук

Добавлено через 7 минут
А нет , вы правы , действительно 2 раза
Спасибо заработала .
Может вы поможете разобраться с остальными функциями ?

Добавлено через 21 час 10 минут
C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
//---------------------------------------------------------------------------
 
#include <vcl.h>
#pragma hdrstop
 
#include "LR8.h"
#include "lr8u.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
int n=9;
TTree *proot;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------
 
void __fastcall TForm1::FormCreate(TObject *Sender)
{
Edit1->Text="9";
StringGrid1->Cells[0][0] = "ô.Â.Î.";
StringGrid1->Cells[1][0] = "Êëþ÷";
StringGrid1->Cells[0][1] = "A00"; StringGrid1->Cells[1][1] = "5";
StringGrid1->Cells[0][2] = "A01"; StringGrid1->Cells[1][2] = "2";
StringGrid1->Cells[0][3] = "A02"; StringGrid1->Cells[1][3] = "4";
StringGrid1->Cells[0][4] = "A03"; StringGrid1->Cells[1][4] = "1";
StringGrid1->Cells[0][5] = "A04"; StringGrid1->Cells[1][5] = "7";
StringGrid1->Cells[0][6] = "A05"; StringGrid1->Cells[1][6] = "6";
StringGrid1->Cells[0][7] = "A06"; StringGrid1->Cells[1][7] = "8";
StringGrid1->Cells[0][8] = "A07"; StringGrid1->Cells[1][8] = "0";
StringGrid1->Cells[0][9] = "A08"; StringGrid1->Cells[1][9] = "9";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BitBtn2Click(TObject *Sender)
{
 TInf A;
  A.fio = StringGrid1->Cells[0][1];
  A.key = StrToInt(StringGrid1->Cells[1][1]);
   MakeList(&proot,A);
  for (int i=2;i<=n;i++){
    A.fio = StringGrid1->Cells[0][i];
    A.key = StrToInt(StringGrid1->Cells[1][i]);
     DobTree(proot,A);
       }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BitBtn3Click(TObject *Sender)
{
 int kl=-1;
 TreeView1->Items->Clear();
  ViewTree(proot,kl);
  TreeView1->FullExpand();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BitBtn4Click(TObject *Sender)
{
 DeleteTree(&proot);
 Memo1->Clear();
 TreeView1->Items->Clear();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BitBtn5Click(TObject *Sender)
{
 Memo1->Clear();
 WrtTree(&proot);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BitBtn1Click(TObject *Sender)
{
 n=StrToInt(Edit1->Text);
  StringGrid1->RowCount = n+1;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BitBtn6Click(TObject *Sender)
{
 Memo1->Clear();
 WrtTreee(&proot);
}
//---------------------------------------------------------------------------
 
 
void __fastcall TForm1::BitBtn7Click(TObject *Sender)
{
 TInf Res;
 int k=StrToInt(Edit2->Text);
 bool b = false ;
 PoiskTree(proot, k, &b, &Res);
  if (b) Memo1->Lines->Add(" 123 ");
}
//---------------------------------------------------------------------------
 
 
void __fastcall TForm1::BitBtn8Click(TObject *Sender)
{
 TInf Inf;
 UnTree(&proot ,Inf);
}
//---------------------------------------------------------------------------

C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#pragma hdrstop
#include "lr8u.h"
#include "lr8.h"
#include <alloc.h>
 
//---------------------------------------------------------------------------
 
#pragma package(smart_init)
 
//---------------------------------------------------------------------------
 
void MakeList(TTree **p,TInf Inf)
{
 *p = new TTree;
 (*p)->Inf = Inf;
 (*p)->A1=NULL;
 (*p)->A2=NULL;
}
 
void DobTree(TTree *proot, TInf Inf )
{
 TTree *p = proot, *q;
 bool bl;
    while (p != NULL) {
     q = p ;
      bl = (Inf.key < p->Inf.key);
       if(bl) p = p->A1;
        else p = p->A2;
              }
    MakeList(&p,Inf);
     if (bl) q->A1=p; else q->A2=p;
}
 
void ViewTree(TTree *p, int kl)
{
 if (p == NULL) return;
  if (kl == -1)
    Form1->TreeView1->Items->AddFirst(NULL,p->Inf.fio+" = "+IntToStr(p->Inf.key));
      else
    Form1->TreeView1->Items->AddChildFirst(Form1->TreeView1->Items->Item[kl], p->Inf.fio+" = "+IntToStr(p->Inf.key));
       kl++;
      ViewTree(p->A1,kl);
      ViewTree(p->A2,kl);
       kl--;
}
 
void DeleteTree(TTree **p)
{
 if (*p == NULL)return;
  DeleteTree(&(*p)->A1);
  DeleteTree(&(*p)->A2);
   delete(*p);
   *p=NULL;
}
 
void WrtTree(TTree **p)
{
 if (*p == NULL )return;
   Form1->Memo1->Lines->Add((*p)->Inf.fio+" "+(*p)->Inf.key);
   WrtTree((&(*p)->A1));
   WrtTree((&(*p)->A2));
}
 
void WrtTreee(TTree **p)
{
 if (*p == NULL )return;
   WrtTree((&(*p)->A1));
   WrtTree((&(*p)->A2));
   Form1->Memo1->Lines->Add((*p)->Inf.fio+" "+(*p)->Inf.key);
}
 
void PoiskTree(TTree *p,int k,bool *bl,TInf *Res)
{
 if ((p != NULL ) && (*bl != true )) {
  if (p->Inf.key != k ) {
  PoiskTree(p->A1,k,bl,Res);
  PoiskTree(p->A2,k,bl,Res);
                }
          else {
        *bl=true;
        *Res = p->Inf;
                }
                       }
}
 
void UnTree(TTree **proot , TInf Inf)
{
 TTree *p = *proot, *q = p , *w, *v;
 while ((p !=NULL ) && (p->Inf.key != Inf.key)){
   q = p ;
    if (p->Inf.key > Inf.key ) p = p->A1;
        else p = p->A2;
                    }
     if (p == NULL) return ;
      if ((p->A1 == NULL) && (p->A2 == NULL)) {
              if (p == q) *proot = NULL;
               else
                if (q->A1 == p) q->A1 = NULL;
                 else q->A2 = NULL;
                   }
  else
   if(p->A1 == NULL) {
     if (p ==q )*proot = p->A2;
      else
       if (q->A1 == p) q->A1 = p->A2;
         else q->A2 = p->A2;
          }
   else
         {
        w = p->A1;
         if (w->A2 == NULL ) w->A2 = p-> A2;
           else {
             do {
               v = w;
                w = w->A2;
                 } while (w->A2 != NULL);
         v->A2 = w->A1;
         w->A1 = p->A1;
         w->A2 = p->A2;
           }
   if (p == *proot) *proot = w;
    else
     if (q->A1 == p) q->A1 = w;
       else q->A2 = w;
         }
    delete(p);
}
C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#ifndef lr8uH
#define lr8uH
#include <vcl.h>
 
struct TInf{
  int key;
  AnsiString fio;
  };
 
 
struct TTree {
   TInf Inf;
   TTree *A1;
   TTree *A2;
    };
 
#endif
 
//---------------------------------------------------------------------------
void MakeList(TTree**,TInf);
void DobTree(TTree*,TInf);
void ViewTree(TTree*, int);
void DeleteTree(TTree**);
void WrtTree(TTree**);
void WrtTreee(TTree**);
void PoiskTree(TTree* , int ,bool *bl ,TInf *Res);
void UnTree(TTree** , TInf Inf);
Помогите реализовать функцию UnTree
хочу удаляемое значение вводить с клавиатуры
0
31.10.2015, 19:28
IT_Exp
Эксперт
87844 / 49110 / 22898
Регистрация: 17.06.2006
Сообщений: 92,604
31.10.2015, 19:28
Помогаю со студенческими работами здесь

Структура данных. Разработать приложение на основе деревьев
здравствуйте, подскажите идею что можно сделать и как можно реализовать программу по...

Программирование с использованием модулей и процедурных типов
Даны матрицы А и В. Из четных сумм элементов строк матрицы А сформировать вектор С. Из сумм...

Функции на основе пользовательских типов данных
Всем привет. Читаю книгу страуструпа и заметил там функции на основе пользовательских типов данных....

Алгоритм с использованием примитивных типов данных
Реализовать алгоритм с использованием примитивных типов данных.


Искать еще темы с ответами

Или воспользуйтесь поиском по форуму:
7
Ответ Создать тему
КиберФорум - форум программистов, компьютерный форум, программирование
Powered by vBulletin
Copyright ©2000 - 2024, CyberForum.ru