Click here to Skip to main content
15,885,896 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
node* rightrotate(struct node *root)
{
    node *node1;
    node1=root->left;
    root->left=node1->right;
    node1->right=root;
    root->h = max(h(root->left), h(root->right));
    node1->h = max(h(node1->left), h(node1->right));
    return node1;
}
node* leftrotate(struct node *root)
{

    node *node1;
    node1=root->right;
    root->right =node1->left;
    node1->left =root;
    root->h = max(h(root->left), h(root->right));
    node1->h = max(h(node1->left), h(node1->right));

    return node1;

}





C#
void preorder(struct node *root)
{
    if(root !=NULL)
    {
        cout<<root->key<<endl;
        preorder(root->right);
        preorder(root->left);
    }





example:

when i get 10 20 30 40 50 25

when i use preorder function

: 30 20 10 20 40 50
Posted
Comments
Andreas Gieriet 29-Jan-13 14:56pm    
Some code is missing.
Did you run the debugger? If not, go an do it.
Andi
mahla.r_1993 29-Jan-13 14:58pm    
#include "stdafx.h"
#include<iostream>
using namespace std ;

struct node
{
int key ;
int h;
node *left;
node *root;
node *right;
};





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->left) , h(n->right));


}





node* rightrotate(struct node *root)
{
node *node1;
node1=root->left;
root->left=node1->right;
node1->right=root;
root->h = max(h(root->left), h(root->right));
node1->h = max(h(node1->left), h(node1->right));
return node1;
}
node* leftrotate(struct node *root)
{

node *node1;
node1=root->right;
root->right =node1->left;
node1->left =root;
root->h = max(h(root->left), h(root->right));
node1->h = max(h(node1->left), h(node1->right));

return node1;

}





void inorder(struct node *root)
{
if(root !=NULL)
{
inorder(root->left);
cout<<root->key<<endl;
inorder(root->right);
}
}

void preorder(struct node *root)
{
if(root !=NULL)
{
cout<<root->key<<endl;
preorder(root->right);
preorder(root->left);
}


}



struct node *remove(struct node *root, int key)
{
node* successor(struct node *x);

struct node *remove_node;
if (root == NULL)
{
return root;
}

if ( key < root->key)
{
root->left = remove(root->left, key);

}
else if ( key > root->key)
{
root->right = remove(root->right,key);

}
else {

if ((root->left == NULL) && (root->right != NULL)){
remove_node = root->right;
*root = *remove_node;
free(remove_node); // this is for free-ing the memory

} else if ((root->right == NULL) && (root->left != NULL)){
remove_node = root->left;
*root = *remove_node;
free(remove_node);

} else if ((root->right == NULL) && (root->left == NULL)){
remove_node = root;
root = NULL;

} //else {

// remove_node = successor(root);
// root->key = remove_node->key;
// root->right = remove(root->right, remove_node->key);
// }
}

if (root == NULL) {
return root;
//lr
if (balanceavl(root) == 2){
if (balanceavl(root->left) == -1) {
root->left = leftrotate(root->left);
return rightrotate(root);
//ll
} else if (balanceavl(root->left) == 1) {
return rightrotate(root);
}
}
//rr
if (balanceavl(root) == -2) {
if (balanceavl(root->right) == -1) {
return leftrotate(root);
}


//rl
else if (balanceavl(root->right) == 1) {
root->right = rightrotate(root->right);
return leftrotate(root);
}
}
}

return root;
}
struct node* newNode(int key)
{
struct node* node = (struct node*)
malloc(sizeof(struct node));
node->key = key;
node->left = NULL;
node->right = NULL;
node->h = 1; // new node is initially added at leaf
return(node);
}
struct node* insert(struct node* root, int key)
{
/* 1. Perform the normal BST rotation */
if (root == NULL)
return(newNode(key));

if (key < root->key)
root->left = insert(root->left, key);
else
root->right = insert(root->right, key);

/* 2. Update height of this ancestor node */
root->h= max(h(root->left), h(root->right)) + 1;

/* 3. Get the balance factor of this ancestor node to check whether
this node became unbalanced */
int balance = balanceavl(root);

// If this node becomes unbalanced, then there are 4 cases

if (root == NULL) {
return root;
//lr
if (balanceavl(root) == 2){
if (balanceavl(root->left) == -1) {
root->left = leftrotate(root->left);
return rightrotate(root);
//ll
} else if (balanceavl(root->left) == 1) {
return rightrotate(root);
}
}
//rr
if (balanceavl(root) == -2) {
mahla.r_1993 29-Jan-13 14:59pm    
it is my code i dont understand what is wrong in this code ?? i can not get a preorder fot avl tree:(
Andreas Gieriet 29-Jan-13 15:02pm    
Did you run the debugger?
Andi
mahla.r_1993 29-Jan-13 15:07pm    
yes i dont understand
plz help me :(

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900