题目
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;
}