首页 二叉树先序、中序、后序三种遍历的非递归.txt

二叉树先序、中序、后序三种遍历的非递归.txt

举报
开通vip

二叉树先序、中序、后序三种遍历的非递归.txt二叉树先序、中序、后序三种遍历的非递归.txt #include #define STACK_MAXLEN 30 //using namespace std; typedef struct BTreeNode { int where; struct BTreeNode *rightChild; struct BTreeNode *leftChild; }TYPE_B_TREE_NODE,*TYPE_pB_TREE_NODE; //堆栈的结构 class TStack { public: vo...

二叉树先序、中序、后序三种遍历的非递归.txt
二叉树先序、中序、后序三种遍历的非递归.txt #include #define STACK_MAXLEN 30 //using namespace std; typedef struct BTreeNode { int where; struct BTreeNode *rightChild; struct BTreeNode *leftChild; }TYPE_B_TREE_NODE,*TYPE_pB_TREE_NODE; //堆栈的结构 class TStack { public: void push(TYPE_pB_TREE_NODE pNode);//入栈 TYPE_pB_TREE_NODE pop();//出栈 int empty() { if ( top == base ) return 1; else return 0; } TStack() { top=base=stackArray; for(int i=0; i < STACK_MAXLEN; i++) stackArray[i]=NULL; //cout<<"堆栈初始化完毕~"<>NodeNum; pBTreeArray=new TYPE_B_TREE_NODE[NodeNum]; for(i=1; i <= NodeNum/2-1; i++) { pBTreeArray[i-1].where=i; pBTreeArray[i-1].leftChild=pBTreeArray+(2*i-1); pBTreeArray[2*i-1].where=2*i; pBTreeArray[i-1].rightChild=pBTreeArray+(2*i); pBTreeArray[2*i].where=2*i+1; } if(2*i+1 == NodeNum ) { pBTreeArray[i-1].where=i; pBTreeArray[i-1].leftChild=pBTreeArray+(2*i-1); pBTreeArray[2*i-1].where=2*i; pBTreeArray[i-1].rightChild=pBTreeArray+(2*i); pBTreeArray[2*i].where=2*i+1; } else { pBTreeArray[i-1].where=i; pBTreeArray[i-1].leftChild=pBTreeArray+(2*i-1); pBTreeArray[2*i-1].where=2*i; pBTreeArray[2*i].rightChild=NULL; } for( int j=i+1; j <=NodeNum; j++ ) { pBTreeArray[j-1].leftChild=NULL; pBTreeArray[j-1].rightChild=NULL; } return pBTree=pBTreeArray+0; } // void PrintBTree(TYPE_pB_TREE_NODE pBTreeRoot) { if( pBTreeRoot != NULL ) { cout<where<leftChild); PrintBTree(pBTreeRoot->rightChild); } } //先序遍历 void PreOrder(TYPE_pB_TREE_NODE pBTreeRoot) { cout<<"先序遍历"<where<leftChild; } while( stack.empty() != 1 ) { pTemporary=stack.pop(); if( pTemporary->rightChild == NULL ) { continue; } else { pTemporary=pTemporary->rightChild; while( pTemporary != NULL ) { cout<where<leftChild; } } } } //中序遍历 void InOrder(TYPE_pB_TREE_NODE pBTreeRoot) { cout<<"中序遍历"<where<leftChild; } while( stack.empty() != 1 ) { pTemporary=stack.pop(); cout<where<rightChild == NULL ) { continue; } else { pTemporary=pTemporary->rightChild; while( pTemporary != NULL ) { // stack.push(pTemporary); pTemporary=pTemporary->leftChild; } } } } //后序遍历 void PostOrder(TYPE_pB_TREE_NODE pBTreeRoot) { cout<<"后序遍历"<where<rightChild !=NULL ) { stack.push(pTemporary->rightChild); pFlag++; // withNoChild++; } if( pTemporary->leftChild !=NULL ) { stack.push(pTemporary->leftChild); pFlag++; //withNoChild++; } } } } void main() { TYPE_pB_TREE_NODE pBTreeRoot; pBTreeRoot=CreateBTree(); // PrintBTree(pBTreeRoot); PreOrder(pBTreeRoot); InOrder(pBTreeRoot); PostOrder(pBTreeRoot); }
本文档为【二叉树先序、中序、后序三种遍历的非递归&#46;txt】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
下载需要: 免费 已有0 人下载
最新资料
资料动态
专题动态
is_477730
暂无简介~
格式:doc
大小:23KB
软件:Word
页数:0
分类:生活休闲
上传时间:2017-11-23
浏览量:20