题目
AVL添加
平衡二叉树,是一种二叉排序树,其中每个结点的左子树和右子树的高度差至多等于1。它是一种高度平衡的二叉排序树。现二叉平衡树结点定义如下:
typedef struct node
{
int val;
struct node *left;
struct node *right;
struct node *parent;
int height;
} node_t;
请实现平衡二叉树的插入算法:
//向根为 root 的平衡二叉树插入新元素 val,成功后返回新平衡二叉树根结点
node_t *avl_insert(node_t *root, int val);
答案
#include "avl.h"
#include <stdio.h>
#include <stdlib.h>
void update(node_t* root)
{
if (root == NULL)
return;
int h = 1;
if (root->left) {
int lh = root->left->height + 1;
h = lh > h ? lh : h;
root->left->parent = root;
}
if

本文介绍了AVL树的基本概念,作为高度平衡的二叉排序树,其左右子树高度差不超过1。接着,详细阐述了如何在C++中实现AVL树的插入算法,以保持树的平衡。

6305

被折叠的 条评论
为什么被折叠?



