平衡二叉树(AVL树)C++代码实现

平衡二叉树(详细解释+完整C语言) 平衡二叉树的插入与删除操作的详细解释与完整C语言代码 阅读详情

平衡二叉树定义

为了使二叉排序树的平均查找长度更小,需要适当控制树高,显然,控制树高的一个有效措施就是尽量保持树的左右子树高度大致平衡,由此产生了平衡二叉树的概念。
G . M . A d e l s o n − V e l s k i i G.M.Adelson-Velskii G.M.Adelson−Velskii和 E . M . L a n d i s E.M.Landis E.M.Landis在1962年提出了一种平衡二叉树模型,故称为 A V L AVL AVL树。在未声明的情况下,平衡二叉树默认指 A V L 树 AVL树 AVL树。

A V L AVL AVL树是一棵二叉排序树,其或者为空,或者满足以下性质:
1)左右子树高度差的绝对值不大于1
2)左右子树都是平衡二叉树

定义平衡因子来研究AVL树:

结点的平衡因子 B F BF BF=结点的左子树高度-结点右子树高度

A V L AVL AVL树类的封装

struct BBTNode
{
	BBTNode*leftson;
	BBTNode*rightson;
	BBTNode*parent;
	int data;
	BBTNode(BBTNode*node1,BBTNode*node2,BBTNode*node3)
	{
		leftson = node1;
		rightson = node2;
		parent = node3;
	}
};

class BBT
{

public:
	BBT(int num);
	~BBT();
	void preorder();
	void insert(int element);
	bool find(int elemnet);
	void del(int element);
	void delall();

protected:
	void insert(int element, BBTNode*&node, BBTNode*&parent);
	int getB_factor(BBTNode*node);
	int getheight(BBTNode*node);
	void LL(BBTNode*&node);
	void LR(BBTNode*&node);
	void RR(BBTNode*&node);
	void RL(BBTNode*&node);
	void preorder(BBTNode*node);
	bool find(int element, BBTNode* node);
	void del(int element, BBTNode*&node);
	void del_directly(BBTNode*&node);
	void del_repalce(BBTNode*&node, BBTNode*&son);
	void del_reconnect(BBTNode*&node);
	void adjust(BBTNode*&node);
	void delall(BBTNode*&node);

private:
	BBTNode* root;
	int num;
};

A V L AVL AVL树的插入操作与平衡化

A V L AVL AVL树的平衡因子为-1,0或1,设计平衡二叉树操作算法的关键在于如何使得平衡二叉树保持平衡。以下四种情况下的插入操作可能会导致原有 A V L AVL AVL树发生不平衡:

  • (1) L L LL LL:若某一结点的平衡因子为1,当在其左子树的左子树上插入一个新结点时会导致该结点的平衡因子由1变为2
  • (2) R R RR RR:若某一节点的平衡因子为-1,当在其右子树的右子树上插入一个新结点会导致该结点的平衡因子由-1变为-2
  • (3) L R LR LR:若某一结点的平衡因子为1,当在其左子树的右子树上插入一个新结点时会导致该结点的平衡因子由1变为2
  • (4) R L RL RL:若某一节点的平衡因子为-1,当在其右子树的左子树上插入一个新的结点时会导致该结点的平衡因子由-1变成-2
    下面具体介绍如何调整与代码实现

L L LL LL型调整

判断条件:
最低不平衡点的平衡因子为2,新插入结点的值小于最低不平衡点的左儿子结点值
在这里插入图片描述

步骤一:将 A A A的左子树 B B B提升为新二叉树的根
步骤二:将原来的 A A A连同右子树 A L A_L AL​向下旋转,使其成为 B B B的右子树
步骤三:将B的原右子树 B R B_R BR​作为左子树连接到 A A A上

代码实现如下:
在这份代码中,参数node就是图中的 A A A,定义的son就是图中的 B B B,需要注意的是在处理node的父节点与son的关系时,如果node的父节点是NULL的话说明node即是根节点root,则需要将son更新为根节点root的操作,这点后面也有,后面将不再提及。

void BBT::LL(BBTNode*&node)
{
	BBTNode* son = node->leftson;
	son->parent = node->parent;
	if (node->parent == NULL) root = son;
	else
	{
		if (node->data < node->parent->data) node->parent->leftson = son;
		else node->parent->rightson = son;
	}
	if (son->rightson != NULL) son->rightson->parent = node;
	node->leftson = son->rightson;
	node->parent = son;
	son->rightson = node;
}

R R RR RR型调整

