Cây nhị phân tìm kiếm có phần tử trùng nhau đc không !
co thi duyet cay nhu the nao?
cho mh cây ví dụ (neu co), ai biết giúp vơi`:dapdau2:
co thi duyet cay nhu the nao?
cho mh cây ví dụ (neu co), ai biết giúp vơi`:dapdau2:
3
0 7
0 3 8
1 7
int chennut(tree &root, int k)
{
if(root!=NULL)
{
if(root->info==k) return 0;
if(root->info>k) return chennut(root->left,k);
else return chennut(root->right,k);
}
else
{
root =new node;
if(root==NULL) return -1;
root->info=k;
root->left=root->right=NULL;
return 1;
}
}
Bạn cần 1 struct kiểu thế này:cho minh hoi, tao 1 cay nhi phan tim kiem kieu phan so. lam nhu the nao ban![]()
struct Fraction {
int num, den;
Fraction(int n, int d) : num(n), den(d) {}
bool operator<(const Fraction &f) const {
return num * f.den < den * f.num;
}
};
struct node
{
int info;
struct node *left, *right;
};
typedef node *tree;
int chennut(tree &root, int k)
{
if(root!=NULL)
{
if(root->info==k) return 0;
if(root->info>k) return chennut(root->left,k);
else return chennut(root->right,k);
}
else
{
root =new node;
if(root==NULL) return -1;
root->info=k;
root->left=root->right=NULL;
return 1;
}
}
int themphantu(tree &root, int x)
{
if(root!=NULL)
{
if(root->info>x)
return themphantu(root->left,x);
else
return themphantu(root->right,x);
}
root =new node;
if(root==NULL) return -1;
root->info=x;
root->left=root->right=NULL;
return 1;
}
void taocay(tree &root)
{
int k,n;
cout<<"\nNhap vao n=";cin>>n;
root=NULL;
for(int i=1;i<=n;i++)
{
cin>>k;
chennut(root,k);
}
}
struct Node {
[B][COLOR=#ff0000]int val;[/COLOR][/B]
Node * left, * right;
... // constructor nếu muốn thêm vào.
}
root->info < k
struct Fraction {
int num, den;
Fraction(int n, int d) : num(n), den(d) {}
bool operator<(const Fraction &f) const {
return num * f.den < den * f.num;
}
// Lưu ý: bạn cần đảm bảo fraction đc đơn giản thì function này mới đúng nhé.
bool operator==(const Fraction &f) const {
return num == f.num && den == f.den;
}
};
// Node của binary tree đây. Mình chỉ đổi mỗi dòng dữ liệu.
struct Node {
[B]Fraction info;[/B]
Node * left, * right;
};
int chennut(Node &tree, [B]Fraction k[/B]) {
...
}
int themphantu(Node &tree, [B]Fraction x[/B]) {
...
}
void taocay(tree &root) {
int [B]kx, ky[/B], n;
cout<<"\nNhap vao n=";cin>>n;
root=NULL;
for(int i=1;i<=n;i++)
{
[B] cin >> kx >> ky;
Fraction k(kx, ky); [/B]// chỗ này cần 1 hàm đơn giản phân số nữa. Có thể cho nó trong constructor của struct hoặc viết riêng.
chennut(root, k);
}
}
#include <iostream.h>
#include <math.h>
struct Fraction {
int num, den;
Fraction(int n, int d) : num(n), den(d) {}
bool operator<const Fraction &f) const
{
return num * f.den < den * f.num;
}
bool operator==(const Fraction &f) const {
return num == f.num && den == f.den;
}
};
struct Node {
Fraction info;
Node * left, * right;
};
int chennut(Node &root, Fraction k) {
if(root!=NULL)
{
if(root->info==k) return 0;
if(root->info>k) return chennut(root->left,k);
else return chennut(root->right,k);
}
else
{
root =new node;
if(root==NULL) return -1;
root->info=k;
root->left=root->right=NULL;
return 1;
}
}
int themphantu(Node &root, Fraction x) {
if(root!=NULL)
{
if(root->info>x)
return themphantu(root->left,x);
else
return themphantu(root->right,x);
}
root =new node;
if(root==NULL) return -1;
root->info=x;
root->left=root->right=NULL;
return 1;
}
void taocay(Node &root)
{
int kx, ky, n;
cout<<"\nNhap vao n=";cin>>n;
root=NULL;
for(int i=1;i<=n;i++)
{
cin >> kx >> ky;
Fraction k(kx, ky);
chennut(root, k);
}
}
int main()
{
int x,y;
Node root;
taocay(root);
cout<<endl;
return 0;
}
#include <iostream>
struct Fraction {
int num, den;
Fraction(int n, int d) : num(n), den(d) {}
bool operator<(const Fraction &f) const {
return num * f.den < den * f.num;
}
bool operator==(const Fraction &f) const {
return num == f.num && den == f.den;
}
};
struct Node {
Fraction info;
Node * left, *right;
Node(Fraction f) : info(f), left(0), right(0) {}
};
int chennut(Node * &root, Fraction k) {
if (root != NULL)
{
if (root->info == k) return 0;
if (k < root->info) return chennut(root->left, k);
else return chennut(root->right, k);
}
else
{
root = new Node(k);
if (root == NULL) return -1;
return 1;
}
}
int themphantu(Node * &root, Fraction x) {
if (root != NULL)
{
if (x < root->info)
return themphantu(root->left, x);
else
return themphantu(root->right, x);
}
root = new Node(x);
if (root == NULL) return -1;
return 1;
}
void taocay(Node * &root)
{
int kx, ky, n;
std::cout << "\nNhap vao n="; std::cin >> n;
for (int i = 1; i <= n; i++)
{
std::cin >> kx >> ky;
Fraction k(kx, ky);
chennut(root, k);
}
}
int main() {
Node * root = 0;
taocay(root);
std::cout << root->info.num << " " << root->info.den << std::endl;
// In thử 2 node trái phải của root.
if (root->left)
std::cout << root->left->info.num << " " << root->left->info.den << std::endl;
if (root->right)
std::cout << root->right->info.num << " " << root->right->info.den << std::endl;
return 0;
}
Fraction tong(Node *root)
{
if(root!=NULL)
return root->info+tong(root->left)+tong(root->right);
}
Bạn chỉ cần viết 1 hàm tính tổng phân số ở trong struct Fraction là đc. Mình viết định nghĩa cho dấu + phân số luôn.nhờ bạn giúp mình sửa code này như thế nào để tính tổng các phân số trên cây; đa tạ thánh.. vấn đề mình hiểu là cộng từ gốc +các phần tử nhánh trái và phải. nhưng phân số thì mh không rành .. hixMã:Fraction tong(Node *root) { if(root!=NULL) return root->info+tong(root->left)+tong(root->right); }
Fraction operator+(const Fraction &f) const {
return Fraction(num * f.den + den * f.num, den * f.den);
}