0% found this document useful (0 votes)
14 views5 pages

Lab 4 Dap An

Uploaded by

chautruong232
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views5 pages

Lab 4 Dap An

Uploaded by

chautruong232
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Bài 1:

#include<iostream>
using namespace std;

//khia bao cau truc cay NPTK


struct Node
{
int info;
Node* left;
Node* right;
};
Node* root;
//Khoi tao cay rong
void init()
{
root = NULL;
}
//them phan tu vao cay
void InsertNode(Node*& p, int x)
{
if (p == NULL)
{
p = new Node;
p->info = x;
p->left = NULL;
p->right = NULL;
}
else
{
if (p->info == x)
return;
else if (p->info > x)
return InsertNode(p->left, x);
else
return InsertNode(p->right, x);
}
}
int sumNode(Node* p)
{
if (p == NULL)
return 0;
else
return p->info + sumNode(p->left) + sumNode(p->right);
}
int findMax(Node* p)
{
if (p == NULL)
return INT_MIN;
Node* current = p;
while (current->right != NULL)
current = current->right;
return current->info;
}
int findMin(Node* p)
{
if (p == NULL)
return INT_MAX;
Node* current = p;
while (current->left != NULL)
current = current->left;
return current->info;
}
int countNode(Node* p)
{
if (p == NULL)
return 0;
return 1 + countNode(p->left) + countNode(p->right);
}
int main()
{
init();
int chon = -1;
int x = 0;
cout << "\n1) Them 1 phantu vao cay"
<< "\n2) Tinh tong cac phantu trong cay"
<< "\n3) GTLN trong cay"
<< "\n4) GTNN trong cay"
<< "\n5) So luong cac nut trong cay"
<< "\n--------------------------" << endl;
do
{
cout << "\nNhap lua chon ban muon: "; cin >> chon;
switch (chon)
{
case 1:
cout << "Nhap phantu muon them: "; cin >> x;
InsertNode(root, x);
break;
case 2:
cout << "Tong cac phantu la: " << sumNode(root) << endl;
break;
case 3:
cout << "GTLN la: " << findMax(root) << endl;
break;
case 4:
cout << "GTNN la: " << findMin(root) << endl;
break;
case 5:
cout << "Tong so nut tren cay la: " << countNode(root) << endl;
}
} while (chon > 0 && chon < 6);
system("pause");
return 0;
}

BT2:

Input:
5
/ \
3 7
/ \ / \
2 4 6 8
Output: 1
Sự khác biệt giữa tất cả các nút liên tiếp nếu được sắp xếp là 1.
Do đó, câu trả lời là 1.

Input:
1
\
6
Output: 5

#include<iostream>
#include<vector>
using namespace std;

//Create Node
struct Node {
int val;
Node* left;
Node* right;
};

Node* createNode(int val)


{
Node* node = new Node;
node->val = val;
node->left = NULL;
node->right = NULL;
return node;
}

//Duyet node trong BST


void preorder(Node* root, vector<int>&v) {
if (root == NULL)
return;

//Them gia tri vao cuoi vector


v.push_back(root->val);

preorder(root->left, v);
preorder(root->right, v);
}

int main() {

Node* root = createNode(5);


root->left = createNode(3);
root->right = createNode(7);
root->left->left = createNode(2);
root->left->right = createNode(4);
root->right->left = createNode(6);
root->right->right = createNode(8);

vector<int>v;
//call for preorder.
preorder(root, v);

int min_di = INT_MAX;


int n = v.size();

//Duyet vector va kiem tra gia tri


for (int i = 0; i<n; i++) {
for (int j = i + 1; j<n; j++) {
int x = abs(v[i] - v[j]);
if (min_di > x) {
min_di = x;
}
}
}
//In ket qua
cout << "the minimum di erence is : " << min_di << endl;
system("pause");
return 0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy