基礎的電子詞典——單詞的增刪查找(平衡二叉排序樹) c語言Linux實訓作業

a.c

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "a.h"
#include "tree.h"

FILE *pf;
FILE *tf;

struct node_wo *tree = NULL;

char *a_to_A(char str[])
{
	int len = strlen(str);
	int i;
	for(i = 0; i<len; i++)
	{
		if(str[i] >= 97 && str[i] <= 122)
		{
			str[i] -= 32;
		}
	}
	return str;
}
void insert__tree(char en[], char cn[])
{
	struct word s;
	a_to_A(en);
	strcpy(s.en, en);
	strcpy(s.cn, cn);
	insert(&tree, &s);
	balance(&tree);
}
void insert_tree()
{
	struct word s;
	char en[40];
	char cn[40];
	printf("請輸入要添加的英文單詞:");
	scanf("%s", en);
	printf("請輸入該單詞的中文解釋:");
	scanf("%s", cn);
	a_to_A(en);
	strcpy(s.en, en);
	strcpy(s.cn, cn);
	insert(&tree, &s);
	balance(&tree);
}
void find_tree()
{
	char en[40];
	printf("請輸入要查找的單詞:");
	struct word *s;
	scanf("%s", en);
	a_to_A(en);
	s = find(tree, en);
	if(s != NULL)
	{
		print_s(s);
	}
	else
	{
		printf("沒有該單詞\n");
	}
}
void revise()
{
	char en[40];
	printf("請輸入要修改的單詞:");
	struct word *s;
	scanf("%s", en);
	a_to_A(en);
	s = find(tree, en);
	printf("請輸入修改之後的中文解釋:");
	char cn[40];
	scanf("%s", cn);
	strcpy(s->cn, cn);
	print_s(s);
}
void delete_tree()
{
	char en[40];
	printf("請輸入要刪除的單詞:");
	scanf("%s", en);
	struct word *s;
	a_to_A(en);
	delete(&tree, en);
	balance(&tree);
}
void travle_tree(struct node_wo *root)
{
	if(root == NULL)
	{
		return ;
	}
	fprintf(tf, "%s\t%s\n", root->data.en, root->data.cn);
	travle_tree(root -> l);
	travle_tree(root -> r);
}
void read_file()
{
	int sum = read_sum();
	if(!sum)
	{
		return ;
	}
	pf = fopen("aa.txt", "r");
	if(pf == NULL)
	{
		return ;
	}
	int i;
	for(i = 0; i< sum; i++)
	{
		struct word w;
		fscanf(pf, "%s\t%s\n", w.en, w.cn);
		a_to_A(w.en);
		insert(&tree, &w);
	}
	balance(&tree);
	fclose(pf);
}
int read_sum()
{
	int sum = 0;
	FILE *p = fopen("sum.txt", "r");
	if(p == NULL)
	{
		sum = 0;
		return sum;
	}
	else 
	{
		fscanf(p, "%d\n", &sum);
		fclose(p);
		return sum;
	}
}
void save_sum()
{
	int sum = getnum(tree);
	FILE *p = fopen("sum.txt", "w");
	fprintf(p, "%d\n", sum);
	fclose(p);
}
void menu()
{
	read_file();
	int x = 0;
	while(1)
	{
		printf("***歡迎使用電子詞典***\n");
		printf("1.新增單詞\n");
		printf("2.查找單詞\n");
		printf("3.刪除單詞\n");
		printf("4.顯示樹\n");
		printf("5.修改單詞\n");
		printf("6.退出\n");
		printf("請選擇您要進行的操作編號:");
		scanf("%d", &x);
		if(x == 1)
		{
			insert_tree();
		}
		else if(x == 2)
		{
			find_tree();
		}
		else if(x == 3)
		{
			delete_tree();
		}
		else if(x == 4)
		{
			travle(tree);
		}
		else if(x == 5)
		{
			revise();
		}
		else if(x == 6)
		{
			save_sum();
			tf = fopen("aa.txt", "w");
			travle_tree(tree);
			fclose(tf);
			printf("感謝您的使用!\n");
			break;
		}
		else printf("輸入錯誤,請重新輸入!!\n");
	}
}
int main()
{
	menu();
	return 0;
}

a.h

#ifndef A
#define A
#include "tree.h"

extern FILE *pf;
extern FILE *tf;

char *a_to_A(char str[]);
void insert_tree();
void find_tree();
void delete_tree();
void revise();
void read_file();
int read_sum();
void save_sum();
void travle_tree(struct node_wo *root);
void menu();

#endif

tree.c

