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 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
|
#include "stdafx.h"
#include "AVL.h"
#include <map>
#include <iostream>
#include <string>
using namespace std;
AVL::AVL(int n):key(n),parent(NULL),l_child(NULL),r_child(NULL),size(1)
{}
/*bool AVL::Insert(int n)
{
//FindParent(n);
}*/
AVL::AVL(){}
AVL::~AVL(void)
{
//std::////cout<<"Destructing .."<<key<<std::endl;
}
AVL* AVL::Insert(AVL *node, AVL *root)
{
if(node->GetKey() > root->GetKey()) // go right
{
if(root->GetRChild()==nullptr)
{
node->SetParent(root);
root->SetRChild(node);
return root;
}
else
{
return Insert(node,root->GetRChild());
}
}
else if(node->GetKey() < root->GetKey()) // go left
{
if(root->GetLChild()==nullptr)
{
node->SetParent(root);
root->SetLChild(node);
return root;
}
else {
return Insert(node,root->GetLChild());
}
}
else
return root;
}
int AVL::GetBalanceFactor(AVL* node)
{
int l_side=0;
int r_side=0;
if(node->GetLChild()!=NULL)
{
l_side=getHeight(node->GetLChild());
}
if(node->GetRChild()!=NULL)
{
r_side=getHeight(node->GetRChild());
}
return l_side-r_side;
}
// returns the kind of inversion to be implemented
int AVL::GetRotationCase(AVL* childNode)
{
int invCase;
AVL *parent=NULL;
AVL* gParent=NULL;
AVL * ggParent=NULL;
parent=childNode->GetParent();
if(parent!=NULL)
gParent=parent->GetParent();
if(gParent!=NULL)
ggParent=gParent->GetParent();
if(gParent->GetLChild()==parent && parent->GetLChild()==childNode)
invCase=1;//LL
else if(gParent->GetLChild()==parent && parent->GetRChild()==childNode)
invCase=3;//LR
else if(gParent->GetRChild()==parent && parent->GetRChild()==childNode)
invCase=2;//RR
else
invCase=4;
return invCase;
}
AVL* AVL::Rotation(AVL* parent,AVL* child, AVL *root,int rCase) // this returns pointer of the root incase it changes
{
////cout<<"Rotating... ";// bring in the node at which disbalance occurs and then operate from there
AVL *tempRoot=NULL;
////cout<<"rotation case is :";
switch (rCase)
{
case LL:
////cout<<"LL"<<endl;
tempRoot=LL_Rotation(parent);
break;
case RR:
////cout<<"RR"<<endl;
tempRoot=RR_Rotation(parent);
break;
case LR:
////cout<<"LR"<<endl;
tempRoot=RR_Rotation(child);
tempRoot=LL_Rotation(child);
break;
case RL:
////cout<<"RL"<<endl;
tempRoot=LL_Rotation(child);
tempRoot=RR_Rotation(child);
break;
default:
break;
}
if(tempRoot!=NULL)
return tempRoot;// returns the new root of the tree
else
return root;
}
AVL* AVL::LL_Rotation(AVL* node)
{
AVL *root=NULL;
AVL *parent=node;
AVL *grandParent=NULL;
AVL *beta=NULL;
AVL * GGPArent=NULL;
grandParent=(parent!=NULL? parent->GetParent():NULL);
beta=(parent!=NULL?parent->GetRChild():NULL);
GGPArent=(grandParent!=NULL?grandParent->GetParent():NULL);
if(GGPArent!=NULL)
{
if(GGPArent->GetLChild()==grandParent)
{
GGPArent->SetLChild(parent);// set pivot to point to the parent of grandParent
}
else
{
GGPArent->SetRChild(parent);
}
parent->SetParent(GGPArent);
}
else
{
root=parent;
parent->SetParent(NULL);
}
parent->SetRChild(grandParent);//set grandParent as pivots right child
grandParent->SetParent(parent);
grandParent->SetLChild(beta);//set right child of pivot as left child of root
if(beta!=NULL)
beta->SetParent(grandParent);
return root;
}
AVL* AVL::RR_Rotation(AVL* node)
{
AVL *root=NULL;
AVL *parent=node;
AVL *grandParent=NULL;
AVL *beta=NULL;
AVL *GGPArent=NULL;
grandParent=parent->GetParent();
beta=parent->GetLChild();
GGPArent=grandParent->GetParent(); // write func for when GGParent doesnot exist
if(GGPArent!=NULL)
{
if(GGPArent->GetLChild()==grandParent)
{
GGPArent->SetLChild(parent);
}
else
{
GGPArent->SetRChild(parent);// set pivot to point to the parent of grandParent
}
parent->SetParent(GGPArent);
}
else
{
root=parent;
parent->SetParent(NULL);
}
parent->SetLChild(grandParent);//set grandParent as pivots right child
grandParent->SetParent(parent);
grandParent->SetRChild(beta);//set right child of pivot as left child of root
if(beta!=NULL)
beta->SetParent(grandParent);
return root;
}
bool AVL::Search(int n,AVL *root)
{
if(root->GetKey()>n)//go left
{
if(root->GetLChild()!=NULL)
return Search(n,root->GetLChild());
else
return false;
}
else if(root->GetKey()<n)//go right
{
if(root->GetRChild()!=NULL)
return Search(n,root->GetRChild());
else
return false;
}
else if (root->GetKey()==n)
return true;
else
return false;
}
int AVL::getHeight(AVL* node)
{
int l=0;
int r=0;
if(node->GetLChild()!=NULL)
{
l= getHeight(node->GetLChild());
}
if(node->GetRChild()!=NULL)
{
r=getHeight(node->GetRChild());
}
return l>r?1+l:1+r;
}
void AVL::InorderPrint(AVL* root)
{
if(root->GetLChild()==NULL)
{
cout<<root->GetKey()<<" ";
}
else
{
InorderPrint(root->GetLChild());
cout<<root->GetKey()<<" ";
}
if(root->GetRChild()!=NULL)
{
InorderPrint(root->GetRChild());
}
}
void AVLInsert()
{
}
|