-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAVL.cpp
More file actions
201 lines (152 loc) · 4.37 KB
/
Copy pathAVL.cpp
File metadata and controls
201 lines (152 loc) · 4.37 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <iostream>
using namespace std;
typedef struct AVLTree
{
int data;
AVLTree *pLeft;
AVLTree *pRight;
int nHeight;
}AVLTree;
int Max(int a,int b);
int Heigth(AVLTree *Node);
AVLTree* insert(int nData, AVLTree *pNode);
AVLTree * LLRotate(AVLTree *pNode);
AVLTree * RRRotate(AVLTree *pNode);
AVLTree * LRRotate(AVLTree *pNode);
AVLTree * RLRotate(AVLTree *pNode);
void DeleteTree(AVLTree **pNode);
void PrintTree(AVLTree *pNode);
const int summary = 100000;
int deepmax = 0x80000000;
int main()
{
AVLTree * pRoot = NULL;
srand(static_cast<unsigned int>(time(NULL)));
for (int i = 0; i < 400000000; i++)
{
pRoot = ::insert(rand()%summary,pRoot);
}
PrintTree(pRoot);
return 0;
}
int Max(int a, int b)
{
return ((a > b)? a:b);
}
int Height(AVLTree *pNode)
{
if ( pNode == NULL)
return -1;
return pNode->nHeight;
}
AVLTree * insert(int nData, AVLTree* pNode)
{
if ( NULL == pNode)
{
pNode = new AVLTree();
pNode->data = nData;
pNode->nHeight = 0;
pNode->pLeft = NULL;
pNode->pRight = NULL;
}
else if (nData < pNode->data )//insert into left tree
{
pNode->pLeft = insert(nData,pNode->pLeft);
if (Height(pNode->pLeft) - Height(pNode->pRight) == 2)//不平衡AVL
{
if (nData < pNode->pLeft->data)
{
pNode = LLRotate(pNode);
}
else
{
pNode = LRRotate(pNode);
}
}
}
else if (nData > pNode->data)
{
pNode->pRight = insert(nData, pNode->pRight);
if ( Height(pNode->pRight)- Height(pNode->pLeft) == 2)
{
if (nData > pNode->pRight->data)
{
pNode = RRRotate(pNode);
}
else
{
pNode = RLRotate(pNode);
}
}
}
pNode->nHeight = Max(::Height(pNode->pLeft),Height(pNode->pRight)) + 1;
return pNode;
}
//**************************************************************************************
// pNode pNode->pLeft
// / /\
// pNode->pLeft --> pNode->pLeft->pLeft pNode
// /
// pNode->pLeft->pLeft
//**************************************************************************************
AVLTree* LLRotate(AVLTree *pNode)
{
AVLTree *pNode1;
pNode1 = pNode->pLeft;
pNode->pLeft = pNode1->pRight;
pNode1->pRight = pNode;
//更新介绍点的高度;
pNode->nHeight = Max(::Height(pNode->pLeft), Height(pNode->pRight))+1;
pNode1->nHeight = Max(::Height(pNode1->pLeft), pNode->nHeight) + 1;
return pNode1;
}
//**************************************************************************************
// pNode pNode->pRight
// \ /\
// pNode->pRight --> pNode pNode->pRight->pRight
// \
// pNode->pRight->pRight
//**************************************************************************************
AVLTree * RRRotate(AVLTree * pNode)//RR
{
AVLTree *pNode1;
pNode1 = pNode->pRight;
pNode->pRight = pNode1->pLeft;
pNode1->pLeft = pNode;
pNode->nHeight = Max(::Height(pNode->pLeft),::Height(pNode->pRight)) + 1;
pNode1->data = Max(::Height(pNode1->pRight), pNode->nHeight) + 1;
return pNode1;
}
AVLTree * LRRotate(AVLTree * pNode)
{
pNode->pLeft = RRRotate(pNode->pLeft);//1.不平衡节点pNode的左儿子处进行右旋转
return (LLRotate(pNode)); //2.不平衡节点pNode处进行左旋转,返回新节点
}
AVLTree *RLRotate(AVLTree * pNode)
{
pNode->pRight = LLRotate(pNode->pRight);//1.不平衡节点pNode的右边儿子处进行左边旋转
return (RRRotate(pNode)); //2.不平衡节点pNode处进行右旋转,返回新节点
}
void DeleteTree(AVLTree **ppNode)
{
if (ppNode == NULL || *ppNode == NULL)
return ;
DeleteTree(&((*ppNode)->pLeft));
DeleteTree(&((*ppNode)->pRight));
delete(*ppNode);
*ppNode = NULL;
}
void ::PrintTree(AVLTree *pNode)
{
if (pNode == NULL)
return ;
static int n = 0;
if (pNode->nHeight > deepmax)
deepmax = pNode->nHeight;
PrintTree(pNode->pLeft);
std::cout <<"["<<++n<<"] : Data = "<<pNode->data<< " Height="<<pNode->nHeight <<" MAX DEEPTH="<<deepmax<< std::endl;
PrintTree(pNode->pRight);
}