自娱自乐型。
1 Tree.h 定义Tree相关的,使用了STL
#ifndef __TREE_H__
#define __TREE_H__
#include <iostream>
#include <stack>
#include <queue>
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<BinaryTreeNode*> 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<BinaryTreeNode*> 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<BinaryTreeNode*> 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<BinaryTreeNode*> 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
2 测试:
// 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[])
{
/*
构造目标树
1
/ \
2 3
/ \ /
4 5 6
/ \
7 8
*/
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"<<endl;
BinaryTreeNode::preOrderVisitRecurs(&testBNTree1,PRINT_BNVALUE);
cout<<"travse pre order NOT recurs"<<endl;
BinaryTreeNode::preOrderVisit(&testBNTree1,PRINT_BNVALUE);
cout<<"travse in order recurs"<<endl;
BinaryTreeNode::inOrderVisitRecurs(&testBNTree1,PRINT_BNVALUE);
cout<<"travse in order NOT recurs"<<endl;
BinaryTreeNode::inOrderVisit(&testBNTree1,PRINT_BNVALUE);
//采用层级遍历清空flags标志
BinaryTreeNode::levelVisit(&testBNTree1,RESET_FLAGS);
cout<<"travse post order recurs"<<endl;
BinaryTreeNode::postOrderVisitRecurs(&testBNTree1,PRINT_BNVALUE);
cout<<"travse post order NOT recurs"<<endl;
BinaryTreeNode::postOrderVisit(&testBNTree1,PRINT_BNVALUE);
cout<<"travse level"<<endl;
BinaryTreeNode::levelVisit(&testBNTree1,PRINT_BNVALUE);
return 0;
}
分享到:
相关推荐
二叉树遍历的特点
数据结构课程设计--二叉树遍历及其应用、对树的先序遍历、后序遍历、中序遍历、层序遍历、二叉树的深度及其叶子树、并打印树形。
1.二叉树的基本操作实现【问题描述】建立一棵二叉树,用递归方法实现二叉树的如下基本操作:(1)按先序序列构造一棵二叉链表表示的二叉树T;...ABCDEFG【选做内容】采用非递归算法实现二叉树遍历。
实验三 二叉树遍历与路径查找(二叉树实验) 实现功能:建立二叉树存储结构、求二叉树的先序遍历、求二叉树的中序遍历、求二叉树的后序遍历、求二叉树的层次遍历、求根到给定结点的路径。 主控菜单: 1.建立二叉树...
二叉树遍历的非递归操作二叉树遍历的非递归操作
一个实现二叉树遍历的程序代码,有先序,中序和后序。
易语言二叉树遍历源码,二叉树遍历,二叉树_取下级树,内存_读整数内存,读整数内存_
二叉树遍历问题二叉树遍历问题二叉树遍历问题二叉树遍历问题二叉树遍历问题二叉树遍历问题二叉树遍历问题二叉树遍历问题二叉树遍历问题二叉树遍历问题二叉树遍历问题二叉树遍历问题二叉树遍历问题二叉树遍历问题...
运用C语言编写的二叉树遍历程序 先序遍历·中序遍历·后序遍历
java 写的算24程序。用两种二叉树遍历,并规整输出字符串
二叉树遍历 二叉树遍历
二叉树遍历算法二叉树遍历算法二叉树遍历算法二叉树遍历算法二叉树遍历算法二叉树遍历算法
用堆栈实现二叉树遍历,C语言实现的数据结构
遍历二叉树 遍历二叉树的先序、中序和非递归遍历二叉树的六种算法
数据结构第七章二叉树遍历的源代码,有用有用~~按先序次序输入二叉树中结点的值(一个字符),空格字符表示空树...
二叉树遍历问题 二叉树遍历问题-前序, 中序, 后序二叉树遍历问题-前序, 中序, 后序二叉树遍历问题-前序, 中序, 后序二叉树遍历问题-前序, 中序, 后序二叉树遍历问题-前序, 中序, 后序二叉树遍历问题-前序...
利用二叉树遍历算法的框架和思路,假设访问结点的具体操作不仅仅局限于输出结点数据域的值,而把访问延伸到对结点的判别、计数等其他操作。这样可以解决一些关于二叉树的其他实际问题。(1) 统计二叉树中结点总数m...
二叉树遍历操作.cpp
二叉树遍历问题-二叉树遍历问题