平衡二叉树(AVL)

平衡二叉树和AVL 目录一、AVL二、平衡二叉树三、代码实现四、测试五、AVL的左旋转和右旋转(1)维护平衡的时机(2)插入的元素在不平衡节点的左侧的左侧右旋代码实现(3)插入的元素在不平衡节点的右侧的右侧左旋代码实现六、LR和RL1、**插入的元素不在平衡节点的左侧的右侧**2、**插入的元素在不平衡结点的左侧的右侧**代码实现七、AVL树的删除八、测试九、完整代码 平衡二叉树和AVL树是在二分搜索树的基础上进行讲解 回忆二分搜索树的问题: 添加数据 :是按照顺序添加的 32145 每个节点的左节点小于根节点,每个节点的右 阅读详情

1、概述
详细的说明
需要说明的是二种重要的旋转方式:
左旋:旧的根节点为新的根节点的左子树
新的根节点的左子树为旧的根节点的右子树
右旋:旧的根节点为新的根节点的右子树
新的根节点的右子树为旧的根节点的左子树
重点说明四种失衡二叉树的解决方式:
1)左左
当根节的左子树的左孩子添加了新的节点,导致二叉树为失衡二叉树。解决方式:
左旋
2)右右
当根节点的右子树的右孩子加入新节点导致二叉树失衡。解决方式:
右旋
3)左右
当根节的左孩子的右子节点加入新的节点,导致二叉树失衡。解决方式:对根节的左子树进行左旋,然后对根节点进行右旋。
左右旋转
4)右左
当根节点的右孩子的左子节点加入新的节点,导师二叉树失衡。
解决方式:对根节点的右子树进行右旋,接着对根节点进行左旋。
在这里插入图片描述
2、程序实现
1).cpp文件

#include "stdafx.h"
#include"AVLTree.h"

int _tmain(int argc, _TCHAR* argv[])
{
	AvlTree<int>tree;
	tree.InsertNode(11);
	tree.InsertNode(7);
	tree.InsertNode(15);
	tree.InsertNode(3);
	tree.InsertNode(9);

	tree.InsertNode(8);
	return 0;
}

2).h文件用于具体的实现

#include<iostream>
#include<memory.h>

int Max(int a, int b)
{
	if (a>b)
	{
		return a;
	}
	return b;
}

template<class T>
class AvlTree
{
	struct TreeNode //节点
	{
		T data;//数据
		int height;//高度
		TreeNode* pleftChild;//左子树
		TreeNode* prightChild;//右子树
	};
public:
	AvlTree()
	{
		pRoot = 0;
	}
	void InsertNode(const T&insertData);//插入节点
private:
	TreeNode* pRoot;//根节点
	void _InsertNode(TreeNode**pNode, const T&insertData);
	int height(TreeNode* pNode);//计算树的高度

	void Rotateleft(TreeNode** pNode);//左旋
	void Rotateright(TreeNode** pNode);//右旋
	void RotateLeftRight(TreeNode** pNode);
	void RotateRightLeft(TreeNode** pNode);
};

template<class T>
int AvlTree<T>::height(TreeNode*pNode)
{
	if (pNode)
	{
		return pNode->height;
	}
	return -1;
}

template<class T>
void AvlTree<T>::InsertNode(const T&insertData)
{
	_InsertNode(&(pRoot), insertData);
}

template<class T>
void AvlTree<T>::_InsertNode(TreeNode**pNode, const T& insertData)
{
	TreeNode* root = *pNode;

	if (!root)
	{
		TreeNode* pNewNode = new TreeNode;
		memset(pNewNode, 0x00, sizeof(TreeNode));
		pNewNode->data = insertData;

		*pNode = pNewNode;
	}
	else
	{
		if (insertData>root->data)//插入值比当前节点的数据大
		{
			_InsertNode(&(root->pleftChild), insertData);//在该节点的左子树上

			if (height(root->prightChild) - height(root->pleftChild)>=2)//此时成为失衡二叉树
			{
				if (insertData>root->pleftChild->data)//插入节点的数据大于最底下一个左子树的数
				{
					Rotateleft(&root);//左旋转
				}
				else
				{
					RotateRightLeft(&root);//右左旋
				}
				*pNode = root;
			}
		}
		if (insertData < root->data)//插入值比当前节点的数据小
		{
			_InsertNode(&(root->pleftChild), insertData);
			if (height(root->prightChild) - height(root->pleftChild) >= 2)//失衡
			{
				if (insertData<root->pleftChild->data)
				{
					Rotateright(&root);//右旋
				}
				else
				{
					RotateLeftRight(&root);//左右旋
				}
				*pNode = root;
			}
		}
	}
	(*pNode)->height = Max(height((*pNode)->pleftChild), height((*pNode)->prightChild)) + 1;
}

