树的基本操作,静态法
来源:互联网 发布:怎么办我爱你网络歌手 编辑:程序博客网 时间:2024/05/21 14:06
树的基本操作
1.定义
lchid和rchild存放的是在tree的序号,不是data值
struct node{int data;int lchild;int rchild;}tree[MAX];
2.新建节点
-1表示没有该节点,代替NULL
int index=0;int newNode(){ tree[index].lchid=-1; tree[index].rchid=-1; return index++;}
3.查找某节点,并修改其值
//root为根节点的下标void search(int root,int x,int newdata){if(root==-1){return;}if(tree[root].data==x){tree[root].data=newdata;}search(tree[root].lchild,x,newdata);search(tree[root].rchild,x,newdata);}
4.插入和建立
//root为根节点的下标void insert(int root,int x){if(root==-1){root=newNode();tree[root].data=x;return;}if(插在左子树的性质){insert(tree[root].lchild,x)}else{insert(tree[root].rchild,x);}}//建立二叉树,其值存在a[]中int create(int n,int a[]){ int root=newNode(); tree[root].data=a[0]; for(int i=1;i<n;i++){ insert(root,data[i]); } return root;}
5.遍历
//先序void pre(int root){if(root==-1){return;}//访问该节点visit(tree[root].data);pre(tree[root].lchild);pre(tree[root].rchild);}//中序void in(int root){if(root==-1){return;}in(tree[root].lchild);//访问该节点visit(tree[root].data);in(tree[root].rchild);}//后序void post(int root){if(root==-1){return;}post(tree[root].lchild);post(tree[root].rchild);//访问该节点visit(tree[root].data);}//层次void dfs(int root){queue <int> q;q.push(root);while(!q.empty()){int now=q.front();q.pop();//访问该节点visit(tree[root].data);if(tree[now].lchild!=-1){q.push(tree[now].lchild);}if(tree[now].rchild!=-1){q.push(tree[now].rchild);}}}
0 0
- 树的基本操作,静态法
- 静态单链表的基本操作
- 静态链表的所有基本操作
- Collections接口的静态方法基本操作
- 串的静态顺序存储基本操作
- 静态链表的基本操作
- 静态顺序表的基本操作
- 静态顺序表的基本操作
- 静态顺序表的基本操作
- 静态链表的基本操作实现
- 静态链表的基本操作实现
- 实现基于静态数组的顺序表的基本操作
- 静态链表的创建以及基本操作
- c语言实现静态链表的基本操作
- 顺序表的基本操作——静态实现
- 通过Freemarker实现页面静态化的基本操作
- 静态链表的基本操作及其应用(实验2.3)
- 静态链表的基本操作及其应用(实验2.3)
- HDU2153 仙人球的残影跑【水题】【蛇形矩阵】
- mybatis insert获取主键
- 账号终于能够找回来了!
- UIApplication
- 最近在看蒋蓝的书
- 树的基本操作,静态法
- SVN冲突解决详解
- 把屏幕坐标转换成经纬度
- 条款3,4:尽量用new和delete而不用malloc和free 尽量使用c++风格的注释
- 信号 中断系统调用 递送
- #OOD#Parking Lot停车场设计
- hdu 4288 Coder (vector)
- iOS Devices: Releases, Firmware, Instruction Sets, Screen Sizes
- 用sp_change_users_login消除Sql Server的孤立用户