?? 6. Vi?t ch??ng tr?nh c?i ??t c?y nh? ph?n t?m ki?m c?n b?ng qu?n l? t? ?i?n
ti?ng Anh chuy?n ng?nh (ch?n 1 trong c?c ng?nh: To?n, L?, H?a, Tin, c) v?i c?c
thao t?c :
a. T?o t? ?i?n
b. T?m ki?m t? trong t? ?i?n
c. X?a t? trong t? ?i?n
d. C?p nh?t th?ng tin tr?n c?y
e. In danh s?ch t? l?n m?n h?nh
#include <iostream>
#include<fstream>
#include<string.h>
#include<iomanip>
using namespace std;
#define LH -1
#define EH 0
#define RH 1
struct WORD
{
string Word;
string Meaning;
};
struct AVLNode
{
int BalFactor;
WORD Info;
AVLNode* pLeft;
AVLNode* pRight;
};
typedef AVLNode *AVLTree;
void CreateAVLTree(AVLTree &T)
{
T=NULL;
}
void RightRight(AVLTree &T)
{
AVLNode* T1 = T->pRight;
T->pRight = T1->pLeft;
T1->pLeft = T;
switch(T1->BalFactor)
{
case RH:
T->BalFactor = EH;
T1->BalFactor = EH; break;
case EH:
T->BalFactor = RH;
T1->BalFactor = LH; break;
}
T = T1;
}
void RightLeft(AVLTree &T)
{
AVLNode* T1 = T->pRight;
AVLNode* T2 = T1->pLeft;
T->pRight = T2->pLeft;
T2->pLeft = T;
T1->pLeft = T2->pRight;
T2->pRight = T1;
switch(T2->BalFactor)
{
case LH:
T->BalFactor = EH;
T1->BalFactor = RH; break;
case EH:
T->BalFactor = EH;
T1->BalFactor = EH; break;
case RH:
T->BalFactor = LH;
T1->BalFactor = EH; break;
}
T2->BalFactor = 0;
T = T2;
}
void LeftLeft(AVLTree &T)
{
AVLNode *T1 = T->pLeft;
T->pLeft = T1->pRight;
T1->pRight = T;
switch(T1->BalFactor)
{
case LH:
T->BalFactor = EH;
T1->BalFactor = EH; break;
case EH:
T->BalFactor = LH;
T1->BalFactor = RH; break;
}
T = T1;
}
void LeftRight(AVLTree &T)
{
AVLNode* T1 = T->pLeft;
AVLNode* T2 = T1->pRight;
T->pLeft = T2->pRight;
T2->pRight = T;
T1->pRight = T2->pLeft;
T2->pLeft = T1;
switch(T2->BalFactor)
{
case LH:
T->BalFactor = RH;
T1->BalFactor = EH; break;
case EH:
T->BalFactor = EH;
T1->BalFactor = EH; break;
case RH:
T->BalFactor = EH;
T1->BalFactor = LH; break;
}
T2->BalFactor = EH;
T = T2;
}
int BalanceLeft(AVLTree &T)
{
AVLNode* T1 = T->pLeft;
switch(T1->BalFactor)
{
case LH: LeftLeft(T); return 2;
case EH: LeftLeft(T); return 1;
case RH: LeftRight(T); return 2;
}
return 0;
}
int BalanceRight(AVLTree &T)
{
AVLNode* T1 = T->pRight;
switch(T1->BalFactor)
{
case LH: RightLeft(T); return 2;
case EH: RightRight(T); return 1;
case RH: RightRight(T); return 2;
}
return 0;
}
//Them tu vao tu dien
int InsertNode(AVLTree &T, WORD W)
{
int Res, i=0;
if(T)
{
while(i <= T->Info.Word.length() || i <= W.Word.length())
{
if(T->Info.Word[i] > W.Word[i])
{
Res = InsertNode(T->pLeft, W);
if(Res < 2)
return Res;
switch(T->BalFactor)
{
case RH: T->BalFactor = EH; return 1;
case EH: T->BalFactor = LH; return 2;
case LH: BalanceLeft(T); return 1;
}
}
else if(T->Info.Word[i] < W.Word[i])
{
Res = InsertNode(T->pRight, W);
if(Res < 2)
return Res;
switch(T->BalFactor)
{
case LH: T->BalFactor = EH; return 1;
case EH: T->BalFactor = RH; return 2;
case RH: BalanceRight(T); return 1;
}
}
else
{
i++; continue;
}
}
}
T = new AVLNode;
if(T == NULL)
return -1;
T->Info = W;
T->BalFactor = EH;
T->pLeft = T->pRight = NULL;
return 2;
}
//Tim tu trong tu dien
AVLNode* SearchNode(AVLTree &T, string word)
{
AVLNode* p=T;
int i=0;
while(p!=NULL){
if(p->Info.Word == word)
return p;
else if (p->Info.Word[i] > word[i])
p=p->pLeft;
else if (p->Info.Word[i] < word[i])
p=p->pRight;
else
{
i++;
}
}
return NULL;
}
//Xuat tu va nghia
void xuatword(WORD a)
{
cout<< setw(20) << left << a.Word<<"|";
cout << setw(20) << left<
}
//Duyet tu dien
void LNR(AVLTree T)
{
if(T!=NULL)
{
LNR(T->pLeft);
xuatword(T->Info);
LNR(T->pRight);
}
}
//Tim phan tu the mang
int SearchStandFor(AVLTree &P, AVLTree &Q, AVLTree& T)
{
int Res;
if(Q->pLeft)
{
Res = SearchStandFor(P, Q->pLeft, T);
if(Res < 2) return Res;
switch(Q->BalFactor)
{
case LH: Q->BalFactor = 0; return 2;
case EH: Q->BalFactor = 1; return 1;
case RH: return BalanceRight(T);
}
}
else
{
P->Info = Q->Info;
P = Q;
Q = Q->pRight;
return 2;
}
}
//Xoa tu ra khoi tu dien
int DeleteNode(AVLTree &T, WORD W)
{
int Res, i=0;
if(T == NULL)
return 0;
while(i < T->Info.Word.length() || i < W.Word.length()){
if(T->Info.Word[i] > W.Word[i])
{
Res = DeleteNode(T->pLeft, W);
if(Res < 2) return Res;
switch(T->BalFactor)
{
case LH: T->BalFactor = 0; return 2;
case EH: T->BalFactor = 1; return 1;
case RH: return BalanceRight(T);
}
}
else if(T->Info.Word[i] < W.Word[i])
{
Res = DeleteNode(T->pRight, W);
if(Res < 2) return Res;
switch(T->BalFactor)
{
case RH: T->BalFactor = 0; return 2;
case EH: T->BalFactor = -1; return 1;
case LH: return BalanceLeft(T);
}
}
else
{
i++;
}
}
AVLNode* p = T;
if(T->pLeft == NULL)
{
T = T->pRight; Res = 2;
}
else if(T->pRight == NULL)
{
T = T->pLeft; Res = 2;
}
else{
Res = SearchStandFor(p, T->pRight,T);
if(Res < 2) return Res;
switch(T->BalFactor)
{
case RH: T->BalFactor = 0; return 2;
case EH: T->BalFactor = -1; return 1;
case LH: return BalanceLeft(T);
}
}
delete p;
return Res;
}
//Cap nhat tu dien
void UpdateNode(AVLTree &T, WORD W1, WORD W2)
{
if (T!=NULL)
{
DeleteNode(T,W1);
InsertNode(T,W2);
}
}
//Nhap tu va nghia
void nhapword(WORD& a)
{
cout<<"NHAP WORD: ";
getline(cin,a.Word);
cout<<"NHAP NGHIA: ";
getline(cin,a.Meaning);
cout<
}
//Doc tu dien trong file
int taotudien(AVLTree & T){
ifstream FileIn;
FileIn.open("TuDien.txt");
if (FileIn.fail() == true)
{
cout << "\nFile khong ton tai";
return 0;
}
WORD w;
int n;
FileIn>>n;
FileIn.get();
for(int i=0; i
{
string x;
getline(FileIn,x);
int pos = 0;
for(int i=0; i < x.length(); i++)
if(x[i] ==' '){
pos = i;
break;}
w.Word =x.substr(0, pos);
w.Meaning = x.substr(pos+1, x.length()-1);
InsertNode(T,w);
}
}
void menu(){
system("cls");
cout<<"
TU DIEN TIENG ANH CHUYEN NGANH(TOAN HOC)
\n";
cout<<"=========================================
=============\n";
cout<<"
MENU
\n";
cout<<"=========================================
=============\n";
cout<<"1) Tao tu dien\n";
cout<<"2) Tim kiem tu trong tu dien\n";
cout<<"3)
Xoa tu trong tu dien\n";
cout<<"4) Cap nhat thong tin tren cay\n";
cout<<"5) In danh sach tu len man hinh\n";
cout<<"6) Thoat chuong trinh\n";
cout<<"=========================================
=============\n";
}
int main() {
menu();
AVLTree T;
CreateAVLTree(T);
string word;
int x;
do{
menu();
cout<<"Nhap lua chon : ";
cin>>x;
switch(x){
case 1:
{
system("cls");
taotudien(T);
cout<< "\nDA TAO TU DIEN THANH CONG\n";
system("pause");
}
break;
case 2:
{
system("cls");
cout<<"NHAP WORD CAN TIM: ";
cin.ignore();
getline(cin,word);
AVLNode* p=SearchNode(T,word);
if(p!=NULL){
cout << setw(14) << right << "WORD"<
<
cout << setw(15) << right << "Meaning"<
xuatword(p->Info);
}
else
cout<<"Khong tim thay trong tu dien!!!\n";
system("pause");
}
break;
case 3:
{
system("cls");
cout<<"NHAP WORD CAN XOA: ";
cin.ignore();
getline(cin,word);
AVLNode* p=SearchNode(T,word);
if (p!=NULL)
{
DeleteNode(T,p->Info);
cout<< "\nDA XOA THANH CONG\n";
}
else
cout<<"Khong tim thay trong tu dien de xoa!!!\n";
system("pause");
break;
}
case 4:
{
system("cls");
cout<<"NHAP WORD CAN THAY DOI TU VA NGHIA: ";
cin.ignore();
getline(cin,word);
AVLNode* p=SearchNode(T,word);
if (p!=NULL)
{
cout<<"CAP NHAT LAI WORD:\n";
WORD newword;
nhapword(newword);
UpdateNode(T,p->Info,newword);
system("cls");
cout<<"DA CAP NHAT THANH CONG\n";
}
else
cout<<"Khong tim thay trong tu dien de thay doi
word!!!\n";
system("pause");
}
break;
case 5:
{
system("cls");
cout<<"===========TU DIEN TIENG ANH CHUYEN
NGANH(TOAN HOC)===========\n";
if(T==NULL)
{
cout<<"\n\n
RONG
\n\n\n";
}
else {
cout << setw(14) << right << "WORD"<
<
cout << setw(15) << right << "Meaning"<
LNR(T);
}
cout<<"=========================================
=====================\n";
system("pause");
}
break;
}
}while(x!=6);
cout<<"\n\nChuong trinh da ket thuc";
}