平衡二叉树实现

#include <stdio.h>
#include <stdlib.h>
#include <queue.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;
};

static struct node_st *tree = NULL;

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)
            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//can be cancel
        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("    ");
    print_s(&root->data);
    draw_(root->l,level+1);
}

static int get_num(struct node_st *root)
{
    if(root == NULL)
        return 0;

    return get_num(root->l) + 1 + get_num(root->r);
}

static struct node_st *find_min(struct node_st *root)//find the most left node
{
    if(root->l == NULL)
        return root;
    return find_min(root->l);
}

static struct node_st *find_max(struct node_st *root)//find the most right node
{
    if(root->r == NULL)
        return root;
    return find_max(root->r);
}

void draw(struct node_st *root)
{
    draw_(root,0);
    printf("\n\n");
    //getchar();
}

static void turn_left(struct node_st **root)
{
    struct node_st *cur = *root;
    *root = cur->r;
    cur->r = NULL;
    find_min(*root)->l = cur;
    //draw(tree);
}

static void turn_right(struct node_st **root)
{
    struct node_st *cur = *root;
    *root = cur->l;
    cur->l = NULL;
    find_max(*root)->r = cur;
    //draw(tree);
}

void balance(struct node_st **root)
{
    int sub;
    
    if(*root == NULL)
        return ;
    while(1)
    {
    sub = get_num((*root)->l) - get_num((*root)->r);

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

void delete(struct node_st **root,int id)
{
    struct node_st **node = root;
    struct node_st *cur = NULL;

    while(*node != NULL && (*node)->data.id != id)
    {
    if(id < (*node)->data.id)
        node = &(*node)->l;
    else
        node = &(*node)->r;
    }

    if(*node == NULL)
        return ;

    cur = *node;

    if(cur->l == NULL)
        *node = cur->r;
    else
    {
        *node = cur->l;
        find_max(cur->l)->r = cur->r;
    }

    free(cur);
}

#if 0
void travel(struct node_st *root) //xian xu bian li
{
    if(root == NULL)
        return ;
    print_s(&root->data);

    travel(root->l);
    travel(root->r);

}
#else
void travel(struct node_st *root)//an ceng bian li
{
    QUEUE *qu;
    struct node_st *cur;
    int ret;

    qu = queue_create(sizeof(struct node_st *));
    if(qu == NULL)
        return ;

    queue_en(qu,&root);
    /*if error*/

    while(1)
    {
        ret = queue_de(qu,&cur);
        if(ret == -1)
            break;
        print_s(&cur->data);
        
        if(cur->l != NULL)
            queue_en(qu,&cur->l);
        if(cur->r != NULL)
            queue_en(qu,&cur->r);
    }

    queue_destroy(qu);

}
#endif


int main()
{
    int i;
    int arr[] = {1,2,3,7,6,5,9,8,4};
    
    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);

    balance(&tree);

    draw(tree);


    travel(tree);

#if 0
    int tmpid = 5;
    delete(&tree,tmpid);

    draw(tree);
#endif

#if 0
    int tmpid = 2;
    datap = find(tree,tmpid);
    if(datap == NULL)
        printf("can  not find the id:%d!!",tmpid);
    else
        print_s(datap);
#endif


    exit(0);
}
#ifndef QUEUE_H__
#define QUEUE_H__

#include "llist.h"

typedef LLIST QUEUE;

QUEUE *queue_create(int );

int queue_en(QUEUE *,const void *);

int queue_de(QUEUE *,void *);

void queue_destroy(QUEUE *);

#endif
queue.h用于设置队列并分层展示
queue.c
#include <stdio.h>
#include <stdlib.h>

#include "queue.h"

QUEUE *queue_create(int size)
{
    return llist_create(size);
}

int queue_en(QUEUE *ptr,const void *data)
{
    return llist_insert(ptr,data,LLIST_BACKWARD);
}

static int always_match(const void *p1,const void *p2)
{
    return 0;
}

int queue_de(QUEUE *ptr,void *data)
{
    return llist_fetch(ptr,(void *)0,always_match,data);
}

void queue_destroy(QUEUE *ptr)
{
    llist_destroy(ptr);
}
llist.h
#ifndef LLIST_H__
#define LLIST_H__

#define LLIST_FORWARD		1
#define LLIST_BACKWARD		2

typedef void llist_op(const void *);

typedef int llist_cmp(const void *,const void *);

struct llist_node_st
{
	//void *data;
	//char data[1024];
	struct llist_node_st *prev;//4
	struct llist_node_st *next;
	//change length
	//char data[0];//length = 0 occupy position
	char data[1];
};

typedef struct 
{
	int size;
	struct llist_node_st head;
}LLIST;

LLIST *llist_create(int initsize);

int llist_insert(LLIST *,const void *data,int mode);

void *llist_find(LLIST *,const void *key,llist_cmp *);

int llist_delete(LLIST *,const void *key,llist_cmp *);

int llist_fetch(LLIST *,const void *key,llist_cmp *,void *data);

void llist_travel(LLIST *,llist_op * );

void *llist_destroy(LLIST *);


#endif

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值