数据结构实验报告.docx

上传人:b****2 文档编号:1818882 上传时间:2022-10-24 格式:DOCX 页数:13 大小:84.27KB
下载 相关 举报
数据结构实验报告.docx_第1页
第1页 / 共13页
数据结构实验报告.docx_第2页
第2页 / 共13页
数据结构实验报告.docx_第3页
第3页 / 共13页
数据结构实验报告.docx_第4页
第4页 / 共13页
数据结构实验报告.docx_第5页
第5页 / 共13页
点击查看更多>>
下载资源
资源描述

数据结构实验报告.docx

《数据结构实验报告.docx》由会员分享,可在线阅读,更多相关《数据结构实验报告.docx(13页珍藏版)》请在冰豆网上搜索。

数据结构实验报告.docx

数据结构实验报告

 

数据结构实验报告

DataStructures

 

班级:

测绘工程卓越1501

学号:

1510070112

姓名:

张丽

 

Non-RecursiveimplementationofBinaryTrees'traversals

Chapter1:

Introduction

Experimentalcontent:

UseNon-RecursivealgorithmtoimplementBinaryTree'preordertraversal,inordertraversalandpostordertraversalinturn.

Backgroundofthealgorithms:

ABinaryTreeisatreeinwhichnonodecanhavemorethantwochildren.ThetraversalofaBinaryTreereferstoprocessingeachnodeinthebinarytreeinacertainordersothateachnodecanbeprocessedonlyonce.Abinarytree is composed ofthreeparts:

therootnode,theleft subtree oftheroot node andtherightsubtreeoftherootnode.Therefore,wecantraversetheentireBinaryTreebytraversing the three parts in turn.Iftheleftandrightarelimited,therearethreewaystotraverse:

preordertraversal,inordertraversalandpostordertraversal.Ontheonehand,notalltheprogramminglanguagesallowrecursion.Ontheotherhand,althoughquiteconcise,recursionprogramsaregenerallynotreadableandefficient.Consequently,wewillusingNon-RecursivealgorithmtoimplementBinaryTree'preordertraversal,inordertraversalandpostordertraversalinturn,whichismorecommon.

Chapter2:

AlgorithmSpecification

Inthealgorithm,BinaryTreewillbestoredinbinarylinkedlistandone-dimensionalarray"*stack[MAXSIZE]"willbeusedtoimplementstack.The traversal route is:

Whengoingdeepalongtheleftsubtree,thenodeentersthestack.Ifitispreordertraversal,itshouldbeprocessedbeforeenteringthestack;When it can not go deep along the left subtree,Justreturn.That is, pop up the node 

from the stack which  entered the stackbefore,and if it is inordertraversal, process 

the node now, and then continue to deep from the right subtree of the node;Ifitis

postordertraversal,thenodeshouldbepressedinthestackagain,andthen

continueto deepfromtherightsubtreeofthenode.

(1)preordertraversal:

Inapreordertraversal,theworkatanodeisperformedbeforeitschildrenareprocessed.Thatistosay,wheneveranodeisencountered,itshouldbeprocessed.Thecodeisasfollows:

voidPreOrder(BiTreeT)

{

BiTNode*stack[MAXSIZE],*p;

inttop=-1;

if(T!

=NULL)

{

top++;

stack[top]=T;

while(top>-1)

{

p=stack[top];

top--;

printf("%c",p->data);

if(p->rchild!

=NULL)

{

top++;

stack[top]=p->rchild;

}

if(p->lchild!

=NULL)

{

top++;

stack[top]=p->lchild;

}

}

printf("\n");

}

}

(2)inordertraversal:

Inainordertraversal,theworkatanodeisperformedbetweenitschildrenareprocessed.Thecodeisasfollows:

voidInOrder(BiTreeT)

{

BiTNode*stack[MAXSIZE],*p;

inttop=-1;

if(T!

=NULL)

{

p=T;

while(top>-1||p!

=NULL)

{

while(p!

=NULL)

{

top++;

stack[top]=p;

p=p->lchild;

}

if(top>-1)

{

p=stack[top];

top--;

printf("%c",p->data);

p=p->rchild;

}

}

printf("\n");

}

}

(3)postordertraversal:

Inapostordertraversal,theworkatanodeisperformedafteritschildrenareprocessed.Thecodeisasfollows:

voidPostOrder(BiTreeT)

{

BiTNode*stack[MAXSIZE],*p;

intsign,top=-1;

if(T!

=NULL)

{

do

{

while(T!

=NULL)

{

top++;

stack[top]=T;

T=T->lchild;

}

p=NULL;

sign=1;

while(top!

=-1&&sign)

{

T=stack[top];

if(T->rchild==p)

{

printf("%c",T->data);

top--;

p=T;

}

else

{

T=T->rchild;

sign=0;

}

}

}while(top!

=-1);

printf("\n");

}

}

Chapter3:

SourceCode(inC)

#include

#include

#defineMAXSIZE50

/*定义二叉树结点类型*/

typedefstructnode

{

chardata;

structnode*lchild,*rchild;

}BiTNode,*BiTree;

/*先序创建二叉树*/

voidCreatBiTree(BiTree*T)

{

charch;

ch=getchar();

/*#代表指针为空*/

if(ch=='#')

{

(*T)=NULL;

}

else

{

(*T)=(BiTNode*)malloc(sizeof(BiTNode));

/*建立根结点*/

(*T)->data=ch;

/*递归先序建立左子树*/

CreatBiTree(&(*T)->lchild);

/*递归先序建立右子树*/

CreatBiTree(&(*T)->rchild);

}

}

/*非递归先序遍历二叉树*/

voidPreOrder(BiTreeT)

{

BiTNode*stack[MAXSIZE],*p;

inttop=-1;

if(T!

=NULL)

{

/*根结点入栈*/

top++;

stack[top]=T;

/*栈不空时循环*/

while(top>-1)

{

/*出栈并访问该节点*/

p=stack[top];

top--;

printf("%c",p->data);

/*右孩子入栈*/

if(p->rchild!

=NULL)

{

top++;

stack[top]=p->rchild;

}

/*左孩子入栈*/

if(p->lchild!

=NULL)

{

top++;

stack[top]=p->lchild;

}

}

printf("\n");

}

}

/*非递归中序遍历二叉树*/

voidInOrder(BiTreeT)

{

BiTNode*stack[MAXSIZE],*p;

inttop=-1;

if(T!

=NULL)

{

p=T;

while(top>-1||p!

=NULL)

{

/*访问p的所有左结点并入栈*/

while(p!

=NULL)

{

top++;

stack[top]=p;

p=p->lchild;

}

if(top>-1)

{

/*出栈并访问该结点*/

p=stack[top];

top--;

printf("%c",p->data);

/*访问p的右孩子*/

p=p->rchild;

}

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

当前位置:首页 > 人文社科 > 法律资料

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1