二叉平衡查找树AvlTree(C实现)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
二叉平衡查找树即是一棵树中所有节点的左右子树高度差不超过1的查找树
头文件——————————————————————————————
#ifndef _AVLTREE_H_
#define _AVLTREE_H_
#include <stdlib.h>
#include <iomanip>
#include <iostream>
 
typedef struct AvlNode *Position;
typedef Position AvlTree;
#define Element int
struct AvlNode
{
     Element data;
     int height;//叶子节点高度定义为0,其父节点为1以此类推
     AvlTree left;
     AvlTree right;
};
 
static int Height(AvlTree avl);
void SwapAvlNode(Position *p1, Position *p2);
Position GetNotBalancedNode(AvlTree avl);
void MakeEmpty(AvlTree* pavl);
Position Find(Element x, AvlTree avl);
Position FindMin(AvlTree avl);
Position FindMax(AvlTree avl);
void Insert(Element x, AvlTree* pavl);
void Delete(Element x, AvlTree* pavl);
Element Retrieve(Position p);
void SingleRotateWithLeftLeft(Position *pK2);
void SingleRotateWithRightRight(Position *pK2);
void DoubleRotateWithLeftRight(Position *pK3);
void DoubleRotateWithRightLeft(Position *pK3);
void PrintTree(AvlTree avl, int Depth, int ctrl);
#endif
源文件————————————————————————————————
#include "./AvlTree.h"
 
