當前位置:首頁 » 編程語言 » trie樹c語言

trie樹c語言

發布時間: 2024-04-03 00:57:28

❶ 求一個實現簡單的英漢詞典(30詞左右)c++的C語言程序

字典最快速的實現方法是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;
}

熱點內容
ftp伺服器對什麼硬體要求高 發布:2024-11-28 14:45:10 瀏覽:650
sql服務管理器下載 發布:2024-11-28 14:45:02 瀏覽:772
windows第三方ftp搭建 發布:2024-11-28 14:43:53 瀏覽:199
asp文件夾上傳 發布:2024-11-28 14:41:13 瀏覽:213
積分的演算法 發布:2024-11-28 14:38:21 瀏覽:503
資料庫樹形查詢 發布:2024-11-28 14:37:08 瀏覽:904
零起點學通c語言視頻 發布:2024-11-28 14:29:42 瀏覽:291
python獲取串口數據 發布:2024-11-28 14:22:39 瀏覽:667
台灣ip代理伺服器雲主機 發布:2024-11-28 14:18:24 瀏覽:465
php寫後端 發布:2024-11-28 14:09:36 瀏覽:235