判断条件:
最低不平衡点的平衡因子为-2,新插入结点的值大于等于最低不平衡点的右儿子结点值
在这里插入图片描述
步骤一:将𝐴的右子树𝐵提升为新二叉树的根
步骤二:将原来的𝐴连同其左子树𝐴𝐿向下旋转,使其成为𝐵的左子树
步骤三:将𝐵的原左子树 B L B_L BL​作为左子树连接到𝐴上

void BBT::RR(BBTNode*&node)
{
	BBTNode*son = node->rightson;
	son->parent = node->parent;
	if (node->parent == NULL) root = son;
	else
	{
		if (node->data < node->parent->data) node->parent->leftson = son;
		else node->parent->rightson = son;
	}
	if(son->leftson!=NULL)son->leftson->parent = node;
	node->rightson = son->leftson;
	node->parent = son;
	son->leftson = node;
}

L R LR LR型调整

判断条件:
最低不平衡点的平衡因子为2,新插入结点的值大于等于最低不平衡点的左儿子结点值
在这里插入图片描述
首先执行RR旋转:
步骤一:将𝐵的右子树𝐶提升为新二叉树的“根”
步骤二:将原来的𝐵连同其左子树 B L B_L BL​向下旋转,使其成为𝐶的左子树
步骤三:将𝐶的原左子树 C L C_L CL​作为右子树连接到𝐵上然后再执行LL旋转:
步骤四:将𝐴的左子树𝐶提升为新二叉树的根
步骤五:将原来的𝐴连同其右子树 A R A_R AR​向下旋转,使其成为𝐶的右子树
步骤六:将𝐶的原右子树 C R C_R CR​作为左子树连接到𝐴上

void BBT::LR(BBTNode*&node)
{
	BBTNode*son = node->leftson;
	BBTNode*grandson = son->rightson;
	grandson->parent = node->parent;
	if (node->parent == NULL) root = grandson;
	else
	{
		if (node->data < node->parent->data) node->parent->leftson = grandson;
		else node->parent->rightson = grandson;
	}
	if(grandson->leftson!=NULL) grandson->leftson->parent = son;
	if(grandson->rightson!=NULL) grandson->rightson->parent = node;
	son->parent = grandson;
	son->rightson = grandson->leftson;
	grandson->leftson = son;
	node->leftson = grandson->rightson;
	node->parent = grandson;
	grandson->rightson = node;
}

R L RL RL型调整

判断条件:
最低不平衡点的平衡因子为-2,新插入结点的值小于最低不平衡点的右儿子结点值
在这里插入图片描述
首先执行LL旋转
步骤一:将𝐵的左子树𝐶提升为新二叉树的“根”
步骤二:将原来的𝐵连同其右子树 B R B_R BR​向下旋转,使其成为𝐶的右子树
步骤三:将𝐶的原右子树 C R C_R CR​作为左子树连接到𝐵上
然后再执行RR旋转
步骤四:将𝐴的右子树𝐶提升为新二叉树的根
步骤五:将原来的𝐴连同其左子树 A L A_L AL​向下旋转,使其成为𝐶的左子树
步骤六:将𝐶的原左子树 C L C_L CL​作为右子树连接到𝐴上

void BBT::RL(BBTNode*&node)
{
	BBTNode*son = node->rightson;
	BBTNode*grandson = son->leftson;
	grandson->parent = node->parent;
	if (node->parent == NULL) root = grandson;
	else
	{
		if (node->data < node->parent->data) node->parent->leftson = grandson;
		else node->parent->rightson = grandson;
	}
	if(grandson->leftson!=NULL) grandson->leftson->parent = node;
	if(grandson->rightson!=NULL) grandson->rightson->parent = son;
	node->parent = grandson;
	node->rightson = grandson->leftson;
	son->parent = grandson;
	son->leftson = grandson->rightson;
	grandson->leftson = node;
	grandson->rightson = son;
}

了解了4种调整方式便可以编写建树操作或插入操作了。
建树主要是调用插入函数,插入函数的逻辑与一般的二叉排序树相同,都是采用递归插入。只不过多了判断是否平衡,若不平衡则调整。