//左旋转:旧的根节点作为新的根节点的左子树
       //新的根节点的左子树为旧根节点的右子树
template<class T>
void AvlTree<T>::Rotateleft(TreeNode** pNode)
{
	TreeNode* pRoot = *pNode;
	TreeNode* pTemp = pRoot;//将原来的根节点保存在临时的节点中

	pRoot = pRoot->prightChild;//新的根节点右子树为新的根节点
	pTemp->prightChild = pRoot->pleftChild;//新的根节点的左子树为旧的根节点的右子树
	pRoot->pleftChild = pTemp;//旧的根节点为新根节点的左子树

	*pNode = pRoot;

	pTemp->height = Max(height(pTemp->pleftChild), height(pTemp->prightChild)) + 1;//重新计算旋转之后的节点
	pRoot->height = Max(height(pTemp->pleftChild), height(pTemp->prightChild)) + 1;
}

//右旋转:旧的根节点作为新的根节点的右子树
//新的根节点的右子树为旧根节点的左子树
template<class T>
void AvlTree<T>::Rotateright(TreeNode** pNode)
{
	TreeNode* pRoot = *pNode;
	TreeNode* pTemp = pRoot;//将原来的根节点保存在临时的节点中

	pRoot = pRoot->pleftChild;//新的根节点右子树为新的根节点
	pTemp->pleftChild = pRoot->prightChild;//新的根节点的右子树为旧的根节点的左子树
	pRoot->prightChild = pTemp;//旧的根节点为新根节点的右子树

	*pNode = pRoot;

	pTemp->height = Max(height(pTemp->pleftChild), height(pTemp->prightChild)) + 1;
	pRoot->height = Max(height(pTemp->pleftChild), height(pTemp->prightChild)) + 1;
}

template<class T>
void AvlTree<T>::RotateLeftRight(TreeNode** pNode)
{
	Rotateleft(&((*pNode)->pleftChild));
	Rotateright(pNode);
}

template<class T>
void AvlTree<T>::RotateRightLeft(TreeNode** pNode)
{
	Rotateright(&((*pNode)->prightChild));
	Rotateleft(pNode);
}
平衡二叉树AVL的c++实现 1 先创建节点类,就是一个普通的节点类的创建,用结构体也可以,唯一值的注意的是要有一个表示当前节点高度的成员,因为节点高度是判断平衡二叉树是否平衡的标志。#include &lt;iostream&gt; using namespace std;template&lt;class T&gt;class TreeNode{public:    T val;    TreeNode&lt;T&gt;*... 阅读详情

相关推荐

电动汽车整车控制器 UDS诊断功能开发调试优化方法

摘要:通过某电动汽车VCU开发过程中,UDS诊断功能的调试,发现一些测试中存在的问题,比如覆盖度不够、效率低下等,提出了相应的优化方法,并经过测试验证其可行性,此方法亦可推广应用至BMS、HCM等其他控制器的UDS诊断开发调试过程中。希望能给相关技术人员带来参考和帮助。

qq_43011586的博客 2142

平衡二叉树(AVL)

** 数据 **//** 相对高度 **//** 父节点 **//** 左子树 **//** 右子树 **/

青年有志 935

缺陷检测:使用PatchCore训练自己的数据集

必须包含有训练图片(无缺陷图片)、测试图片(缺陷图片)和ground_truth,并且ground_truth必须与对应图片的名称相同。同上一篇结果一样,我的输出为原始图片大小,但是不知道是不是因为图片不是正方形的,导致训练结果不好。编写一个脚本,重新规划数据集的路径规则,但是我稍微写复杂了一点,使用了类来操作。第二种(复杂):编写一个自己的Dataset,参考。第一种(简单):将数据集路径规则改成项目中。返回的必须是一个字典类型,可以阅读。本人此次使用的是第一种方法。运行时,参数部分直接输入。

xiaoxTai的博客 6476

平衡二叉树(AVL树)

一、平衡二叉树概述 1.1 什么是平衡二叉树 平衡二叉树也叫 AVL 树。平衡二叉树是具有以下特点的二叉查找树:它是一棵空树或它的左右两个子树的高度差的绝对值不超过 1, 并且左右两个子树都是一棵平衡二叉树。 图一 平衡二叉树 图二 非平衡二叉树 1.2 为什么要有平衡二叉树 在上一篇文章 二叉排序树 中对二叉排序树做了介

Ronz 的博客 7917

【FPGA教程案例100】深度学习1——基于CNN卷积神经网络的手写数字识别纯Verilog实现,使用mnist手写数字数据库