int Max(int a, int b)
{
     if(a <= b)
          return b;
     return a;
}
void SwapAvlNode(Position *p1, Position *p2)
{
     Position tmp = *p1;
     *p1 = *p2;
     *p2 = tmp;
}
int Abs(int a)
{
     if(a < 0) return -a;
     return a;
}
static int Height(AvlTree avl)
{
     if(NULL == avl)
          return -1;
     else
          return avl->height;
}
Position GetNotBalancedNode(AvlTree avl)
{
     if(NULL == avl)
          return NULL;
     else
     {
          if(Height(avl->left) - Height(avl->right) == Abs(2))//not balanced
               return avl;
          else
          {
               Position res = GetNotBalancedNode(avl->left);
               if(NULL != res)//avl->left is not balanced
                    return res;
               else
                    return GetNotBalancedNode(avl->right);
          }
     }
}
void MakeEmpty(AvlTree* pavl)
{
     if(NULL != (*pavl))
     {
          MakeEmpty(&((*pavl)->left));
          MakeEmpty(&((*pavl)->right));
          free(*pavl);
          *pavl = NULL;
     }
}
Position Find(Element x, AvlTree avl)
{
     Position pos = avl;
     while(NULL != pos)
     {
          if(x < Retrieve(pos))
               pos = pos->left;
          else if(x > Retrieve(pos))
               pos = pos->right;
          else
               break;
     }
     return pos;
}
Position FindMin(AvlTree avl)
{
     while(NULL != avl && NULL != avl->left)
          avl = avl->left;
     return avl;
}
Position FindMax(AvlTree avl)
{
     while(NULL != avl && NULL != avl->right)
          avl = avl->right;
     return avl;
}
void Insert(Element x, AvlTree* pavl)
{
     if(NULL == (*pavl))
     {
          Position tmp = (Position)malloc(sizeof(struct AvlNode));
          if(NULL == tmp)
               return ;
          tmp->data = x;
          tmp->height = 0;
          tmp->left = tmp->right = NULL;
          *pavl = tmp;
     }
     else
     {
          if(x < Retrieve(*pavl))//在*pavl的左儿子上插入
          {
               Insert(x, &((*pavl)->left));
               if(Height((*pavl)->left) - Height((*pavl)->right) == 2)//不平衡
               {
                    if(x < Retrieve((*pavl)->left))//左儿子的左子树
                         SingleRotateWithLeftLeft(pavl);
                    else//左儿子的右子树
                         DoubleRotateWithLeftRight(pavl);
               }
          }
          else if(x > Retrieve(*pavl))//在*pavl的右儿子上插入
          {
               Insert(x, &((*pavl)->right));
               if(Height((*pavl)->right) - Height((*pavl)->left) == 2)//不平衡
               {
                    if(x > Retrieve((*pavl)->right))//右儿子的右子树
                         SingleRotateWithRightRight(pavl);
                    else//右儿子的左子树
                         DoubleRotateWithRightLeft(pavl);
               }
          }
     }
     (*pavl)->height = Max(Height((*pavl)->left), Height((*pavl)->right)) + 1;
}
void Delete(Element x, AvlTree* pavl)
{
     if(NULL == *pavl)
          return ;
     if(x < Retrieve((*pavl)))//go left
          Delete(x, &((*pavl)->left));
     else if(x > Retrieve((*pavl)))//go right
          Delete(x, &((*pavl)->right));
     else if(NULL != (*pavl)->left && NULL != (*pavl)->right)//*pavl has two children
     {
          //利用右子树的最小值tmp->data来替代被删除的节点上的值x,然后在右子树上递归的删除值tmp->data
          Position tmp = FindMin((*pavl)->right);
          (*pavl)->data = tmp->data;
          Delete(tmp->data, &((*pavl)->right));
     }
     else//*pavl has none or one child
     {
          Position tmp = *pavl;
          if(NULL == (*pavl)->left)//*pavl has right child
               *pavl = (*pavl)->right;
          else if(NULL == (*pavl)->right)//*pavl has left child
               *pavl = (*pavl)->left;
          free(tmp);
     }
     if(NULL != *pavl)//最后更新*pavl节点上的高度
     {
          (*pavl)->height = Max(Height((*pavl)->left), Height((*pavl)->right)) + 1;
          if(2 == Height((*pavl)->left) - Height((*pavl)->right))//not balanced
          {
               if(NULL == (*pavl)->left->right)
                    SingleRotateWithLeftLeft(pavl);
               else
                    DoubleRotateWithLeftRight(pavl);
          }
          else if(2 == Height((*pavl)->right) - Height((*pavl)->left))//not balance
          {
               if(NULL == (*pavl)->right->left)
                    SingleRotateWithRightRight(pavl);
               else
                    DoubleRotateWithRightLeft(pavl);
          }
     }
}
Element Retrieve(Position p)
{
     return p->data;
}
void SingleRotateWithLeftLeft(Position *pK2)
{
     Position k2 = *pK2;
     Position k1 = k2->left;
     k2->left = k1->right;
     k1->right = k2;
     k1->height = Max(Height(k1->left), Height(k2)) + 1;
     k2->height = Max(Height(k2->left), Height(k2->right)) + 1;
     *pK2 = k1;
}
void SingleRotateWithRightRight(Position *pK2)
{
     Position k2 = *pK2;
     Position k1 = k2->right;
     k2->right = k1->left;
     k1->left = k2;
     k1->height = Max(Height(k2), Height(k1->right)) + 1;
     k2->height = Max(Height(k2->left), Height(k2->right)) + 1;
     *pK2 = k1;
}
void DoubleRotateWithLeftRight(Position *pK3)
{
     SingleRotateWithRightRight(&((*pK3)->left));
     SingleRotateWithLeftLeft(pK3);
}
void DoubleRotateWithRightLeft(Position *pK3)
{
     SingleRotateWithLeftLeft(&((*pK3)->right));
     SingleRotateWithRightRight(pK3);
}
void PrintTree(AvlTree avl, int Depth, int ctrl)//ctrl:0=root 1=left 2=right
{
     
     if(NULL != avl)
     {
          std::cout<<std::setw(Depth);
          if(0 == ctrl)
               std::cout<<"rt:";
          else if(1 == ctrl)
               std::cout<<"l";
          else if(2 == ctrl)
               std::cout<<"r";
          std::cout<<avl->data<<std::endl;
          PrintTree(avl->left, Depth+3, 1);
          PrintTree(avl->right, Depth+3, 2);
     }
}

  

posted @   老司机  阅读(492)  评论(0编辑  收藏  举报
编辑推荐:
· Linux glibc自带哈希表的用例及性能测试
· 深入理解 Mybatis 分库分表执行原理
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
· 现代计算机视觉入门之:什么是图片特征编码
阅读排行:
· 手把手教你在本地部署DeepSeek R1,搭建web-ui ,建议收藏!
· Spring AI + Ollama 实现 deepseek-r1 的API服务和调用
· 数据库服务器 SQL Server 版本升级公告
· C#/.NET/.NET Core技术前沿周刊 | 第 23 期(2025年1.20-1.26)
· 程序员常用高效实用工具推荐,办公效率提升利器!
点击右上角即可分享
微信分享提示