线索二叉树实验Word下载.docx

上传人:b****3 文档编号:7855048 上传时间:2023-05-09 格式:DOCX 页数:17 大小:17KB
下载 相关 举报
线索二叉树实验Word下载.docx_第1页
第1页 / 共17页
线索二叉树实验Word下载.docx_第2页
第2页 / 共17页
线索二叉树实验Word下载.docx_第3页
第3页 / 共17页
线索二叉树实验Word下载.docx_第4页
第4页 / 共17页
线索二叉树实验Word下载.docx_第5页
第5页 / 共17页
线索二叉树实验Word下载.docx_第6页
第6页 / 共17页
线索二叉树实验Word下载.docx_第7页
第7页 / 共17页
线索二叉树实验Word下载.docx_第8页
第8页 / 共17页
线索二叉树实验Word下载.docx_第9页
第9页 / 共17页
线索二叉树实验Word下载.docx_第10页
第10页 / 共17页
线索二叉树实验Word下载.docx_第11页
第11页 / 共17页
线索二叉树实验Word下载.docx_第12页
第12页 / 共17页
线索二叉树实验Word下载.docx_第13页
第13页 / 共17页
线索二叉树实验Word下载.docx_第14页
第14页 / 共17页
线索二叉树实验Word下载.docx_第15页
第15页 / 共17页
线索二叉树实验Word下载.docx_第16页
第16页 / 共17页
线索二叉树实验Word下载.docx_第17页
第17页 / 共17页
亲,该文档总共17页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

线索二叉树实验Word下载.docx

《线索二叉树实验Word下载.docx》由会员分享,可在线阅读,更多相关《线索二叉树实验Word下载.docx(17页珍藏版)》请在冰点文库上搜索。

线索二叉树实验Word下载.docx

preThreaded,

midThreaded,

postThreaded};

template<

classElement_type>

structBiThrNode{

Element_typedata;

BiThrNode*lchild,*rchild;

boolltag,rtag;

};

