AVL TREE in C ++

I have a problem with this very simple block of code. please give me your advice. (My problem has been solved, and in solving this problem, the person with id stakx really helped me, the only problem was that I used stack <treeNode> when I closely followed the push method from the stack, there is a copy process when I I’m writing head-> object = number, so finally I created a stack of pointers, for example this stack <treeNode *>, and it really solved the problem, now I have no problems, I am very grateful to the stakx person.)

in front of the code you need to get the following tree

alt text http://img44.imageshack.us/img44/7016/avlimage06.jpg As you can see in the picture, the root is 8 and the stack has two nodes ie 6 and 4. I pass this stack and root node to the following code

void Avltree::attachwithtree(treeNode* tree, Stack<treeNode>&s)
{
if(!s.isempty())
{
treeNode *stacknode;
stacknode=s.pop();
cout<<"\ninside the attachwithtree function, stack node is "<<stacknode->data;
stacknode->right=tree;//attaching the passed node to the right of popped node
root=stacknode;//setting the root to stack node which is the private data member of class
updatebalance(root);//this function is ok, it does not create problem
while(!s.isempty())
{
cout<<"\nstack is still not empty";
stacknode=s.pop();
cout<<"\nright side of "<<root->data<<" is "<<(root->right)->data;
//the below three lines causing the problem i don't know why,
root=stacknode;
treeNode* temp;
temp=root->right;
cout<<"\n\n\nthe right side of "<<temp->data<<" is now "<<(temp->right)->data;
updatebalance(root);
}

the output of this function is given by the alt text formula
http://img704.imageshack.us/img704/5976/avlimage07.jpg


here is the stack pop method code I'm using

template <class t>
t * Stack<t>::pop()
{
if(topelement!=NULL)
{
t* num;
current=topelement;
num=&(current->object);
topelement=topelement->preptr;
current=topelement;
return(num);
}
else
{
head=NULL;
}
}


here is the stack push method code

template <class t>
void Stack<t>::push(t &number)
{
Node<t>* newNode=new Node<t>;
if(head==NULL)
{
head=newNode;
topelement=newNode;
current=newNode;
head->object=number;
head->preptr=NULL;
}
else
{
topelement=newNode;
newNode->preptr=current;
current=topelement;
newNode->object=number;
}
}
+5
source share
2 answers

Original answer:

, node 4 node 6 (, node 7 ), node 6 ( node 8 ) ? , , node 6.

:

:

void Avltree::attachwithtree(treeNode* tree, Stack<treeNode>&s)

s Stack<treeNode>.

, Stack<treeNode*>?

treeNode , X , X, X. , , , , !?

, , node 6 , node 7. , node . , node, .

, node 6. . .

node 4 . node 6, , , , , ! 7 node 6.

:

, , . ( ) ( ).

, , .

#include <iostream>

class someObject
{
private:
    int _value;
public:
    someObject(int value) : _value(value) { }

    int getValue()
    {
        return _value;
    }
};

void someFunction(someObject objCopy, someObject* objPtr)
{
    std::cout << "objCopy.getValue() -> " << objCopy.getValue() << std::endl;
    std::cout << "objPtr->getValue() -> " << objPtr->getValue() << std::endl;
    if ( &objCopy != objPtr )
    {
        std::cout << "objCopy is not actually *objPtr but a copy of it." << std::endl;
    }
    else
    {
        std::cout << "objCopy and *objPtr are one and the same object." << std::endl;
    }
}


int main()
{
    someObject X(17);
    someFunction(X, &X);

    return 0;
}

. , pop , , , , .

+4

Stack? STL , pop() void.

, stakx - . pop() , , . - ?

+1

All Articles