<分区>
我一直了解到,仅通过引用传递变量意味着该变量将保留对函数中变量所做的任何更改,并且函数的工作不会改变,但在这种情况下,函数的工作方式完全不同。 唯一的区别在于两个代码中这个函数的定义。 无效旋转(节点*&); 无效旋转(节点*);
这是第一个代码
#include<iostream>
#include<stack>
using namespace std;
struct node
{
char data;
node* left;
node* right;
node()
{
left=0;
right=0;
}
};
class AVL
{
private:
node* root;
public:
AVL()
{
root=NULL;
}
void initialize();
void rotate(node*&);
node*& return_n()
{
return root->left;
}
void traverse();
};
void AVL::initialize()
{
root=new node;
root->data='a';
root->left=new node;
root->left->data='b';
root->left->left=new node;
root->left->left->data='c';
root->left->left->left=new node;
root->left->left->left->data='d';
}
void AVL::traverse()
{
node* temp=root;
if(temp!=0)
{
stack<node*> s;
s.push(temp);
while(!s.empty())
{
temp=s.top();
s.pop();
cout<<endl<<temp->data<<endl;
if(temp->left!=0)
{
s.push(temp->left);
cout<<"Left: "<<s.top()->data<<endl;
}
else
{
cout<<"Left: NULL"<<endl;
}
if(temp->right!=0)
{
s.push(temp->right);
cout<<"Right: "<<s.top()->data<<endl;
}
else
{
cout<<"Right: NULL ";
}
}
}
}
void AVL::rotate(node*& temp)
{
node* temp2=temp->left;
cout<<endl<<"Step1"<<endl;
cout<<endl<<"Temp"<<temp->data<<endl;
cout<<endl<<"Temp2"<<temp2->data<<endl;
traverse();
temp->left=temp2->right;
cout<<endl<<endl<<endl;
cout<<endl<<"Step2"<<endl;
cout<<endl<<"Temp"<<temp->data<<endl;
cout<<endl<<"Temp2"<<temp2->data<<endl;
traverse();
temp2->right=temp;
cout<<endl<<endl<<endl;
cout<<endl<<"Step3"<<endl;
cout<<endl<<"Temp"<<temp->data<<endl;
cout<<endl<<"Temp2"<<temp2->data<<endl;
traverse();
temp=temp2;
cout<<endl<<endl<<endl;
cout<<endl<<"Step4"<<endl;
cout<<endl<<"Temp"<<temp->data<<endl;
cout<<endl<<"Temp2"<<temp2->data<<endl;
traverse();
system("pause");
}
int main()
{
AVL obj;
obj.initialize();
obj.traverse();
obj.rotate(obj.return_n());
return 0;
}
这是第二个代码
#include<iostream>
#include<stack>
using namespace std;
struct node
{
char data;
node* left;
node* right;
node()
{
left=0;
right=0;
}
};
class AVL
{
private:
node* root;
public:
AVL()
{
root=NULL;
}
void initialize();
void rotate(node*);
node*& return_n()
{
return root->left;
}
void traverse();
};
void AVL::initialize()
{
root=new node;
root->data='a';
root->left=new node;
root->left->data='b';
root->left->left=new node;
root->left->left->data='c';
root->left->left->left=new node;
root->left->left->left->data='d';
}
void AVL::traverse()
{
node* temp=root;
if(temp!=0)
{
stack<node*> s;
s.push(temp);
while(!s.empty())
{
temp=s.top();
s.pop();
cout<<endl<<temp->data<<endl;
if(temp->left!=0)
{
s.push(temp->left);
cout<<"Left: "<<s.top()->data<<endl;
}
else
{
cout<<"Left: NULL"<<endl;
}
if(temp->right!=0)
{
s.push(temp->right);
cout<<"Right: "<<s.top()->data<<endl;
}
else
{
cout<<"Right: NULL ";
}
}
}
}
void AVL::rotate(node* temp)
{
node* temp2=temp->left;
cout<<endl<<"Step1"<<endl;
cout<<endl<<"Temp"<<temp->data<<endl;
cout<<endl<<"Temp2"<<temp2->data<<endl;
traverse();
temp->left=temp2->right;
cout<<endl<<endl<<endl;
cout<<endl<<"Step2"<<endl;
cout<<endl<<"Temp"<<temp->data<<endl;
cout<<endl<<"Temp2"<<temp2->data<<endl;
traverse();
temp2->right=temp;
cout<<endl<<endl<<endl;
cout<<endl<<"Step3"<<endl;
cout<<endl<<"Temp"<<temp->data<<endl;
cout<<endl<<"Temp2"<<temp2->data<<endl;
traverse();
temp=temp2;
cout<<endl<<endl<<endl;
cout<<endl<<"Step4"<<endl;
cout<<endl<<"Temp"<<temp->data<<endl;
cout<<endl<<"Temp2"<<temp2->data<<endl;
traverse();
system("pause");
}
int main()
{
AVL obj;
obj.initialize();
obj.traverse();
obj.rotate(obj.return_n());
return 0;
}