首页 > 其他 > 详细

AVL-tree 平衡二叉树代码实现

时间:2014-03-06 13:21:19      阅读:454      评论:0      收藏:0      [点我收藏+]

In computer science, an AVL tree (Adelson-Velskii and Landis‘ tree, named after the inventors) is a self-balancing binary search tree. It was the first such data structure to be invented.In an AVL tree, the heights of the two child subtrees of any node differ by at most one; if at any time they differ by more than one, rebalancing is done to restore this property. Lookup, insertion, and deletion all take O(log n) time in both the average and worst cases, where n is the number of nodes in the tree prior to the operation. Insertions and deletions may require the tree to be rebalanced by one or moretree rotations.

The AVL tree is named after its two Soviet inventors, G. M. Adelson-Velskii and E. M. Landis, who published it in their 1962 paper "An algorithm for the organization of information".

AVL trees are often compared with red-black trees because both support the same set of operations and take O(log n) time for the basic operations. For lookup-intensive applications, AVL trees are faster than red-black trees because they are more rigidly balanced. Similar to red-black trees, AVL trees are height-balanced. Both are in general not weight-balanced nor μ-balanced for any bubuko.com,布布扣; That is, sibling nodes can have hugely differing numbers of descendants.

我的代码实现:

avl_tree.h

#ifndef _avl_TREE_H
#define _avl_TREE_H 1

//declaration for node
struct node
{
	int data;
	int height;
	int balance_factor;
	struct node* parent;
	struct node* rightchild;
	struct node* leftchild;
};

#include "avl_insert_node.h"
#include "avl_height.h"
#include "print_tree.h"
#include "single_rotation_with_left.h"
#include "single_rotation_with_right.h"
#include "double_rotation_with_left.h"
#include "double_rotation_with_right.h"

#endif

avl_tree.c

/*****************************************************************************************
code writer : EOF
code date : 2014.03.01
e-mail: jasonleaster@gmail.com

code purpose:
			This is the main functin of this program. Just modify a avl-tree and print out 
			the data we writed in.
If something could be changed into better situation, please touch me by e-mail. Thank you!

******************************************************************************************/
#include <stdio.h>
#include "avl_tree.h"

#define ARRAY_SIZE 10

int main()
{
	int array[ARRAY_SIZE] = {2,4,23,15,98,34,66,75,41,17};
	int temp = 0;
	
	struct node* root;
	root = NULL;

	for(temp = 0;temp < ARRAY_SIZE;temp++)
	{
		root =  avl_insert_node(&root,NULL,array[temp]);
	}

	if(root == NULL)
	{
		printf("insert failed!\nProcess end!\n");
	}

	print_tree(root);

	return 0;
}

avl_height.c

/************************************************************************
code writer :EOF
data : 2014.02.28
code purpose:
	My implementation for avl_height function
	
	This function would return current node‘s height and if current 
node is NULL, -1 would be returned.

	If this node is not NULL ,return height of the node,else return -1.
*************************************************************************/
#include "avl_tree.h"
#include "stdio.h"

int avl_height(struct node* p_node)
{
	if(p_node != NULL)
	{
		return p_node->height; //If current node is not NULL, we return the height of current node.
	}
	else
	{
		return -1;// If current node is NULL, we assume that the height of current node is -1.
	}
}
avl_insert_node.c

/*****************************************************************************************
code writer : EOF
code date : 2014.03.01
e-mail: jasonleaster@gmail.com

code purpose:
		implementation of function avl_insert_node
If something could be changed into better situation, please touch me by e-mail. Thank you!

******************************************************************************************/
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include "avl_tree.h"

struct node* avl_insert_node(struct node** pp_node, struct node* p_parent,int number)
{
	if((*pp_node) == NULL)
	{
		(*pp_node) = (struct node*)malloc(sizeof(struct node));
		if((*pp_node) == NULL)
		{
			printf("malloc faild!\n");
			return 0;
		}

		//initilization for new node
		(*pp_node)->data = number;
		(*pp_node)->height = 0;
		(*pp_node)->parent = p_parent;
		(*pp_node)->rightchild = NULL;
		(*pp_node)->leftchild  = NULL;
		
	}
	else if((*pp_node)->data > number)//right_rotation
	{
		(*pp_node)->leftchild = avl_insert_node(&((*pp_node)->leftchild),(*pp_node),number);
		//Now, we have inserted a node and interrupt the balance of the tree. So, we should rebalance the tree.What we should do is rotation.
		
		(*pp_node)->height = (avl_height((*pp_node)->leftchild) > avl_height((*pp_node)->rightchild) ?				      avl_height((*pp_node)->leftchild) : avl_height((*pp_node)->rightchild))+1;
		
		if(abs(avl_height((*pp_node)->rightchild) - avl_height((*pp_node)->leftchild))  == 2)
		{
			if((*pp_node)->leftchild->data > number)
			{
				(*pp_node) = single_rotation_with_right(*pp_node);
			}
			else if((*pp_node)->leftchild->data < number)
			{
				(*pp_node) = double_rotation_with_right(*pp_node);
			}
		}
	}
	else if((*pp_node)->data < number)//left_rotation
	{
		
		(*pp_node)->rightchild = avl_insert_node(&((*pp_node)->rightchild),(*pp_node),number);
		
		(*pp_node)->height = (avl_height((*pp_node)->leftchild) > avl_height((*pp_node)->rightchild) ?				      avl_height((*pp_node)->leftchild) : avl_height((*pp_node)->rightchild))+1;
	
		if(abs(avl_height((*pp_node)->rightchild) - avl_height((*pp_node)->leftchild))  == 2)
		{
			if((*pp_node)->rightchild->data < number)
			{
				(*pp_node) = single_rotation_with_left(*pp_node);
			}
			else if((*pp_node)->rightchild->data > number)		
			{
				(*pp_node) = double_rotation_with_left(*pp_node);
			}
		}
	}
	
