树的操作:
1、树的构建
Node build(Node p, int &k, string s){
if(s[k] == '#'){
k++;
return NULL;
}
p = new node();
p -> ch = s[k++];
p -> lc = build(p->lc,k,s);
p -> rc = build(p->rc,k,s);
return p;
}
用字符串s来构建,k作为索引
2、树的遍历
先序:
void show(Node p){
if(p == NULL) return;
cout << p -> ch;
show(p -> lc);
show(p -> rc);
}
中序:
void show(Node p){
if(p == NULL) return;
show(p -> lc);
cout << p -> ch;
show(p -> rc);
}
后序:
void show(Node p){
if(p == NULL) return;
show(p -> lc);
show(p -> rc);
cout << p -> ch;
}
三个的区别:只是改变语句顺序
3、交换二叉树每个结点的左孩子和右孩子
void change(Node p){
Node t;
if(p == NULL) return;
t = p -> lc;
p -> lc = p -> rc;
p -> rc = t;
change(p -> lc);
change(p -> rc);
}
标签:Node,23.10,return,lc,show,16,rc,NULL From: https://www.cnblogs.com/atrue/p/17766877.html