BBT::BBT(int num)
{
	this->num = num;
	root = new BBTNode(NULL, NULL, NULL);
	for (int i = 1; i <= num; i++)
	{
		int element;
		cin >> element;
		if (i == 1)
		{
			root->data = element;
			continue;
		}
		insert(element, root, root->parent);
	}
}
void BBT::insert(int element, BBTNode*&node, BBTNode*&parent)
{
	if (node == NULL)
	{
		BBTNode*s = new BBTNode(NULL, NULL, NULL);
		s->data = element;
		s->parent = parent;
		node = s;
		BBTNode* p = node->parent;
		int factor = getB_factor(p);
		while (factor>=-1&& factor <=1)
		{
			p = p->parent;
			if (p == NULL) break;
			factor = getB_factor(p);
		}
		if (factor == 2)
		{
			if (element < p->leftson->data) LL(p);
			else LR(p);
		}
		if (factor == -2)
		{
			if (element < p->rightson->data) RL(p);
			else RR(p);
		}
	}
	else if (element >= node->data)
		insert(element, node->rightson, node);
	else
		insert(element, node->leftson, node);

}

void BBT::insert(int element)
{
	if (num == 0)
	{
		root = new BBTNode(NULL, NULL, NULL);
		root->data = element;
	}
	else insert(element, root, root->parent);
	num++;
}

A V L AVL AVL树查找操作

查找操作与一般的二叉排序树相同,采用递归查找。

bool BBT::find(int elemnet)
{
	return find(elemnet, root);
}

bool BBT::find(int element, BBTNode*node)
{
	if (node == NULL) return false;
	if (node->data == element) return true;
	if (node->data > element) return find(element, node->leftson);
	if (node->data < element) return find(element, node->rightson);
}

A V L AVL AVL树删除结点操作

这一节以如图所示平衡二叉树举例
在这里插入图片描述
删除结点主要有三种方法,三种方法基于三种情况。但都需要进行调整以维护树的平衡,都调用了adjust函数,这个在后面会阐述具体实现。
情形1:待删除结点是叶子结点,例如删除图中值为50的结点。采用直接删除法,只需delete和断绝其与父节点关系。

void BBT::del_directly(BBTNode*&node)
{
	BBTNode* p = new BBTNode(NULL, NULL, NULL);
	p = node;
	BBTNode* s = p->parent;
	if (p->data < s->data) s->leftson = NULL;
	else s->rightson = NULL;
	delete p;
	adjust(s);
}

情形2:待删除结点的度为1,即只有一个子结点,例如图中的值为60的结点。采用顶替法,将待删除结点的子结点放到待删除结点原来的位置上,并与待删除结点的父节点建立关系。

void BBT::del_repalce(BBTNode*&node, BBTNode*&son)
{
	BBTNode*p = new BBTNode(NULL, NULL, NULL);
	p = node;
	BBTNode* s = p->parent;
	if (p->data < s->data) s->leftson = son;
	else s->rightson = son;
	son->parent = s;
	delete p;
	adjust(s);
}

情形3:待删除结点的度为2,即有2个子结点,采用重新连接法。实现原理为找到比待删除结点值大或相等的最小值的结点。将该结点放置到待删除结点的位置上。寻找的过程就是寻找待删除结点的右子树的最左下方的结点。
例如:删除图中值为90的结点,我们采用重新连接法找到的结点是100。删除图中值为110的结点,我们采用重新连接法找到的结点是120。
细心的朋友可以发现我在这里举了2个例子,是的,因为找到的结点可以分为两种情况。
实际上,情形3是可以转化为前2个情形的,我们大可不必删除我们原来要删除的结点,只需要将找到的结点的值付给原来要删除的结点,这也是一种变相的删除。
但是删除还是不可避免的,我们现在需要删除的是我们找到的结点。
这就可以归类为前两个情形了。
情形3-1:找到的结点是叶子结点,则符合情形1,直接删除。
情形3-2:找到的结点是其父节点的右孩子,则符合情形2,将找到的结点的子树替代我们通过重新连接法找到的结点的位置。

void BBT::del_reconnect(BBTNode*&node)
{
	BBTNode*p = new BBTNode(NULL, NULL, NULL);
	p = node;
	p = p->rightson;
	while (p->leftson != NULL) p = p->leftson;
	node->data = p->data;
	BBTNode* s = p->parent;
	if (p->leftson==NULL&&p->rightson==NULL)  del_directly(p);
	else del_repalce(p, p->rightson);
	adjust(s);
}

