学习笔记 2017-3-2

来源:互联网 发布:usm锐化算法 编辑:程序博客网 时间:2024/06/04 17:58
&是取地址运算符,&i是变量i的地址。
一个变量的地址称为变量的指针。

如果有一个变量专门用来存放地址(即指针)的,则它称为指针变量。

如果把==错写成=的话,编译会完成,是不会有错误提示的。

C++中return语句的用法

二叉树:

#include<iostream>#include<string>using namespace std;#define N 8int level=0;bool found;struct Binode{char data;Binode *lchild=NULL,*rchild=NULL;};class Bitree{Binode Q[N];//void Release(Binode *bt){//if(bt!=NULL){//Release(bt->lchild);//Release(bt->rchild);//delete bt;//}//}public:struct Binode *root;Bitree(Binode *&bt,char a[],int low1,int high1,char b[],int low2,int high2);//~Bitree(){//Release(root);//}void Preorder(Binode *bt);//前序遍历void Inorder(Binode *bt);//zhongvoid Postorder(Binode *bt);//hou void Levelorder(Binode *bt);//层序遍历 int Locate(char a,Binode *bt);//求指定数据所在层数 };Bitree::Bitree(Binode *&bt,char a[],int low1,int high1,char b[],int low2,int high2){if((high1-low1)!=(high2-low2)){cout<<"error";}else if(low1>high1){bt=NULL;}else{Binode *t=new Binode;t->data=a[low1];bt=t; int s=low2;while(b[s]!=a[low1]&&s<high2){s++;}if(b[s]!=a[low1]) cout<<"ERROR";else{Bitree(t->lchild,a,low1+1,s-low2+low1,b,low2,s-1);Bitree(t->rchild,a,low1+s-low2+1,high1,b,s+1,high2);}}}void Bitree::Preorder(Binode *bt){if(bt==NULL) return;//是==号不是= else cout<<bt->data;Preorder(bt->lchild);Preorder(bt->rchild);}void Bitree::Inorder(Binode *bt){if(bt==NULL) return;Inorder(bt->lchild);cout<<bt->data;Inorder(bt->rchild);}void Bitree::Postorder(Binode *bt){if(bt==NULL) return;Postorder(bt->lchild);Postorder(bt->rchild);cout<<bt->data;}void Bitree::Levelorder(Binode *bt){int front,rear;front=rear=-1;if(bt==NULL) return;Q[++rear]=*bt;Binode *p;while(front!=rear){p=&Q[++front];cout<<p->data;if(p->lchild!=NULL) Q[++rear]=*p->lchild;if(p->rchild!=NULL) Q[++rear]=*p->rchild;}}//方法1 int Bitree::Locate(char s, struct Binode *bt){if(bt!=NULL&&!found){level++;if(bt->data==s) found=true;else{Locate(s,bt->lchild);Locate(s,bt->rchild);if(!found)level--;} }return level;} //方法2 //void Bitree::Locate(char s, struct Binode *bt){//if(bt!=NULL){//level++;//if(bt->data==s) {//cout<<level;//return;//}//else{//Locate(s,bt->lchild);//Locate(s,bt->rchild);////level--;//} //}//} int main(){char x[]={'A','B','C','D','E','F','G','H'};char y[]={'C','D','B','A','F','E','H','G'};char z='H';Bitree p(p.root,x,0,7,y,0,7);p.Preorder(p.root);cout<<endl;p.Inorder(p.root);cout<<endl;p.Postorder(p.root);cout<<endl;p.Levelorder(p.root);cout<<endl;cout<<p.Locate(z,p.root);}
疑问:为什么二叉树可以不用写析构函数也可以执行?如果写了析构函数,程序可以编译但会停止工作(windows正在查找该问题的解决方案),除非把第21行的struct Binode *root;改为struct Binode *root=new Binode;


0 0
原创粉丝点击