AVLTree创建与删除

各种折腾...

  1 #include <iostream>

  2 #define MAX(a,b) ((a)>(b)?(a):(b))

  3 #define SIZEOFARRY(a) sizeof(a)/sizeof(int)

  4 #define ASSERT(a) if((a)==NULL)exit(0)

  5 

  6 using namespace std;

  7 

  8 typedef  int Datatype;

  9 

 10 typedef struct AVLTNode

 11 {

 12     struct AVLTNode*    pLeft;

 13     struct AVLTNode*    pRight;

 14     int                height;

 15     Datatype        data;

 16 }*pNode;

 17 

 18 typedef struct AVLTNode* Tree;

 19 

 20 

 21 class AVLTree

 22 {

 23 public:

 24     AVLTree(){root=NULL;}

 25     ~AVLTree(){}

 26     void InsertNode(Datatype x){root=InsertNode(x,root);};

 27     void DeleteNode(Datatype x){root = DeleteNode(x, root);};

 28     void PrintTreeByMin(){_PrintTreeByMin(root);}

 29 private:

 30     int BalanceFactor(AVLTNode* node);

 31     AVLTNode* Rotate(AVLTNode* node);

 32     void _PrintTreeByMin(AVLTNode* node);

 33     AVLTNode* InsertNode(Datatype x, AVLTNode* node);

 34     AVLTNode* DeleteNode(Datatype x, AVLTNode* node);

 35     int Height(AVLTNode* node);

 36     AVLTNode* RightRotate(AVLTNode* root);

 37     AVLTNode* LeftRotate(AVLTNode* root);

 38     AVLTNode* RightLeftRotate(AVLTNode* root);

 39     AVLTNode* LeftRightRotate(AVLTNode* root);

 40 public:

 41     AVLTNode* root;

 42 };

 43 

 44 int AVLTree::Height(AVLTNode* node)

 45 {

 46     if (!node)

 47     {

 48         return 0;

 49     }

 50     return node->height;

 51 }

 52 AVLTNode* AVLTree::RightRotate(AVLTNode* root)

 53 {

 54     AVLTNode *temp;

 55     temp = root->pLeft;

 56     root->pLeft = temp->pRight;

 57     temp->pRight = root;

 58 

 59     root->height = MAX(Height(root->pLeft), Height(root->pRight)) + 1;

 60     temp->height = MAX(Height(temp->pLeft), Height(temp->pRight)) + 1;

 61 

 62     return temp;

 63 }

 64 AVLTNode* AVLTree::LeftRotate(AVLTNode* root)

 65 {

 66     AVLTNode *temp;

 67     temp = root->pRight;

 68     root->pRight = temp->pLeft;

 69     temp->pLeft = root;

 70 

 71     root->height = MAX(Height(root->pLeft), Height(root->pRight)) + 1;

 72     temp->height = MAX(Height(temp->pLeft), Height(temp->pRight)) + 1;

 73 

 74     return temp;

 75 }

 76 AVLTNode* AVLTree::RightLeftRotate(AVLTNode* root)

 77 {

 78     root->pRight = RightRotate(root->pRight);

 79 

 80     return LeftRotate(root);

 81 }

 82 AVLTNode* AVLTree::LeftRightRotate(AVLTNode* root)

 83 {

 84     root->pLeft = LeftRotate(root->pLeft);

 85     return RightRotate(root);

 86 }

 87 

 88 AVLTNode* AVLTree::InsertNode(Datatype x,AVLTNode* node)

 89 {

 90     if (node==NULL)

 91     {

 92         node = new AVLTNode;

 93         ASSERT(node);

 94         node->data = x;

 95         node->pLeft = NULL;

 96         node->pRight = NULL;

 97         node->height = 1;

 98         return node;

 99     }

100     else if (x < node->data)

101     {

102         node->pLeft = InsertNode(x, node->pLeft);

103         if (Height(node->pLeft)-Height(node->pRight) == 2)

104         {

105             if (x < node->pLeft->data)

106             {

107                 node = RightRotate(node);

108             }

109             else

110             {

111                 node = LeftRightRotate(node);

112             }

113         }        

114     }

115     else

116     {

117         node->pRight = InsertNode(x, node->pRight);

118         if (Height(node->pRight)-Height(node->pLeft) == 2)

119         {

120             if (x > node->pRight->data)

121             {

122                 node = LeftRotate(node);

123             }

124             else

125             {

126                 node = RightLeftRotate(node);

127             }

128         }

129     }

130     node->height = MAX(Height(node->pLeft), Height(node->pRight)) + 1;

131     return node;

132 }

