|
bolaga_qoshish(daraxt->bolalar[0], 4)
|
bet | 2/2 | Sana | 15.12.2023 | Hajmi | 383,67 Kb. | | #119458 |
Bog'liq Amirov Temurbek MTA4 О себе эссе, Sug`urta sohasiga oid iqtisodiy matn, Shuhratjon Xoliyorov, [, Mengliyev muhiddin, MAHKAMOV, Дони, 3762301297, 1695214521, xojimurod, БСАТ якуний, Boshlang’ich ta’limda aktdan foydalanish fanidan maruzlar matni-fayllar.org, 1 Шароб фан дастури Наргиза вариант, 16 Market Kaf 04.MARKETING TADQIQOTLARI TDIU ARM, Metod Gendernaya psixologiya i psixologiya seksual\'nosti Klinicheskaya psixologiya po specializaci Patopsixologicheskaya diagnostika i psixoterapiya-012bolaga_qoshish(daraxt->bolalar[0], 4);
bolaga_qoshish(daraxt->bolalar[0], 5);
cout << "Daraxt yaratildi. Boshlangich tugun qiymati: " << daraxt->qiymat << endl;
daraxt_chapini_chiqarish(daraxt);
delete daraxt;
return 0;
}
RESULT :
MAVZU : 11 - Binar daraxtlar bilan ishlash algoritmlari
CODE:
#include
#include
using namespace std;
class BinarTugun {
public:
int qiymat;
BinarTugun* chap;
BinarTugun* ong;
BinarTugun(int qiymat) : qiymat(qiymat), chap(nullptr), ong(nullptr) {}
};
BinarTugun* binar_daraxt_chapini_yarat(int qiymat) {
return new BinarTugun(qiymat);
}
void tugun_qoshish(BinarTugun* &tugun, int qiymat) {
if (tugun == nullptr) {
tugun = binar_daraxt_chapini_yarat(qiymat);
} else if (qiymat < tugun->qiymat) {
tugun_qoshish(tugun->chap, qiymat);
} else {
tugun_qoshish(tugun->ong, qiymat);
}
}
void tola_chiqarish(BinarTugun* tugun) {
if (tugun != nullptr) {
tola_chiqarish(tugun->chap);
cout << tugun->qiymat << " ";
tola_chiqarish(tugun->ong);
}
}
int main() {
BinarTugun* binar_daraxt = nullptr;
int qiymatlar[] = {15, 8, 25, 3, 12, 20, 30};
for (int qiymat : qiymatlar) {
tugun_qoshish(binar_daraxt, qiymat);
}
cout << "Binar daraxt to'la chiqarilgan holda: ";
tola_chiqarish(binar_daraxt);
cout << endl;
delete binar_daraxt;
return 0;
}
RESULT:
12-mavzu: Heap tree ko‘rinishidagi binar daraxtlar bilan ishlash algoritmlari
Code :
#include
#include
using namespace std;
class HeapTugun {
public:
int qiymat;
HeapTugun(int qiymat) : qiymat(qiymat) {}
};
int dastlabki_indeks(int indeks) {
return (indeks - 1) / 2;
}
int ong_bolasini_indeksi(int indeks) {
return 2 * indeks + 2;
}
int chap_bolasini_indeksi(int indeks) {
return 2 * indeks + 1;
}
void element_qoshish(vector &heap, int qiymat) {
HeapTugun* tugun = new HeapTugun(qiymat);
heap.push_back(tugun);
int indeks = heap.size() - 1;
while (indeks != 0 && heap[dastlabki_indeks(indeks)]->qiymat < heap[indeks]->qiymat) {
swap(heap[indeks], heap[dastlabki_indeks(indeks)]);
indeks = dastlabki_indeks(indeks);
}
}
HeapTugun* eng_katta_elementni_olish(vector &heap) {
if (heap.empty()) {
return nullptr;
}
HeapTugun* eng_katta = heap[0];
heap[0] = heap.back();
heap.pop_back();
int indeks = 0;
while (true) {
int chap_indeksi = chap_bolasini_indeksi(indeks);
int ong_indeksi = ong_bolasini_indeksi(indeks);
int oz_indeksi = indeks;
if (chap_indeksi < heap.size() && heap[chap_indeksi]->qiymat > heap[oz_indeksi]->qiymat) {
oz_indeksi = chap_indeksi;
}
if (ong_indeksi < heap.size() && heap[ong_indeksi]->qiymat > heap[oz_indeksi]->qiymat) {
oz_indeksi = ong_indeksi;
}
if (oz_indeksi == indeks) {
break;
}
swap(heap[indeks], heap[oz_indeksi]);
indeks = oz_indeksi;
}
return eng_katta;
}
void ekranga_chiqarish(const vector &heap) {
for (const auto &tugun : heap) {
cout << tugun->qiymat << " ";
}
cout << endl;
}
int main() {
vector heap;
element_qoshish(heap, 4);
element_qoshish(heap, 10);
element_qoshish(heap, 7);
element_qoshish(heap, 1);
element_qoshish(heap, 3);
cout << "Boshlang'ich Heap tree: ";
ekranga_chiqarish(heap);
HeapTugun* eng_katta = eng_katta_elementni_olish(heap);
cout << "Eng katta element: " << eng_katta->qiymat << endl;
cout << "Qolgan Heap tree: ";
ekranga_chiqarish(heap);
for (HeapTugun* tugun : heap) {
delete tugun;
}
return 0;
}
RESULT :
|
| |