三份代码中都有adjust函数,现在就来讲讲这个adjust函数。由于删除节点后,不能保证整个树依然处于平衡状态,所以需要由被删除结点位置自下而上进行检查,进行调整。
有人可能有疑问了,这样的话是不是直接把插入时的调整部分的代码拿来copy就可以了?
并不是这样,在那个部分,我们是根据新插入结点的值来判断究竟是什么类型的不平衡情况的。
所以,这里给出一种新的不平衡情况的判断:

  • L L LL LL:某一节点的平衡因子为2,其左儿子结点的平衡因子为1
  • L R LR LR:某一节点的平衡因子为2,其左儿子结点的平衡因子为-1
  • R R RR RR:某一节点的平衡因子为-2,其右儿子结点的平衡因子为-1
  • R L RL RL:某一节点的平衡因子为-2,其左儿子结点的平衡因子为1
void BBT::adjust(BBTNode*&s)
{
	while (s != NULL)
	{
		int factor = getB_factor(s);
		if (factor == 2)
		{
			if (getB_factor(s->leftson) == 1) LL(s);
			else LR(s);
		}
		if (factor == -2)
		{
			if (getB_factor(s->rightson) == 1) RL(s);
			else RR(s);
		}
		s = s->parent;
	}
}

删除操作的代码为:

void BBT::del(int element)
{
	del(element, root);
}

void BBT::del(int element, BBTNode*& node)
{
	if (num == 0||node==NULL) return;
	if (num == 1)
	{
		delete root;
		return;
	}
	if (node->data == element)
	{
		if (node->leftson == NULL && node->rightson == NULL)       del_directly(node);
		else if (node->leftson == NULL && node->rightson != NULL)  del_repalce(node, node->rightson);
		else if (node->leftson != NULL && node->rightson == NULL)  del_repalce(node, node->leftson);
		else  del_reconnect(node);
		num--;
		return;
	}
	if (node->data > element) del(element, node->leftson);
	if (node->data < element) del(element, node->rightson);
}

总源代码

#include<iostream>
#include<algorithm>
using namespace std;


struct BBTNode
{
	BBTNode*leftson;
	BBTNode*rightson;
	BBTNode*parent;
	int data;
	BBTNode(BBTNode*node1,BBTNode*node2,BBTNode*node3)
	{
		leftson = node1;
		rightson = node2;
		parent = node3;
	}
};

class BBT
{

public:
	BBT(int num);
	~BBT();
	void preorder();
	void insert(int element);
	bool find(int elemnet);
	void del(int element);
	void delall();

protected:
	void insert(int element, BBTNode*&node, BBTNode*&parent);
	int getB_factor(BBTNode*node);
	int getheight(BBTNode*node);
	void LL(BBTNode*&node);
	void LR(BBTNode*&node);
	void RR(BBTNode*&node);
	void RL(BBTNode*&node);
	void preorder(BBTNode*node);
	bool find(int element, BBTNode* node);
	void del(int element, BBTNode*&node);
	void del_directly(BBTNode*&node);
	void del_repalce(BBTNode*&node, BBTNode*&son);
	void del_reconnect(BBTNode*&node);
	void adjust(BBTNode*&node);
	void delall(BBTNode*&node);

private:
	BBTNode* root;
	int num;
};

void BBT::LL(BBTNode*&node)
{
	BBTNode* son = node->leftson;
	son->parent = node->parent;
	if (node->parent == NULL) root = son;
	else
	{
		if (node->data < node->parent->data) node->parent->leftson = son;
		else node->parent->rightson = son;
	}
	if (son->rightson != NULL) son->rightson->parent = node;
	node->leftson = son->rightson;
	node->parent = son;
	son->rightson = node;
}

void BBT::LR(BBTNode*&node)
{
	BBTNode*son = node->leftson;
	BBTNode*grandson = son->rightson;
	grandson->parent = node->parent;
	if (node->parent == NULL) root = grandson;
	else
	{
		if (node->data < node->parent->data) node->parent->leftson = grandson;
		else node->parent->rightson = grandson;
	}
	if(grandson->leftson!=NULL) grandson->leftson->parent = son;
	if(grandson->rightson!=NULL) grandson->rightson->parent = node;
	son->parent = grandson;
	son->rightson = grandson->leftson;
	grandson->leftson = son;
	node->leftson = grandson->rightson;
	node->parent = grandson;
	grandson->rightson = node;
}

void BBT::RR(BBTNode*&node)
{
	BBTNode*son = node->rightson;
	son->parent = node->parent;
	if (node->parent == NULL) root = son;
	else
	{
		if (node->data < node->parent->data) node->parent->leftson = son;
		else node->parent->rightson = son;
	}
	if(son->leftson!=NULL)son->leftson->parent = node;
	node->rightson = son->leftson;
	node->parent = son;
	son->leftson = node;
}

