数据结构 -- 二叉搜索树应用(B*树)
程序员文章站
2022-03-15 15:09:13
Sample Input:4 28 19fishanimalbirdfruitanimal cat 30fish goldfish 50animal dog 20bird blackbird 10animal bear 10fruit mango 100animal alligator 50animal tiger 3animal lion 3fish swordfish 10animal deer 5animal cow 15fish garfish 5fish .....
Sample Input:
4 28 19
fish
animal
bird
fruit
animal cat 30
fish goldfish 50
animal dog 20
bird blackbird 10
animal bear 10
fruit mango 100
animal alligator 50
animal tiger 3
animal lion 3
fish swordfish 10
animal deer 5
animal cow 15
fish garfish 5
fish catfish 55
fish salmon 40
bird crow 20
bird dove 10
bird flamingo 15
fruit apple 50
fruit banana 50
fruit nectarine 10
fruit coconut 10
fruit peach 40
fruit apricot 30
fruit avocado 25
fruit cherry 100
fruit cranberry 100
animal horse 6
search fruit avocado
search fish tilapia
search animal cow
search bird crow
search bird cow
search animal cat
item_before animal deer
height_balance animal
height_balance bird
height_balance fish
search flower rose
count animal
count fruit
delete animal cat
search animal cat
count animal
delete fish swordfish
delete fruit avocado
delete_tree animal
Sample Output:
animal bird fish fruit
===animal===
alligator bear cat cow deer dog horse lion tiger
===bird===
blackbird crow dove flamingo
===fish===
catfish garfish goldfish salmon swordfish
===fruit===
apple apricot avocado banana cherry coconut cranberry mango nectarine peach
=====Processing Commands=====
25 avocado found in fruit
tilapia not found in fish
15 cow found in animal
20 crow found bird
cow not found in bird
30 cat found in animal
item before deer: 4
animal: left height 1, right height 3, difference 2, not balanced
bird: left height -1, right height 2, difference 3, not balanced
fish: left height 1, right height 1, difference 0, balanced
flower does not exist
animal count 142
fruit count 515
cat deleted from animal
cat not found in animal
animal count 112
swordfish deleted from fish
avocado deleted from fruit
animal deleted
#include <cstring>
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#define ll long long
#define _for(i, a, b) for (ll i = (a); i < (b); ++i)
#define sc scanf
#define pr printf
#define TLE \
ios::sync_with_stdio(false); \
cin.tie(0);
const int maxn = 1e5 + 5;
const int INF = 0x3f3f3f3f;
using namespace std;
struct item_node_struct
{
char name[32];
int count;
struct item_node_struct *left, *right;
};
typedef struct item_node_struct item_node;
struct tree_name_node_struct
{
char treeName[32];
struct tree_name_node_struct *left, *right;
item_node *theTree;
};
typedef struct tree_name_node_struct tree_name_node;
tree_name_node *TOT_head = NULL;
tree_name_node *Search_Res = NULL;
void Search_Stree(tree_name_node *B, char *str)
{
if (B == NULL)
{
Search_Res = NULL;
return;
}
if (strcmp(B->treeName, str) == 0)
{
Search_Res = B;
}
else
{
if (strcmp(str, B->treeName) < 0)
{
Search_Stree(B->left, str);
}
else if (strcmp(str, B->treeName) > 0)
{
Search_Stree(B->right, str);
}
}
return;
}
item_node *Search_Res_item = NULL;
void Search_Sitem(item_node *B, char *str)
{
if (B == NULL)
{
Search_Res_item = NULL;
return;
}
if (strcmp(B->name, str) == 0)
{
Search_Res_item = B;
}
else
{
if (strcmp(str, B->name) < 0)
{
Search_Sitem(B->left, str);
}
else if (strcmp(str, B->name) > 0)
{
Search_Sitem(B->right, str);
}
}
return;
}
void AddTreeOfTrees(tree_name_node *B, char *str)
{
if (strcmp(str, B->treeName) < 0)
{
if (B->left == NULL)
{
B->left = (tree_name_node *)malloc(sizeof(tree_name_node));
B->left->left = NULL;
B->left->right = NULL;
B->left->theTree = NULL;
strcpy(B->left->treeName, str);
}
else
{
AddTreeOfTrees(B->left, str);
}
}
else if (strcmp(str, B->treeName) > 0)
{
if (B->right == NULL)
{
B->right = (tree_name_node *)malloc(sizeof(tree_name_node));
B->right->left = NULL;
B->right->right = NULL;
B->right->theTree = NULL;
strcpy(B->right->treeName, str);
}
else
{
AddTreeOfTrees(B->right, str);
}
}
return;
}
void AddItemTree(item_node *B, char *str, int count)
{
if (strcmp(str, B->name) < 0)
{
if (B->left == NULL)
{
B->left = (item_node *)malloc(sizeof(item_node));
B->left->left = NULL;
B->left->right = NULL;
strcpy(B->left->name, str);
B->left->count = count;
}
else
{
AddItemTree(B->left, str, count);
}
}
else if (strcmp(str, B->name) > 0)
{
if (B->right == NULL)
{
B->right = (item_node *)malloc(sizeof(item_node));
B->right->left = NULL;
B->right->right = NULL;
strcpy(B->right->name, str);
B->right->count = count;
}
else
{
AddItemTree(B->right, str, count);
}
}
return;
}
void InOrderThreading_TOT(tree_name_node *B)
{
if (B == NULL)
return;
InOrderThreading_TOT(B->left);
cout << B->treeName << " ";
InOrderThreading_TOT(B->right);
}
void InOrderThreading_item(item_node *B)
{
if (B == NULL)
return;
InOrderThreading_item(B->left);
cout << B->name << " ";
InOrderThreading_item(B->right);
}
void InOrderThreading_TOT_To_item(tree_name_node *B)
{
if (B == NULL)
return;
InOrderThreading_TOT_To_item(B->left);
cout << endl
<< "===" << B->treeName << " ===" << endl;
InOrderThreading_item(B->theTree);
InOrderThreading_TOT_To_item(B->right);
}
void Search(char *tree, char *item)
{
Search_Stree(TOT_head, tree);
if (Search_Res == NULL)
{
cout << tree << " does not exist" << endl;
return;
}
item_node *si = Search_Res->theTree;
Search_Sitem(si, item);
if (Search_Res_item == NULL)
{
cout << item << " not found in " << tree << endl;
return;
}
item_node *ans = Search_Res_item;
if (ans->count == 0)
{
cout << item << " not found in " << tree << endl;
}
else
cout << ans->count << " " << item << " found in " << tree << endl;
return;
}
int _size = 0;
void item_size(item_node *B)
{
if (B == NULL)
return;
item_size(B->left);
_size++;
item_size(B->right);
}
int ValToPos(item_node *B, char *item)
{
if (B == NULL)
return 0;
if (strcmp(item, B->name) == 0)
{
_size = 0;
item_size(B->left);
return _size;
}
else if (strcmp(item, B->name) > 0)
{
_size = 0;
item_size(B->left);
int now = _size;
return (ValToPos(B->right, item) + now + 1);
}
else if (strcmp(item, B->name) < 0)
{
return ValToPos(B->left, item);
}
}
void Item_before(char *tree, char *item)
{
int ans = 0;
Search_Stree(TOT_head, tree);
if (Search_Res == NULL)
{
cout << tree << " does not exist" << endl;
return;
}
item_node *si = Search_Res->theTree;
cout << "item before " << item << ": " << ValToPos(si, item) << endl;
}
int Find_depth(item_node *B)
{
if (B == NULL)
return -1;
else
return max(Find_depth(B->left), Find_depth(B->right)) + 1;
}
void Height_balance(char *tree)
{
Search_Stree(TOT_head, tree);
if (Search_Res == NULL)
{
cout << tree << " does not exist" << endl;
return;
}
item_node *si = Search_Res->theTree;
int l, r;
l = Find_depth(si->left);
r = Find_depth(si->right);
cout << "tree: left height " << l << " right height " << r << ", difference " << abs(l - r) << ", ";
if (abs(l - r) > 1)
{
cout << "not balanced" << endl;
}
else
{
cout << "balanced" << endl;
}
}
int sum_count = 0;
void item_sum_count(item_node *B)
{
if (B == NULL)
return;
item_sum_count(B->left);
sum_count += B->count;
item_sum_count(B->right);
}
void Count(char *tree)
{
Search_Stree(TOT_head, tree);
if (Search_Res == NULL)
{
cout << tree << " does not exist" << endl;
return;
}
item_node *si = Search_Res->theTree;
sum_count = 0;
item_sum_count(si);
cout << tree << " count " << sum_count << endl;
}
void Delete_free(item_node *B)
{
if (B == NULL)
return;
item_size(B->left);
item_size(B->right);
free(B);
return;
}
void Delete(char *tree, char *item)
{
Search_Stree(TOT_head, tree);
if (Search_Res == NULL)
{
cout << tree << " does not exist" << endl;
return;
}
item_node *si = Search_Res->theTree;
Search_Sitem(si, item);
if (Search_Res_item == NULL)
{
cout << item << " not found in " << tree << endl;
return;
}
item_node *sii = Search_Res_item;
sii->count = 0;
cout << item << " deleted from " << tree << endl;
return;
}
void Delete_tree(char *tree)
{
Search_Stree(TOT_head, tree);
if (Search_Res == NULL)
{
cout << tree << " does not exist" << endl;
return;
}
item_node *si = Search_Res->theTree;
free(Search_Res->theTree);
Search_Res->theTree = NULL;
cout << tree << " deleted" << endl;
return;
}
int main()
{
int n_trees, n_items, n_commands;
cin >> n_trees >> n_items >> n_commands;
for (int i = 1; i <= n_trees; ++i)
{
char str[32];
cin >> str;
if (TOT_head == NULL)
{
TOT_head = (tree_name_node *)malloc(sizeof(tree_name_node));
TOT_head->left = NULL;
TOT_head->right = NULL;
TOT_head->theTree = NULL;
strcpy(TOT_head->treeName, str);
//cout << TOT_head->treeName << endl;
}
else
{
AddTreeOfTrees(TOT_head, str);
}
}
char Stree[32], Sitem[64];
int Citem;
for (int i = 1; i <= n_items; ++i)
{
cin >> Stree >> Sitem >> Citem;
tree_name_node *st;
Search_Stree(TOT_head, Stree);
st = Search_Res;
if (st->theTree == NULL)
{
st->theTree = (item_node *)malloc(sizeof(item_node));
st->theTree->left = NULL;
st->theTree->right = NULL;
strcpy(st->theTree->name, Sitem);
st->theTree->count = Citem;
}
else
{
AddItemTree(st->theTree, Sitem, Citem);
}
}
InOrderThreading_TOT(TOT_head);
InOrderThreading_TOT_To_item(TOT_head);
cout << endl
<< "=====Processing Commands=====" << endl;
string opt;
char tree[32], item[32];
for (int i = 1; i <= n_commands; ++i)
{
cin >> opt;
if (opt == "search")
{
cin >> tree >> item;
Search(tree, item);
}
else if (opt == "item_before")
{
cin >> tree >> item;
Item_before(tree, item);
}
else if (opt == "height_balance")
{
cin >> tree;
Height_balance(tree);
}
else if (opt == "count")
{
cin >> tree;
Count(tree);
}
else if (opt == "delete")
{
cin >> tree >> item;
Delete(tree, item);
}
else if (opt == "delete_tree")
{
cin >> tree;
Delete_tree(tree);
}
}
return 0;
}
本文地址:https://blog.csdn.net/weixin_45919985/article/details/110229704