c语言平衡树代码
avl_tree.h
#ifndef _AVL_TREE
#define _AVL_TREE
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <math.h>
#include "queue.h"
#define LH 1
#define RH -1
#define EH 0
typedef struct BSTNode{
int val;
int bf;
int hight;
struct BSTNode *lchild, *rchild;
}BSTNode;
int max(int a, int b)
{
if (a > b)
{
return a;
}
return b;
}
int get_hight(BSTNode * node)
{
if (!node)
{
return 0;
}
return node->hight;
}
BSTNode *new_node(int e)
{
BSTNode *node = (BSTNode *)malloc(sizeof(BSTNode));
memset(node, 0x00, sizeof(BSTNode));
node->hight = 1;
node->val = e;
node->bf = 0;
return node;
}
BSTNode *right_rotate(BSTNode *node)
{
BSTNode *lc = node->lchild;
node->lchild = lc->rchild;
lc->rchild = node;
node->hight = max(get_hight(node->lchild), get_hight(node->rchild)) + 1;
lc->hight = max(get_hight(lc->lchild), get_hight(lc->rchild)) + 1;
return lc;
}
BSTNode *left_rotate(BSTNode *node)
{
BSTNode *rc = node->rchild;
node->rchild = rc->lchild;
rc->lchild = node;
node->hight = max(get_hight(node->lchild), get_hight(node->rchild)) + 1;
rc->hight = max(get_hight(rc->lchild), get_hight(rc->rchild)) + 1;
return rc;
}
BSTNode *insert_node(BSTNode *root, BSTNode *node)
{
if (root == NULL)
{
root = node;
return root;
}
if (node->val > root->val)
{
root->rchild = insert_node(root->rchild, node);
}
else if (node->val < root->val)
{
root->lchild = insert_node(root->lchild, node);
}
root->hight = max(get_hight(root->lchild), get_hight(root->rchild)) + 1;
root->bf = get_hight(root->lchild) - get_hight(root->rchild);
if (root->bf == 2 && root->lchild->bf == 1)
{
return right_rotate(root);
}
if (root->bf == 2 && root->lchild->bf == -1)
{
root->lchild = left_rotate(root->lchild);
return right_rotate(root);
}
if (root->bf == -2 && root->rchild->bf == -1)
{
return left_rotate(root);
}
if (root->bf == -2 && root->rchild->bf == 1)
{
root->rchild = right_rotate(root->rchild);
return left_rotate(root);
}
return root;
}
BSTNode *del_node(BSTNode *root, int e)
{
if ( !root )
{
return NULL;
}
if (e > root->val)
{
root->rchild = del_node(root->rchild, e);
}
else if (e < root->val)
{
root->lchild = del_node(root->lchild, e);
}
else
{
if (root->lchild == NULL && root->rchild == NULL)
{
free(root);
root= NULL;
}
else if (root->lchild)
{
BSTNode *temp = root->lchild;
while (temp->rchild)
{
temp = temp->rchild;
}
root->val = temp->val;
root->lchild = del_node(root->lchild, temp->val);
}
else
{
BSTNode *temp = root->rchild;
while (temp->lchild)
{
temp = temp->lchild;
}
root->val = temp->val;
root->rchild = del_node(root->rchild, temp->val);
}
}
if ( !root )
{
return NULL;
}
root->hight = max(get_hight(root->lchild), get_hight(root->rchild)) + 1;
root->bf = get_hight(root->lchild) - get_hight(root->rchild);
if (root->bf == 2 && root->lchild->bf == 1)
{
return right_rotate(root);
}
if (root->bf == 2 && root->lchild->bf == -1)
{
root->lchild = left_rotate(root->lchild);
return right_rotate(root);
}
if (root->bf == -2 && root->rchild->bf == -1)
{
return left_rotate(root);
}
if (root->bf == -2 && root->rchild->bf == 1)
{
root->rchild = right_rotate(root->rchild);
return left_rotate(root);
}
return root;
}
#endif
queue.h
#ifndef _QUEUE_H
#define _QUEUE_H
#include <stdio.h>
#include <stdlib.h>
#include "avl_tree.h"
typedef BSTNode* type;
typedef struct Node{
type val;
struct Node *next;
}Node;
typedef struct queue{
int size;
Node *front;
Node *rear;
}queue;
Node *new_queue_node(type e)
{
Node *node = (Node *)malloc(sizeof(Node));
node->val = e;
node->next = NULL;
}
void queue_init(queue *q)
{
q->size = 0;
q->front = q->rear = NULL;
}
int queue_isempty(queue *q)
{
return q->size == 0;
}
void queue_push(queue *q, type e)
{
Node *node = new_queue_node(e);
if (q->front == NULL)
{
q->front = q->rear = node;
}
else
{
q->rear->next = node;
q->rear = node;
}
q->size++;
}
void queue_pop(queue *q)
{
if (queue_isempty(q))
{
return ;
}
Node *temp = q->front;
q->front = q->front->next;
free(temp);
temp = NULL;
q->size--;
}
#endif
main.c //测试程序
#include <stdio.h>
#include "queue.h"
#include "avl_tree.h"
void seqenc_traversal(BSTNode *root)
{
if ( !root )
{
return;
}
int i;
queue *q = (queue *)malloc(sizeof(queue));
queue_init(q);
queue_push(q, root);
while ( !queue_isempty(q) )
{
int len = q->size;
for (i = 0; i < len; i++)
{
BSTNode *temp = q->front->val;
printf("%d ", temp->val);
if (temp->lchild)
{
queue_push(q, temp->lchild);
}
if (temp->rchild)
{
queue_push(q, temp->rchild);
}
queue_pop(q);
}
}
}
int main()
{
BSTNode *root;
root = insert_node(root, new_node(100));
root = insert_node(root, new_node(50));
root = insert_node(root, new_node(150));
root = insert_node(root, new_node(75));
root = insert_node(root, new_node(25));
root = insert_node(root, new_node(0));
root = insert_node(root, new_node(30));
root = insert_node(root, new_node(35));
root = insert_node(root, new_node(33));
root = insert_node(root, new_node(34));
root = del_node(root, 50);
seqenc_traversal(root);
printf("\n");
}