前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >手写AVL 树

手写AVL 树

作者头像
ShenduCC
发布2019-04-01 14:59:53
4780
发布2019-04-01 14:59:53
举报
文章被收录于专栏:算法修养算法修养

平衡二叉树

左旋,右旋,左右旋,右左旋

具体原理就不说了,网上教程很多。这里只实现了建树的过程,没有实现删除节点的操作。

下一篇会实现删除节点的操作。

代码语言:javascript
复制
//
//  main.cpp
//  AVL
//
//  Created by 小康 on 2019/3/30.
//  Copyright © 2019 小康. All rights reserved.
//
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

using namespace std;

struct Tree
{
    Tree* leftChild;
    Tree* rightChild;
    int key;
    int value;
    int leftHeight;
    int rightHeight;
};

int num;
void LeftSpin(Tree*& root)
{
    Tree* temp = root;
    Tree* temp2 = root->rightChild->leftChild;
    root = temp->rightChild;
    root->leftChild = temp;
    temp->rightChild = temp2;
    
    root->leftChild->rightHeight = root->leftChild->rightChild==NULL?0:max(root->leftChild->rightChild->leftHeight,root->leftChild->rightChild->rightHeight)+1;
    
    root->leftHeight = max(root->leftChild->leftHeight,root->leftChild->rightHeight)+1;
}

void RightSpin(Tree*& root)
{
    Tree* temp = root;
    Tree* temp2 = root->leftChild->rightChild;
    
    root = temp->leftChild;
    root->rightChild = temp;
    temp->leftChild = temp2;
    
    root->rightChild->leftHeight =root->rightChild->leftChild==NULL?0:max(root->rightChild->leftChild->leftHeight,root->rightChild->leftChild->rightHeight)+1;
    root->rightHeight = max(root->rightChild->leftHeight,root->rightChild->rightHeight)+1;
}

void LeftRightSpin(Tree*& root)
{
    LeftSpin(root->leftChild);
    RightSpin(root);
}

void RightLeftSpin(Tree*& root)
{
    RightSpin(root->rightChild);
    LeftSpin(root);
}

void Insert(Tree*& root,int key,int value)
{
    if(root==NULL)
    {
        root = new Tree;
        root->leftChild=NULL;
        root->rightChild=NULL;
        root->key = key;
        root->value = value;
        root->leftHeight = 0;
        root->rightHeight = 0;
        return;
    }
   
    if(key < root->key)
    {
        Insert(root->leftChild,key,value);
        root->leftHeight = max(root->leftChild->leftHeight,root->leftChild->rightHeight)+1;
        if(root->leftHeight > root->rightHeight+1)
        {
            if(root->leftChild ->leftHeight > root->leftChild->rightHeight)
            {
                RightSpin(root);
            }
            else if(root->leftChild->leftHeight < root->leftChild->rightHeight)
            {
                LeftRightSpin(root);
            }
        }
    }
    else{
        Insert(root->rightChild, key,value);
        root->rightHeight = max(root->rightChild->leftHeight,root->rightChild->rightHeight)+1;
        
        if(root->leftHeight<root->rightHeight-1)
        {
            if(root->rightChild->rightHeight > root->rightChild->leftHeight)
            {
                LeftSpin(root);
            }
            else if(root->rightChild->rightHeight<root->rightChild->leftHeight)
            {
                RightLeftSpin(root);
            }
        }
    }
}

int Get(Tree* root,int key)
{
    if(key<root->key)
    {
       return Get(root->leftChild,key);
    }
    if(key>root->key)
    {
       return Get(root->rightChild,key);
    }
   
    return root->value;
    
}

int main()
{
    
    Tree* root = NULL ;
    
    Insert(root, 1, 1);
    Insert(root, 2, 2);
    Insert(root, 3, 3);
    Insert(root, 4, 4);
    printf("%d",Get(root,1));
    printf("%d",Get(root,2));
    printf("%d",Get(root,3));
    //Insert(root, 4, 5);
    
    
    
    return 0;
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-03-30 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档