11.07.2011, 23:20. Просмотров 1370. Ответов 0
все никак немогу доработать добавление
надо чтобы пользователь сначала ввел значение узла, а затем ввел значение узла после которого добавить
дерево произвольное, т.е. кол-во потомков каждого узла может быть разным
помогите пожалуйста доработать программу
вот мой код:
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
| #include "stdafx.h"
#include <iostream>
using namespace std;
// Описание дерева
struct Info
{
int Key;
};
// Создание дерева
struct Node
{
Info data;
Node *son;
Node *brother;
};
Node *top=NULL;
Info *RetInfo = NULL, *DelInfo = NULL;
// Добавление элемента в дерево
Node *Add(Node *&top, int Key, int AddKey)
{
top = new Node;
top -> data.Key = Key;
top -> brother = NULL;
Node *p1 = top->son, *p2 =top->brother;
if(p1==NULL)
{
p1=p2;
p2=NULL;
}
top->son=Add(p1, Key, AddKey);
if(p1!=NULL)
top->son->brother=Add(p2, Key, AddKey);
return top;
}
//Вывод дерева на экран
void Display(Node *top, int lvl)
{
lvl++;
if(top==NULL) return;
if(top!=NULL)
{
Display(top -> son,lvl);
for(int i=1;i<=lvl*2;i++) cout << " ";
cout<<top->data.Key<<endl;
//top=top->son;
Display(top -> brother,lvl);
}
}
// Поиск элемента в дереве
Info *Search(Node *&top, int SearchKey)
{
if(top == NULL) return RetInfo;
if(top -> data.Key == SearchKey) RetInfo = &(top -> data);
else RetInfo = Search((top -> son), SearchKey);
return RetInfo;
}
// Главная функция
void main()
{
setlocale(LC_ALL,"Russian");
int otv, SearchKey, lvl, Key, AddKey;
do
{
cout << "Возможные действия:" << endl
<< "1. Добавить узел в дерево" << endl
<< "2. Вывести дерево на экран" << endl
<< "3. Поиск узла в дереве" << endl
<< "0. Выход" << endl
<< "Ваш выбор:";
cin >> otv;
cout << endl;
system("cls");
switch(otv)
{
case 1:
cout << "Добавить узел:";
cin >> Key;
cin >> AddKey;
cout << endl;
Add(top, Key, AddKey);
cout << "Узел добавлен." << endl << endl;
break;
case 2:
if(top==NULL) cout << " Дерево пустое." << endl;
else
{
cout << "Содержимое дерева: " << endl;
lvl=0;
Display(top,lvl);
}
cout << endl << endl;
break;
case 3:
if(top==NULL) cout << " Дерево пустое." << endl;
else
{
cout << "Найти узел:";
cin >> SearchKey;
cout << endl;
RetInfo = Search(top, SearchKey);
if(RetInfo == NULL)
cout << "Элемент не найден." << endl;
else
cout << "Элемент найден." << top -> data.Key << endl;
RetInfo=NULL;
}
break;
case 0:
otv = 0;
break;
default:
cout << "Недопустимое значение" << endl << endl;
break;
}
}
while(otv != 0);
} |
|
Добавлено через 1 час 13 минут
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
| #include "stdafx.h"
#include <conio.h>
#include <iostream>
using namespace std;
struct Node;
struct List{
List *next;//след. ссылка
Node *node;//ссылка
};
struct Node{
int s; //cимвол
List *list;//ссылка на список
};
List *MakeSomeList();
Node *Add(int s, Node *root, int &f);
List *FindEmptyPointer(Node *root, List *&Empty);//поиск "пустого" указателя
void Search(Node *&root,int s, int i);//поиск символа в дереве
void Search(Node *&root,int s, int i)
{
if(root -> s == s)
{
i ++;
cout<<i;
}
if(root -> list -> node != NULL)
return Search(root -> list -> node, s, i);
if(root -> list -> next != NULL)
return Search(root -> list -> next -> node, s, i);
}
void Display(Node *root, int lvl)
{
lvl++;
if(root==NULL) return;
if(root!=NULL)
{
Display(root -> list -> next -> node,lvl);
for(int i=1;i<=lvl*2;i++)
cout << " ";
cout<< root -> s <<endl;
}
Display(root -> list -> node,lvl);
}
List *MakeSomeList()
{
List *p = new List;
List *ptr = p;
p -> node = NULL;
for(int i=rand()%2+2;i>0;i--)// создание ссылок наследования узла 2-4
{
p -> next = new List;
p -> next -> node = NULL;//инициализируем НУЛЕМ указатели на узлы
p = p-> next;
}
p -> next = NULL;
p -> node = NULL;
return ptr;
}
Node *Add(int s, Node *&root,List *Empty)
{
if(root==NULL)//если пустое дерево
{
root=new Node;
root -> s = s;
root -> list = MakeSomeList();
return root;
}
List *p = FindEmptyPointer(root,Empty);//находим НУЛЕВУЮ ссылку в дереве,
Empty = NULL;
p -> node = new Node;// по ней создаем узел
p -> node -> s = s;
p -> node -> list = MakeSomeList();//и этому узлу приписываем список указателей
}
List *FindEmptyPointer(Node *root, List *&Empty)//поиск "пустого" указателя
{
if(Empty != NULL)
return Empty;
if(root -> list -> node == NULL)
{
Empty = root -> list;
return Empty;
}
if(root -> list -> node != NULL)
return FindEmptyPointer(root -> list -> node, Empty);
if(Empty != NULL)
return Empty;
if(root -> list -> next != NULL)
return FindEmptyPointer(root -> list -> next -> node, Empty);
}
void main()
{
setlocale(LC_ALL,"Russian");
int s;
int sw;
int i=0, lvl=0;
Node *root=NULL;
List *Empty=NULL;
do {
cout << "Возможные действия:" << endl
<< "1. Добавить узел в дерево" << endl
<< "2. Вывести дерево на экран" << endl
<< "3. Поиск узла в дереве" << endl
<< "0. Выход" << endl
<< "Ваш выбор:";
cin>>sw;
cout << endl;
system("cls");
switch(sw)
{
case 1:
{
cout<<"\n\nВведите элемент>>";
cin>>s;
if(root==NULL) root=Add(s,root,Empty);
else Add(s,root,Empty);
break;
}
case 2:
{
if(root==NULL) break;
Display(root,lvl);
break;
}
//case '3': DelSomeNode(root);break;
case 3:
{
cout<<"Введите символ для поиска>>";
cin>>s;i=0;
Search(root,s,i);
break;
}
}
}while(sw!=0);
} |
|
а если так?
0
|