AVL树实现文件C语言(AVLTree.c)
2010-10-01 01:35
561 查看
/* AVLTree.c -- AVL树实现文件 */ #include <stdio.h> #include <stdlib.h> #include "AVLTree.h" /* 局部函数声明 */ static int Left_Above_And_Beyond_Right (const Item left, const Item right) ; static int Left_Is_Less_Than_Right (const Item left, const Item right) ; static Position Make_Node (const Item item) ; static void Copy_Item_To_Node (Node * * pnode, const Item * const pitem) ; static int Height_Count (Position position) ; static Position Single_Rotate_With_Left (Position K2) ; static Position Single_Rotate_With_Right (Position K2) ; static Position Double_Rotate_With_Left (Position K3) ; static Position Double_Rotate_With_Right (Position K3) ; static Max (const int num, const int the_other_num) ; /* 接口函数定义 */ void InitializeAVLTree (Tree * ptree) { *ptree = NULL ; } int AVLTreeIsEmpty (const Tree tree) { return NULL == tree ; } Position Find (const Tree tree, const Item item) { if (NULL == tree) return NULL ; else if (Left_Above_And_Beyond_Right (item, tree -> item)) return Find (tree -> right, item) ; else if (Left_Is_Less_Than_Right (item, tree -> item)) return Find (tree -> left, item) ; /* 找到该结点 */ else return tree ; } Position FindMax (const Tree tree) { if (NULL == tree) return NULL ; else if (tree -> right != NULL) return FindMax (tree -> right) ; else return tree ; } Position FindMin (const Tree tree) { if (NULL == tree) return NULL ; else if (tree -> left != NULL) return FindMin (tree -> left) ; else return tree ; } Tree Insert (Tree tree, const Item item) { if (NULL == tree) { tree = Make_Node (item) ; if (NULL == tree) return NULL ; } else if (Left_Above_And_Beyond_Right (item, tree -> item)) { tree -> right = Insert (tree -> right, item) ; if (2 == Height_Count (tree -> right) - Height_Count (tree -> left)) { if (Left_Above_And_Beyond_Right (item, tree -> right -> item)) tree = Single_Rotate_With_Right (tree) ; else tree = Double_Rotate_With_Right (tree) ; } } else if (Left_Is_Less_Than_Right (item, tree -> item)) { tree -> left = Insert (tree -> left, item) ; if (2 == Height_Count (tree -> left) - Height_Count (tree -> right)) { if (Left_Is_Less_Than_Right (item, tree -> left -> item)) tree = Single_Rotate_With_Left (tree) ; else tree = Double_Rotate_With_Left (tree) ; } } tree -> height = Max (Height_Count (tree -> left), Height_Count (tree -> right)) + 1 ; return tree ; } void PostorderTraverse (const Tree tree, void (* pfun) (const Position position)) { if (tree != NULL) { PostorderTraverse (tree -> left, pfun) ; (* pfun) (tree) ; PostorderTraverse (tree -> right, pfun) ; } } Tree Delete (Tree tree, const Item item) { Position temp ; if (NULL == tree) /* 其实可以什么都不做, 因为下面有返回.这里满足条件返回为了体现思维的逻辑性 */ return NULL ; else if (Left_Above_And_Beyond_Right (item, tree -> item)) tree -> right = Delete (tree -> right, item) ; else if (Left_Is_Less_Than_Right (item, tree -> item)) tree -> left = Delete (tree -> left, item) ; else if (tree -> left && tree -> right) { temp = FindMin (tree -> right) ; Copy_Item_To_Node (&tree, &(temp -> item)) ; tree -> right = Delete (tree -> right, tree -> item) ; } else { temp = tree ; if (NULL == tree -> left) tree = tree -> right ; else if (NULL == tree -> right) tree = tree -> left ; free (temp) ; } if (tree != NULL) { tree -> height = Max (Height_Count (tree -> left), Height_Count(tree -> right)) + 1 ; if (2 == Height_Count (tree -> left) - Height_Count (tree -> right)) { if (NULL == tree -> left -> right) tree = Single_Rotate_With_Left (tree) ; else if (NULL == tree -> left -> left) tree = Double_Rotate_With_Left (tree) ; /* 左右全为不为NULL的情况下, 执行双旋转. 双旋转使树的数据分布更紧凑 */ else tree = Double_Rotate_With_Left (tree) ; } else if (2 == Height_Count (tree -> right) - Height_Count (tree -> left)) { if (NULL == tree -> right -> left) tree = Single_Rotate_With_Right (tree) ; else if (NULL == tree -> right -> right) tree = Double_Rotate_With_Right (tree) ; else tree = Double_Rotate_With_Right (tree) ; } } return tree ; } void EmptyTheAVLTree (Tree tree) { if (tree != NULL) { EmptyTheAVLTree (tree -> left) ; EmptyTheAVLTree (tree -> right) ; free (tree) ; } } /* 局部函数定义 */ static int Left_Above_And_Beyond_Right (const Item left, const Item right) { return left > right ; } static int Left_Is_Less_Than_Right (const Item left, const Item right) { return left < right ; } static Position Make_Node (const Item item) { Position new_node ; new_node = (Position) malloc (sizeof (Node)) ; if (NULL == new_node) return NULL ; else { Copy_Item_To_Node (&new_node, &item) ; new_node -> height = 0 ; new_node -> left = new_node -> right = NULL ; return new_node ; } } static void Copy_Item_To_Node (Node * * pnode, const Item * const pitem) { (*pnode) -> item = *pitem ; } static int Height_Count (Position position) { if (NULL == position) return -1 ; else return position -> height ; } static Position Single_Rotate_With_Left (Position K2) { Position K1 ; K1 = K2 -> left ; K2 -> left = K1 -> right ; K1 -> right = K2 ; K2 -> height = Max (Height_Count (K2 -> left), Height_Count (K2 -> right)) + 1 ; K1 -> height = Max (Height_Count (K1 -> left), K2 -> height) + 1 ; return K1 ; } static Position Single_Rotate_With_Right (Position K2) { Position K1 ; K1 = K2 -> right ; K2 -> right = K1 -> left ; K1 -> left = K2 ; K2 -> height = Max (Height_Count (K2 -> left), Height_Count (K2 -> right)) + 1 ; K1 -> height = Max (Height_Count (K1 -> right), K2 -> height) + 1 ; return K1 ; } static Position Double_Rotate_With_Left (Position K3) { K3 -> left = Single_Rotate_With_Right (K3 -> left) ; return Single_Rotate_With_Left (K3) ; } static Position Double_Rotate_With_Right (Position K3) { K3 -> right = Single_Rotate_With_Left (K3 -> right) ; return Single_Rotate_With_Right (K3) ; } static Max (const int num, const int the_other_num) { return num >= the_other_num ? num : the_other_num ; }
相关文章推荐
- AVL树头文件C语言(AVLTree.h)
- 关于C++类库KYLib: 用C语言实现平衡二叉树(AVL tree)的源码
- 伸展树实现文件C语言(Aplaytree.c)
- 二叉搜索树实现文件C语言(BinarySearchTree.c)
- 【证明可用】平衡二叉树(avl tree)的C语言实现
- AVL树的C++实现 Implement of AVL tree
- C语言实现文件复制
- 链表实现文件C语言
- linux下c语言使用libcurl和实现文件上传
- C语言 文件I/O:实现文件打开创建复制关闭
- PCM WAVE格式详解及用C语言实现wave文件的读取
- 开放定址散列表(平方探测法)实现文件C语言
- C语言 以数据块的形式读写文件详解及实现代码
- 用c语言实现读取配置文件源码
- Linux下使用socket传输文件的C语言简单实现
- 基于POSIX在Linux下C语言实现读取整个目录文件[Get a List of Files in a Directory with C]
- AVL树的旋转、插入、删除及遍历C语言实现
- C语言实现修改文本文件中特定行的实现代码(百度一天没找到,bing搜索十分钟就找到了)
- 数据结构与算法(C#实现)系列---AVLTree(二)
- Atitit。Tree文件解析器的原理流程与设计实现 java c# php js