情景引入—小小日记
6/1的我开开心心的度过儿童节,复习一哈数据结构课上的内容。
but!,这红红的一条让我心慌。不可创建变量!
下面是当时的源码(二叉排序树)
#include <iostream>
using namespace std;
typedef int datatype;
struct Node{
datatype data =0;
Node *lchild = NULL, *rchild = NULL;
};
void Insert(Node *root,datatype data){
Node *p=NULL;
if(root==NULL){
p= (Node *)malloc(sizeof(Node));
p->data=data;
p->lchild=NULL;
p->rchild=NULL;
root=p;
}else
if(data < root->data){
Insert(root->lchild,data);
}else{
Insert(root->rchild,data);
};
return ;
}
void Print(Node *root){
if(root==NULL)return;
Print(root->lchild);
cout<<root->data;
Print(root->rchild);
}
datatype a[100];
int main(){//Binary_Search_Tree
int n;
datatype data;
Node * BST=NULL;
cout<<"输入插入个数"<<endl;
cin>>n;
//datatype a[n];
cout<<"输入插入数据"<<endl;
for(int i=0;i<n;i++){
cin>>a[i];
}
//63 90 70 55 67 42 98
for(int i=0;i<n;i++){
Insert(BST,a[i]);
}
Print(BST);
return 0;
}
聪明的你能发现错误吗?
#include<iostream>
using namespace std;
void f1(int *c,int d){
cout<<*c<<endl;
cout<<c<<endl;
cout<<&c<<" #将b所指的变量地址 赋给 新的指针变量c 使c指向a#"<<endl;
cout<<endl;
//cout<<d<<endl;
//cout<<&d<<endl;
//调用函数完成后会把c销毁
}
void f2(int *&c,int d){
cout<<*c<<endl;
cout<<c<<endl;
cout<<&c<<" #引用原b地址#"<<endl;
cout<<endl;
//cout<<d<<endl;
//cout<<&d<<endl;
//此时c即是b的别名,修改c即是修改b
}
int main(){
int a=1;
int *b=&a;
cout<<a<<endl;
cout<<&a<<" #&a#"<<endl;
cout<<endl;
cout<<*b<<" #a#"<<endl;
cout<<b<<" #&a#"<<endl;
cout<<&b<<" #&b#"<<endl;
cout<<endl;
f1(b,a);
f2(b,a);
return 0;
}
这下终于搞懂了吧,所以二叉排序树的的插入有以下两种写法:
1.这种写法好理解些,但是申请了临时结构体指针变量root
#include <iostream>
using namespace std;
typedef int datatype;
//63 90 70 55 67 42 98
struct Node{
datatype data;
Node *lchild, *rchild;
};
Node *BST=NULL;
Node* Insert(Node *root,datatype data){
Node *p=NULL;
if(root==NULL){
p= (Node *)malloc(sizeof(Node));
p->data=data;
p->lchild=NULL;
p->rchild=NULL;
root=p;
}
else if(data < root->data){
root->lchild=Insert(root->lchild,data);
}else{
root->rchild=Insert(root->rchild,data);
}
return root;
}
void Print(Node *root){
if(root==NULL)return;
Print(root->lchild);
cout<<root->data<<" ";
Print(root->rchild);
}
datatype a[100];
int main(){//Binary_Search_Tree
int n;
//datatype data;
cout<<"输入插入个数"<<endl;
cin>>n;
//datatype a[n];
cout<<"输入插入数据"<<endl;
for(int i=0;i<n;i++){
cin>>a[i];
}
for(int i=0;i<n;i++){
BST=Insert(BST,a[i]);
}
Print(BST);
return 0;
}
2.此写法简单但是对*&root要理解为传入指针的引用。
#include <iostream>
using namespace std;
typedef int datatype;
//63 90 70 55 67 42 98
struct Node{
datatype data;
Node *lchild, *rchild;
};
Node *BST=NULL;
void Insert(Node *&root,datatype data){
Node *p=NULL;
if(root==NULL){
p= (Node *)malloc(sizeof(Node));
p->data=data;
p->lchild=NULL;
p->rchild=NULL;
root=p;
}
else if(data < root->data){
Insert(root->lchild,data);
}else{
Insert(root->rchild,data);
}
}
void Print(Node *root){
if(root==NULL)return;
Print(root->lchild);
cout<<root->data<<" ";
Print(root->rchild);
}
datatype a[100];
int main(){//Binary_Search_Tree
int n;
cout<<"输入插入个数"<<endl;
cin>>n;
cout<<"输入插入数据"<<endl;
for(int i=0;i<n;i++){
cin>>a[i];
}
for(int i=0;i<n;i++){
Insert(BST,a[i]);
}
Print(BST);
return 0;
}