Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++
#include "stdafx.h"
#include<iostream>
using namespace std ;
 
struct node
{
    int data ;
    int h;
    node *leftchild;
    node *current;
    node *rightchild;
};
 

 

 

int h(struct node* x)
{
    if (x == NULL)
        return 0;
    return x->h;
}
 

 

int balanceavl(struct node *n)
{
    if (n == NULL)
        return 0;
    return (h(n->leftchild) , h(n->rightchild));
 

}
 

 

 
node* ll(struct node *b)
{
    struct node *a = b->leftchild;
    struct node *c = a->rightchild;
 
    a->rightchild = b;
    b->leftchild = c;
 
    b->h = max(h(b->leftchild), h(b->rightchild))+1;
    a->h = max(h(a->leftchild), h(a->rightchild))+1;
 
    return a;
}
 

node* rr(struct node*a)
{
 struct node *b = a->rightchild;
 struct node *c = b->leftchild;
 a->rightchild = c->leftchild;
 b->leftchild = c->rightchild;
 c->rightchild = b;
 c->leftchild = a;
 a->h = max ( h(a->leftchild) , h(a->rightchild) ) + 1 ;
 b->h = max ( h(b->leftchild) , h(b->rightchild) ) + 1 ;
 c->h= max ( h(a) , h(b) ) + 1 ;
 return c;
}
 

 

 

node* insert( struct node *current,int data)
{
 
 if( current==NULL )
 {
    node* x = new node ;
    x->data = data;
    x->leftchild = NULL;
    x->rightchild = NULL;
    x->h = 1;
    return(x);
 }
 else if( data < current->data )
 {
  current->leftchild = insert(current->leftchild,data );
 }
 else
     current->rightchild =insert (current->rightchild,data);
 current->h =max(h(current ->leftchild), h(current->rightchild)) + 1;
 

 

 

 int b = balanceavl(current);
 

    //ll
 if (b > 1 && data < current->leftchild->data)
        return ll(current);
 
 //rr
 if (b < -1 && data > current->rightchild->data)
     return rr(current);
 
    //lr
 if (b > 1 && data> current->leftchild->data)
    {
        current->leftchild =  rr(current->leftchild);
        return ll(current);
    }
 
    //rl
 if (b < -1 && data < current->rightchild->data)
    {
        current->rightchild = ll(current->rightchild);
        return rr(current);
    }
 
 return current;
 

 

 
}
///////////////////////////////////////////////////////
void preorder(struct node *current)
{
    if(current != NULL)
    {
        cout<<current->data;
        preorder(current->leftchild);
 
        preorder(current->rightchild);
    }
}
//////////////////////////////////////

 

 
void inorder(struct node *current)
{
    if(current !=NULL)
    {
        inorder(current->leftchild);
        cout<<current->data;
        inorder(current->rightchild);
    }
 

}
 

 node* Remove(struct node *current , int data)
{
    if (current== NULL)
        return current ;
 
    if (data< current->data)
        current->leftchild = Remove(current->leftchild, data);
 
    else if(data > current->data )
        current->rightchild = Remove(current->rightchild, data);
 
    else
    {
 
        if( (current->leftchild == NULL) || (current->rightchild == NULL) )
        {
            struct node *temp = current->leftchild ? current->leftchild :current->rightchild;
 
            if(temp == NULL)
            {
                temp = current;
               current = NULL;
            }
            else
            {
                *current = *temp;
 
            }
 
            free(temp);
        }
        else
        {
            struct node* root = current->rightchild;
            while (root->leftchild != NULL)
                root = root->leftchild;
            struct node* temp = root;
           current->data= root->data;
           current->rightchild = Remove(current->rightchild, root->data);
        }
    }
    if (current == NULL)
        return current;
 
    current->h = max(h(current->leftchild), h(current->rightchild)) + 1;
    int b = balanceavl(current);
 
    //ll
    if (b > 1 && balanceavl(current->leftchild) >= 0)
        return ll(current);
 

    //lr
    if (b > 1 && balanceavl(current->leftchild) < 0)
    {
        current->leftchild =  rr(current->leftchild);
        return ll(current);
    }
 

    //rr
    if (b < -1 && balanceavl(current->rightchild) <= 0)
        return rr(current);
 

   //rl
    if (b < -1 && balanceavl(current->rightchild) > 0)
    {
        current->rightchild = ll(current->rightchild);
        return rr(current);
    }
 
    return current;
}
 

 

 

 

int main()
{
    struct node *current= NULL;
 
    int k,m;
 
    do{
 

        cout<<"if u want add item in avl tree plz enter 1\nif u want remove item from avl tree plz enter 2\nif u want show item plz enter 3\nif u want exit enter 0\n";
 

        cin >>k;
 
        switch(k)
        {
        case 1:
        {
            cout<<"enter your item:";
            cin>>m;
            current =insert(current, m);
            cout<<endl;
            break;
 
        }
        case 2:
        {
            cout<<"enter your item:";
            cin>>m;
            Remove(current,m);
            inorder(current);
            cout<<endl;
            break;
        }
        case 3:
        {
            cout<<"preorder: ";
            preorder(current);
            cout<<"\t"<<"inorder: ";
            inorder(current);
            cout<<endl;
            break;
 
        }
        default:
        cout<<" no item :( ";
        }
 

 
    }while(k!= 0);
 
    return 0;
}
Posted 27-Jan-13 21:45pm
Comments
Tadit Dash at 28-Jan-13 2:52am
   
Please be specific while asking question.
We cannot get anything by seeing this bunch of code.
What exactly you need?
J.Surjith Kumar at 29-Jan-13 5:12am
   
Don't expect anyone to read the entire code and create the test cases for your code. Be specific about what you want.
nv3 at 26-Oct-14 18:56pm
   
Before you even think about having anyone else look at your code, get the indentation corrected and delete any distracting empty line. If you think that is too much work, don't expect anyone else to spend a minute with your code.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

The best test case for Cod is always to deep fry it in a good batter and serve with chips and peas softened according to your lattitude. Salt and vinegar are optional but will only fail to improve the worst examples of the art.
Cod is therefore the only justification for the existence of Grimsby which would otherwise be considered a crime against civilisation. You can test this by going there, they'd be glad of the business. :-{}
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

It is your turn to test your code. Why arent you write so much code wihtout any ideas of using it.
 
My personal opinion is that you are doing bad things in begging other people to do your work.
 
Arent you afraid of bad karma?
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 304
1 Sergey Alexandrovich Kryukov 255
2 Maciej Los 250
3 Shweta N Mishra 216
4 PIEBALDconsult 174
0 OriginalGriff 7,660
1 Sergey Alexandrovich Kryukov 7,072
2 DamithSL 5,586
3 Manas Bhardwaj 4,946
4 Maciej Los 4,665


Advertise | Privacy | Mobile
Web03 | 2.8.1411023.1 | Last Updated 26 Oct 2014
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100