AVL树实现文件C语言(AVLTree.c)

/* 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树
    原文地址: https://blog.csdn.net/Golden_Shadow/article/details/5917497
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