133 

134 void AVLTree::_PrintTreeByMin(AVLTNode* node)

135 { 

136     if (!node)

137     {

138         return;

139     }

140     _PrintTreeByMin(node->pLeft);

141     cout<<node->data<<"("<<node->height<<")"<<" ";

142     _PrintTreeByMin(node->pRight);

143 }

144 //对于二叉树的删除操作,先递归的搜索要删除的节点A,用该节点的右儿子

145 //的最左儿子B代替该节点,以A的右儿子和B当前的数值作为线索继续遍历,

146 //只到找到B,删除B。然后递归的对B的祖先节点进行平衡调整,具体调整操作

147 //和二叉树的插入操作的平衡调整相同。

148 AVLTNode* AVLTree::DeleteNode(Datatype x, AVLTNode* node)

149 {

150     AVLTNode *temp=NULL;

151     if (node == NULL)

152     {

153         return NULL;

154     }

155     if (node->data == x)

156     {

157         if (node->pRight==NULL)

158         {

159             //无有孩子

160             temp = node;

161             node = node->pLeft;

162             free(temp);

163             temp = NULL;

164         }

165         else

166         {

167             //有则用其右孩子的最左孩子替换该节点

168             temp = node->pRight;

169             while(temp->pLeft)

170             {

171                 temp = temp->pLeft;

172             }

173             node->data = temp->data;

174             node->pRight = DeleteNode(node->data, node->pRight);//删除的值,和传入的根结点发生了变化

175             node->height = MAX(Height(node->pLeft), Height(node->pRight))+1;

176         }

177         return node; //

178     }else if (node->data < x)

179     {

180         node->pRight = DeleteNode(x, node->pRight);

181         if (node->pRight)

182         {

183             node->pRight = Rotate(node->pRight);

184         }

185         

186     }

187     else

188     {

189         node->pLeft = DeleteNode(x, node->pLeft);

190         if (node->pLeft)

191         {

192             node->pLeft = Rotate(node->pLeft);

193         }

194     }

195     node->height = MAX(Height(node->pLeft), Height(node->pRight))+1;

196     node = Rotate(node);

197     return node;

198 }

199 

200 int AVLTree::BalanceFactor(AVLTNode* node)

201 {

202     if (!node)

203     {

204         return 0;

205     }

206     return Height(node->pLeft)-Height(node->pRight);

207 }

208 

209 AVLTNode* AVLTree::Rotate(AVLTNode* node)

210 {

211     if(Height(node->pLeft) - Height(node->pRight) == 2)

212     {

213         if(Height(node->pLeft->pLeft) >= Height(node->pLeft->pRight))

214         {

215             node = RightRotate(node);

216         }

217         else{

218             node = LeftRightRotate(node);

219         }

220     }

221     if(Height(node->pRight) - Height(node->pLeft) == 2)

222     {

223         if(Height(node->pRight->pRight) >= Height(node->pRight->pLeft))

224         {

225             node = LeftRotate(node);

226         }

227         else{

228             node = RightLeftRotate(node);

229         }

230     }

231 

232     return node;

233 }

234 

235 

236 int main()

237 {

238     AVLTree avltree;

239 

240     int num[] = {16,3,7,11,9,26,18,14,15};

241     for (int i=0; i<SIZEOFARRY(num); i++)

242     {

243         avltree.InsertNode(num[i]);

244     }

245 

246     avltree.PrintTreeByMin();

247     cout<<endl;

248     avltree.DeleteNode(14);

249     avltree.PrintTreeByMin();

250     cout<<endl;

251     avltree.DeleteNode(26);

252     avltree.PrintTreeByMin();

253 

254     cout<<endl;

255     return 0;

256 }

你可能感兴趣的:(tree)