void BBT::RL(BBTNode*&node)
{
	BBTNode*son = node->rightson;
	BBTNode*grandson = son->leftson;
	grandson->parent = node->parent;
	if (node->parent == NULL) root = grandson;
	else
	{
		if (node->data < node->parent->data) node->parent->leftson = grandson;
		else node->parent->rightson = grandson;
	}
	if(grandson->leftson!=NULL) grandson->leftson->parent = node;
	if(grandson->rightson!=NULL) grandson->rightson->parent = son;
	node->parent = grandson;
	node->rightson = grandson->leftson;
	son->parent = grandson;
	son->leftson = grandson->rightson;
	grandson->leftson = node;
	grandson->rightson = son;
}

int BBT::getheight(BBTNode*node)
{
	if (node == NULL) return 0;
	return max(getheight(node->leftson), getheight(node->rightson)) + 1;
}

int BBT::getB_factor(BBTNode*node)
{
	return getheight(node->leftson) - getheight(node->rightson);
}

void BBT::insert(int element, BBTNode*&node, BBTNode*&parent)
{
	if (node == NULL)
	{
		BBTNode*s = new BBTNode(NULL, NULL, NULL);
		s->data = element;
		s->parent = parent;
		node = s;
		BBTNode* p = node->parent;
		int factor = getB_factor(p);
		while (factor>=-1&& factor <=1)
		{
			p = p->parent;
			if (p == NULL) break;
			factor = getB_factor(p);
		}
		if (factor == 2)
		{
			if (element < p->leftson->data) LL(p);
			else LR(p);
		}
		if (factor == -2)
		{
			if (element < p->rightson->data) RL(p);
			else RR(p);
		}
	}
	else if (element >= node->data)
		insert(element, node->rightson, node);
	else
		insert(element, node->leftson, node);

}

BBT::BBT(int num)
{
	this->num = num;
	root = new BBTNode(NULL, NULL, NULL);
	for (int i = 1; i <= num; i++)
	{
		int element;
		cin >> element;
		if (i == 1)
		{
			root->data = element;
			continue;
		}
		insert(element, root, root->parent);
	}
}

void BBT::preorder(BBTNode*node)
{
	if (node == NULL) return;
	cout << node->data << endl;
	preorder(node->leftson);
	preorder(node->rightson);
}

void BBT::preorder()
{
	preorder(root);
}

void BBT::insert(int element)
{
	if (num == 0)
	{
		root = new BBTNode(NULL, NULL, NULL);
		root->data = element;
	}
	else insert(element, root, root->parent);
	num++;
}

bool BBT::find(int element, BBTNode*node)
{
	if (node == NULL) return false;
	if (node->data == element) return true;
	if (node->data > element) return find(element, node->leftson);
	if (node->data < element) return find(element, node->rightson);
}

bool BBT::find(int elemnet)
{
	return find(elemnet, root);
}

void BBT::adjust(BBTNode*&s)
{
	while (s != NULL)
	{
		int factor = getB_factor(s);
		if (factor == 2)
		{
			if (getB_factor(s->leftson) == 1) LL(s);
			else LR(s);
		}
		if (factor == -2)
		{
			if (getB_factor(s->rightson) == 1) RL(s);
			else RR(s);
		}
		s = s->parent;
	}
}

void BBT::del_directly(BBTNode*&node)
{
	BBTNode* p = new BBTNode(NULL, NULL, NULL);
	p = node;
	BBTNode* s = p->parent;
	if (p->data < s->data) s->leftson = NULL;
	else s->rightson = NULL;
	delete p;
	adjust(s);
}

void BBT::del_repalce(BBTNode*&node, BBTNode*&son)
{
	BBTNode*p = new BBTNode(NULL, NULL, NULL);
	p = node;
	BBTNode* s = p->parent;
	if (p->data < s->data) s->leftson = son;
	else s->rightson = son;
	son->parent = s;
	delete p;
	adjust(s);
}

void BBT::del_reconnect(BBTNode*&node)
{
	BBTNode*p = new BBTNode(NULL, NULL, NULL);
	p = node;
	p = p->rightson;
	while (p->leftson != NULL) p = p->leftson;
	node->data = p->data;
	BBTNode* s = p->parent;
	if (p->leftson==NULL&&p->rightson==NULL)  del_directly(p);
	else del_repalce(p, p->rightson);
	adjust(s);
}

