今天给各位分享c语言编写树的遍历的知识,其中也会对c++树的遍历进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
1、C语言 树的生成和遍历2、c语言如何实现一棵二叉树的遍历3、如何用C语言实现层次遍历二叉树?4、各路英雄好汉请注意!哪位高手会用C语言编写树的建立和遍历?5、C语言二叉树的遍历。6、急求C语言写二叉树的遍历
C语言 树的生成和遍历
#include //头文件
#include
typedef struct BiTNode
{
char data;
struct BiTNode *lchild,*rchild;
}
BiTNode,*BiTree;//定义结点类型
BiTree CreateBiTree()//创建树
{
char p;BiTree T;
scanf(“%c”,p);
if(p==’ ‘)
T=NULL;
else
{
T=(BiTNode *)malloc(sizeof(BiTNode));//为结点开辟空间
T-data=p;
T-lchild=CreateBiTree();
T-rchild=CreateBiTree();
}
return (T);
}
void PreOrder(BiTree T)//先序
{
if(T!=NULL)
{
printf(“%c”,T-data);
PreOrder(T-lchild);
PreOrder(T-rchild);
}
}
void InOrder(BiTree T)//中序
{
if(T!=NULL)
{
InOrder(T-lchild);
printf(“%c”,T-data);
InOrder(T-rchild);
}
}
void PostOrder(BiTree T)//后序
{
if(T!=NULL)
{
PostOrder(T-lchild);
PostOrder(T-rchild);
printf(“%c”,T-data);
}
}
void main()//主函数
{
BiTree Ta;
Ta=CreateBiTree();
printf(“先序遍历:”);
printf(“\n”);
PreOrder(Ta);
printf(“\n”);
printf(“中序遍历:”);
printf(“\n”);
InOrder(Ta);
printf(“\n”);
printf(“后序遍历:”);
printf(“\n”);
PostOrder(Ta);
}
给你个简单的例子:
AB***
其中*代表空格
复杂点的例子
ABC**DE*f**g***
其中*代表空格
c语言如何实现一棵二叉树的遍历
今天我也遇到这道题了,经过我的研究,我觉得应该是如下的解答:
首先画出该树 :如下图左边所示。然后根据树的二叉链表表示法表示存储结构如图右边所示:
注意这里的指针域为左边表示第一个孩子*firstchild,右边表示兄弟*nextsibling
紧接着就涉及到了树与二叉树的转换:
核心思想:左子树放孩子,右子树放兄弟,则有如图所示的二叉树:
如何用C语言实现层次遍历二叉树?
下面是c语言的前序遍历二叉树的算法,在这里假设的节点元素值假设的为字符型,
说明:算法中用到了结构体,也用到了递归的方法,你看看怎么样,祝你好运!
#include”stdio.h”
typedef
char
elemtype;
typedef
struct
node
//定义链表结构
{
elemtype
data;
//定义节点值
struct
note
*lchild;
//定义左子节点值
struct
note
*rchild;
//定义右节点值
}btree;
preorder(btree
*root)
//前序遍历
{
if(roof!=null)
//如果不是空节点
{
printf(“%c\n”,root-data);
//输出当前节点
preorder(root-lchild);
//递归前序遍历左子节点
preorder(root-rchild);
//递归前序遍历右子节点
}
return;
//结束
}
各路英雄好汉请注意!哪位高手会用C语言编写树的建立和遍历?
分类: 电脑/网络 程序设计 其他编程语言
问题描述:
数据结构老师在课堂上讲了树的建立和遍历的算法,回头让我自己用C语言编写它们的程序 ,可谓绞尽脑汁,但总运行不了.
解析:
#include iostream
#include string
#include stdio.h
#include cstdio
#include stdlib.h
using namespace std;
/
#define MAXSIZE 20
/
typedef struct BiTree 二叉树结构体定义
{
char date; 节点值
struct BiTree *lchild; 左字树指针
struct BiTree *rchild; 右字树指针
}BitNode,*BitTree;
/
typedef struct
{
BitTree data[MAXSIZE]; 堆栈结构体定义
int top; 存储二叉树的指针
}SeqStack;
/
typedef struct
{
BitTree data[MAXSIZE]; 队列结构体定义
int front, rear; 存储二叉树的指针
}Queue;
/
void InitQueue(Queue q) 初始化队列函数
{
q.front=0; 采用数组连续存储
q.rear=0;
}
/
void InsqQueue(Queue q,BitTree T) 入队函数
{
q.rear++;
q.data[q.rear]= T;
}
/
int EmptyQueue(Queue q) 判断队列是否为空函数
{
if(q.rear==q.front)
return 1; 空则返回1,否则0
else
return 0;
}
/
void OutsqQueue(Queue q,BitTree p) 出队函数
{
p=q.data[q.front+1];
q.front++;
}
/
int i = 0; 全局常量,用来对建立二叉树时读取字符数组
char ch[] = {“ABD##EJ###CF#I##G##”}; 二叉树结点数组表示
/
int Empty_SeqStack(SeqStack *s) 判断栈空函数
{
if (s-top == -1) top为-1时为空
return 1;
else
return 0;
}
/
BitNode *Pop_SeqStack(SeqStack *s) 退栈函数
{
BitNode *p;
if(Empty_SeqStack ( s ) ) 调用判断栈空函数
return 0; 栈空不能出栈
else
{
p = s-data[s-top];
s-top–;
return p;
} 栈顶元素存入p,返回p
}
/
int Push_SeqStack (SeqStack *s, BitNode *c) 压栈函数
{
if (s-top == MAXSIZE-1)
return 0; 栈满不能入栈
else
{
s-top++; 压栈
s-data[s-top] = c;
return 1;
}
}
SeqStack *Init_SeqStack() 栈的初始化函数
{
SeqStack *s; 定义栈指针
s = (SeqStack *)malloc(sizeof(SeqStack)); 分配存储空间
s-top = -1; 栈的初始top值为-1
return s;
}
/
void CreatBiTree(BitTree T) 采用递归方法建立二叉树函数
{
if(ch[i] == ‘#’) 遇到#号指针为空
{
T = NULL;
++i;
}
else
{
T = (BitNode *)malloc(sizeof(BitNode)); 动态分配内存
T – date = ch[i]; 负值
++i; 数组编号增1
CreatBiTree(T – lchild); 建立左字树
CreatBiTree(T – rchild); 建立右字树
}
}
void PostOrder(BitNode *T) 递归法后续遍历函数
{
if(T)
{
PostOrder(T – lchild);
PostOrder(T – rchild);
cout T – date “\t”;
}
}
/
void PreOrder(BitNode *T) 非递归前序遍历函数
{
BitNode *p;
p = T;
SeqStack *sk;
sk = Init_SeqStack(); 建立堆栈sk
while(p != NULL) 节点指针不为空循环
{
cout p – date; 输出结点值
if(p – rchild != NULL)
Push_SeqStack(sk,p – rchild); 右子树非空压栈
if(p – lchild != NULL)
p = p – lchild;
else
if(!Empty_SeqStack(sk)) 栈不空出栈
p = Pop_SeqStack(sk);
else
p = NULL; 负值空使循环结束
}
}
/
void TravelOrder(BitNode *T) 按层次遍历函数
{
Queue q;
InitQueue(q); 初始化队列
BitTree p,pl,pr;
p = T;
if(p)
InsqQueue(q,p); 指针非空入队
while(!EmptyQueue(q)) 队不空循环
{
OutsqQueue(q,p); 出队
cout p – date; 访问输出结点值
pl = p – lchild; 指向左子树
pr = p – rchild; 右子树
if(pl) 非空入队
InsqQueue(q,pl);
if(pr)
InsqQueue(q,pr);
}
}
/
int get_choice() 提示函数显示程序功能
{
int choice;
cout endl;
cout “请选择遍历输出的方式:” endl;
cout “1前序遍历此二叉树”;
cout “2为后序遍历此二叉树” endl;
cout “3为按层次遍历此二叉树”;
cout “4退出” endl;
cin choice;
return choice;
}
/
void outchar() 原始二叉树表示函数
{
cout “这是二叉树的形态” endl;
cout ” ” “A” endl;
cout ” ” “B” ” ” “C” endl;
cout ” ” “D” ” ” “E” ” ” “F” ” ” “G” endl;
cout ” ” “\t” “J” “\t” “I” endl;
}
/
void main()
{
int selection;
BitTree t;
CreatBiTree(t); 创建二叉树
outchar();
selection = get_choice(); 输出提示信息
while(selection != 4) 循环显示
{
if(selection == 1)
PreOrder(t); 选择1前序遍历
else
if(selection == 2) 选择2后序遍历
PostOrder(t);
else
if(selection == 3) 选择3层次遍历
TravelOrder(t);
selection = get_choice();
}
free(t);
1.递归方法建立二叉树
2.循环显示操作
2.1前序非递归遍历
2.2后序递归遍历
2.3按层次遍历
}
/
/
我同学的作业。。。
C语言二叉树的遍历。
二叉树的前中后遍历(递归与非递归)
#includestdio.h
#includestdlib.h
typedef struct NODE
{
char value;
struct NODE *LChild;
struct NODE *RChild;
}BiTNode,*BiTree; //二叉树数据结构
BiTree root;
typedef struct node
{
BiTNode *pointer;
struct node *link;
}LinkStackNode,*LinkStack; //链栈数据结构
LinkStack S;
int count = 0;
//BiTNode * InitTree(BiTree Tree);
BiTNode *CreateTree(BiTree Tree); //创建二叉树
void PreOrder(BiTree Tree); //递归前序遍历二叉树
void MidOrder(BiTree Tree); //递归中序遍历二叉树
void PostOrder(BiTree Tree); //递归后序遍历二叉树
void NPreOrder(BiTree Tree); //非递归前序遍历二叉树
void NMidOrder(BiTree Tree); //非递归中序遍历二叉树
void NPostOrder(BiTree Tree); //非递归后序遍历二叉树
//—————————————————
LinkStackNode *InitLinkStack(LinkStack top); //初始化链栈
void Push(LinkStack top,BiTNode *p); //进栈操作
BiTNode * Pop(LinkStack top); //出栈操作
//int IsEmpty(LinkStack S); //判断栈是否为空
void main()
{
//BiTree tree;
//root = InitTree(tree);
root = CreateTree(root);
PreOrder(root);
printf(“\n”);
MidOrder(root);
printf(“\n”);
PostOrder(root);
printf(“\n”);
NPreOrder(root);
printf(“\n”);
NMidOrder(root);
printf(“\n”);
NPostOrder(root);
printf(“\n”);
}
/*BiTNode * InitTree(BiTree Tree)
{
//BiTNode *root;
//root = Tree;
Tree = (BiTNode *)malloc(sizeof(BiTNode));
Tree = NULL;
//Tree-LChild = NULL;
//Tree-RChild = NULL;
return Tree;
}*/
//二叉树的扩展先序遍历的创建
BiTNode * CreateTree(BiTree Tree)
{
char ch;
ch = getchar();
if(ch == ‘.’)
Tree = NULL;
else
{
Tree = (BiTNode *)malloc(sizeof(BiTNode));
if(Tree)
{
Tree-value = ch;
Tree-LChild = CreateTree(Tree-LChild);
Tree-RChild = CreateTree(Tree-RChild);
}
}
return Tree;
}
//递归前序遍历二叉树
void PreOrder(BiTree Tree)
{
if(Tree)
{
printf(“%c”,Tree-value);
PreOrder(Tree-LChild);
PreOrder(Tree-RChild);
}
}
//递归中序遍历二叉树
void MidOrder(BiTree Tree)
{
if(Tree)
{
MidOrder(Tree-LChild);
printf(“%c”,Tree-value);
MidOrder(Tree-RChild);
}
}
//递归后序遍历二叉树
void PostOrder(BiTree Tree)
{
if(Tree)
{
PostOrder(Tree-LChild);
PostOrder(Tree-RChild);
printf(“%c”,Tree-value);
}
}
//非递归前序遍历二叉树
void NPreOrder(BiTree Tree)
{
BiTNode *p;
S = InitLinkStack(S);
p = Tree;
while(p || count != 0)
{
if(p)
{
if(p-RChild)
Push(S,p-RChild);
printf(“%c”,p-value);
p = p-LChild;
}
else
p = Pop(S);
}
}
//非递归中序遍历二叉树
void NMidOrder(BiTree Tree)
{
//char ch;
BiTNode *p;
S = InitLinkStack(S);
p = Tree;
while(p || count != 0)
{
if(p)
{
Push(S,p);
p = p-LChild;
}
else
{
p = Pop(S);
printf(“%c”,p-value);
p = p-RChild;
}
}
}
//非递归后序遍历二叉树
void NPostOrder(BiTree Tree)
{
BiTNode *p,*q = NULL;
S = InitLinkStack(S);
p = Tree;
while(p || count != 0)
{
if(p)
{
Push(S,p);
p = p-LChild;
}
else
{
p = S-link-pointer;
if(p-RChild == NULL || p-RChild == q)
{
p = Pop(S);
printf(“%c”,p-value);
q = p;
p = NULL;
}
else
{
//p = Pop(S);
p = p-RChild;
}
}
}
}
//初始化链栈
LinkStackNode *InitLinkStack(LinkStack top)
{
top = (LinkStackNode *)malloc(sizeof(LinkStackNode));
return top;
}
//进栈操作
void Push(LinkStack top,BiTNode *p)
{
LinkStackNode *temp;
temp = (LinkStackNode *)malloc(sizeof(LinkStackNode));
if(temp)
{
temp-pointer = p;
temp-link = top-link;
top-link = temp;
count++;
}
}
//出栈操作
BiTNode * Pop(LinkStack top)
{
//char ch;
BiTNode *p;
LinkStackNode *temp;
p = (BiTNode *)malloc(sizeof(BiTNode));
temp = top-link;
if(temp)
{
top-link = temp-link;
p = temp-pointer;
free(temp);
count–;
}
return p;
}
急求C语言写二叉树的遍历
BinaryTree.h:
/********************************************************************
created: 2006/07/04
filename: BinaryTree.h
author: 李创
purpose: 演示二叉树的算法
*********************************************************************/
#ifndef BinaryTree_H
#define BinaryTree_H
#i nclude stdlib.h
#i nclude stack
class BinaryTree
{
private:
typedef int Item;
typedef struct TreeNode
{
Item Node;
TreeNode* pRight;
TreeNode* pLeft;
TreeNode(Item node = 0, TreeNode* pright = NULL, TreeNode* pleft = NULL)
: Node(node)
, pRight(pright)
, pLeft(pleft)
{
}
}TreeNode, *PTreeNode;
public:
enum TraverseType
{
PREORDER = 0, // 前序
INORDER = 1, // 中序
POSTORDER = 2, // 后序
LEVELORDER = 3 // 层序
};
BinaryTree(Item Array[], int nLength);
~BinaryTree();
PTreeNode GetRoot()
{
return m_pRoot;
}
// 遍历树的对外接口
// 指定遍历类型和是否是非递归遍历,默认是递归遍历
void Traverse(TraverseType traversetype, bool bRec = true);
private:
PTreeNode CreateTreeImpl(Item Array[], int nLength);
void DetroyTreeImpl(PTreeNode pTreenode);
void PreTraverseImpl(PTreeNode pTreenode); // 递归前序遍历树
void InTraverseImpl(PTreeNode pTreenode); // 递归中序遍历树
void PostTraverseImpl(PTreeNode pTreenode); // 递归后序遍历树
void NoRecPreTraverseImpl(PTreeNode pTreenode); // 非递归前序遍历树
void NoRecInTraverseImpl(PTreeNode pTreenode); // 非递归中序遍历树
void NoRecPostTraverseImpl(PTreeNode pTreenode); // 非递归后序遍历树
void LevelTraverseImpl(PTreeNode pTreenode);
PTreeNode m_pRoot; // 根结点
// 采用STL里面的stack作为模拟保存链表结点的stack容器
typedef std::stackBinaryTree::PTreeNode TreeNodeStack;
};
#endif
BinaryTree.cpp:
/********************************************************************
created: 2006/07/04
filename: BinaryTree.cpp
author: 李创
purpose: 演示二叉树的算法
*********************************************************************/
#i nclude iostream
#i nclude assert.h
#i nclude queue
#i nclude “BinaryTree.h”
BinaryTree::BinaryTree(Item Array[], int nLength)
: m_pRoot(NULL)
{
assert(NULL != Array);
assert(nLength 0);
m_pRoot = CreateTreeImpl(Array, nLength);
}
BinaryTree::~BinaryTree()
{
DetroyTreeImpl(m_pRoot);
}
// 按照中序递归创建树
BinaryTree::PTreeNode BinaryTree::CreateTreeImpl(Item Array[], int nLength)
{
int mid = nLength / 2;
PTreeNode p = new TreeNode(Array[mid]);
if (nLength 1)
{
p-pLeft = CreateTreeImpl(Array, nLength / 2);
p-pRight = CreateTreeImpl(Array + mid + 1, nLength / 2 – 1);
}
return p;
}
void BinaryTree::DetroyTreeImpl(PTreeNode pTreenode)
{
if (NULL != pTreenode-pLeft)
{
DetroyTreeImpl(pTreenode-pLeft);
}
if (NULL != pTreenode-pRight)
{
DetroyTreeImpl(pTreenode-pRight);
}
delete pTreenode;
pTreenode = NULL;
}
// 遍历树的对外接口
// 指定遍历类型和是否是非递归遍历,默认是递归遍历
void BinaryTree::Traverse(TraverseType traversetype, bool bRec /*= true*/)
{
switch (traversetype)
{
case PREORDER: // 前序
{
if (true == bRec)
{
std::cout “递归前序遍历树\n”;
PreTraverseImpl(m_pRoot);
}
else
{
std::cout “非递归前序遍历树\n”;
NoRecPreTraverseImpl(m_pRoot);
}
}
break;
case INORDER: // 中序
{
if (true == bRec)
{
std::cout “递归中序遍历树\n”;
InTraverseImpl(m_pRoot);
}
else
{
std::cout “非递归中序遍历树\n”;
NoRecInTraverseImpl(m_pRoot);
}
}
break;
case POSTORDER: // 后序
{
if (true == bRec)
{
std::cout “递归后序遍历树\n”;
PostTraverseImpl(m_pRoot);
}
else
{
std::cout “非递归后序遍历树\n”;
NoRecPostTraverseImpl(m_pRoot);
}
}
break;
case LEVELORDER: // 层序
{
std::cout “层序遍历树\n”;
LevelTraverseImpl(m_pRoot);
}
}
std::cout std::endl;
}
// 递归前序遍历树
void BinaryTree::PreTraverseImpl(PTreeNode pTreenode)
{
if (NULL == pTreenode)
return;
std::cout “Item = ” pTreenode-Node std::endl;
PreTraverseImpl(pTreenode-pLeft);
PreTraverseImpl(pTreenode-pRight);
}
// 非递归前序遍历树
void BinaryTree::NoRecPreTraverseImpl(PTreeNode pTreenode)
{
if (NULL == pTreenode)
return;
TreeNodeStack NodeStack;
PTreeNode pNode;
NodeStack.push(pTreenode);
while (!NodeStack.empty())
{
while (NULL != (pNode = NodeStack.top())) // 向左走到尽头
{
std::cout “Item = ” pNode-Node std::endl; // 访问当前结点
NodeStack.push(pNode-pLeft); // 左子树根结点入栈
}
NodeStack.pop(); // 左子树根结点退
栈
if (!NodeStack.empty())
{
pNode = NodeStack.top();
NodeStack.pop(); // 当前结点退栈
NodeStack.push(pNode-pRight); // 当前结点的右子树根结点入栈
}
}
}
// 中序遍历树
// 中序遍历输出的结果应该和用来初始化树的数组的排列顺序一致
void BinaryTree::InTraverseImpl(PTreeNode pTreenode)
{
if (NULL == pTreenode)
return;
if (NULL != pTreenode-pLeft)
{
InTraverseImpl(pTreenode-pLeft);
}
std::cout “Item = ” pTreenode-Node std::endl;
if (NULL != pTreenode-pRight)
{
InTraverseImpl(pTreenode-pRight);
}
}
// 非递归中序遍历树
void BinaryTree::NoRecInTraverseImpl(PTreeNode pTreenode)
{
if (NULL == pTreenode)
return;
TreeNodeStack NodeStack;
PTreeNode pNode;
NodeStack.push(pTreenode);
while (!NodeStack.empty())
{
while (NULL != (pNode = NodeStack.top())) // 向左走到尽头
{
NodeStack.push(pNode-pLeft);
}
NodeStack.pop();
if (!NodeStack.empty() NULL != (pNode = NodeStack.top()))
{
std::cout “Item = ” pNode-Node std::endl;
NodeStack.pop();
NodeStack.push(pNode-pRight);
}
}
}
// 后序遍历树
void BinaryTree::PostTraverseImpl(PTreeNode pTreenode)
{
if (NULL == pTreenode)
return;
if (NULL != pTreenode-pLeft)
{
PostTraverseImpl(pTreenode-pLeft);
}
if (NULL != pTreenode-pRight)
{
PostTraverseImpl(pTreenode-pRight);
}
std::cout “Item = ” pTreenode-Node std::endl;
}
// 非递归后序遍历树
void BinaryTree::NoRecPostTraverseImpl(PTreeNode pTreenode)
{
if (NULL == pTreenode)
return;
TreeNodeStack NodeStack;
PTreeNode pNode1, pNode2;
NodeStack.push(pTreenode);
pNode1 = pTreenode-pLeft;
bool bVisitRoot = false; // 标志位,是否访问过根结点
while (!NodeStack.empty())
{
while (NULL != pNode1) // 向左走到尽头
{
NodeStack.push(pNode1);
pNode1 = pNode1-pLeft;
}
pNode1 = NodeStack.top();
NodeStack.pop();
if (NULL == pNode1-pRight) // 如果没有右子树就是叶子结点
{
std::cout “Item = ” pNode1-Node std::endl;
pNode2 = pNode1;
pNode1 = NodeStack.top();
if (pNode2 == pNode1-pRight) // 如果这个叶子结点是右子树
{
std::cout “Item = ” pNode1-Node std::endl;
NodeStack.pop();
pNode1 = NULL;
}
else // 否则访问右子树
{
pNode1 = pNode1-pRight;
}
}
else // 访问右子树
{
if (pNode1 == pTreenode true == bVisitRoot) // 如果已经访问过右子树那么就退出
{
std::cout “Item = ” pNode1-Node std::endl;
return;
}
else
{
if (pNode1 == pTreenode)
{
bVisitRoot = true;
}
NodeStack.push(pNode1);
pNode1 = pNode1-pRight;
}
}
}
}
// 按照树的层次从左到右访问树的结点
void BinaryTree::LevelTraverseImpl(PTreeNode pTreenode)
{
if (NULL == pTreenode)
return;
// 层序遍历用于保存结点的容器是队列
std::queuePTreeNode NodeQueue;
PTreeNode pNode;
NodeQueue.push(pTreenode);
while (!NodeQueue.empty())
{
pNode = NodeQueue.front();
NodeQueue.pop();
std::cout “Item = ” pNode-Node std::endl;
if (NULL != pNode-pLeft)
{
NodeQueue.push(pNode-pLeft);
}
if (NULL != pNode-pRight)
{
NodeQueue.push(pNode-pRight);
}
}
}
main.cpp
/********************************************************************
created: 2006/07/04
filename: main.cpp
author: 李创
purpose: 测试二叉树的算法
*********************************************************************/
#i nclude “BinaryTree.h”
#i nclude stdio.h
#i nclude stdlib.h
#i nclude time.h
#i nclude iostream
void DisplayArray(int array[], int length)
{
int i;
for (i = 0; i length; i++)
{
printf(“array[%d] = %d\n”, i, array[i]);
}
}
void CreateNewArray(int array[], int length)
{
for (int i = 0; i length; i++)
{
array[i] = rand() % 256 + i;
}
}
int main()
{
int array[10];
srand(time(NULL));
// 创建数组
CreateNewArray(array, 10);
DisplayArray(array, 10);
BinaryTree *pTree = new BinaryTree(array, 10);
// 测试前序遍历
pTree-Traverse(BinaryTree::PREORDER);
std::cout “root = ” pTree-GetRoot()-Node std::endl;
std::cout “root-left = ” pTree-GetRoot()-pLeft-Node std::endl;
std::cout “root-right = ” pTree-GetRoot()-pRight-Node std::endl;
pTree-Traverse(BinaryTree::PREORDER, false);
// 测试中序遍历
pTree-Traverse(BinaryTree::INORDER);
std::cout “root = ” pTree-GetRoot()-Node std::endl;
std::cout “root-left = ” pTree-GetRoot()-pLeft-Node std::endl;
std::cout “root-right = ” pTree-GetRoot()-pRight-Node std::endl;
pTree-Traverse(BinaryTree::INORDER, false);
// 测试后序遍历
pTree-Traverse(BinaryTree::POSTORDER);
std::cout “root = ” pTree-GetRoot()-Node std::endl;
std::cout “root-left = ” pTree-GetRoot()-pLeft-Node std::endl;
std::cout “root-right = ” pTree-GetRoot()-pRight-Node std::endl;
pTree-Traverse(BinaryTree::POSTORDER, false);
// 测试层序遍历
pTree-Traverse(BinaryTree::LEVELORDER);
system(“pause”);
delete pTree;
return 0;
}
c语言编写树的遍历的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于c++树的遍历、c语言编写树的遍历的信息别忘了在本站进行查找喔。