平衡二叉搜索树的简单操作(AVL)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_35691619/article/details/78917039

结构体

typedef struct AVLNode * Position;
typedef Position AVLTree;

struct AVLNode {
  int data;
  AVLTree left;
  AVLTree right;
  int height;
};


LL

AVLTree singleLeftRotation(AVLTree A) {
  AVLTree B = A->left;
  A->left = B->right;
  B->right = A;
  A->height = max(getTreeHeight(A->left), getTreeHeight(A->right)) + 1;
  B->height = max(getTreeHeight(B->left), getTreeHeight(B->right)) + 1;

  return B;
}


RR

AVLTree singleRightRotation(AVLTree A) {
  AVLTree B = A->right;
  A->right = B->left;
  B->left = A;

  A->height = max(getTreeHeight(A->left), getTreeHeight(A->right)) + 1;
  B->height = max(getTreeHeight(B->left), getTreeHeight(B->right)) + 1;

  return B;
}


LR

AVLTree doubleLeftRightRotation(AVLTree A) {
  A->left = singleRightRotation(A->left);
  return singleLeftRotation(A);
}



RL

AVLTree doubleRightRotation(AVLTree A) {
  A->right = singleLeftRotation(A->right);
  return singleRightRotation(A);
}



树高

int getTreeHeight(AVLTree T)
{
    int HL = 0, HR = 0;
    int max = 0;
    if (T) {
        if (T->left) {
            HL = getTreeHeight(T->left);
        }
        if (T->right) {
            HR = getTreeHeight(T->right);
        }
        max = (HL > HR) ? (HL + 1) : (HR + 1);
    }
    return  max;
}


具体如下

#include<stdio.h>
#include<stdlib.h>



int max(int a, int b) {
  return a>b?a:b;
}

int getTreeHeight(AVLTree T)
{
    int HL = 0, HR = 0;
    int max = 0;
    if (T) {
        if (T->left) {
            HL = getTreeHeight(T->left);
        }
        if (T->right) {
            HR = getTreeHeight(T->right);
        }
        max = (HL > HR) ? (HL + 1) : (HR + 1);
    }
    return  max;
}

AVLTree singleLeftRotation(AVLTree A) {
  AVLTree B = A->left;
  A->left = B->right;
  B->right = A;
  A->height = max(getTreeHeight(A->left), getTreeHeight(A->right)) + 1;
  B->height = max(getTreeHeight(B->left), getTreeHeight(B->right)) + 1;

  return B;
}

AVLTree singleRightRotation(AVLTree A) {
  AVLTree B = A->right;
  A->right = B->left;
  B->left = A;

  A->height = max(getTreeHeight(A->left), getTreeHeight(A->right)) + 1;
  B->height = max(getTreeHeight(B->left), getTreeHeight(B->right)) + 1;

  return B;
}

AVLTree doubleLeftRightRotation(AVLTree A) {
  A->left = singleRightRotation(A->left);
  return singleLeftRotation(A);
}

AVLTree doubleRightRotation(AVLTree A) {
  A->right = singleLeftRotation(A->right);
  return singleRightRotation(A);
}


AVLTree insert(AVLTree T, int x) {
  if (!T) {
    T = (AVLTree)malloc(sizeof(struct AVLNode));
    T->data = x;
    T->left = T->right = NULL;
    T->height = 0;
  } else if (x < T->data) {
      T->left = insert(T->left, x);
      if (getTreeHeight(T->left)-getTreeHeight(T->right) == 2) {
        T = (x < T->left->data)?(singleLeftRotation(T)):(doubleLeftRightRotation(T));
      }
  } else if (x > T->data) {
    T->right = insert(T->right, x);
    if (getTreeHeight(T->right) - getTreeHeight(T->left) == 2) {
      T = (x > T->right->data)?(singleRightRotation(T)):(doubleRightRotation(T));
    }
  }

  T->height = max(getTreeHeight(T->left), getTreeHeight(T->right)) + 1;
  return T;
}

void preOrder(AVLTree BST) {
  if (!BST) {
    return ;
  } else {
    printf("%d ", BST->data);
    preOrder(BST->left);
    preOrder(BST->right);
  }
}

int main(void)
{
  int N, x;
  AVLTree root = NULL;
  printf("输入原始数据个数:\n");
  scanf("%d", &N);
  for (int i = 0; i < N; i++) {
    scanf("%d", &x);
    root = insert(root, x);
  }

  /*
  函数调用省略,很多的找最小,最大,树高,遍历的写法都是和树是一样的
  */

  return 0;
}


猜你喜欢

转载自blog.csdn.net/qq_35691619/article/details/78917039
今日推荐