classBiThrTree{

public:

BiThrTree();

BiThrTree(constchar*filename);

//根据文件构造二叉树

~BiThrTree();

//析构函数

voidBuild(constchar*filename);

//根据文件创建二叉树

voidPreOrder();

//先序遍历二叉树

voidMidOrder();

//中序遍历二叉树

voidPostOrder();

//后序遍历二叉树

BiThrNode<

Element_type>

*PreSuc(BiThrNode<

*node)const;

//先序后继

*MidSuc(BiThrNode<

//中序后继

*PostUsher(BiThrNode<

//后序前驱

voidPostPrint();

//输出后序遍历二叉树

voidPostPrint(BiThrNode<

*node);

voidPreThread();

//先序线索化

voidPreThread(BiThrNode<

*node);

voidMidThread();

//中序线索化

voidMidThread(BiThrNode<

voidPostThread();

//后序线索化

voidPostThread(BiThrNode<

voidInsertAtL(Element_typedata);

//插入为左子树先序最后一个结点的右孩子

voidClear();

//删除二叉树

voidClear(BiThrNode<

voidVisit(BiThrNode<

//访问结点

private:

voidCreatNode(BiThrNode<

*root;

ifstreamfile;

Threadstatus;

*pre;

BiThrTree<

:

:

BiThrTree(){

root=NULL;

status=notThreaded;

}

BiThrTree(constchar*filename){

Build(filename);

~BiThrTree(){

Clear();

voidBiThrTree<

Build(constchar*filename){

if(root!

=NULL)

chartree[40];

intchild;

file.open(filename);

file>

>

tree>

child;

if(child==0){

file>

data;

root=newBiThrNode<

;

root->

data=data;

lchild=NULL;

rchild=NULL;

ltag=false;

rtag=false;

CreatNode(root);

}

elseroot=NULL;

PreOrder(){

if(status==notThreaded)

PreThread();

*te=root;

while(te!

=NULL){

Visit(te);

te=PreSuc(te);

MidOrder(){

MidThread();

if(te==NULL)

return;

while(te->

lchild!

=NULL)

te=te->

lchild;

te=MidSuc(te);

voidBiThrTree<

PostOrder(){

if(status==notThreaded){

PostThread();

PostPrint();

BiThrNode<

*BiThrTree<

PreSuc(BiThrNode<

*node)const{

if(!

node->

ltag)

returnnode->

elsereturnnode->

rchild;

MidSuc(BiThrNode<

if(node->

rtag)

returnnode->

*te=node->

returnte;

while(!

te->

ltag)

returnte;

PostUsher(BiThrNode<

*node)const{

rtag)

returnnode->

else

PostPrint(){

PostPrint(root);

PostPrint(BiThrNode<

*node){

if(node==NULL)

return;

PostPrint(PostUsher(node));

cout<

data<

"

"

;

PreThread(){

if(status==notThreaded){

pre=NULL;

if(root!

PreThread(root);

status=preThreaded;

PreThread(BiThrNode<

*node){

*te=pre;

pre=node;

PreThread(node->

lchild);

rchild);

if(node->

lchild==NULL){

node->

ltag=true;

lchild=te;

if(te!

=NULL&

&

rchild==NULL){

te->

rtag=true;

rchild=node;

MidThread(){

*te;

te=root;

pre=NULL;

if(te==NULL)

return;

MidThread(te);

status=midThreaded;

elsecout<

thistreeisthreaded/n"

MidThread(BiThrNode<

MidThread(node->

PostThread(){

if(te==NULL)

/*while(te->

lchild!

te=te->

}*/

PostThread(te);

status=postThreaded;

PostThread(BiThrNode<

if(node==NULL)

PostThread(node->

if(node->

lchild==NULL){

node->

lchild=pre;

if(pre!

=NULL&

pre->

rchild==NULL){

pre->

InsertAtL(Element_typedata){

*node=newBiThrNode<

*end,*te,*pre;

if(root->

end=NULL;

elseend=root->

pre=root;

te=PreSuc(pre);

while(te!

=end){

pre=te;

te=PreSuc(te);

rchild=end;

Clear(){

Clear(root);

root=NULL;

else{

BiThrNode<

*node,*te;

node=root;

while(node!

=NULL){

te=node;

switch(status){

casepreThreaded:

node=PreSuc(node);

break;

casemidThreaded:

node=MidSuc(node);

casepostThreaded:

node=PostUsher(node);

default:

return;

}

delete(te);

}

Clear(BiThrNode<

if(node==NULL)

Clear(node->

lchild);

rchild);

rchild=NULL;

deletenode;

Visit(BiThrNode<

CreatNode(BiThrNode<

intlchild,rchild;

lchild>

if(lchild==0){

lchild=newBiThrNode<

te=node->

te->

CreatNode(te);

if(rchild==0){

rchild=newBiThrNode<

voidpart1(constchar*file){

树"

file<

\n"

BiThrTree<

char>

tree(file);

树的先序遍历:

tree.PreOrder();

\n\n"

voidpart2(constchar*file){

树的中序遍历:

tree.MidOrder();

voidpart3(constchar*file){

树的后序遍历:

tree.PostOrder();

voidpart4(constchar*file){

先序遍历最初的树\n"

endl;

插入结点X:

tree.InsertAtL('

X'

);

结果(先序遍历):

 

intmain(){

part1("

FULL41.CBT"

LETTER.CBT"

part2("

part3("

part4("

system("

pause"

return0;

六、完整的实验结果记录∶

(为了方便,将此次试验重新分为四个部分)

先序线索化二叉树,再先序遍历二叉树

预期结果:

Full41.cbt:

ABCDEFGHIJKLMN

Letter.cbt:

abcdefghijklmnopqrstuvwxyz

中序线索化二叉树,再中序遍历二叉树

FULL41.CBT:

DCEBGFHAKJLINMO

LETTER.CBT:

dfgeihjclkmbonpartsuqwxvyz

后序线索化二叉树,再后序遍历二叉树

DECGHFBKLJNOM

gfijhedlmkcopnbtusrxwzyvqa

ABCDEFGHXIJKLMNO

abcdefghijklmnopXqrstuvwxyz

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 高等教育 > 研究生入学考试

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2