这里只给出一个简单的实现,具体原理参见Google、Baidu的各个角落及各大教材。做两点说明:
仅仅实现了必要的左旋、右旋处理,左平衡、右平衡操作,以及数据的插入操作,
不可重复插入数据。

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
typedef int Type; //~ 节点数据类型
typedef struct Node
{
	Type data;
	int bf;
	Node *left, *right;
	Node(){ bf = 0; left = right = 0; }
} AVLNode, *AVLTree; //~ 平衡树及其节点
 
void RRotate(AVLTree & T) //~ 右旋处理,T的左孩子成为新子树根
{
	AVLNode * tmp = T->left;
	T->left = tmp->right;
	tmp->right = T;
	T = tmp;
}
 
void LLotate(AVLTree & T) //~ 左旋处理,T的右孩子成为新子树根
{
	AVLNode * tmp = T->right;
	T->right = tmp->left;
	tmp->left = T;
	T = tmp;
}
 
void LBalance(AVLTree & T) //~ 左平衡处理
{
	AVLNode * l = T->left;
	switch(l->bf)
	{
		case 1: //~ LL,单右旋转
			RRotate(T);
			T->bf = l->bf = 0; //~ 更新平衡因子
			return ;
		case -1: //~ LR双旋转
			AVLNode * lr = l->right;
			switch(lr->bf) //~ 更新平衡因子
			{
				case 1:
					l->bf = 0;
					T->bf = -1;
					break;
				case -1:
					T->bf = 0;
					l->bf = 1;
					break;
			}// switch
			lr->bf = 0;
			LLotate(l);
			RRotate(T);
	}// switch
}
 
void RBalance(AVLTree & T) //~ 右平衡处理
{
	AVLNode * r = T->right;
	switch(r->bf)
	{
		case -1: //~ RR,单左旋转
			LLotate(T);
			T->bf = r->bf = 0;
			return ;
		case 1: //~ RL双旋转
			AVLNode * rl = r->left;
			switch(rl->bf)//~ 更新平衡因子
			{
				case 1:
					r->bf = -1;
					T->bf = 0;
					break;
				case -1:
					T->bf = 1;
					r->bf = 0;
					break;
			}// switch
			rl->bf = 0;
			RRotate(r);
			LLotate(T);
	}// switch
}
bool Insert(AVLTree & T, Type & data) //~ 通过插入操作构建AVL树
{
	bool taller = true; //~ 标识此次插入操作是否使该树长高
	if(!T) //~ 该子树为空树,长高
	{
		T = new AVLNode;
		T->data = data;
		return taller;
	} // if
	if(data == T->data) return false; //~ 该数据已存在,不重复插入
	if(data < T->data) //~ 需要将该数据插入左子树
	{
		if(!Insert(T->left, data)) return false; //~ 左子树未长高
		switch(T->bf) //~ 左子树长高,判断是否需要调整
		{
			case 1: //~ 失衡,许进行左平衡调整
				LBalance(T);
				return false;
			case 0: //~ 未失衡,只需调整当前节点的平衡因子
				T->bf = 1;
				return true;
			case -1: //~ 未失衡,只需调整当前节点的平衡因子
				T->bf = 0;
				return false;
		} // switch
	} // if
	else
	{
		if(!Insert(T->left, data)) return false;
		switch(T->bf)
		{
			case 1: //~ 未失衡,只需调整当前节点的平衡因子
				T->bf = 0;
				return false;
			case 0: //~ 未失衡,只需调整当前节点的平衡因子
				T->bf = -1;
				return true;
			case -1: //~ 失衡,许进行右平衡调整
				RBalance(T);
				return false;
		} // switch
	} // if
}
Tags: ,,.
Home

4 Comments so far

Leave a comment

Name(required)
Mail (required),(will not be published)
  4 + 6 = ?
Please leave these two fields as-is:
Website(recommended)

Fields in bold are required. Email addresses are never published or distributed.

Some HTML code is allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>
URLs must be fully qualified (eg: http://www.dutor.net),and all tags must be properly closed.

Line breaks and paragraphs are automatically converted.

Please keep comments relevant. Off-topic, offensive or inappropriate comments may be edited or removed.