bạn xem chương trình này giúp mình với, hàm main giờ viết sao?
#include<iostream>
using namespace std;
class Tree {
private:
struct node {
int key;
float data;
node* left;
node* right;
};
node* root;
node* makeEmpty(node* t);
node* insertprivate(int x, float y, node* t);
node* findMin(node* t);
node* removeprivate(int x, float y, node* t);
void inorderprivate(node* t);
node* findprivate(node* t, int x);
int countprivate(node* t);
node* findNodeWithKey(int index, node* t);
public:
Tree();
~Tree();
void insert(int x, float y);
void remove(int x, float y);
void inorder();
void find(int x);
int count();
float& operator[](int index);
};
Tree::Tree() {
root = NULL;
}
Tree::~Tree() {
root = makeEmpty(root);
}
Tree::node* Tree::makeEmpty(node* t) {
if (t == NULL)
return NULL;
{
makeEmpty(t->left);
makeEmpty(t->right);
delete t;
}
return NULL;
}
void Tree::insert(int x, float y) {
root = insertprivate(x, y, root);
}
Tree::node* Tree::insertprivate(int x, float y, node* t)
{
if (t == NULL)
{
t = new node;
t->key = x;
t->data = y;
t->left = t->right = NULL;
}
else if (x < t->key)
t->left = insertprivate(x, y, t->left);
else if (x > t->data)
t->right = insertprivate(x, y, t->right);
return t;
}
void Tree::remove(int x, float y) {
root = removeprivate(x, y, root);
}
Tree::node* Tree::removeprivate(int x, float y, node* t) {
node* temp;
if (t == NULL)
return NULL;
else if (x < t->key)
t->left = removeprivate(x, y, t->left);
else if (x > t->key)
t->right = removeprivate(x, y, t->right);
else if (t->left && t->right)
{
temp = findMin(t->right);
t->key = temp->key;
t->right = removeprivate(t->key, t->data, t->right);
}
else
{
temp = t;
if (t->left == NULL)
t = t->right;
else if (t->right == NULL)
t = t->left;
delete temp;
}
return t;
}
void Tree::inorder() {
inorderprivate(root);
cout << endl;
}
void Tree::inorderprivate(node* t) {
if (t == NULL)
return;
inorderprivate(t->left);
cout << t->key << "( " << t->data << " )\t";
inorderprivate(t->right);
}
void Tree::find(int x) {
root = findprivate(root, x);
}
Tree::node* Tree::findprivate(node* t, int x) {
if (t == NULL)
return NULL;
else if (x < t->data)
return findprivate(t->left, x);
else if (x > t->data)
return findprivate(t->right, x);
else
return t;
}
Tree::node* Tree::findMin(node* t)
{
if (t == NULL)
return NULL;
else if (t->left == NULL)
return t;
else
return findMin(t->left);
}
int Tree::countprivate(node* t)
{
if (t == NULL)
return 0;
else
return (countprivate(t->right) + countprivate(t->left)) + 1;
}
int Tree::count()
{
cout << "Count = " << countprivate(root);
cout << endl;
return 0;
}
float& Tree::operator[](int index) {
node* t = findNodeWithKey(index, root);
if (t) return t->data;
}
Tree::node* Tree::findNodeWithKey(int index, node* t) {
if (!t || t->key == index) return t;
return findNodeWithKey(index, index < t->key ? t->left : t->right);
}
int main() {
Tree* bst = new Tree();
bst->insert(20, 0.1);
bst->insert(25, 0.2);
bst->insert(10, 0.5);
bst->inorder();
bst->count();
bst->remove(20, 0.1);
bst->inorder();
bst->count();
//Tree A;
//cout << "A[key] = " << ;
return 0;
}