Click here to Skip to main content
12,511,648 members (46,330 online)
Rate this:
 
Please Sign up or sign in to vote.
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;
 
}




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 29-Jan-13 8:51am
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
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 :(
Andreas Gieriet 29-Jan-13 15:27pm
   
What "yes"?
So you did run the debugger and stepped through the code?

If you don't know running a program in the debugger you better learn this first. It's not magic: you need to set a breakpoint at the location you want the program to stop in order to do single stepping after that and to look at the variables.
Check your debugger documentation on how to set a breakpoint, run the program to the breakpoint, do single step, look at variable contents, finally continue and/or stop debugging.

If you can not spend the time to learn the debugger well enough to debug your programs, you are screwed up. That's basic.

If this is your code, you should be familiar with it to debug.

If this is homework assignment, please lable it as such.

Cheers
Andi

BTW: This is from the style plain C code (not C++ code), except for the cin/cout io.
mahla.r_1993 29-Jan-13 15:57pm
   
i know how to use debugger but i can not find my problem :(

help !!!!!!
Andreas Gieriet 29-Jan-13 17:41pm
   
Some comments:
- The balance functions returns an weird value:
return (h(...), h(...));.
This is the comma operator - by intension?
- What is your main function? Where do these numbers in your original questions come from?
- newNode is a mess, remove is a mess too
- Why do you use malloc in C++ - this is crap, sorry.

I'm tempted to give up with such a code base and start from scratch: What do you want to achieve? What is the program supposed to do?

Andi
jibesh 29-Jan-13 20:22pm
   
Yes as Andi said in his last comment , you only said you are having problem but what is the problem? is it the expected output 10,20,30,40 is not getting the out when you call preorder? at which point you are invoking the preorder method?

by looking at the preorder method its obvious that it prints left and right child of the rootNode recursively so the given out put looks ok to me.

i.e
30
10 20

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160929.1 | Last Updated 29 Jan 2013
Copyright © CodeProject, 1999-2016
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