• 04-树5 Root of AVL Tree


      这道题目要求找出AVL树的根节点,重点考查了AVL树的旋转(右单旋、左单旋、右-左双旋和左-右双旋)与插入操作。

      1 #include <stdio.h>
      2 #include <stdlib.h>
      3 
      4 typedef struct AVLNode *AVLTree;
      5 typedef int ElementType;
      6 struct AVLNode {
      7     ElementType Data;
      8     AVLTree Left, Right;
      9     int Height;    // 树高 
     10 };
     11 
     12 int Max(int a, int b);
     13 int GetHeight(AVLTree T);
     14 AVLTree SingleLeftRotation(AVLTree A);
     15 AVLTree SingleRightRotation(AVLTree A);
     16 AVLTree DoubleLeftRightRotation(AVLTree A);
     17 AVLTree DoubleRightLeftRotation(AVLTree A);
     18 AVLTree Insert(AVLTree T, ElementType X);
     19 
     20 int main()
     21 {
     22     int N, i;
     23     ElementType data;
     24     AVLTree T;
     25     
     26     scanf("%d", &N);
     27     for ( i = 0; i < N; i++ ) {
     28         scanf("%d", &data);
     29         T = Insert(T, data);
     30     }
     31     printf("%d", T->Data);
     32     
     33     return 0;
     34 }
     35 
     36 AVLTree Insert(AVLTree T, ElementType X)
     37 {
     38     if ( !T ) {
     39         T = (AVLTree)malloc(sizeof(struct AVLNode));
     40         T->Data = X;    T->Height = 0;
     41         T->Left = T->Right = NULL;
     42     }
     43     else {
     44         if ( X < T->Data ) {
     45             // 插入 T 的左子树 
     46             T->Left = Insert(T->Left, X);
     47             // 如果需要 左旋
     48             if ( GetHeight(T->Left)-GetHeight(T->Right) == 2 ) 
     49                 if ( X < T->Left->Data )    // 左单旋
     50                     T = SingleLeftRotation(T);
     51                 else                         // 左-右双旋 
     52                     T = DoubleLeftRightRotation(T); 
     53         }
     54         else if ( X > T->Data ) {
     55             // 插入 T 的右子树
     56             T->Right = Insert(T->Right, X);
     57             // 如果需要右旋
     58             if ( GetHeight(T->Left)-GetHeight(T->Right) == -2 )
     59                 if ( X > T->Right->Data )         // 右单旋 
     60                     T = SingleRightRotation(T);
     61                 else                            // 右-左双旋 
     62                     T = DoubleRightLeftRotation(T);
     63         }
     64         /*  else X == T->Data 无须插入 */
     65     }
     66     // 更新树的高度 
     67     T->Height = Max( GetHeight(T->Left), GetHeight(T->Right) ) + 1;
     68     
     69     return T;
     70 }
     71 
     72 AVLTree DoubleRightLeftRotation(AVLTree A)
     73 {
     74     //  B 与 C 做 左单旋,C 被返回
     75     A->Right = SingleLeftRotation(A->Right);
     76     // A 与 C 做  右单旋, C 被返回
     77     return SingleRightRotation(A); 
     78 } 
     79 
     80 AVLTree DoubleLeftRightRotation(AVLTree A)
     81 { /* 注意:A必须有一个左子结点B,且B必须有一个右子结点C */
     82   /* 将A、B与C做两次单旋,返回新的根结点C */
     83   
     84     // B 与 C 做 右单旋, C被返回 
     85     A->Left = SingleRightRotation(A->Left);
     86     // A 与 C 做 左单旋, C被返回 
     87     return SingleLeftRotation(A);
     88 }
     89 
     90 AVLTree SingleRightRotation(AVLTree A)
     91 {
     92     AVLTree B = A->Right;
     93     A->Right = B->Left;
     94     B->Left = A;
     95     A->Height = Max( GetHeight(A->Left), GetHeight(A->Right) ) + 1;
     96     B->Height = Max( A->Height, GetHeight(B->Right) ) + 1;
     97     
     98     return B;
     99 }
    100 
    101 AVLTree SingleLeftRotation(AVLTree A)
    102 { /* 注意:A必须有一个左子结点B */
    103   /* 将A与B做左单旋,更新A与B的高度,返回新的根结点B */  
    104     AVLTree B = A->Left;
    105     A->Left = B->Right;
    106     B->Right = A;
    107     A->Height = Max( GetHeight(A->Left), GetHeight(A->Right) ) + 1;
    108     B->Height = Max( GetHeight(B->Left), A->Height ) + 1;
    109     
    110     return B;
    111 }
    112 
    113 int GetHeight(AVLTree T)
    114 {
    115     if ( !T )    return 0;
    116     return T->Height;
    117 }
    118 
    119 int Max(int a, int b)
    120 {
    121     return ( a > b ? a : b );
    122 }
  • 相关阅读:
    maven项目部署到tomcat中没有classe文件的问题汇总
    Tomcat远程调试模式及利用Eclipse远程链接调试
    FastDFS 常见问题
    Linux Crontab 定时任务 命令详解
    EChart 关于图标控件的简单实用
    java 通过zxing生成二维码
    Mybatis typeAliases别名
    Mybatis 实现手机管理系统的持久化数据访问层
    Mybatis 实现传入参数是表名
    Mybatis关于like的字符串模糊处理
  • 原文地址:https://www.cnblogs.com/wgxi/p/9998334.html
Copyright © 2020-2023  润新知