You are on page 1of 28

AVL

1.
AVL .
public class AvlNode {
int data;
AvlNode left;
AvlNode right;
int height;
public AvlNode(int value) {
this.value = value;
left = null;
public class AvlTree {
right = null;
AvlNode position;
height = 0;
AvlNode avlTree;
}
public AvlNode makeEmpty(AvlNode t) {
if(t != null) {
makeEmpty(t.left);
makeEmpty(t.right);
t = null;
}
return null;
}


public static int height (AvlNode position) {
if (position == null)
return -1;
else
return position.height;
}
public static int max (int lhs, int rhs) {
return lhs > rhs ? lhs : rhs;
}

:
1
:
(height(k2.left) - height(k2.right)) == 2
E
( insert) (k1)
aj (k2):
k1 k2 ( k2>k1)
k1,
X, Y Z :
o X k1 k2 => k1,
o Y k1, k2=> k2,
o Z k2 k1 => k2.


/* Ovaa funkcija moze da se povika samo ako k2 ima levo dete
*/
/* Pravi rotacija pomegu jazel (k2) i negovoto levo dete */
/* Se azuriraat visinite, a potoa se vraka noviot koren */

public static AvlNode singleRotateWithLeft (AvlNode k2) {


AvlNode k1;
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; /* Noviot koren */
}

: (k2)
(k1) (k3)
k3.

/* Ovaa funkcija moze da se povika samo ako k3 ima levo dete */


/* i levoto dete na K3 ima desno dete */
/* Se pravi leva desna rotacija */
/* se vraka noviot koren */
public static AvlNode doubleRotateWithLeft (AvlNode k3) {
k3.left = singleRotateWithRight(k3.left);
return singleRotateWithLeft(k3);
}

:
1
:
(height( k1.right ) - height( k1.left )) == 2
E
( insert) (k2)
aj (k1):
k2 k1 ( k2>k1)
k2,
X, Y Z :
o X k1 k2 => k1,
o Y k2, k1 => k1,
o Z k2 k1 => k2 .


/* Ovaa funkcija moze da se povika samo ako k1 ima desno dete */
/* Pravi rotacija pomegu jazel (k1) i negovoto desno dete */
/* Se azuriraat visinite, a potoa se vraka noviot koren */
public static AvlNode singleRotateWithRight (AvlNode k1) {
AvlNode 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; /* Noviot koren */

: (k2)
(k1) (k3)
k3.

/*
/*
/*
/*

Ovaa funkcija moze da se povika samo ako k3 ima desno dete */


i desnoto dete na K3 ima levo dete */
Se pravi desna leva rotacija */
se vraka noviot koren */

public static AvlNode doubleRotateWithRight (AvlNode k3) {


k3.right = singleRotateWithLeft(k3.right);
return singleRotateWithRight(k3);
}

3.
AVL .
public AvlNode insert (int value, AvlNode t) {
if(t == null) {
t = new AvlNode(value);}
else if(value < t.data) {
t.left=insert(value,t.left);
if(height(t.left)-height(t.right) == 2)
if(value < t.left.data)
t = singleRotateWithLeft(t);
else
t = doubleRotateWithLeft(t);
}
else if(value > t.data) {
t.right=insert(value,t.right);
if(height(t.right)-height(t.left) == 2)
if(value > t.right.data)
t = singleRotateWithRight(t);
else
t = doubleRotateWithRight(t);
}
t.height= max(height(t.left), height(t.right))+1;
return t;
}


public class Btree {

private int[] keys;


private Btree[] subs;
private int order;
public Btree (int n) {
keys = new int[n-1];
subs = new Btree[n];
order = n;
}
+ :
:
( ) .
.

You might also like