void BBT::del(int element, BBTNode*& node)
{
	if (num == 0||node==NULL) return;
	if (num == 1)
	{
		delete root;
		return;
	}
	if (node->data == element)
	{
		if (node->leftson == NULL && node->rightson == NULL)       del_directly(node);
		else if (node->leftson == NULL && node->rightson != NULL)  del_repalce(node, node->rightson);
		else if (node->leftson != NULL && node->rightson == NULL)  del_repalce(node, node->leftson);
		else  del_reconnect(node);
		num--;
		return;
	}
	if (node->data > element) del(element, node->leftson);
	if (node->data < element) del(element, node->rightson);
}

void BBT::del(int element)
{
	del(element, root);
}

void BBT::delall(BBTNode*&node)
{
	if (node == NULL) return;
	delall(node->leftson);
	delall(node->rightson);
	BBTNode* s = new BBTNode(NULL, NULL, NULL);
	s = node;
	delete s;
}

void BBT::delall()
{
	delall(root);
	num = 0;
}

BBT::~BBT()
{
	delall();
}

int main()
{
	//test data:100 90 80 60 70 50 120 110 150 87
	BBT Tree(10);
	Tree.insert(30);
	Tree.preorder();
	cout << Tree.find(30) << endl;
	cout << Tree.find(999) << endl;
	//Tree.del(90);
	Tree.del(110);
	Tree.preorder();
	return 0;
}
平衡二叉树(代码) #include<stdio.h> #include <stdlib.h> #include <stdbool.h> typedef struct TreeNode { int data; struct TreeNode* left; struct TreeNode* right; }TreeNode; TreeNode* create_node(int data) { TreeNode* node = malloc(sizeof(TreeNode)); n 阅读详情

相关推荐

平衡二叉排序树(完整案例详解及完整C代码实现)

写在前面:博主是一位普普通通的19届双非软工在读生,平时最大的爱好就是听听歌,逛逛B站。博主很喜欢的一句话花开堪折直须折,莫待无花空折枝:博主的理解是头一次为人,就应该做自己想做的事,做自己不后悔的事,做自己以后不会留有遗憾的事,做自己觉得有意义的事,不浪费这大好的青春年华。博主写博客目的是记录所学到的知识并方便自己复习,在记录知识的同时获得部分浏览量,得到更多人的认可,满足小小的成就感,同时在写博客的途中结交更多志同道合的朋友,让自己在技术的路上并不孤单。 目录: 1.平衡二叉树简介 2.二叉排序.

记录博主学到的点滴 6259

数据结构之——平衡二叉树代码实现

