小型英汉词典(黑白界面下的)使用了二叉搜索树C++

// 小型英汉词典.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

class Node
{
public:
	string English;
	string Chinese;
	Node *left;
	Node *right;
};

//runtime: O(logn) on average, O(n) worst case
string search(Node *&root, string English)//return false if the key doesn't exist
{
if(root==NULL)
   return "单词不存在";
if(English < root->English)
   return search(root->left,English);
else if(English > root->English)
   return search(root->right,English);
else
	return root->Chinese;
}

//runtime: O(logn)on average, O(n) worst case
string insert(Node *&root, string English,string Chinese)//return false if the key already exists
{
if(root==NULL)
{
   Node *node = new Node;
   node->English = English;
   node->Chinese = Chinese;
   node->left = node->right = NULL;
   root = node;
   return "插入成功";
}
else if(English < root->English)
   return insert(root->left,English,Chinese);
else if(English > root->English)
   return insert(root->right,English,Chinese);
else
   return "插入失败";
}

//runtime:O(logn) on average, O(n) worst case
string remove(Node *&root,string English)//return false if the key doesn't exist.
{
if(root==NULL)//no such key
   return "删除失败";
else if(English < root->English)
   return remove(root->left,English);
else if(English > root->English)
   return remove(root->right,English);
else//node found
{
   if((root->left==NULL)&&(root->right==NULL))//no child(leaf node)
   {
    Node *tmp = root;
    root = NULL;
    delete tmp;
   }
   else if((root->left==NULL)||(root->right==NULL))//one child
   {
    Node *tmp = root;
    if(root->left==NULL)
     root = root->right;
    else
     root = root->left;
    delete tmp;
   }
   else//two children:replace node value with inorder successor and delete that node
   {
    Node *tmp = root->right;
    while(tmp->left!=NULL)
     tmp = tmp->left;
    string tmpEnglish = tmp->English;
    remove(root,tmpEnglish);
    root->English = tmpEnglish;
   }
   return "删除成功";
}
}

string createIndex(Node *&root)//create a dictionary index
{
	string s;
	char ch[50];
	for(char i='a';i<='z';i++)
	{
		s=i;
		s="lib/"+s+".txt";
		ifstream infile(s);
		for(;!infile.eof();)
		{
		int k=0;
		infile.getline(ch,50);
		int i;
		for(i=0;i<50;i++)
		{
			if(ch[i]=='\0')
				break;
			if(ch[i]==' ')
				k=i;
		}
		string judgeword(ch);
		if(judgeword.length()!=0){
			string English = judgeword.substr(0,k);
			string Chinese = judgeword.substr(k+1,i);
			insert(root,English,Chinese);}
		}
		infile.close();
	}
	return "词典打开结束";
}

void menu()
{
	cout<<"_________________________"<<endl;
	cout<<"|     使用说明          |"<<endl;
	cout<<"|1、打开词典 2、查找单词|"<<endl;
	cout<<"|3、删除单词 4、添加单词|"<<endl;
	cout<<"|5、关闭词典            |"<<endl;
	cout<<"|查找和删除只需输入英文 |"<<endl;
	cout<<"|添加需要输入中文和英文 |"<<endl;
	cout<<"|_______________________|"<<endl;
}

void fun()
{
Node *root = NULL;
int k;
string Chinese,English;
while(true)
{
	cin>>k;
	if(k==1)
	{
	cout<<createIndex(root)<<endl;
	}else if(k==2){
		cout<<"输入英文:"<<endl;
		cin>>English;
		cout<<search(root,English)<<endl;
	}else if(k==3){
		cout<<"输入英文:"<<endl;
		cin>>English;
		cout<<remove(root,English)<<endl;
	}else if(k==4){
		cout<<""<<endl;
		cin>>English;
		cout<<""<<endl;
		cin>>Chinese;
		cout<<insert(root,English,Chinese)<<endl;
	}else if(k==5){
		break;
	}else{}
}
}

int main()
{
menu();
fun();
return 0;
}

  • 5
    点赞
  • 50
    收藏
    觉得还不错? 一键收藏
  • 9
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值