MNIST 数据库有一个 60,000 示例的训练集,还有一个 10,000 示例的测试集。这些数字已在大小方面规范化,在固定大小的图像中居中。如下图所示:我们任意选择0~9是个数字,将其转化为数字:0_0.txt1_0.txt2_0.txt3_0.txt4_0.txt5_0.txt6_0.txt7_0.txt8_0.txt9_0.txt。

FPGA/MATLAB学习教程/源码/项目合作开发 7824

基于MATLAB的拉盖尔-高斯光束叠加相位模拟

首先,我们需要设置模拟所需的参数,如光腰半径、传播距离、光束的角动量等。其中,(U)表示光束的电场复振幅,(r)和(\phi)分别是极坐标系下的径向和角向坐标,(z)是传播距离,(A)是归一化系数,(w_p)是光束的光腰半径,(L_p^q)是拉盖尔多项式,(k)是波数,(l)是光束的角动量。通过修改模拟参数,如光腰半径、传播距离、角动量等,您可以进一步探索和理解拉盖尔-高斯光束的叠加相位效应。拉盖尔-高斯光束是一种特殊的光束模式,具有广泛的应用领域,包括光学信息处理、激光加工和量子光学等。

m0_47037246的博客 945

C++——AVL平衡二叉树

AVL树是⼀颗⾼度平衡搜索⼆叉树,通过控制⾼度差去控制平衡。引⼊⼀个(balance factor)的概念,每个结点都有⼀个平衡因⼦,,也就是说任何结点的平衡因⼦等于0/1/-1,AVL树并不是必须要平衡因⼦,但是有了平衡因⼦可以更⽅便我们去进⾏观察和控制树是否平衡。⾼度可以控制在logN,那么增删查改的效率也可以控制在OlogN) ,相⽐⼆叉搜索树有了本质的提升。

曾几何时的博客 1171

ChatGPT搭配Claude 4写文献综述效果出奇的好!四步法直接写出逻辑清晰、层次分明的文献综述(附复制即用专业提示词)

请梳理【研究领域】中核心理论的发展演进过程,包括每一阶段理论的核心观点、提出背景与推动力,并指出理论演化中的关键节点或范式转变。请根据以下文献摘要内容,帮助我梳理该主题的研究趋势,包括:常见研究视角、研究方法的演变、研究对象的变化方向等。请根据以下研究空白与我已有的研究基础,帮我生成一个清晰的研究定位,包括:研究主题、研究对象、方法路径和理论框架。先要明确这个研究领域中一个主导或热门的理论,比如社会支持理论、认知负荷理论等,强调这个理论内部的不一致性、适用性争议,通过交代这⼀争议的背景,引发读者兴趣。

智能AI让学术科研更容易 568

AVL平衡二叉树

