//#include <sys/types.h>
//#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "tree.h"
#define NAMESIZE 32
int xiaoyu(const char *str1,const char *str2)
{
int x=strcmp(str1,str2);
if(x<0) return 1;
else return 0;
}
void print_s(void *data)
{
struct score *d = data;
printf("英文单词:%s\t\t中文释义:%s\n", d->en, d->cn);
}
void print_save(void *data)
{
struct score *d = data;
fprintf(pwrite,"%s\t%s\n",d->en,d->cn);
}
int insert(struct node_st **root,struct score *data)
{
struct node_st *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 score *find(struct node_st *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);
}
#if 1
void travel(struct node_st *root)
{
if (root==NULL) {
return;
}
travel(root->l);
print_s(&root->data);
travel(root->r);
}
void travel_save(struct node_st *root)
{
if (root==NULL) {
return;
}
print_save(&root->data);
travel_save(root->l);
travel_save(root->r);
}
#else
void travel(struct node_st *root)
{
QUEUE *q;
struct node_st *cur;
int ret;
if (root == NULL) {
return;
}
q = queue_creat(sizeof(struct node_st *));
queue_enq(q, &root);
while (1) {
ret = queue_deq(q, &cur);
if (ret == -1) {
break;
}
print_s(&cur->data);
if (cur->l != NULL) {
queue_enq(q, &cur->l);
}
if (cur->r != NULL) {
queue_enq(q, &cur->r);
}
}
queue_destroy(q);
}
#endif
static void draw__(struct node_st *root, int level)
{
int i;
if (root == NULL) {
return;
}
draw__(root->r, level + 1);
for (i = 0; i < level; i++) {
printf(" ");
}
print_s(&root->data);
draw__(root->l, level + 1);
}
void draw(struct node_st *root)
{
draw__(root, 0);
getchar();
}
static struct node_st **find__(struct node_st **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_st *find_max(struct node_st *root)
{
while (root->r != NULL) {
root = root->r;
}
return root;
}
static struct node_st *find_min(struct node_st *root)
{
while (root->l != NULL) {
root = root->l;
}
return root;
}
void delete(struct node_st **root, char *en)
{
struct node_st **node, *save;
node = find__(root,en);
if (node == NULL) {
return;
}
save = *node;
if (save->l == NULL) {
*node = save->r;
} else {
*node = save->l;
find_max(save->l)->r = save->r;
}
free(save);
}
int getnum(struct node_st *root)
{
if (root == NULL) {
return 0;
}
return getnum(root->l) + 1 + getnum(root->r);
}
static void turn_left(struct node_st **root)
{
struct node_st *cur, *right;
cur = *root; right = cur->r;
*root = right;
cur->r = NULL;
find_min(right)->l = cur;
}
static void turn_right(struct node_st **root)
{
struct node_st *cur, *left;
cur = *root; left = cur->l;
*root = left;
cur->l = NULL;
find_max(left)->r = cur;
}
void balance(struct node_st **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);
}