
Сообщение от Amgalan
подскажите идею какую -нибудь
|
Поучаствую в честь праздника )))
В гугле есть немало вариантов вывода дерева
Захотелось придумать свой
Для начала сделал обычный класс дерева в консоли
| 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
| #include <iostream>
#include <ctime>
#include <vector>
#include <string>
template <class T>
class BinaryTree
{
private:
struct Node
{
T Data;
int Count;
Node *Left;
Node *Right;
Node(T _Data )
: Data(_Data), Count(0),Left(NULL), Right(NULL)
{
}
};
Node* Root;
void Insert( T _Data,Node *¶mnode)
{
if(paramnode == NULL )
{
paramnode = new Node(_Data);
paramnode->Count = 1;
}
else
{
if(_Data == paramnode->Data)
paramnode->Count++;
else if(_Data < paramnode->Data)
{
Insert(_Data, paramnode->Left);
}
else
{
Insert(_Data, paramnode->Right);
}
}
}
std::vector<Node*> PrintTree(Node* theRoot)
{
std::vector<Node*> myvector, othervector;
if(theRoot != NULL)
{
myvector.push_back(theRoot);
othervector = PrintTree(theRoot->Left);
myvector.insert(myvector.begin(), othervector.begin(), othervector.end());
othervector = PrintTree(theRoot->Right);
myvector.insert(myvector.begin(), othervector.begin(), othervector.end());
}
return myvector;
}
Node *Search(T _Data, Node *paramNode)
{
if(paramNode != NULL)
{
if(_Data == paramNode->Data)
return paramNode;
if(_Data < paramNode->Data)
return Search(_Data, paramNode->Left);
else
return Search(_Data, paramNode->Right);
}
else return NULL;
}
public:
BinaryTree()
:Root(NULL)
{
}
~BinaryTree()
{
DestroyTree(Root);
}
void AddItem(T value)
{
Insert( value,Root);
}
std::vector<Node*> PrintTree()
{
return PrintTree(Root);
}
void PrintTree(int value)
{
return PrintTree(Root, value);
}
bool Search(T _Data)
{
return Search(_Data, Root);
}
void DestroyTree(Node *paramnode)
{
if(paramnode!=NULL)
{
DestroyTree(paramnode->Left);
DestroyTree(paramnode->Right);
delete paramnode;
paramnode = NULL;
}
}
};
int main()
{
srand((unsigned)time(NULL));
BinaryTree<int> *bTree = new BinaryTree<int>();
for (int i = 0; i < 10; i++)
{
bTree->AddItem((double)rand() / (RAND_MAX + 1) * (100 - (-100)) + (-100));
}
for(auto & item : bTree->PrintTree())
std::cout << item->Data << ' ' << item->Count <<std::endl;
delete bTree;
BinaryTree<std::string> *stringTree = new BinaryTree<std::string>();
std::string strarr[] = {"a question"," and", "answer ","site for professional"};
for(auto & item : strarr)
stringTree->AddItem(item);
for(auto & item : stringTree->PrintTree())
std::cout << item->Data << ' ' << item->Count <<std::endl;
delete stringTree;
system("pause");
} |
|
Под WinForms переделал так :
В структуру узла ввел переменные для координат
и булеву переменную для определения корневого узла
(Если узел корневой то будем чертить от него линию )
BinaryTree.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
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
| #pragma once
#include <vector>
#include <string>
template <class T>
class BinaryTree
{
private:
struct Node
{
T Data;
int Count;
int theX, theY;
Node *Left, *Right;
bool testRoot;
Node(T _Data ) : Data(_Data),testRoot(false)
{
Count = theX = theY = 0;
Left = Right = NULL;
}
};
Node* Root;
void Insert( T _Data,Node *¶mnode, int _theX, int _theY)
{
if(paramnode == NULL )
{
paramnode = new Node(_Data);
paramnode->testRoot = true;
paramnode->Count = 1;
paramnode->theX = _theX;
paramnode->theY = _theY;
}
else
{
if(_Data == paramnode->Data)
paramnode->Count++;
else if(_Data < paramnode->Data)
{
paramnode->testRoot = false;
Insert(_Data, paramnode->Left,paramnode->theX - 70, paramnode->theY - 30);
}
else if(_Data > paramnode->Data)
{
paramnode->testRoot = false;
Insert(_Data, paramnode->Right, paramnode->theX + 70, paramnode->theY - 30);
}
}
}
std::vector<Node*> PrintTree(Node* paramnode)
{
std::vector<Node*> myvector, othervector;
if(paramnode != NULL)
{
myvector.push_back(paramnode);
othervector = PrintTree(paramnode->Left);
myvector.insert(myvector.begin(), othervector.begin(), othervector.end());
othervector = PrintTree(paramnode->Right);
myvector.insert(myvector.begin(), othervector.begin(), othervector.end());
}
return myvector;
}
Node *Search(T _Data, Node *paramNode)
{
if(paramNode != NULL)
{
if(_Data == paramNode->Data)
return paramNode;
if(_Data < paramNode->Data)
return Search(_Data, paramNode->Left);
else
return Search(_Data, paramNode->Right);
}
else return NULL;
}
public:
BinaryTree()
:Root(NULL)
{
}
~BinaryTree()
{
DestroyTree(Root);
}
void Clear()
{
DestroyTree(Root);
}
void AddItem(T value)
{
int startX = 250;
int startY = 250;
Insert( value, Root, startX, startY);
}
std::vector<Node*> PrintTree()
{
return PrintTree(Root);
}
bool Search(T _Data)
{
return Search(_Data, Root);
}
void DestroyTree(Node *paramnode)
{
if(paramnode!= NULL)
{
DestroyTree(paramnode->Left);
DestroyTree(paramnode->Right);
delete paramnode;
paramnode = NULL;
}
Root = NULL;
}
}; |
|
Дальше остается добавить этот класс к проекту WinForms
На Форме :
Панель для вывода дерева ,
3 кнопки ,тестбокс для поиска элемента .
лэйблы для надписей
и ричтекстбокс .
Form1.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
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
| #pragma once
#include <vector>
#include <ctime>
#include "BinaryTree.h"
namespace CliCreateForms {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Collections::Generic;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
/// <summary>
/// Сводка для Form1
/// </summary>
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
panel1->BackColor = Color::AntiqueWhite;
bTree = new BinaryTree<int>();
}
protected:
/// <summary>
/// Освободить все используемые ресурсы.
/// </summary>
~Form1()
{
delete bTree;
if (components)
{
delete components;
}
}
private: System::Windows::Forms::Button^ button1;
private: System::Windows::Forms::RichTextBox^ richTextBox1;
private: System::Windows::Forms::Button^ button2;
private: System::Windows::Forms::Panel^ panel1;
BinaryTree<int> *bTree;
private: System::Windows::Forms::Label^ label1;
private: System::Windows::Forms::Label^ label2;
private: System::Windows::Forms::Button^ button3;
private: System::Windows::Forms::TextBox^ textBox1;
private: System::Windows::Forms::Label^ label3;
protected:
protected:
private:
/// <summary>
/// Требуется переменная конструктора.
/// </summary>
System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
/// <summary>
/// Обязательный метод для поддержки конструктора - не изменяйте
/// содержимое данного метода при помощи редактора кода.
/// </summary>
void InitializeComponent(void)
{
this->button1 = (gcnew System::Windows::Forms::Button());
this->richTextBox1 = (gcnew System::Windows::Forms::RichTextBox());
this->button2 = (gcnew System::Windows::Forms::Button());
this->panel1 = (gcnew System::Windows::Forms::Panel());
this->label1 = (gcnew System::Windows::Forms::Label());
this->label2 = (gcnew System::Windows::Forms::Label());
this->button3 = (gcnew System::Windows::Forms::Button());
this->textBox1 = (gcnew System::Windows::Forms::TextBox());
this->label3 = (gcnew System::Windows::Forms::Label());
this->SuspendLayout();
//
// button1
//
this->button1->Location = System::Drawing::Point(31, 371);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(75, 23);
this->button1->TabIndex = 0;
this->button1->Text = L"Создать";
this->button1->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
//
// richTextBox1
//
this->richTextBox1->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 12.25F));
this->richTextBox1->Location = System::Drawing::Point(574, 46);
this->richTextBox1->Name = L"richTextBox1";
this->richTextBox1->Size = System::Drawing::Size(125, 184);
this->richTextBox1->TabIndex = 1;
this->richTextBox1->Text = L"";
//
// button2
//
this->button2->Location = System::Drawing::Point(150, 371);
this->button2->Name = L"button2";
this->button2->Size = System::Drawing::Size(75, 23);
this->button2->TabIndex = 2;
this->button2->Text = L"Очистить";
this->button2->UseVisualStyleBackColor = true;
this->button2->Click += gcnew System::EventHandler(this, &Form1::button2_Click);
//
// panel1
//
this->panel1->Location = System::Drawing::Point(12, 12);
this->panel1->Name = L"panel1";
this->panel1->Size = System::Drawing::Size(541, 340);
this->panel1->TabIndex = 3;
//
// label1
//
this->label1->AutoSize = true;
this->label1->Location = System::Drawing::Point(571, 18);
this->label1->Name = L"label1";
this->label1->Size = System::Drawing::Size(51, 13);
this->label1->TabIndex = 4;
this->label1->Text = L"Элемент";
//
// label2
//
this->label2->AutoSize = true;
this->label2->Location = System::Drawing::Point(633, 18);
this->label2->Name = L"label2";
this->label2->Size = System::Drawing::Size(66, 13);
this->label2->TabIndex = 5;
this->label2->Text = L"Количество";
//
// button3
//
this->button3->Location = System::Drawing::Point(598, 311);
this->button3->Name = L"button3";
this->button3->Size = System::Drawing::Size(75, 23);
this->button3->TabIndex = 6;
this->button3->Text = L"Поиск";
this->button3->UseVisualStyleBackColor = true;
this->button3->Click += gcnew System::EventHandler(this, &Form1::button3_Click);
//
// textBox1
//
this->textBox1->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 12.25F));
this->textBox1->Location = System::Drawing::Point(584, 279);
this->textBox1->Name = L"textBox1";
this->textBox1->Size = System::Drawing::Size(100, 26);
this->textBox1->TabIndex = 7;
//
// label3
//
this->label3->AutoSize = true;
this->label3->Location = System::Drawing::Point(559, 263);
this->label3->Name = L"label3";
this->label3->Size = System::Drawing::Size(155, 13);
this->label3->TabIndex = 8;
this->label3->Text = L"Введите элемент для поиска";
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(718, 403);
this->Controls->Add(this->label3);
this->Controls->Add(this->textBox1);
this->Controls->Add(this->button3);
this->Controls->Add(this->label2);
this->Controls->Add(this->label1);
this->Controls->Add(this->panel1);
this->Controls->Add(this->button2);
this->Controls->Add(this->richTextBox1);
this->Controls->Add(this->button1);
this->Name = L"Form1";
this->Text = L"Form1";
this->ResumeLayout(false);
this->PerformLayout();
}
#pragma endregion
void ClearAll()
{
panel1->Controls->Clear();
panel1->Refresh();
richTextBox1->Clear();
bTree->Clear();
}
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
Graphics ^panelGraphics = panel1->CreateGraphics();
int startX = 0, startY = 0;
int curX = 0, curY = 0;
for (int i = 0; i < 15; i++)
{
bTree->AddItem((double)rand() / (RAND_MAX + 1) * (100 - (-100)) + (-100));
}
for(auto & item : bTree->PrintTree())
{
curX = item->theX;
curY = item->theY;
Label^ label = gcnew Label();
label->Font = gcnew System::Drawing::Font("verdana",12);
label->Width = 40;
label->BackColor = Color::YellowGreen;
if ( curX > panel1->Width - label->Width )
curX = panel1->Width - label->Width - 5;
if ( curY < 0 )
curY = 5;
label->Location = Point(curX, curY);
label->Text = Convert::ToString((int) item->Data);
if( startX != startY != 0 )
{
panelGraphics->DrawLine(gcnew Pen(Color::Brown, 4),Point(startX, startY), Point(curX, curY));
}
panelGraphics->FillRectangle(Brushes::Black , curX- 2, curY - 2, 40, label->Height);
panel1->Controls->Add(label);
if( item->testRoot )
{
startX = curX;
startY = curY;
}
richTextBox1->AppendText(String::Format(" {0} {1}\r\n", item->Data , item->Count));
}
}
private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e)
{
ClearAll();
}
private: System::Void button3_Click(System::Object^ sender, System::EventArgs^ e)
{
if(textBox1->Text->Length != 0)
{
int number = 0;
if(! Int32::TryParse(textBox1->Text, number))
{
MessageBox::Show(L"Осторожно! Ошибка преобразования");
return;
}
bTree->Search(number) ?
MessageBox::Show(L"Элемент найден"): MessageBox::Show(L"Элемент не найден");
}
else
MessageBox::Show(L"Введите элемент для поиска!");
}
};
} |
|
2
|