一、结构定义 public class BalanceTree { //根节点 private Node root; /** * 静态内部类,定义节点。 */ private static class Node { //数据域 private int data; //父节点 ...

m0_37914588的博客 4944

平衡二叉树AVL的c++实现

1 先创建节点类,就是一个普通的节点类的创建,用结构体也可以,唯一值的注意的是要有一个表示当前节点高度的成员,因为节点高度是判断平衡二叉树是否平衡的标志。#include &lt;iostream&gt; using namespace std;template&lt;class T&gt;class TreeNode{public:    T val;    TreeNode&lt;T&gt;*...

qq_36101436的博客 3107

C/C++数据结构(十一)—— 平衡二叉树(AVL树)

平衡二叉树又称为 AVL 树,其实就是一颗 平衡的二叉搜索树,解决了二叉搜索树的不平衡问题。

Edison's 小宇宙 2万+

QML 地图修改插件源码(三),Map在Plugin中设置加载地图类型

常用的地图种类分为交通图,地形图,卫星图等等,在QML的Map(以OSM地图为例)中提供activeMapType属性用于读取当前显示的地图类型(注意:该属性为只读属性,不能用于赋值),QML中地图的加载寻找根据瓦片的文件名进行查找,例如名为"osm_100-l-5-2-2-3.png"的瓦片,其中5代表了该地图的类型

zjgo007的专栏 3168

ESP32实战:破解校园网WPA2-Enterprise认证的完整指南

本文详细介绍了如何使用ESP32开发板破解校园网WPA2-Enterprise认证的完整指南。通过硬件准备、环境搭建、核心代码实现及常见问题排查,帮助开发者解决企业级WiFi连接难题,特别适用于校园网环境下的物联网设备开发。

weixin_29194817的博客 462

C++实现平衡二叉树(AVL树)完整代码

C++实现平衡二叉树(AVL树)完整代码

LTZAD的博客 237

Ubuntu 22.04下Realtek R8125网卡驱动安装避坑指南(附DKMS自动编译配置)

本文详细介绍了在Ubuntu 22.04系统上为Realtek R8125 2.5GbE网卡安装驱动的完整流程与避坑指南。针对内核通用驱动r8169的冲突问题,重点讲解了如何通过DKMS(Dynamic Kernel Module Support)实现驱动的持久化自动编译与安装,确保系统内核升级后网卡驱动依然有效。内容包括环境准备、驱动源码获取、DKMS标准配置、驱动冲突解决(设置黑名单)以及日常维护与故障排查方法,旨在为用户提供一个一劳永逸的稳定解决方案。

echo99的专栏 1030

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

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

weixin_64132124的博客 1063

从原理到应用:深入解析双编码器在机器人关节力矩感知中的实现

本文深入解析了双编码器方案在机器人关节力矩感知中的实现原理与应用。该方案通过在电机侧和输出侧分别安装编码器,利用两者位置差异间接高精度计算外力,克服了传统电流环精度不足和扭矩传感器成本高昂的缺点。它为实现协作机器人的丝滑拖动示教、灵敏碰撞检测以及足式机器人的动态平衡与柔顺控制提供了关键技术支撑,是目前高性能机器人力控的主流选择。

t8u9v0w1x的博客 667

手把手教你用STM32F103C8T6单片机,做一个即插即用的USB游戏手柄(附完整代码)

本文详细介绍了如何使用STM32F103C8T6单片机制作即插即用的USB游戏手柄,涵盖硬件搭建、HID协议解析及完整代码实现。通过深入讲解USB HID协议的七大关键描述符,帮助开发者快速掌握嵌入式USB设备开发技巧,打造个性化游戏控制器。

weixin_30887919的博客 443

C++ 实现平衡二叉树(AVL树)(完整代码)

#include <iostream> using namespace std; typedef int KeyType; class AVLNode { friend class AVLTree; public: AVLNode() :lchild(nullptr), rchild(nullptr) {}; private: KeyType key; int bf; AVLNode *lchild; AVLNode *rchild; }; class AVLTree { pub

m0_51955470的博客 1375

避坑指南:在Ubuntu 20.04上从零部署IS-Fusion环境(含CUDA 11.1和PyTorch 1.10.1配置)

本文提供了在Ubuntu 20.04系统上从零部署IS-Fusion 3D目标检测环境的详细避坑指南。内容涵盖CUDA 11.1、PyTorch 1.10.1、spconv等核心组件的精确版本安装与兼容性配置,并指导如何准备nuScenes数据集及启动模型训练,旨在帮助研究者和开发者高效搭建稳定的多模态融合算法开发环境。

gitlab7runner的博客 556

平衡二叉树(AVL树)创建、查找、插入操作 《大话数据结构》 c++实现代码

//平衡二叉树,或者称为AVL树 #include&lt;iostream&gt; using namespace std; typedef int status; #define true 1 #define false 0 #define LH +1 //左高 #define EH 0 //等高 #define RH -1 //右高 //二叉链表结点结构定义 ...

m0_38047231的博客 1360

MATLAB基础应用精讲-【强化学习】Actor - Critic 算法(附python代码实现)

这种架构结合了策略梯度方法(Policy - Gradient Methods)和价值函数估计方法(Value Function Estimation Methods)的优点。在强化学习中,智能体(Agent)需要通过与环境(Environment)的交互来学习最优的行为策略,以获得最大的累积奖励(Cumulative Reward)。

qq_36130719的博客 377

QLineEdit控件: 实现IP地址输入框(基于编辑信号)

在开发网络应用程序时,经常需要用户输入IP地址。直接使用普通输入框容易导致格式错误,用户体验也较差。本文将手把手教你用Qt的QLineEdit控件实现一个智能IP地址输入框。即使你是刚接触Qt的新手,也能通过本文理解核心实现原理。

u011186532的专栏 655

AVL树(考研的同学需要注意其定义)的判定(某公司2012年校园招聘笔试题目)

平衡二叉树又叫AVL(人名的简称)树,在不同的教材中,对AVL树的定义是不同的。考研指定的教材是严奶奶编写的教材,我们来看看该书上(P233)是如何定义的: AVL树或者是一棵空树,或者满足以下条件: (1). 其左子树和右子树都为AVL树 (2). 左子树和右子树的高度之差的绝对值不超过1 然而,有很多教材不是这么定义的,而是把

认知 行动 坚持 1万+

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

数据结构-考研难点代码突破(C++实现树型查找 - 平衡二叉树(AVL树)的基本操作(增删))

调整平衡因子a,b平衡因子都变为0,调整a,b节点父指针,调整树的根节点,处理子树的情况,细节和右单旋类似。观察上图,相当于把c节点拆开c做根节点,c的左子树给b右树,c的右子树给a的左树。如果新节点插入的位置在c的右子树上,经过旋转会到a的左子树上,b的平衡因子为-1,a的平衡因子为0(如上图).调整步骤:将a节点连接到b右节点,将原来b右子树连接到a左子树上,这样a,b节点的平衡因子又变回0。如果新节点插入的位置在c的左子树上,经过旋转会到b的右子树上,b的平衡因子为0,a的平衡因子为1.

dodamce的博客 911

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

怀旧版电脑GBC模拟器

再现原版GBC掌机的界面,电脑运行连游戏界面都一样大小,很怀旧,很真实,(英文版)强烈推荐,很小的文件,很难才找到的。

判断二叉树是否AVL树

一、算法思想: 递归法判断一个二叉树是否平衡二叉树(AVL树),可以根据它的定义写出代码:(1)空树是一个AVL树;(2)只有一个根结点的树是一个AVL树;(2)左子树是一颗AVL树,且右子树是一个AVL树,且左子树的高度与右子树的高度差绝对值不超过1。 二、代码: /* name: author:followStep description: date:2018/3/1...

EverydayIsLife的博客 2463

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

Contest100000614 - 《算法笔记》9.5小节——数据结构专题(2)->平衡二叉树(AVL)

题目链接 问题 A: 算法9-9~9-12:平衡二叉树的基本操作 AVL 树的基本操作,套用固定模板即可。参考代码如下。 #include&lt;iostream&gt; #include&lt;algorithm&gt; using namespace std; struct node { int data; int height = 1; node* left = NULL; ...

Armstrong的博客 268

平衡二叉树(AVL树)C/C++、C#代码实现

平衡二叉树是带有平衡条件的二叉查找树,指的是空树或者任一结点左、右高度差的j绝对值不超过1的二叉树. 比如: 实现的难点在于,二叉树的平衡旋转 分为四种旋转,RR、LL、LR、RL旋转 RR旋转 麻烦结点在发现者右子树的右边,所以叫RR插入,需要RR旋转 LL旋转 麻烦结点在发现者左子树的左边,所以叫LL插入,需要LL旋转 LR旋转 RL旋转 C/C++实现 #include <s...

qq_44800780的博客 632

AVL树详解

AVL树 参考了:http://www.cppblog.com/cxiaojia/archive/2012/08/20/187776.html 修改了其中的错误,代码实现并亲自验证过。   平衡二叉树(Balanced Binary Tree)是二叉查找树的一个进化体,也是第一个引入平衡概念的二叉树。1962年,G.M. Adelson-Velsky 和 E.M. Landis发明了这棵树,...

weixin_30535913的博客 101

C++实现树 - 07 平衡二叉树

写在前面: 这一讲我们来讲讲目前为止难度最大的一种树,当然后面要有 B 树、B+ 树和红黑树等着我们。同样,我会将详细的代码附到详解的最后。 平衡二叉树的定义 我们之前学了二叉排序树,但是二叉排序树存在一个致命的问题,如果我每次插入的值都比上一次插入的大,那它就会形成一个斜树,这对我们的查找删除等功能影响很大。 我们经过平衡操作就可以得到这样一颗树(具体如何操作下面会讲到): 所以平衡二叉树就由此诞生了,平衡二叉树定义其可以为空树,但是每个结点的左右子树的高度只差不能超过 1 ,所以就引入了一个新概.

Newin2020的博客 2182

平衡二叉树(AVL树)的简单实现

Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->#include stdlib.h>templatetypename T>class CAVLTree;templatetypename T>class CAVLTre

654

手写平衡二叉树 (AVL树)

下面是一个完整的C++实现手写平衡二叉树(AVL树)的代码。AVL树是一种自平衡二叉搜索树,其中每个节点的左右子树高度差不超过1。

chenmi123321的博客 323

生成最少节点的AVL树

摘要: (1)通过画图分析可知,最小AVL树是一种递归生成的结构,具有高度h的最小树由H-1和h-2的树作为左右子树加一个根节点构成;Position GenTree( int Height, int *LastNode ) { Position T; if( Height >= 0 ) { T = (Position)malloc( sizeof( *T ) ); T->Left = GenT

pyb的博客 1091
上一篇: 【算法笔记】全域哈希表
下一篇: 【深度学习笔记】机器学习模型
RealWeakCoder
博客等级 码龄7年 236粉丝 68原创
评论 7
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值
⌂ 换网址