Есть код:
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.
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <iostream>
#include <cstdlib>
#include <vector.h>
#include <math.h>
using namespace std;
struct TNode
{
int Key;
int index;
int Count;
TNode *Left;
TNode *Right;
};
class TArbre
{
private:
TNode *Tree;
void Search (int,TNode**);
public:
vector<int> index;
TArbre() {Tree=NULL;}
TNode** Rendre_la_racine () {return &Tree;}
void BuildTree ();
void CleanTree (TNode **);
void Gauche_detour (TNode **);
void Imprimante (TNode **,int);
int Altitude (TNode **);
void Reindex (TNode **, vector<int> &);
void iGauche_detour (TNode **, vector<int> &);
void icGauche_detour (TNode **, vector<int> &);
bool IsInIndex (const int &, vector<int> &);
};
int main ()
{
TArbre A;
A.index.reserve( 10 );
A.BuildTree ();
cout<<"\nВывод дерева:\n";
A.Imprimante (A.Rendre_la_racine(), 0 );
cout<<"\nВысота дерева:"<<A.Altitude(A.Rendre_la_racine())<<endl;
cout<<"\nЛевосторонний обход дерева: ";
A.Gauche_detour (A.Rendre_la_racine());
A.Reindex (A.Rendre_la_racine(), A.index);
bool cont=true;
while (cont==true)
{
cout<<"\nВведите искомый элемент: ";
int z;
cin>>z;
if (A.IsInIndex (z, A.index)==true)
{
cout<<"\nТакой элемент в дереве есть.";
}
else
{
cout<<"\nТакого элемента в дереве нет.";
}
cout<<"\nХотите попробовать найти другой элемент? (1/0)";
int b;
cin>>b;
if (b== 0 ) cont=false;
}
A.CleanTree (A.Rendre_la_racine());
return 0 ;
}
void TArbre::BuildTree ()
{
int el;
cout<<"Вводите количество ключей вершин дерева: ";
int cnt;
cin>>cnt;
for(int i= 0 ;i<cnt;i++)
{cout<<"Введите "<<i+ 1 <<"-ю вершину дерева: "; cin>>el; Search (el,&Tree);}
}
void TArbre::Search (int x,TNode **p)
{
if (*p==NULL)
{// Вершины в дереве нет; включить ее.
*p = new(TNode);
(**p).Key = x; (**p).Count = 1 ;
(**p).Left = NULL; (**p).Right = NULL; }
else
if (x<(**p).Key) Search (x,&((**p).Left));
else
if (x>(**p).Key) Search (x,&((**p).Right));
else (**p).Count = (**p).Count + 1 ;
}
void TArbre::Gauche_detour (TNode **w)
{
if (*w!=NULL)
{
cout<<(**w).Key<<" ";
Gauche_detour (&((**w).Left));
Gauche_detour (&((**w).Right));
}
}
void TArbre::CleanTree (TNode **w)
{
if (*w!=NULL)
{ CleanTree (&((**w).Left));
CleanTree (&((**w).Right));
delete *w; }
}
void TArbre::Imprimante (TNode **w,int l)
{
int i;
if (*w!=NULL)
{ Imprimante (&((**w).Right),l+ 1 );
for (i= 1 ; i<=l; i++) cout<<" ";
cout<<(**w).Key<<endl;
Imprimante (&((**w).Left),l+ 1 ); }
}
int TArbre::Altitude (TNode **w)
{
int h1,h2;
if (*w==NULL) return (- 1 );
else
{
h1 = Altitude (&((**w).Left));
h2 = Altitude (&((**w).Right));
if (h1>h2) return ( 1 + h1);
else return ( 1 + h2);
}
}
void TArbre::iGauche_detour (TNode **w, vector<int> &index)
{
if (*w!=NULL)
{
index.resize(index.size()+ 1 );
index.at(index.size()- 1 )=(**w).Key;
iGauche_detour (&((**w).Left), index);
iGauche_detour (&((**w).Right), index);
}
}
void TArbre::icGauche_detour (TNode **w, vector<int> &index)
{
if (*w!=NULL)
{
for (int i= 0 ;i<index.size();i++)
{
if (index.at(i)==(**w).Key)
{
(**w).index=index.at(i);
}
}
icGauche_detour (&((**w).Left), index);
icGauche_detour (&((**w).Right), index);
}
}
void TArbre::Reindex (TNode **w, vector<int> &index)
{
index.clear();
iGauche_detour (w, index);
for(int j= 0 ;j<index.size();j++)
{
for(int i= 0 ;i<index.size()-j;i++)
{
if (index.at(i)>index.at(i+ 1 ))
{
int buf=index.at(i);
index.at(i)=index.at(i+ 1 );
index.at(i+ 1 )=buf;
}
}
}
for (int i= 0 ;i<index.size();i++)
{
index.at(i)=i;
}
icGauche_detour (w, index);
}
bool TArbre::IsInIndex (const int &x, vector<int> &index)
{
int a= 0 ;
int b=(index.size()- 1 );
int c=- 1 ;
bool rst=false;
while ((a!=b)&&(rst==false))
{
c=trunc((b-a)/ 2 );
if (index.at(c)==x) rst=true;
if (index.at(c)<x) b=trunc(b/ 2 );
if (index.at(c)>x)
{
if (b<=(c+ 1 ))
{
a=c+ 1 ;
}
}
}
if (index.at(a)==x) rst=true;
return rst;
}
В результате работы программа выдаёт:
Вводите количество ключей вершин дерева: 5
Введите 1-ю вершину дерева: 1
Введите 2-ю вершину дерева: 4
Введите 3-ю вершину дерева: 6
Введите 4-ю вершину дерева: 2
Введите 5-ю вершину дерева: 6
Вывод дерева:
6
4
2
1
Высота дерева:2
terminate called after throwing an instance of 'std::out_of_range'
what(): vector::_M_range_check
Левосторонний обход дерева: 1 4 2 6 /bin/sh: line 1: 5560 Аварийный останов /home/dumanovsky/Pascal_Labs/Lab4_C/Lab4_2/Lab4/debug/./src/lab4
Нажмите Enter для завершения!
Может кто знает, что не так? Заранее спасибо.