二叉树实现

#include<stdio.h>
#include<stdlib.h>
 
#define NAMESIZE 32
 
struct score_st{
    int id;
    char name[NAMESIZE];
    int math;
    int chinese;
};
 
struct node_st{
    struct score_st data;
    struct node_st *l,*r;
};
 
void print_s(struct score_st *d)
{
    printf("%d %s %d %d\n", d->id, d->name, d->math, d->chinese);
}
 
int insert(struct node_st **root, struct score_st *data)
{
    struct node_st *node;
    if(*root == NULL)
    {
        node = malloc(sizeof(*node));
        if(node == NULL)
        {
            printf("malloc err!\n");
            return -1;
        }
        node->data = *data;
        node->l = NULL;
        node->r = NULL;
        *root = node;
        return 0;
    }
    if(data->id <= (*root)->data.id)
        return insert(&(*root)->l, data);
    else
        return insert(&(*root)->r, data);
}
 
struct score_st *find(struct node_st *root, int id)
{
    if(root == NULL)
        return NULL;
    if(id == root->data.id)
        return &root->data;
    if(id < root->data.id)
        return find(root->l, id);
    else
        return find(root->r, id);
}

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("  ");
	for(i = 0; i < level; i++)
		printf("  ");
	print_s(&root->data);
	for(i = 0; i < level; i++)
        printf("  ");
	draw_(root->l, level+1);
}
void draw(struct node_st *root)
{
	draw_(root,0);

}

int get_num(struct node_st *root)
{
	//int i = 0;
	if(root == NULL)
		return 0;
	else
		return 1 + get_num(&root->l) + get_num(&root->r); 
}

void turn_left(struct node_st **root)
{
	struct node_st **node = root;
	struct node_st *node1 = NULL;
	root = &((*root)->r);
	node->r = NULL;//这个不能忘

	node1 = (*root)->l;
	while(node1 != NULL)
		node1 = node1->l;
	node1 = *node;
}

//另外实现方法

struct node_st *find_l(struct node_st *root)
{
	if(root == NULL)
		return root;
	return find_l(&(root->l));
}
void turn_left(struct node_st **root)
{
	struct node_st *cur = *root;
	*root = cur->r;
	cur->r = NULL;

	find_l(*root)->l = cur;
}

void balance(struct node_st **root)
{
	if(*root == NULL)
		return;
	int sub;

	while(1)
	{
		sub = get_num(&(*root)->l) - get_num(&(*root)->r);
		if(sub >= -1 || sub <= 1)
			break;
		if(sub < -1)
			turn_left(root);
		if(sub > 1)
			turn_right(root);
	}
	balance(&(*root)->l);
	balance(&(*root)->r);
}

int main()
{
    int i;
    int arr[] = {1, 2, 3, 7, 6, 5, 9, 8, 4};
    struct node_st *tree = NULL;
    struct score_st tmp, *datap;
 
    for(i = 0; i < sizeof(arr)/sizeof(*arr); i++)
    {
        tmp.id = arr[i];
        snprintf(tmp.name, NAMESIZE, "stu%d", arr[i]);
        tmp.math = rand() % 100;
        tmp.chinese = rand() % 100;
        insert(&tree, &tmp);
    }
	draw(tree);
#if 0 
    int tmpid = 12;
    datap = find(tree, tmpid);
    if(datap == NULL)
        printf("can't find %d\n", tmpid);
    else
        print_s(datap);
#endif
 
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值