目录 什么是平衡二叉树,平衡因子  实现AVL树  基于AVL的映射map(接口在以前博客中) 基于AVL的集合set(接口在以前博客中)   什么是平衡二叉树,平衡因子          实现AVL树 package com.suanfa.AVLTree; import java.util.ArrayList; /** * AVL树,平衡二叉树,优化二分搜...

百味皆苦的博客 927

[xboard-ok210] 44 s5pv210开发板介绍和对比

文章目录前言四大S3C6410提供商友坚开发板UT-S5PV210华天正(润尔)real210飞凌开发板ok210九鼎x210友善之臂开发板tiny210mini210smart210天嵌开发板tq210sate210xboardxboardxboard之ok210 前言 四大S3C6410提供商 友坚开发板 UT-S5PV210 华天正(润尔) real210 飞凌开发板 ok210 九鼎 x210 友善之臂开发板 tiny210 mini210 smart210 天嵌开发板 tq210 sate210

鹰之翔 286

【自然语言处理】情感分析(一):基于 NLTK 的 Naive Bayes 实现

朴素贝叶斯(Naive Bayes)分类器可以用来确定输入文本属于某一组类别的概率。例如,预测评论是正面的还是负面的。它是 “朴素的”,它假设文本中的单词是独立的(但在现实的自然人类语言中,单词的顺序传达了上下文信息)。尽管有这些假设,但朴素贝叶斯在使用少量训练集预测类别时具有很高的准确性。

Data · Model · Agent · Repeat 2173

AVL平衡二叉树(二)

这是之前的AVL平衡二叉树的一个实现代码 点击打开链接 下面是看另一本书的实现的 没有了一重重的switch{case...},理解上不太一样,我觉得两种都可以。 //"avltree.h" class AVL_Tree; class AVLNode { friend AVL_Tree; public: AVLNode() {LeftChild = RightChild =

Kay's space 1745

第54节:vue3+cesiumjs

对之前的vue2+cesiumjs 进行了升级

qq_29384639的博客 62

平衡二叉树 AVL树

平衡二叉树平衡二叉树的定义最小不平衡子树平衡二叉树的实现原理算法实现参考文献 平衡二叉树的定义 平衡二叉树又称:Self-Balancing Binary Search Tree 或 Height-Balanced Binary Search Tree:高度平衡的二叉排序树。 平衡二叉树:是一种二叉排序树,它的每一个节点的左子树和右子树的高度差小于等于一。 由两位俄罗斯数学家 G M Adelso...

人工智能视觉分析算法学习实践和经验分享。 795

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

什么是平衡二叉树(AVL)

前言 Wiki:在计算机科学中,AVL树是最早被发明的自平衡二叉查找树。在AVL树中,任一节点对应的两棵子树的最大高度差为1,因此它也被称为高度平衡树。查找、插入和删除在平均和最坏情况下的时间复杂度都是{\displaystyle O(\log {n})} 。增加和删除元素的操作则可能需要借由一次或多次树旋转,以实现树的重新平衡。AVL 树得名于它的发明者 G. ...

程序员吴师兄的博客 1565

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

平衡二叉树AVL、哈夫曼树

文章目录定义平衡二叉树的插入AVL的最少结点 完全不知道写的什么东西 定义 首先,平衡二叉树也是二叉排序树(搜索树) 其次,AVL树的任何一个结点,左右子树的高度差的绝对值不超过1。 平衡二叉树可以是是空树 \qquad因为一般的二叉排序树,如果直接输入排序数据,就会产生一棵单支树,其平均查找长度为O(n)O(n)O(n)。 \qquad如果限制其每个结点的左右子树高度差不超过1,那么平均查找复杂度为O(logn)O(logn)O(logn)。 平衡因子:某节点的左子树与右子树的高度(深度)差即为该节点

weixin_43811173的博客 833

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

平衡二叉树AVL的创建

①定义 平衡二叉树:对于二叉树中任意节点,左子树的高度和右子树的高度差的绝对值不超过1.还有一点,平衡二叉树是一种位置高度平衡的二叉搜索树。二叉搜索树详见上一篇博客。 ②平衡二叉树的四种旋转 平衡二叉树的创建在于对于插入节点后,需要对插入之后的树进行维护高度。 维护高度的方法有四种:左旋,右旋,左右旋,右左旋。 树的结构 typedef struct Node *node; struct Node...

阿凡提的博客 297

平衡二叉树AVL

What 平衡二叉树AVL:平衡二叉树也叫平衡二叉搜索树(Self-balancing binary search tree),又被称为AVL树, 可以保证查询效率较高。它是一 棵空树或它的左右两个子树的高度差的绝对值不超过1,并且左右两个子树都是一棵平衡二叉树。平衡二叉树的常用实现方法有红黑树、AVL、替罪羊树、Treap、伸展树等 如图1中的第一棵树和第二棵树都是平衡二叉树...

c梦的博客 252

【C++】平衡二叉树(AVL树)迭代版

有语言基础和搜索树的细心看完,相信你有所收获!

weixin_64132124的博客 1056

平衡二叉树(AVL)定义及其旋转

平衡二叉树(AVL) 平衡二叉树(AVL),是一个二叉排序树,同时任意节点左右两个子树的高度差(或平衡因子,简称BF)的绝对值不超过1,并且左右两个子树也满足。 为什么使用平衡二叉树 通过二叉查找树的查找操作可以发现,一棵二叉查找树的查找效率取决于树的高度,如果使树的高度最低,那么树的查找效率也会变高。 如下面一个二叉树,全部由右子树构成 这个时候的二叉树其实就类似于链表,此时的查找时间复杂度为O(n),而AVL树的查找时间复杂度为O(logn)。之前讲过O(logn)耗时是小于O(n..

weixin_42640226的博客 1481

平衡二叉树 AVL

平衡二叉树 AVL 先弄清楚几个概念: 1)满二叉树:除了叶子节点,都是满的; 2)完全二叉树:整体而言,空缺的节点一定是位于树的右下方;整棵树的叶子节点最大深度值和最小深度值,相差不会超过1; 3)平衡二叉树:对于任意一个节点,左子树和右子树的高度差不能超过1。 1 平衡因子 通过平衡因子可以判断出是否为平衡二叉树,这决定了是否需要进行左旋转或右旋转操作。 第一步,标注节点的高度;节点的高度=...

喜欢吃冰棍de谷利文君的博客 1042
上一篇: 哈希表的实现
下一篇: 动态规划与背包问题
靠普科技
博客等级 码龄7年 14粉丝 20原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值