#include <sys/types.h>
#include <unistd.h>

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include "tree.h"

int xiaoyu(const char *str1, const char *str2)
{
	if(strcmp(str1, str2) < 0)
	{
		return 1;
	}
	else 
	{
		return 0;
	}
}
void print_s(void *data)
{
	struct word *ww = data;
	printf("英文單詞:%s\t中文解釋:%s\n", ww->en, ww->cn);
}

int insert(struct node_wo **root, struct word *data)
{
	struct node_wo *newnode;
	if(*root == NULL)
	{
		newnode = malloc(sizeof(*newnode));
		newnode -> data = *data;
		newnode ->l = newnode -> r = NULL;
		*root = newnode;
		return 0;
	}
	if(xiaoyu(data->en, (*root)->data.en))
	{
		return insert(&(*root)->l, data);
	}
	else
	{
		return insert(&(*root)->r, data);
	}
}
struct word *find(struct node_wo *root, char *en)
{
	if(root == NULL)
	{
		return NULL;
	}
	if(strcmp(en, root->data.en) == 0)
	{
		return &root->data;
	}
	if(xiaoyu(en, root->data.en))
	{
		return find(root->l, en);
	}
	return find(root->r, en);
}
void travle(struct node_wo *root)
{
	if(root == NULL)
	{
		return ;
	}
	travle(root -> l);
	print_s(&root->data);
	travle(root -> r);
}
static void draw_(struct node_wo *root, int high)
{
	int i;
	if(root == NULL)
	{
		return ;
	}
	draw_(root->r, high+1);
	for(i = 0; i<high; i++)
	{
		printf("   ");
	}
	print_s(&root->data);
	draw_(root->l, high + 1);
}
void draw(struct node_wo *root)
{
	draw_(root, 0);
	getchar();
}
static struct node_wo **find_(struct node_wo **root, char *en)
{
	if(*root == NULL)
	{
		return NULL;
	}
	if(strcmp(en, (*root)->data.en) == 0)
	{
		return root;
	}
	if(xiaoyu(en, (*root)->data.en))
	{
		return find_(&(*root)->l, en);
	}
	return find_(&(*root)->r, en);
}
static struct node_wo *find_max(struct node_wo *root)
{
	while(root->r != NULL)
	{
		root = root -> r;
	}
	return root;
}
static struct node_wo *find_min(struct node_wo *root)
{
	while(root-> l != NULL)
	{
		root = root -> l;
	}
	return root;
}
void delete(struct node_wo **root, char *en)
{
	struct node_wo **cur, *save;
	cur = find_(root, en);
	if(cur == NULL)
	{
		return ;
	}
	save = *cur;
	if(save->l == NULL)
	{
		*cur = save->r;
	}
	else 
	{
		*cur = save->l;
		find_max(save->l)->r = save->r;
	}
	free(save);
}
int getnum(struct node_wo *root)
{
	if(root == NULL)
	{
		return 0;
	}
	return getnum(root->l) + 1 + getnum(root->r);
}
static void turn_left(struct node_wo **root)
{
	struct node_wo *cur, *right;
	cur = *root;
	right = cur -> r;
	*root = right;
	cur->r = NULL;
	find_min(right)->l = cur;
}
static void turn_right(struct node_wo **root)
{
	struct node_wo *cur, *left;
	cur = *root;
	left = cur->l;
	*root = left;
	cur->l = NULL;
	find_max(left)->r = cur;
}
void balance(struct node_wo **root)
{
	int sub;
	if(*root == NULL)
	{
		return ;
	}
	while(1)
	{
		sub = getnum((*root) -> l) - getnum((*root) -> r);
		if(sub >= -1 && sub <= 1)
		{
			break;
		}
		if(sub < -1)
		{
			turn_left(root);
		}
		else 
		{
			turn_right(root);
		}
	}
	balance(&(*root) -> l);
	balance(&(*root) -> r);
}


tree.h

#ifndef tree__
#define tree__

struct word
{
	char en[40];
	char cn[40];
};
struct node_wo
{
	struct word data;
	struct node_wo *l,*r;
};

extern struct node_wo *tree;

int xiaoyu(const char *str1, const char *str2);
int insert(struct node_wo **root, struct word *data);
void balance(struct node_wo **root);
void delete(struct node_wo **root, char *en);
void draw(struct node_wo *root);
void travle(struct node_wo *root);
struct word *find(struct node_wo *root, char *en);
void print_s(void *data);
int getnum(struct node_wo *root);

#endif

 

發佈了101 篇原創文章 · 獲贊 65 · 訪問量 4354
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章