hehe tớ sẽ gửi lên
lúc không có null mà tớ nhập phần tử mới là bị close program liền 
Code:
#include<iostream.h>
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
typedef struct tagTNODE
{
int Key;
struct tagTNODE *pLeft,*pRight;
}TNODE;
typedef TNODE *PTRTREE;
PTRTREE TREE;
TNODE *Newnode (int x)
{
TNODE *p;
p=new tagTNODE;
if (p==NULL)
{
cout<<"Khong du bo nho"; return NULL;
}
p->Key=x;
p->pRight=NULL;
p->pLeft=NULL;
return p;
}
void InitList(PTRTREE TREE)
{
TREE=NULL;
}
void Insert_node(PTRTREE &TREE,int x)
{
if (TREE == NULL)
{
TREE=Newnode(x);
TREE->Key = x;
TREE->pLeft=TREE->pRight= NULL;
}
else if (x == TREE->Key)
{
cout<<"gia tri bi trung";
}
else if (x < TREE->Key)
Insert_node(TREE->pLeft,x);
else Insert_node(TREE->pRight,x);
}
//Duyet cay NLR
void NLR(PTRTREE TREE)
{
if (TREE!=NULL)
{
cout<<TREE->Key<<"\t";
NLR(TREE->pLeft);
NLR(TREE->pRight);
}
}
//Duyet cay LRN
void LRN(PTRTREE TREE)
{
if (TREE!=NULL)
{
LRN(TREE->pLeft);
LRN(TREE->pRight);
cout<<TREE->Key<<"\t";
}
}
//Duyet cay RNL
void RNL(PTRTREE TREE)
{
if (TREE!=NULL)
{
RNL(TREE->pRight);
cout<<TREE->Key<<"\t";
RNL(TREE->pLeft);
}
}
//Duyet cay LNR
void LNR(PTRTREE TREE)
{
if (TREE!=NULL)
{
LNR(TREE->pLeft);
cout<<TREE->Key<<"\t";
LNR(TREE->pRight);
}
}
//Search
PTRTREE Search(PTRTREE TREE, int x)
{
PTRTREE p;
p = TREE;
while (p !=NULL)
{
if (x == p->Key)
{
cout<<"Tim Thay "<<x<<" Trong Cay Nhi Phan"<<endl;
cout<<p->Key;
return (p);
}
else
if (x < p->Key)
p=p->pLeft;
else
p=p->pRight;
}
cout<<"Khong Tim Thay "<<x<<" Trong Cay Nhi Phan"<<endl;
return (NULL);
}
//Tim node the mang
void SearchStandFor(PTRTREE &r, PTRTREE &q)
{
if (r->pRight) SearchStandFor(r->pRight,q);
else
{
q->Key = r->Key;
q = r;
r = r->pLeft;
}
}
//Xoa Node Trong Cay
void Remove_node(PTRTREE &TREE, int x)
{
PTRTREE p;
if(TREE == NULL)
cout<<"Node p khong co"<<endl;
else
{
if (x < TREE->Key) Remove_node(TREE->pLeft,x);
else
if (x > TREE->Key) Remove_node(TREE->pRight,x);
else
{
p = TREE;
if(p->pRight == NULL)
TREE = p->pLeft;
else
if (p->pLeft == NULL)
TREE = p->pRight;
else
if (TREE->pLeft != NULL)
SearchStandFor(TREE->pLeft,p);
else
SearchStandFor(TREE->pRight,p);
delete p;
}
}
}
//Huy Toan Bo Cay
void RemoveTree(PTRTREE &TREE)
{
if (TREE!=NULL)
{
RemoveTree(TREE->pLeft);
RemoveTree(TREE->pRight);
delete(TREE);
TREE=NULL;
}
}
void main()
{
int n, i, x,chon,k;
do
{
cout<<"1:Them Node Vao Cay"<<endl;
cout<<"2:Duyet Cay Theo Node Left Right"<<endl;
cout<<"3:Duyet Cay Theo Left Right Node"<<endl;
cout<<"4:Duyet Cay Theo Right Node Left"<<endl;
cout<<"5:Duyet Cay Theo Left Node Right"<<endl;
cout<<"6:Tim Kiem Node"<<endl;
cout<<"7:Xoa Node Nhap Tu Phim"<<endl;
cout<<"8:Xoa Toan Bo Cay"<<endl;
cin>>chon;
switch (chon)
{
case 1:
cout<<"nhap So Lan Can Nhap"<<endl;
cin>>k;
for(int i=1;i<=k;i++)
{
cout<<"Gia tri cua node:";
cin>>x;
Insert_node(TREE,x);
}
break;
case 2:
cout<<"Duyet Cay Theo Node Left Right: "<<endl;
NLR(TREE);
cout<<endl;
break;
case 3:
cout<<"Duyet Cay Theo Left Right Node :"<<endl;
LRN(TREE);
cout<<endl;
break;
case 4:
cout<<"Duyet Cay Theo Right Node Left "<<endl;
RNL(TREE);
cout<<endl;
break;
case 5:
cout<<"Duyet Cay Theo Left Node Right :"<<endl;
LNR(TREE);
cout<<endl;
break;
case 6:
cout<<"Tim Kiem Node";
cout<<"\nNhap gia tri can tim:";
cin>>x;
Search(TREE,x);
break;
case 7:
cout<<"Xoa Node Nhap Tu Ban Phim"<<endl;
cout<<"\nNhap gia tri can xoa:";
cin>>x;
Remove_node(TREE,x);
break;
case 8:
cout<<"Xoa Toan Bo Node"<<endl;
RemoveTree(TREE);
break;
}
}while (chon!=0);
}