0% found this document useful (0 votes)
10 views11 pages

WeakAVLMap Java

script of weak all map java

Uploaded by

Diptanshu Rajan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views11 pages

WeakAVLMap Java

script of weak all map java

Uploaded by

Diptanshu Rajan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

package col106.assignment4.

WeakAVLMap;
import java.util.Vector;
import java.util.Queue;
import java.util.LinkedList;

public class WeakAVLMap<K extends Comparable,V> implements


WeakAVLMapInterface<K,V>{

public int rotater=0;


public Node<K,V> root;
public Node<K, V> current;
public WeakAVLMap(){
root = null;

public class Node<K,V>{


K key;
V value;
int rank;
Node<K,V> parent;
Node<K,V> left;
Node<K,V> right;

public Node(K key, V value) {

if(key==null && value ==null){


this.key = null;
this.value = null;
this.rank=0;
this.left=null;
this.right=null;
}

else{
this.key = key;
this.value = value;
this.rank=1;
this.left=new Node<K,V>(null,null);
this.right=new Node<K,V>(null,null);
}

}
}

public int rank_diff(Node child){


return (child.parent.rank-child.rank);
}
public void left_left(Node x){

Node mid = x.parent;


Node top = mid.parent;
Node sup_top = null;

int breaker=0;
if(top.parent!=null) sup_top = top.parent;
else breaker=1;

top.rank--;
if(breaker==0){
if(sup_top.left==top) sup_top.left=mid;
if(sup_top.right==top) sup_top.right=mid;
}
mid.parent=sup_top;

top.left=mid.right;
mid.right.parent=top;

mid.right =top;
top.parent = mid;

if(breaker==1) root = mid;

rotater++;
}

public void left_right(Node x){


Node mid = x.parent;
Node top = mid.parent;
Node sup_top = null;

int breaker=0;
if(top.parent!=null) sup_top = top.parent;
else breaker=1;

x.rank++;
mid.rank--;
top.rank--;
if(breaker==0){
if(sup_top.left==top) sup_top.left=x;
if(sup_top.right==top) sup_top.right=x;
}

x.parent = sup_top;

mid.right=x.left;
x.left.parent=mid;

top.left=x.right;
x.right.parent=top;

x.left=mid;
x.right=top;

top.parent=x;
mid.parent=x;

if(breaker==1) root = x;

rotater = rotater+2;
}

public void right_right(Node x){


Node mid = x.parent;
Node top = mid.parent;
Node sup_top = null;

int breaker=0;
if(top.parent!=null) sup_top = top.parent;
else breaker=1;

top.rank--;
if(breaker==0){
if(sup_top.left==top) sup_top.left=mid;
if(sup_top.right==top) sup_top.right=mid;
}
mid.parent = sup_top;

top.right=mid.left;
mid.left.parent=top;

mid.left=top;
top.parent=mid;

if(breaker==1) root = mid;

rotater++;
}

public void right_left(Node x){

Node mid = x.parent;


Node top = mid.parent;
Node sup_top = null;

int breaker=0;
if(top.parent!=null) sup_top = top.parent;
else breaker=1;

x.rank++;
mid.rank--;
top.rank--;
if(breaker==0){
if(sup_top.left==top) sup_top.left=x;
if(sup_top.right==top) sup_top.right=x;
}

x.parent=sup_top;

top.right=x.left;
x.left.parent=top;

mid.left=x.right;
x.right.parent=mid;

x.left=top;
x.right=mid;

top.parent=x;
mid.parent=x;

if(breaker==1) root = x;

rotater = rotater+2;
}
public void rearange(Node x){
int my_count=0;
Node store = x;
Node sibling =null;
Node parent = null;
int checker = rank_diff(x);
int del_check=0;
if(toDelete!=null){

if(toDelete.parent==null) del_check=1;
else del_check = rank_diff(toDelete);
}

if(checker==0 || del_check > 0 ){


while(rank_diff(x)==0 || del_check!=0){

parent = x.parent;

if(parent.left==x) {
sibling = parent.right;
sibling.parent=parent;
}
else{
sibling = parent.left;
sibling.parent=parent;
}

if(rank_diff(sibling)==1 && del_check == 0){


del_check=0;
parent.rank++;
x=x.parent;
my_count++;
if(x.parent==null) {
is_ext(x);
if(del_check==0){
remap_delete(x.left);
remap_delete(x.right);
}
break;
}

else {
x=store;
for(int i=0;i<my_count-1;i++){
x=x.parent;
}
store=x;
Node mid = x.parent;
Node top = mid.parent;
Node sup_top = null;

Node store_mid = mid;


Node store_top = top;

int breaker=0;
if(top.parent!=null) sup_top = top.parent;
else breaker=1;

//LEFT LEFT CASE


if(mid.left==x && top.left==mid){
if(del_check!=0) {
mid.rank++;
//top.rank--;
}
left_left(x);

is_ext(store);
is_ext(store_mid);
is_ext(store_top);

if(mid.parent==null) root=mid;
}

//LEFT RIGHT CASE


else if(mid.right==x && top.left==mid){
if(del_check!=0) {
x.rank++;
top.rank--;
}
left_right(x);
is_ext(store);
is_ext(store_mid);
is_ext(store_top);

if(x.parent==null) root=x;
}

//RIGHT RIGHT CASE


else if(mid.right==x && top.right==mid){
if(del_check!=0) {
mid.rank++;
//top.rank--;
}

right_right(x);

is_ext(store);
is_ext(store_mid);
is_ext(store_top);

if(mid.parent==null) root=mid;
}

//RIGHT LEFT CASE


else if(mid.left==x && top.right==mid){
if(del_check!=0) {
x.rank++;
top.rank--;
}
right_left(x);

is_ext(store);
is_ext(store_mid);
is_ext(top);

if(x.parent==null) root=x;
}

break;
}
}

}
public void is_ext(Node n){
if(n.left==null || n.right==null) return;
if(n.left.key==null && n.right.key==null) n.rank=1;
}
public V put(K key, V value){

Node<K, V> toInsert = new Node<K, V>(key, value);


V old_value = null;
if(root==null){
root = toInsert;
root.parent=null;
current = root;
return null;
}

current = root;

while(true){

if(current.key.equals(key)){
old_value=current.value;
current.value=value;
return old_value;
}

else if(key.compareTo(current.key)>0){
if(current.right.key==null){
current.right = toInsert;
toInsert.parent=current;

rearange(toInsert);
return null;
}
else current = current.right;
}
else if(key.compareTo(current.key)<0){
if(current.left.key==null){
current.left = toInsert;
toInsert.parent=current;

rearange(toInsert);
return null;
}
else current = current.left;
}
}

}
public Node toDelete;
public V remove(K key){

Node x=root;
V to_return=null;
while(x.key!=null){

if(x.key.equals(key)){
toDelete=x;
}

if(key.compareTo(x.key)<0) x=x.left;
else x=x.right;
}

if (toDelete==null || toDelete.key==null) return to_return;


to_return = (V)toDelete.value;

//BOTH EXTERNAL NODES OF toDelete


if(toDelete.left.key==null && toDelete.right.key==null){

if(toDelete==root){
to_return = (V)toDelete.value;
root=null;
toDelete = null;
return to_return;
}
else{
to_return = (V)toDelete.value;

toDelete.key = null;
toDelete.value = null;
toDelete.rank=0;
toDelete.left=null;
toDelete.right=null;

Node toDelete_parent = toDelete.parent;


is_ext(toDelete_parent);

remap_delete(toDelete);
remap_delete(toDelete_parent);

toDelete = null;
return to_return;
}
}

//ONLY one EXTERNAL NODE of toDelete


else if(toDelete.left.key==null || toDelete.right.key==null){

Node internal = null;


if(toDelete.left.key==null) internal = toDelete.right;
else internal = toDelete.left;

if(toDelete.parent==null){
root = internal;
toDelete = null;
return to_return;
}
else{
if(toDelete==toDelete.parent.left){
toDelete.parent.left=internal;
internal.parent=toDelete.parent;
}
else{
toDelete.parent.right=internal;
internal.parent=toDelete.parent;
}
to_return = (V)toDelete.value;
}
is_ext(internal);
remap_delete(internal);
remap_delete(internal.parent);

toDelete = null;
return to_return;
}

//BOTH INTERNAL NODE of toDelete


else{

Node temp = toDelete.right;

if(temp.left.key==null){

toDelete.key=temp.key;
toDelete.value=temp.value;
toDelete.right=temp.right;
temp.right.parent=toDelete;
is_ext(toDelete);
is_ext(temp.right);
remap_delete(temp.right);
remap_delete(toDelete);
remap_delete(toDelete.left);

toDelete = null;

return to_return;
}

else{
while(temp.left.key!=null){
temp=temp.left;
}

toDelete.key=temp.key;
toDelete.value=temp.value;

temp.parent.left=temp.right;
temp.right.parent=temp.parent;

is_ext(temp.parent);
remap_delete(temp.parent);
remap_delete(temp.parent.left);
remap_delete(temp.parent.parent);

toDelete = null;
return to_return;

}
}

public void remap_delete(Node x){


if(x==root) return;
if(rank_diff(x)==2) return;

if(rank_diff(x)>2){

while(rank_diff(x)!=2){

Node sibling = null;


if(x==x.parent.left) sibling = x.parent.right;
else sibling = x.parent.left;

if(rank_diff(sibling)==2){
x.parent.rank--;
x=x.parent;
if(x.parent==null) break;
}
else if(rank_diff(sibling)==1){
Node child1=sibling.left;
child1.parent=sibling;
Node child2=sibling.right;
child2.parent=sibling;

if(rank_diff(child1)==2 && rank_diff(child2)==2){

x.parent.rank--;
sibling.rank--;
x=x.parent;
if(x.parent==null) {
is_ext(x);
is_ext(sibling);
break;
}
}

else if(rank_diff(child1)==1 &&


rank_diff(child2)==1){

Node t = null;

if(sibling==x.parent.left) t = child1;
else t = child2;

Node t_mid = t.parent;


Node t_top = t_mid.parent;

rearange(t);

is_ext(t);
is_ext(t_mid);
is_ext(t_top);

break;
}
else if(rank_diff(child1)==1 ||
rank_diff(child2)==1){

Node t = null;
if(rank_diff(child1)==1) t = child1;
else t = child2;

Node t_mid = t.parent;


Node t_top = t_mid.parent;

rearange(t);

is_ext(t);
is_ext(t_mid);
is_ext(t_top);

break;
}

}
}

}
return;

}
public V get(K key){

Node x=root;

while(x.key!=null){

if(x.key.equals(key)){
return (V)x.value;
}

if(key.compareTo(x.key)<0) x=x.left;
else x=x.right;
}
return null;
}
public Vector in_order;
public void ranger(Node x,K key1, K key2){

toDelete=null;
if(x.key==null) return;

if(key1.compareTo(x.key)<0){

ranger(x.left,key1,key2);
}

if(key1.compareTo(x.key)<=0 && key2.compareTo(x.key)>=0){

in_order.add(x.value);

if(key2.compareTo(x.key)>0){

ranger(x.right,key1,key2);
}

return;
}

public Vector<V> searchRange(K key1, K key2){

in_order = new Vector();


ranger(root,key1,key2);
if(in_order.size()==0){
in_order.add(null);
}
return in_order;
}

public int rotateCount(){


return rotater;
}

public int real_height(Node n){


if(n==null) return 0;
if(n.key==null) return 0;
else{
return 1+max(real_height(n.left),real_height(n.right));
}
}

public int max(int p, int q){


if(p>q) return p;
else return q;
}

public int getHeight(){


int ans = real_height(root);
return ans;
}

public Vector<K> BFS(){

Vector ans = new Vector();


Queue <Node <K,V>> Q = new LinkedList <Node <K,V>> ();

Node<K, V> x = root;


Q.add(x);
while(Q.size()>0) {

x=Q.poll();
if(x==null) break;
ans.add(x.key);
//System.out.println(x.key+" "+x.rank);
if(x.left.key!=null && x.right.key!=null) {

Q.add(x.left);
Q.add(x.right);
}

else if(x.left.key!=null) {

Q.add(x.left);
}
else if(x.right.key!=null) {

Q.add(x.right);
}

}
return ans;
}
}

You might also like