#includeusing namespace std;
struct node {//cu 1 con tro tro toi 1 node va node pleft va node pright la nhanh
int data;
node* pleft;
node* pright;
};
node* getNode(int data)
{
node* p = new node;//no phai tao 1 con tro roi cap phat cho no 1 cai node de co no quan ly
p->pleft = p->pright = NULL;//tao vung nho cho no quan ly
p->data = data;//tao vung nho cho no quan ly
return p;
}
void insert(node* &root, int data)//co nghia khi no bang NULL no chua tro di dau ca ma neu truyen tham
//tri vi con neu no da tro di chung ta co quyen truyen tham tri boi vi no thay doi cung chi thay doi tren
//dia chi duoc tro
{
node* p = getNode(data);
if (root == NULL)
{
root = p;
}
else if (root->data < data)
{
insert(root->pright, data);
}
else if (root->data > data)
{
insert(root->pleft, data);
}
}
void print(node* root)
{
if (root!= NULL)
{
print(root->pleft);
cout << root->data << " ";//co nghia no in tung node chu khong phai in trong pleft thoi cu di duoc nhanh ben do
//la no in ra duoc 1 node
print(root->pright);//no di sang ben phai de no kiem tra xem ben phai co node ma di sang duoc ben trai hay ko
}
}
void main()
{
node* root = NULL;
insert(root, 3);
insert(root, 5);
insert(root, 2);
print(root);
}


đối với việc xóa 1 phần tử từ cây nhị phân chúng ta 
xét các trường hợp
đầu tiên ta phải đi tìm được phần tử cần xóa bằng đệ quy
-cây không có node: ko xóa
-xóa node có 1 con:xóa node lấy node con lên thay dù nó có 1 node con hay không node con cũng như nhau
-cây có 2 node: thay node root bằng node con nhỏ nhất của node phải của nó
node* minValueNode(struct node* node)
{
struct node* current = node;

/* loop down to find the leftmost leaf */
while (current && current->pleft != NULL)
current = current->pleft;

return current;
}

/* Given a binary search tree and a key, this function deletes the key
and returns the new root */
node* deleteNode(node* root, int key)
{
// base case
if (root == NULL) return root;
// If the key to be deleted is smaller than the root's key,
// then it lies in left subtree
if (key < root->data)
root->pleft = deleteNode(root->pleft, key);

// If the key to be deleted is greater than the root's key,
// then it lies in right subtree
else if (key > root->data)
root->pright = deleteNode(root->pright, key);

// if key is same as root's key, then This is the node
// to be deleted
else//tim duoc cai key =root->data roi
{
// node with only one child or no child
if (root->pleft == NULL)//neu node do co node ben trai la NULL hoac ca 2 node la NULL
{
node* temp = root->pright;
free(root);
return temp;
}
else if (root->pright == NULL)//node ben phai la NULL
{
struct node* temp = root->pleft;
free(root);
return temp;
}

// node with two children: Get the inorder successor (smallest
// in the right subtree)
node* temp = minValueNode(root->pright);

// Copy the inorder successor's content to this node
root->data = temp->data;

// Delete the inorder successor
root->pright = deleteNode(root->pright, temp->data);
}
return root;
}
chúng ta không thể xóa node trực tiếp được vì như thế sẽ mất hết dữ liệu nên ta phải tìm cái nút con bên trái nhất của node con bên phải để thế mạng khi xóa