博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
我也来发段代码,二叉树的遍历
阅读量:6972 次
发布时间:2019-06-27

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

  hot3.png

从来没发过代码,咱也发一次。第一次这么认真搞代码。哈哈

#ifndef __TREE_H__#define __TREE_H__//定义二叉树的一些操作,使用了STL,#include 
#include
#include
using std::iostream;using std::cout;using std::endl;using std::stack;using std::queue;struct BinaryTreeNode;typedef void (*BTREENODE_VISIT)(BinaryTreeNode* pBTreeNode);struct BinaryTreeNode{ int data; int flags; //0:not in stack, 1: in stack int height; BinaryTreeNode* pLeftChildren; BinaryTreeNode* pRightChildren; BinaryTreeNode() { pLeftChildren = pRightChildren = NULL; flags = 0; height = 0; } BinaryTreeNode(int data) { pLeftChildren = pRightChildren = NULL; this->data = data; flags = 0; height = 0; } void createTrees(BinaryTreeNode *pLeft,BinaryTreeNode* pRight) { pLeftChildren = pLeft; pRightChildren = pRight; } static void preOrderVisitRecurs(BinaryTreeNode* pBTree,BTREENODE_VISIT pBTNodeVisitFunc) { if(pBTree) { if(pBTNodeVisitFunc) pBTNodeVisitFunc(pBTree); if(pBTree->pLeftChildren) preOrderVisitRecurs(pBTree->pLeftChildren,pBTNodeVisitFunc); if(pBTree->pRightChildren) preOrderVisitRecurs(pBTree->pRightChildren,pBTNodeVisitFunc); } return; } static void preOrderVisit(BinaryTreeNode* pBTree, BTREENODE_VISIT pBTNodeVisitFunc) { if(pBTree == NULL) return; typedef stack
CBTNodeStack; CBTNodeStack btnStack; btnStack.push(pBTree); while(btnStack.size() > 0) { BinaryTreeNode* pCurrent = btnStack.top(); if(pCurrent) pBTNodeVisitFunc(pCurrent); btnStack.pop(); if(pCurrent->pRightChildren) btnStack.push(pCurrent->pRightChildren); if(pCurrent->pLeftChildren) btnStack.push(pCurrent->pLeftChildren); } }; static void inOrderVisitRecurs(BinaryTreeNode* pBTree,BTREENODE_VISIT pBTNodeVisitFunc) { if(pBTree) { if(pBTree->pLeftChildren) inOrderVisitRecurs(pBTree->pLeftChildren,pBTNodeVisitFunc); if(pBTNodeVisitFunc) pBTNodeVisitFunc(pBTree); if(pBTree->pRightChildren) inOrderVisitRecurs(pBTree->pRightChildren,pBTNodeVisitFunc); } return; } static void inOrderVisit(BinaryTreeNode* pBTree,BTREENODE_VISIT pBTNodeVisitFunc) { if(pBTree == NULL) return; typedef stack
CBTNodeStack; CBTNodeStack btnStack; btnStack.push(pBTree); pBTree->flags = 1; while(btnStack.size() > 0) { pBTree = btnStack.top(); while(pBTree->pLeftChildren != NULL && pBTree->pLeftChildren->flags == 0) { btnStack.push(pBTree->pLeftChildren); pBTree->pLeftChildren->flags = 1; pBTree = btnStack.top(); } pBTree = btnStack.top(); if(pBTNodeVisitFunc) pBTNodeVisitFunc(pBTree); //visit the left leaf or leftest root btnStack.pop(); if(pBTree->pRightChildren && pBTree->pRightChildren->flags == 0) { btnStack.push(pBTree->pRightChildren); pBTree->pRightChildren->flags = 1; continue; } } return; } static void postOrderVisitRecurs(BinaryTreeNode* pBTree,BTREENODE_VISIT pBTNodeVisitFunc) { if(pBTree) { if(pBTree->pLeftChildren) postOrderVisitRecurs(pBTree->pLeftChildren,pBTNodeVisitFunc); if(pBTree->pRightChildren) postOrderVisitRecurs(pBTree->pRightChildren,pBTNodeVisitFunc); if(pBTNodeVisitFunc) pBTNodeVisitFunc(pBTree); } return; } static void postOrderVisit(BinaryTreeNode* pBTree,BTREENODE_VISIT pBTNodeVisitFunc) { if(pBTree == NULL) return; typedef stack
CBTNodeStack; CBTNodeStack btnStack; btnStack.push(pBTree); pBTree->flags = 1; while(btnStack.size() > 0) { pBTree = btnStack.top(); while(pBTree->pLeftChildren != NULL && pBTree->pLeftChildren->flags == 0) { btnStack.push(pBTree->pLeftChildren); pBTree->pLeftChildren->flags = 1; pBTree = btnStack.top(); } pBTree = btnStack.top(); if(pBTree->pRightChildren && pBTree->pRightChildren->flags == 0) { btnStack.push(pBTree->pRightChildren); pBTree->pRightChildren->flags = 1; continue; } if(pBTNodeVisitFunc) pBTNodeVisitFunc(pBTree); btnStack.pop(); } return; }; static void levelVisit(BinaryTreeNode* pBTree,BTREENODE_VISIT pBTNodeVisitFunc) { typedef queue
CBTNodeQueue; CBTNodeQueue btQueue; btQueue.push(pBTree); while(btQueue.size()) { BinaryTreeNode* pCurrent = btQueue.front(); if(pCurrent->pLeftChildren) btQueue.push(pCurrent->pLeftChildren); if(pCurrent->pRightChildren) btQueue.push(pCurrent->pRightChildren); if(pBTNodeVisitFunc) pBTNodeVisitFunc(pCurrent); btQueue.pop(); } }};#endif

