zoukankan      html  css  js  c++  java
  • Algorithms: 二叉平衡树(AVL)

    二叉平衡树(AVL):

      这个数据结构我在三月份学数据结构结构的时候遇到过。但当时没调通。也就没写下来。前几天要用的时候给调好了!详细AVL是什么,我就不介绍了,维基百科都有。

     后面两月又要忙了。和同学组队去比赛,预计博客这边也不常写了。等这段时间过了再继续更新!

     这是我第一次画电路图(原理图)晒晒,事实上我对电子非常感兴趣的。看着网上人家做的电子作品。就想自己也做做。兴奋的想试试。呵呵,以后我做电子小作品了也把他放到博客,开源和大家一起分享。DIY的乐趣。

     

    第一次正儿八经会电路图,别笑啊!

    /*------------------------------------------------------------------------------------
     * Project: AVLTree.h
     * Name: zwp
     * Date: 2014/3
     *------------------------------------------------------------------------------------*/
    
    
    #ifndef AVLTREE_H_
    #define AVLTREE_H_
    
    
    
    typedef int ElementType;
    
    
    typedef struct AvlNode
    {
    	ElementType Element;
    	AvlNode*	Left;
    	AvlNode*    	Right;
    	int			Height;
    
    }*AvlTree;
    
    
    
    /*
    ** 初始化
    */
    AvlTree Initialize(void);
    
    /*
    ** 遍历树
    */
    void Traverse(AvlTree H);
    
    
    /*
    ** 清空元素
    */
    void MakeEmpty(AvlTree H);
    
    
    /*
    ** 寻找元素
    */
    AvlTree Find(ElementType X, AvlTree T);
    
    /*
    ** 找最大
    */
    AvlTree FindMax(AvlTree T);
    
    /*
    ** 找最小
    */
    AvlTree FindMin(AvlTree T);
    
    
    /*
    ** 插入元素
    */
    AvlTree Insert(ElementType X, AvlTree T);
    
    
    /*
    ** 删除元素
    */
    AvlTree Delete(ElementType X, AvlTree T);
    
    
    
    
    
    #endif


     

    /*-----------------------------------------------------------------------------
     * Project: AVLTree.cpp
     * Name: zwp
     * Date: 2014/3
     *------------------------------------------------------------------------------*/
    
    
    #include "AVLTree.h"
    #include <stdio.h>
    #include <stdlib.h>
    #include <malloc.h>
    
    
    
    /*
    ** 返回树的高度
    */
    static int Height(AvlTree P)
    {
    	if(P == NULL)
    		return -1;
    	else
    		return P->Height;
    }
    
    
    /*
    ** 返回最大值
    */
    int Max(int num1, int num2)
    {
    	return ((num1 > num2) ?

    num1 : num2); } /* ** 右旋转 */ static AvlTree SingleRotateWithLeft(AvlTree K2) { AvlTree K1; K1 = K2->Left; K2->Left = K1->Right; K1->Right = K2; K2->Height = Max(Height(K2->Left), Height(K2->Right)) + 1; K1->Height = Max(Height(K1->Left), K2->Height) + 1; return K1; } /* ** 左旋转 */ static AvlTree SingleRotateWithRight(AvlTree K2) { AvlTree K1; K1 = K2->Right; K2->Right = K1->Left; K1->Left = K2; K2->Height = Max(Height(K2->Left), Height(K2->Right)) + 1; K1->Height = Max(Height(K1->Right), K2->Height) + 1; return K1; } /* ** 右双旋转 */ static AvlTree DoubleRotateWithLeft(AvlTree K3) { /* Rotate between K1 and K2 * K2 Left Rotate */ K3->Left = SingleRotateWithRight(K3->Left); /* Rotate between K3 and K2 * K2 Right Rotate */ return SingleRotateWithLeft(K3); } /* ** 左双旋转 */ static AvlTree DoubleRotateWithRight(AvlTree K3) { /* Rotate between K1 and K2 * K2 Right Rotate */ K3->Right = SingleRotateWithLeft(K3->Right); /* Rotate between K3 and K2 * K2 Left Rotate */ return SingleRotateWithRight(K3); } AvlTree Insert(ElementType X, AvlTree T) { if(T == NULL) { /* Create and return a one-node tree */ T = (AvlTree)malloc(sizeof(struct AvlNode)); if(T == NULL) printf("Out of space... "); else { T->Element = X; T->Height = 0; T->Left = T->Right = NULL; } } else if(X < T->Element) /* 插入树的左子枝 */ { T->Left = Insert(X, T->Left); /* 左子树的高度与右子树高度相差2 */ if(Height(T->Left) - Height(T->Right) == 2) { if(X < T->Left->Element) T = SingleRotateWithLeft(T); /* CASE : left-left sing rotate Right */ else T = DoubleRotateWithLeft(T); /* CASE : left-right double rotate Right */ } } else if(X > T->Element) /* 插入树的右子枝 */ { T->Right = Insert(X, T->Right); /* 右子树的高度与左子树高度相差2 */ if(Height(T->Right) - Height(T->Left) == 2) { if(X > T->Right->Element) T = SingleRotateWithRight(T); /* CASE: right-right sing rotate Left */ else T = DoubleRotateWithRight(T); /* CASE: right-left double rotate Left */ } } /* * else X is in the tree already * +1 because root Node */ T->Height = Max(Height(T->Left), Height(T->Right)) + 1; return T; } /* ** 初始化 */ AvlTree Initialize(void) { /* Create and return a one-node tree */ AvlTree T = (AvlNode*)malloc(sizeof( struct AvlNode)); T->Element = 0; T->Height = 0; T->Left = T->Right = NULL; return T; } /* ** 寻找元素 */ AvlTree Find(ElementType X, AvlTree T) { if(T == NULL) return NULL; if(X < T->Element) return Find(X, T->Left); else if(X > T->Element) return Find(X, T->Right); else return T; } /* ** 找最大 */ AvlTree FindMax(AvlTree T) { ElementType max = 0; AvlTree P = T; /* Find in AVLTree Right */ while(P != NULL) { if(max < P->Element) { max = P->Element; P = P->Right; } else P = P->Right; } return P; } /* ** 找最小 */ AvlTree FindMin(AvlTree T) { ElementType min = 0; AvlTree P = T; while(P != NULL) { if(min > P->Element) { min = P->Element; P = P->Left; } else P = P->Left; } return P; } /* ** 删除元素 */ AvlTree Delete(ElementType X, AvlTree T) { AvlTree TmpCell; if(T == NULL) printf("Elememt not found.... "); else if(X < T->Element) T->Left = Delete(X, T->Left); /* Go Left */ else if(X > T->Element) T->Right = Delete(X, T->Right); /* Go Right */ else if(T->Left && T->Right) { /* ** Two children ** Replace with smallest in right subtree */ TmpCell = FindMin(T->Right); T->Element = TmpCell->Element; T->Height = TmpCell->Height; T->Right = Delete(T->Element, T->Right); } else { /* ** One or zero children */ TmpCell = T; if(T->Left == NULL) T = T->Right; else if(T->Right == NULL) T = T->Left; free(TmpCell); } return T; } /* ** 清空元素 */ void MakeEmpty(AvlTree H) { if(H != NULL) { MakeEmpty(H->Left); MakeEmpty(H->Right); free(NULL); } } /* ** 遍历树 */ void Traverse(AvlTree H) { if(H != NULL) { Traverse(H->Left); printf("%d ", H->Element); Traverse(H->Right); } }


     

    /*-----------------------------------------------------------------------------
     * Project: Main.cpp
     * Name: zwp
     * Date: 2014.3
     *------------------------------------------------------------------------------*/
    
    
    
    #include "AVLTree.h"
    #include <stdio.h>
    #include <stdlib.h>
    
    
    
    
    int main(int argc, char* argv[])
    {
    
    	AvlTree T =  Initialize();
    
    	for(int index = 0; index < 10; ++ index)
    		T = Insert(index, T);
    
    	
    
    
    	Traverse(T);
    
    
    	//printf("%d 
    ", Find(3, T));
    
    	system("pause");
    
    }
    
    


     

  • 相关阅读:
    搜索框用定时器限制发送请求
    vue的生命周期,钩子函数
    事件委托的实现流程
    在vscode中快速生成vue模板
    JS继承
    各种宽高
    ES6新特性
    python入门学习一
    字符编码
    npm install --save 与 npm install --save-dev 的区别
  • 原文地址:https://www.cnblogs.com/mqxnongmin/p/10535145.html
Copyright © 2011-2022 走看看