二叉树的基本操作
来源:互联网 发布:ahc官网扫码软件 编辑:程序博客网 时间:2024/06/11 12:46
#include<iostream>using namespace std;typedef struct btnode{char element;btnode *lchild;btnode *rchild;btnode(){lchild=rchild=NULL;}btnode(char& x){element=x;lchild=rchild=NULL;}btnode(char& x,btnode *l,btnode *r){ element=x; lchild=l; rchild=r;}}btnode;btnode* MakeTree(char x,btnode *l,btnode *r){ if(!x) return NULL; btnode *q=new btnode(x,l,r); l=NULL; r=NULL; return q;}void Clear(btnode *t){ if(t) { Clear(t->lchild); Clear(t->rchild); cout<<"***now clear "<<t->element<<endl; delete t; }}void Visit(char& x){ cout<<x<<" ";}void PreOrder(void(*Visit)(char& x),btnode *t){ if(t) { Visit(t->element); PreOrder(Visit,t->lchild); PreOrder(Visit,t->rchild); }}void InOrder(void(*Visit)(char& x),btnode *t){ if(t) { InOrder(Visit,t->lchild); Visit(t->element); InOrder(Visit,t->rchild); }}void PostOrder(void(*Visit)(char& x),btnode *t){ if(t) { PostOrder(Visit,t->lchild); PostOrder(Visit,t->rchild); Visit(t->element); }}int Size(btnode *t){ if(!t) return 0; else return Size(t->lchild)+Size(t->rchild)+1;}void BreakTree(char &x,btnode* &t,btnode* l,btnode* r){ if(!t||&l==&r) return; x=t->element; l=t->lchild; r=t->rchild; t=NULL;}int main(){ char x; btnode *a,*b,*c,*d; a=MakeTree('S',NULL,NULL); b=MakeTree('W',NULL,NULL); c=MakeTree('D',a,b); d=MakeTree('R',NULL,NULL); a=MakeTree('G',d,c); PreOrder(Visit,a); cout<<"\n"<<Size(a)<<endl; BreakTree(x,a,d,c); PreOrder(Visit,d); Clear(d); cout<<endl; PreOrder(Visit,c); Clear(c); Clear(a);}
0 0
- 二叉树的基本操作
- 二叉树的基本操作
- 二叉树的基本操作
- 二叉树的基本操作
- 二叉树的基本操作
- 二叉树的基本操作
- 二叉树的基本操作
- 二叉树的基本操作
- 二叉树的基本操作
- 二叉树的基本操作
- 二叉树的基本操作
- 二叉树的基本操作
- 二叉树的基本操作
- 二叉树的基本操作
- 二叉树的基本操作
- 二叉树的基本操作
- 二叉树的基本操作
- 二叉树的基本操作
- 机试算法讲解: 第38题 求Dijkstra最短路径及花费
- photoshop小应用
- MongoDB与Elasticsearch达到数据同步的一点尝试
- 深入分析Java线程中断机制
- 士兵杀敌(一)
- 二叉树的基本操作
- HDU 5338 ZZX and Permutations 线段树
- Permutations
- 机试算法讲解: 第39题 DAG之QQ师徒关系
- 【Swift】学习笔记(八)——类和结构体
- 数据块的操作 DataChunk
- JavaScript中的枚举类型
- ubuntu 常用命令大全
- UVa 232 Crossword Answers