测试例子:

树的布局如下

                 1

            2          3

     4        5     6

             7           8

// DataStructTest.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include "Tree.h"void PRINT_BNVALUE(BinaryTreeNode* pBTreeNode){	cout<<"TreeVale " << pBTreeNode->data << endl;}void RESET_FLAGS(BinaryTreeNode* pBTreeNode){	pBTreeNode->flags = 0;}int _tmain(int argc, _TCHAR* argv[]){	BinaryTreeNode testBNTree8(8);	BinaryTreeNode testBNTree7(7);	BinaryTreeNode testBNTree6(6);	BinaryTreeNode testBNTree5(5);	BinaryTreeNode testBNTree4(4);	BinaryTreeNode testBNTree3(3);	BinaryTreeNode testBNTree2(2);	BinaryTreeNode testBNTree1(1);	testBNTree1.createTrees(&testBNTree2,&testBNTree3);	testBNTree2.createTrees(&testBNTree4,&testBNTree5);	testBNTree5.createTrees(&testBNTree7,NULL);	testBNTree3.createTrees(&testBNTree6,NULL);	testBNTree6.createTrees(NULL,&testBNTree8);	cout<<"travse pre order recurs"<

转载于:https://my.oschina.net/innost/blog/60805

你可能感兴趣的文章
python 10.19作业
查看>>
groupby以后取每组前n行
查看>>
第二十一课:js属性操作的兼容性问题
查看>>
【学习Android NDK开发】Primitive Types Map(基本类型映射)
查看>>
选择问题(第k小元素)(分治法)
查看>>
POJ3438 ZOJ2886 UVALive3822 Look and Say【数列】
查看>>
深度残差网络
查看>>
iOS7默认状态栏文字颜色为黑色,项目需要修改为白色。
查看>>
c链表
查看>>
(七)jQuery中的DOM操作
查看>>
js获取页面传过来的参数
查看>>
第7章 数组实验
查看>>
cacti快速安装
查看>>
firefox下img元素和空div以及选中div中文字拖拽效果处理
查看>>
vue中eventbus 多次触发的问题
查看>>
两场CF
查看>>
Mahalanobis Distance(马氏距离)
查看>>
KVO和通知中心
查看>>
Master Nginx(1) - Installing Nginx and Third-Party Modules
查看>>
语义标签
查看>>