xxxxxxxxxx
private void RightRotation(AVLTree<T> v){
int BF_right = MAX_VALUE;
if(v.getRight() != null){
BF_right = v.getRight().getBF();
if(BF_right == 1){
RightLeft(v);
RightRightRotate(v);
}else if(BF_right <= 0){
RightRightRotate(v);
}
}
}
private void RightLeft(AVLTree<T> v){
AVLTree<T> v_right = null;
AVLTree<T> v_right_left = null;
AVLTree<T> v_right_left_right = null;
if(v.right != null){
v_right = v.right;
v.right.height -=1;
if(v.right.left != null){
v.right.left.height +=1;
v_right_left = v.right.left;
if(v.right.left.right != null){
v_right_left_right = v.right.left.right;
}
}
v.left.right = v_right_left_right;
v.left = v_right_left;
v.left.left = v_right;
}
}
private void RightRightRotate(AVLTree<T> v){
v.height -=2;
AVLTree<T> v_right= null;
AVLTree<T> v_right_left= null;
AVLTree<T> v_right_right= null;
if(v.right != null){
v_right = v.right;
if(v.right.left != null){
v_right_left = v.right.left;
}
if(v.right.right != null){
v_right_right = v.right.right;
}
AVLTree<T> new_v = new AVLTree<T>(v.val);
new_v.right = v.right;
new_v.left = v.left;
v.left = new_v;
v.val = v.right.val;
v.left.right = v_right_left;
v.right = v_right_right;
}
}
private void LeftRotation(AVLTree<T> v){
int BF_left = MAX_VALUE;
if(v.getLeft() != null){
BF_left = v.getLeft().getBF();
if(BF_left == -1){
leftRightRotate(v);
leftLeftRotate(v);
}else if (BF_left >= 0){
leftLeftRotate(v);
}
}
}
private void leftLeftRotate(AVLTree<T> v){
v.height -=2;
AVLTree<T> v_left = null;
AVLTree<T> v_left_right= null;
AVLTree<T> v_left_left= null;
if(v.left != null){
v_left = v.left;
if(v.left.right != null){
v_left_right = v.left.right;
}
if(v.left.left != null){
v_left_left = v.left.left;
}
AVLTree<T> new_v = new AVLTree<T>(v.val);
new_v.left = v.left;
new_v.right = v.right;
v.right = new_v;
v.val = v.left.val;
v.right.left = v_left_right;
v.left = v_left_left;
}
}
private void leftRightRotate(AVLTree<T> v){
AVLTree<T> v_left = null;
AVLTree<T> v_left_right = null;
AVLTree<T> v_left_right_left = null;
if(v.left != null){
v_left = v.left;
v.left.height -=1;
if(v.left.right != null){
v.left.right.height +=1;
v_left_right = v.left.right;
if(v.left.right.left != null){
v_left_right_left = v.left.right.left;
}
}
v.left.right = v_left_right_left;
v.left = v_left_right;
v.left.left = v_left;
}
}
xxxxxxxxxx
z z x
/ \ / \ / \
y T4 Left Rotate (y) x T4 Right Rotate(z) y z
/ \ - - - - - - - - -> / \ - - - - - - - -> / \ / \
T1 x y T3 T1 T2 T3 T4
/ \ / \
T2 T3 T1 T2
xxxxxxxxxx
#include <stdio.h>
#include "avltree.h"
/*
remove all nodes of an AVL tree
*/
void dispose(node* t)
{
if( t != NULL )
{
dispose( t->left );
dispose( t->right );
free( t );
}
}
/*
find a specific node's key in the tree
*/
node* find(int e, node* t )
{
if( t == NULL )
return NULL;
if( e < t->data )
return find( e, t->left );
else if( e > t->data )
return find( e, t->right );
else
return t;
}
/*
find minimum node's key
*/
node* find_min( node* t )
{
if( t == NULL )
return NULL;
else if( t->left == NULL )
return t;
else
return find_min( t->left );
}
/*
find maximum node's key
*/
node* find_max( node* t )
{
if( t != NULL )
while( t->right != NULL )
t = t->right;
return t;
}
/*
get the height of a node
*/
static int height( node* n )
{
if( n == NULL )
return -1;
else
return n->height;
}
/*
get maximum value of two integers
*/
static int max( int l, int r)
{
return l > r ? l: r;
}
/*
perform a rotation between a k2 node and its left child
note: call single_rotate_with_left only if k2 node has a left child
*/
static node* single_rotate_with_left( node* k2 )
{
node* k1 = NULL;
k1 = k2->left;
k2->left = k1->right;
k1->right = k2;
k2->height = max( height( k2->left ), height( k2->right ) ) + 1;
k1->height = max( height( k1->left ), k2->height ) + 1;
return k1; /* new root */
}
/*
perform a rotation between a node (k1) and its right child
note: call single_rotate_with_right only if
the k1 node has a right child
*/
static node* single_rotate_with_right( node* k1 )
{
node* k2;
k2 = k1->right;
k1->right = k2->left;
k2->left = k1;
k1->height = max( height( k1->left ), height( k1->right ) ) + 1;
k2->height = max( height( k2->right ), k1->height ) + 1;
return k2; /* New root */
}
/*
perform the left-right double rotation,
note: call double_rotate_with_left only if k3 node has
a left child and k3's left child has a right child
*/
static node* double_rotate_with_left( node* k3 )
{
/* Rotate between k1 and k2 */
k3->left = single_rotate_with_right( k3->left );
/* Rotate between K3 and k2 */
return single_rotate_with_left( k3 );
}
/*
perform the right-left double rotation
notes: call double_rotate_with_right only if k1 has a
right child and k1's right child has a left child
*/
static node* double_rotate_with_right( node* k1 )
{
/* rotate between K3 and k2 */
k1->right = single_rotate_with_left( k1->right );
/* rotate between k1 and k2 */
return single_rotate_with_right( k1 );
}
/*
insert a new node into the tree
*/
node* insert(int e, node* t )
{
if( t == NULL )
{
/* Create and return a one-node tree */
t = (node*)malloc(sizeof(node));
if( t == NULL )
{
fprintf (stderr, "Out of memory!!! (insert)\n");
exit(1);
}
else
{
t->data = e;
t->height = 0;
t->left = t->right = NULL;
}
}
else if( e < t->data )
{
t->left = insert( e, t->left );
if( height( t->left ) - height( t->right ) == 2 )
if( e < t->left->data )
t = single_rotate_with_left( t );
else
t = double_rotate_with_left( t );
}
else if( e > t->data )
{
t->right = insert( e, t->right );
if( height( t->right ) - height( t->left ) == 2 )
if( e > t->right->data )
t = single_rotate_with_right( t );
else
t = double_rotate_with_right( t );
}
/* Else X is in the tree already; we'll do nothing */
t->height = max( height( t->left ), height( t->right ) ) + 1;
return t;
}
/*
remove a node in the tree
*/
node* delete( int e, node* t )
{
printf( "Sorry; Delete is unimplemented; %d remains\n", e );
return t;
}
/*
data data of a node
*/
int get(node* n)
{
return n->data;
}
/*
Recursively display AVL tree or subtree
*/
void display_avl(node* t)
{
if (t == NULL)
return;
printf("%d",t->data);
if(t->left != NULL)
printf("(L:%d)",t->left->data);
if(t->right != NULL)
printf("(R:%d)",t->right->data);
printf("\n");
display_avl(t->left);
display_avl(t->right);
}
Code language: C++ (cpp)
xxxxxxxxxx
package il.ac.telhai.ds.trees;
public class AVLTree<T extends Comparable<T>>{
private static final int MAX_VALUE = 999999999;
public T val;
public AVLTree<T> left;
public AVLTree<T> right;
public int height;
//add the value to the tree, and return the updated root of the tree.
public AVLTree(T value){
this.val = value;
left = null;
right = null;
height = 0;
}
//return the value in this node
public T getValue(){
return val;
}
//return the left subTree of this node
public AVLTree<T> getLeft(){
return left;
}
//return the right subTree of this node
public AVLTree<T> getRight(){
return right;
}
public AVLTree<T> add(T val) {
if(val == null) return null;
if(this.val.compareTo(val) < 0){
if(this.right == null){
this.height += 1;
this.right = new AVLTree<T>(val);
return this;
}
this.height += 1;
this.right = this.right.add(val);
}else if(this.val.compareTo(val) > 0){
if(this.left == null){
this.height += 1;
this.left = new AVLTree<T>(val);
return this;
}
this.height += 1;
this.left = this.left.add(val);
}else{
return null;
}
int V_BF = this.getBF();
switch(V_BF){
case 2:{
LeftRotation(this);
break;
}
case -2:{
RightRotation(this);
break;
}
}
return this;
}
private int getBF(){
return HackHeight(left) - HackHeight(right);
}
private int GetHight(){
return HackHeight(this);
}
private int HackHeight(AVLTree<T> node){
int lef=0,righ=0;
if(node == null) return -1;
lef= HackHeight(node.getLeft());
righ= HackHeight(node.getRight());
return Math.max(righ, lef)+1;
}
}
xxxxxxxxxx
class treeNode(object):
def __init__(self, value):
self.value = value
self.l = None
self.r = None
self.h = 1
class AVLTree(object):
def insert(self, root, key):
if not root:
return treeNode(key)
elif key < root.value:
root.l = self.insert(root.l, key)
else:
root.r = self.insert(root.r, key)
root.h = 1 + max(self.getHeight(root.l),
self.getHeight(root.r))
b = self.getBal(root)
if b > 1 and key < root.l.value:
return self.rRotate(root)
if b < -1 and key > root.r.value:
return self.lRotate(root)
if b > 1 and key > root.l.value:
root.l = self.lRotate(root.l)
return self.rRotate(root)
if b < -1 and key < root.r.value:
root.r = self.rRotate(root.r)
return self.lRotate(root)
return root
def lRotate(self, z):
y = z.r
T2 = y.l
y.l = z
z.r = T2
z.h = 1 + max(self.getHeight(z.l),
self.getHeight(z.r))
y.h = 1 + max(self.getHeight(y.l),
self.getHeight(y.r))
return y
def rRotate(self, z):
y = z.l
T3 = y.r
y.r = z
z.l = T3
z.h = 1 + max(self.getHeight(z.l),
self.getHeight(z.r))
y.h = 1 + max(self.getHeight(y.l),
self.getHeight(y.r))
return y
def getHeight(self, root):
if not root:
return 0
return root.h
def getBal(self, root):
if not root:
return 0
return self.getHeight(root.l) - self.getHeight(root.r)
def preOrder(self, root):
if not root:
return
print("{0} ".format(root.value), end="")
self.preOrder(root.l)
self.preOrder(root.r)
Tree = AVLTree()
root = None
root = Tree.insert(root, 1)
root = Tree.insert(root, 2)
root = Tree.insert(root, 3)
root = Tree.insert(root, 4)
root = Tree.insert(root, 5)
root = Tree.insert(root, 6)
# Preorder Traversal
print("Preorder traversal of the",
"constructed AVL tree is")
Tree.preOrder(root)
print()
xxxxxxxxxx
z z x
/ \ / \ / \
y T4 Left Rotate (y) x T4 Right Rotate(z) y z
/ \ - - - - - - - - -> / \ - - - - - - - -> / \ / \
T1 x y T3 T1 T2 T3 T4
/ \ / \
T2 T3 T1 T2