平衡二叉树

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

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

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

//
// 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?:max(root->leftChild->rightChild->leftHeight,root->leftChild->rightChild->rightHeight)+; root->leftHeight = max(root->leftChild->leftHeight,root->leftChild->rightHeight)+;
} 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?:max(root->rightChild->leftChild->leftHeight,root->rightChild->leftChild->rightHeight)+;
root->rightHeight = max(root->rightChild->leftHeight,root->rightChild->rightHeight)+;
} 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 = ;
root->rightHeight = ;
return;
} if(key < root->key)
{
Insert(root->leftChild,key,value);
root->leftHeight = max(root->leftChild->leftHeight,root->leftChild->rightHeight)+;
if(root->leftHeight > root->rightHeight+)
{
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)+; if(root->leftHeight<root->rightHeight-)
{
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, , );
Insert(root, , );
Insert(root, , );
Insert(root, , );
printf("%d",Get(root,));
printf("%d",Get(root,));
printf("%d",Get(root,));
//Insert(root, 4, 5); return ;
}

手写AVL 树(上)的更多相关文章

  1. 手写AVL 树(下)

    上一篇 手写AVL树上实现了AVL树的插入和查询 上代码: 头文件:AVL.h #include <iostream> template<typename T1,typename T ...

  2. 手写AVL平衡二叉搜索树

    手写AVL平衡二叉搜索树 二叉搜索树的局限性 先说一下什么是二叉搜索树,二叉树每个节点只有两个节点,二叉搜索树的每个左子节点的值小于其父节点的值,每个右子节点的值大于其左子节点的值.如下图: 二叉搜索 ...

  3. AVL树的理解及自写AVL树

    AVL树是最先发明的自平衡二叉查找树.在AVL树中任何节点的两个子树的高度最大差别为一,所以它也被称为高度平衡树.查找.插入和删除在平均和最坏情况下都是O(log n).增加和删除可能需要通过一次或多 ...

  4. Android+TensorFlow+CNN+MNIST 手写数字识别实现

    Android+TensorFlow+CNN+MNIST 手写数字识别实现 SkySeraph 2018 Email:skyseraph00#163.com 更多精彩请直接访问SkySeraph个人站 ...

  5. 用BP人工神经网络识别手写数字

    http://wenku.baidu.com/link?url=HQ-5tZCXBQ3uwPZQECHkMCtursKIpglboBHq416N-q2WZupkNNH3Gv4vtEHyPULezDb5 ...

  6. 一步一步写平衡二叉树(AVL树)

    平衡二叉树(Balanced Binary Tree)是二叉查找树的一个进化体,也是第一个引入平衡概念的二叉树.1962年,G.M. Adelson-Velsky 和 E.M. Landis发明了这棵 ...

  7. 一看就懂的K近邻算法(KNN),K-D树,并实现手写数字识别!

    1. 什么是KNN 1.1 KNN的通俗解释 何谓K近邻算法,即K-Nearest Neighbor algorithm,简称KNN算法,单从名字来猜想,可以简单粗暴的认为是:K个最近的邻居,当K=1 ...

  8. JDK动态代理深入理解分析并手写简易JDK动态代理(上)

    原文同步发表至个人博客[夜月归途] 原文链接:http://www.guitu18.com/se/java/2019-01-03/27.html 作者:夜月归途 出处:http://www.guitu ...

  9. django 常用方法总结 < 手写分页-上传头像-redis缓存,排行 ...>

    1.不使用自带模块<Paginator>的手写分页功能views.pydef post_list(request): page = request.GET.get('page', 1) # ...

随机推荐

  1. C# System.Threading.AutoResetEvent

    表示线程同步事件在一个等待线程释放后收到信号时自动重置. using System; using System.Threading; // Visual Studio: Replace the def ...

  2. Linux内存、性能诊断中vmstat命令的详解

    vmstat命令是最常见的Linux/Unix监控工具,可以展现给定时间间隔的服务器的状态值,包括服务器的CPU使用率,内存使用,虚拟内存 交换情况,IO读写情况.相比top,通过vmstat可以看到 ...

  3. AI 名校课程&书籍 需要学习

    斯坦福李飞飞-深度学习计算机视觉 http://study.163.com/course/introduction/1003223001.htm 斯坦福李飞飞-深度学习计算机视觉---视频下载 htt ...

  4. AI习惯的数学书籍、计算机经典书籍

    http://download.csdn.net/download/wz619899442/8405297 https://www.amazon.com/Introduction-Automata-T ...

  5. vs code 设置问题

    现已取消 .vue 文件与 HTML 的默认关联,需要手动配置.vue 文件里不能使用div + Tab 键快速生成 html 代码   "emmet.syntaxProfiles" ...

  6. JQuery DataTables Selected Row

    获取单行选中的值 $('#MonitoringTypeTable tbody').on('click', 'tr', function () { if ($(this).hasClass('selec ...

  7. redhat 7.5 更换 yum源

    因为 redhat 的 yum 是收费,所以需要换成 Centos 的 yum 才可以 首先,卸载 redhat 的 yum 软件 sudo rpm -qa|grep yum 其次,下载 Centos ...

  8. WordPress 安装插件导致 HTTP 500 内部服务器错误的问题

      春节这几天忙着过节,一直没有看网站,今天登陆上来看到插件有更新,点开更新后,悲剧发生了.页面就无法加载,出现错误无法加载了,着实让我慌了慌(想到重来就郁闷) Chrome:该网页无法正常工作www ...

  9. Glide和Govendor安装和使用

    两个都是Go的包管理工具,二选一 Glide参考:golang 依赖管理 /etc/profile #Go export GOROOT=/home/lintong/software/go export ...

  10. 同时执行多个$.getJSON() 数据混乱的问题的解决

    在执行之前加$.ajaxSettings.async = false; (同步执行)执行你的代码之后及时恢复为$.ajaxSettings.async = true: (异步执行)不然影响别的地方的需 ...