3-Amaliy ish

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

1-Topshiriq

22. Daraxtning balandligini aniqlash

 Masala: Daraxt balandligini (eng uzoq yo‘l) aniqlaydigan algoritm yozing.


 Kiritish: [10, 5, 15, 3]
 Chiqish: 3

#include <iostream>

#include <algorithm>

using namespace std;

struct Node {

int data;

Node* left;

Node* right;

Node(int value) {

data = value;

left = right = nullptr;

};

int treeHeight(Node* root) {

if (root == nullptr) {

return 0;

int leftHeight = treeHeight(root->left);

int rightHeight = treeHeight(root->right);

return max(leftHeight, rightHeight) + 1;

Node* insert(Node* root, int value) {


if (root == nullptr) {

return new Node(value);

if (value < root->data) {

root->left = insert(root->left, value);

} else {

root->right = insert(root->right, value);

return root;

int main() {

cout<<"Yuldashbayeva Mehriniso ";

int arr[] = {10, 5, 15, 3};

int n = sizeof(arr) / sizeof(arr[0]);

Node* root = nullptr;

for (int i = 0; i < n; i++) {

root = insert(root, arr[i]);

int height = treeHeight(root);

cout << "Daraxtning balandligi: " << height << endl;

return 0;

}
2 – topshiriq

Har bir talaba oʻziga tegishli variantni tanlab uni graf koʻrinishiga oʻtirishi kerak,
hosil boʻlgan grafda oʻzaro eng uzoqda joylashgan tugunlar orasida eng qisqa
yoʻlni Dijkstra usullarida topishi va natijani chiqarishi kerak .

Variant22
054078
500000
400995
009010
709107
805070

#include <iostream>

#include <vector>

#include <climits>

using namespace std;

int dijkstra(int n, const vector<vector<int>>& graph, int src, int dest) {

vector<int> dist(n, INT_MAX);

dist[src] = 0;

vector<bool> visited(n, false);


for (int i = 0; i < n - 1; i++) {

int minDist = INT_MAX, u = -1;

for (int j = 0; j < n; j++) {

if (!visited[j] && dist[j] < minDist) {

minDist = dist[j];

u = j;

visited[u] = true;

for (int v = 0; v < n; v++) {

if (!visited[v] && graph[u][v] != 0 && dist[u] != INT_MAX && dist[u] + graph[u][v] < dist[v]) {

dist[v] = dist[u] + graph[u][v];

return dist[dest];

int main() {

cout<<"Yuldashbayeva Mehriniso ";

vector<vector<int>> graph = {

{0, 5, 4, 0, 7, 8},

{5, 0, 0, 0, 0, 0},

{4, 0, 0, 9, 9, 5},

{0, 0, 9, 0, 1, 0},
{7, 0, 9, 1, 0, 7},

{8, 0, 5, 0, 7, 0}

};

int n = graph.size();

int minDistance = INT_MAX;

int node1 = -1, node2 = -1;

for (int i = 0; i < n; i++) {

for (int j = i + 1; j < n; j++) {

int dist = dijkstra(n, graph, i, j);

if (dist < minDistance) {

minDistance = dist;

node1 = i;

node2 = j;

cout << "Eng uzoq tugunlar: " << node1 << " va " << node2 << endl;

cout << "Eng qisqa yo'l masofasi: " << minDistance << endl;

return 0;

You might also like