	return (*pp_node);
}
double_rotation_with_left.c

/******************************************************************************
code writer : EOF
code date : 2014.03.01
e-mail : jasonleaster@gmail.com
code purpose :
		implementation of function double_rotation_with_left
If something could be changed into a situation, please touch me. Thank you!

*******************************************************************************/

#include "avl_tree.h"
#include "stdio.h"

struct node* double_rotation_with_left(struct node* p_node)
{
	struct node* temp = NULL;

	p_node = single_rotation_with_left(p_node);
	
	return  single_rotation_with_right(p_node);
}

double_rotation_with_right.c

/************************************************************************************
code writer : EOF
code date : 2014.03.01
e-mail : jasonleaster@gmail.com
code purpose :
		implementation of function double_rotation_with_right
If something could be changed into a better situation, please touch me. Thank you!
**********************************************************************************/
#include "avl_tree.h"

struct node* double_rotation_with_right(struct node* p_node)
{

	p_node = single_rotation_with_right(p_node);

	return single_rotation_with_left(p_node);
}
single_rotation_with_left.c

/*********************************************************************
code writer: EOF
code date: 2014.02.28
e-mail: jasonleaster@gmail.com
code purpose:
	implementation of function single_rotation_with_left

	If something could be changed into better situation, please 
touche me by e-mail

**********************************************************************/

#include "avl_tree.h"

struct node* single_rotation_with_left(struct node* p_node)
{	
//when there are only three nodes in the tree, we make the middile node -- *p_node as the root-node.
	struct node* temp = p_node->rightchild;
	
	p_node->rightchild = temp->leftchild;
	temp->leftchild = p_node;

	p_node->height = (avl_height(p_node->rightchild) > avl_height(p_node->leftchild) ?		   	  avl_height(p_node->rightchild) : avl_height(p_node->leftchild))+1;
	
	temp->height = (avl_height(temp->rightchild) > avl_height(temp->leftchild) ?			avl_height(temp->rightchild) : avl_height(temp->leftchild))+1;

	return temp;//new root	
}
single_rotation_with_right.c

/*********************************************************************************************
code writer : EOF
code date : 2014.03.01
e-mail : jasonleaster@gmail.com
code purpose :
		implementation of function single_rotation_with_right
If something could be changed into a better situation, please touch me. Thank you!

*********************************************************************************************/
#include "avl_tree.h"

struct node* single_rotation_with_right(struct node* p_node)
{
	struct node*  temp;
	temp = p_node->leftchild;
	p_node->leftchild = temp->rightchild;
	temp->rightchild = p_node;

	p_node->height = (avl_height(p_node->rightchild) > avl_height(p_node->leftchild) ? 			  avl_height(p_node->rightchild) : avl_height(p_node->leftchild))+1;
	
	temp->height = (avl_height(temp->leftchild) > avl_height(temp->rightchild) ? 			avl_height(temp->leftchild) : avl_height(temp->rightchild))+1;	
	return temp;
}

print_tree.c
/*******************************************************************************
* code writer: EOF
* Date : 2014.02.20
* code purpose:
		This code is the definition for function -- print_tree
*e-mail:jasonleaster@gmail.com

If there somthing wrong with my code, please touch me by e-mail.Thank you!
*****************************************************************************/
#include "avl_tree.h"
#include "stdio.h"

int print_tree(struct node* p_node)// use recursion to print out the data in the binary tree
{
	// print the left side of the binary tree
	if((p_node->leftchild) != NULL)
	{
		print_tree(p_node->leftchild);
	}

	printf("%d\n",p_node->data);

	// print the right side of the binary tree
	if((p_node->rightchild) != NULL)
	{
		print_tree(p_node->rightchild);
	}
	//printf("%d\n",p_node->data); we don‘t need this 
}

I don‘t want to show all the header files. It is meaningless. But here is a simple model:

#ifndef _SINGLE_ROTATION_WITH_LEFT_H
#define _SINGLE_ROTATION_WITH_LEFT_H 1

	extern struct node* single_rotation_with_left(struct node* p_node);

#endif
Namely, every source file has their own header file.



AVL- tree is here.





















AVL-tree 平衡二叉树代码实现,布布扣,bubuko.com

AVL-tree 平衡二叉树代码实现

原文:http://blog.csdn.net/cinmyheart/article/details/20527037

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!