博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
二叉链表类
阅读量:4605 次
发布时间:2019-06-09

本文共 1821 字,大约阅读时间需要 6 分钟。

将数据和二叉链表的各种运算(如二叉链表的生成,二叉链表的前序、中序、后序遍历)封装在一起,构成二叉链表类。

Binary_Tree.h:

#include 
using namespace std;//定义二叉链表结点类型template
struct Btnode{ T d; Btnode *lchild; Btnode *rchild;};//二叉链表类template
class Binary_Tree{private: Btnode
*BT;public: Binary_Tree() { BT=NULL; return; } void creat_Binary_Tree(T); void pretrav_Binary_Tree(); void intrav_Binary_Tree(); void postrav_Binary_Tree();};//生成二叉链表template
void Binary_Tree
::creat_Binary_Tree(T end){ Btnode
*p; T x; cin>>x; if (x==end) { return; } p=new Btnode
; p->d=x; p->lchild=NULL; p->rchild=NULL; BT=p; creat(p,1,end); creat(p,2,end); return; }template
static void creat(Btnode
*p,int k, T end){ Btnode
*q; T x; cin>>x; if (x!=end) { q=new Btnode
; q->d=x; q->lchild=NULL; q->rchild=NULL; if (k==1) { p->lchild=q; } if (k==2) { p->rchild=q; } creat(q,1,end); creat(q,2,end); } return ;}//前序遍历二叉链表template
void Binary_Tree
::pretrav_Binary_Tree(){ Btnode
*p; p=BT; //从根结点开始前序遍历 pretrav(p); cout<
static void pretrav(Btnode
*p){ if (p!=NULL) { cout<
d<<" "; pretrav(p->lchild); pretrav(p->rchild); } return ;}//中序遍历二叉链表template
void Binary_Tree
::intrav_Binary_Tree(){ Btnode
*p; p=BT; intrav(p); cout<
static void intrav(Btnode
*q){ if (q!=NULL) { intrav(q->lchild); cout<
d<<" "; intrav(q->rchild); } return ;}//后续遍历二叉树template
void Binary_Tree
::postrav_Binary_Tree(){ Btnode
*p; p=BT; postrav(p); cout<
static void postrav(Btnode
*q){ if (q!=NULL) { postrav(q->lchild); postrav(q->rchild); cout<
d<<" "; } return ;}

 

Binary_Tree.cpp:

#include "Binary_Tree.h"int main(){    Binary_Tree
b; cout<<"输入各结点值,以-1为结束符值:"<

 

转载于:https://www.cnblogs.com/2007winter/archive/2012/06/04/2534323.html

你可能感兴趣的文章
yum install 安装报错
查看>>
什么是Referer?Referer的作用?空Referer是怎么回事?
查看>>
php中的全局变量引用
查看>>
java swing 计算器
查看>>
httpClient get方式抓取数据
查看>>
iphone 常用控件列表
查看>>
cf C On Number of Decompositions into Multipliers
查看>>
Tiling
查看>>
教你一招 - 如何安装nopcommerce2.5
查看>>
Oracle Statistic 统计信息 小结(转载)
查看>>
C#特性-表达式树
查看>>
分享一个JQ对listbox进行排序的脚本
查看>>
poj3278Catch That Cow(BFS)
查看>>
第十一章 认识与学习BASH
查看>>
基于Andoird 4.2.2的Account Manager源代码分析学习:创建选定类型的系统帐号
查看>>
使用Hexo搭建个人博客并部署到GitHub或码云上全过程
查看>>
[软件]Xcode查找系统framework所在路径
查看>>
海量数据系统对比
查看>>
典型用户和用户场景描述
查看>>
搭建企业级网络共享服务(FTP,NFS,Samba)
查看>>