Activity之间切换的动画
2013-02-27 00:13
260 查看
用C++设计一个小型的英汉词典 悬赏分:20 | 解决时间:2010-1-1 11:01 | 提问者:天蓝∮圣鼠
设计一个英汉词典,支持功能:Member (查找)、Insert (插入)、Delete (删除) 操作。查找单词、插入单词(插入时,先查找,找不到插入,找到提示用户)、删除单词(删除时,先查找,找到删除,找不到提示用户)。
实现字典的常用方法有:有序线性表(Memeber用二分检索实现)、AVL树(二叉搜索树)、Patricia Trie、散列表等,这些方法中我只会有序线性表和Patricia Trie,哪位大侠能给我个二叉搜索树或者散列表的源代码研究研究?谢谢!
问题补充:
为什么没人帮我啊....
最佳答案
字典最快速的实现方法是trie tree。
这个树是专门用来实现字典的。但是trie tree的删除操作比较麻烦。用二叉查找树可以实现,速度也可以很快。AVL tree只不过是平衡的二叉树,在字典这个应用上没有客观的速度提升,因为字典不会产生极端化的二叉树(链表)。
下面是我的二叉查找树的代码。二叉查找树的优点是实现容易,而且它的inorder traverse既是按照字母顺序的输出。
//binary search tree, not self-balancing
//by Qingxing Zhang, Dec 28,2009. prep for google interview
#include <iostream>
using namespace std;
struct BST
{
int data;
BST *left;
BST *right;
};
//runtime: O(logn) on average, O(n) worst case
bool search(BST *&root, int key)//return false if the key doesn't exist
{
if(root==NULL)
return false;
if(key < root->data)
return search(root->left,key);
else if(key > root->data)
return search(root->right,key);
else
return true;
}
//runtime: O(logn)on average, O(n) worst case
bool insert(BST *&root, int key)//return false if the key already exists
{
if(root==NULL)
{
BST *node = new BST;
node->data = key;
node->left = node->right = NULL;
root = node;
return true;
}
else if(key < root->data)
return insert(root->left,key);
else if(key > root->data)
return insert(root->right,key);
else
return false;
}
//runtime:O(logn) on average, O(n) worst case
bool remove(BST *&root,int key)//return false if the key doesn't exist.
{
if(root==NULL)//no such key
return false;
else if(key < root->data)
return remove(root->left,key);
else if(key > root->data)
return remove(root->right,key);
else//node found
{
if((root->left==NULL)&&(root->right==NULL))//no child(leaf node)
{
BST *tmp = root;
root = NULL;
delete tmp;
}
else if((root->left==NULL)||(root->right==NULL))//one child
{
BST *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
{
BST *tmp = root->right;
while(tmp->left!=NULL)
tmp = tmp->left;
int tmpdata = tmp->data;
remove(root,tmpdata);
root->data = tmpdata;
}
return true;
}
}
//runtime:O(n)
void inorder(BST *&node)
{
if(node!=NULL)
{
inorder(node->left);
cout << node->data << " ";
inorder(node->right);
}
}
//runtime:O(n)
void preorder(BST *&node)
{
if(node!=NULL)
{
cout << node->data << " ";
preorder(node->left);
preorder(node->right);
}
}
//runtime:O(n)
void postorder(BST *&node)
{
if(node!=NULL)
{
postorder(node->left);
postorder(node->right);
cout << node->data << " ";
}
}
int main()
{
bool b;
BST *root = NULL;
b = insert(root,1);
b = insert(root,3);
b = insert(root,7);
b = insert(root,5);
b = insert(root,77);
b = insert(root,10);
b = insert(root,4);
b = insert(root,13);
//inorder
cout << "In-order:";
inorder(root);
cout << endl;
//preorder
cout << "Pre-order:";
preorder(root);
cout << endl;
//postorder
cout << "Post-order:";
postorder(root);
cout << endl;
// search for 7
if(search(root,7))
cout << "7 found!" << endl;
else
cout << "7 doesn't exist!" << endl;
b = remove(root,7);
cout << "----------------" << endl;
//inorder
cout << "In-order:";
inorder(root);
cout << endl;
//preorder
cout << "Pre-order:";
preorder(root);
cout << endl;
//postorder
cout << "Post-order:";
postorder(root);
cout << endl;
if(search(root,7))
cout << "7 found!" << endl;
else
cout << "7 doesn't exist!" << endl;
return 0;
}
用C++设计一个小型的英汉词典_百度知道
相关文章推荐
- Activity之间动画的切换
- android动画-Activity之间切换动画
- android的 activity之间切换去除动画之原始activity不调用finish
- 安卓手机卫士学习笔记系列——两个Activity之间切换动画效果overridependingtransition的使用
- Activity之间切换的动画效果
- Android中Activity之间切换动画的修改
- activity之间切换的动画效果
- Android中的动画详解系列【4】——Activity之间切换动画
- 如何实现Activity之间的切换动画(一)
- Android中的动画详解系列【4】——Activity之间切换动画
- Android中的动画详解系列【4】——Activity之间切换动画
- Android 关于 Activity 之间的切换动画
- Android 关于 Activity 之间的切换动画
- android中2个activity之间的切换动画效果(共12种)
- Activity之间切换动画
- Activity之间切换的动画
- Android中的动画具体解释系列【4】——Activity之间切换动画
- activity之间及fragment之间切换的动画效果
- 手势检测实现相册的左右滑动(并加上移动与旋转的特效) 首先,activity_main.xml里,使用ViewFlipper组件(可使用动画控制多个组件之间的切换效果) <?xml version="
- 【Android】Activity之间的动画切换