exam7-2-2.cpp
来自「包含各种测试,查找和算法等代码,如冒泡算法,树的遍历,链表,队列,堆栈等」· C++ 代码 · 共 65 行
CPP
65 行
#include <iostream.h>
#include <stdlib.h>
#include "BiTreeNode.h"
//#include "BiTreetraverse.h"
#include "BiTreeLib2.h"
const MaxStackSize = 100;
typedef BiTreeNode<char> DataType;
#include "SeqStack2.h"
void NotRecurPreOrder(DataType *t, void Visit(char item))
//使用Visit(item)函数非递归前序遍历二叉树t
{
SeqStack stack;
DataType *p;
stack.Push(t);
while(stack.NotEmpty())
{
p = stack.Pop();
Visit(p->data);
if(p->Right() != NULL)
stack.Push(p->Right());
if(p->Left() != NULL)
stack.Push(p->Left());
}
}
void MakeCharTree(BiTreeNode<char>* &root)
{
BiTreeNode<char> *b, *c, *d, *e, *f, *g, *null = NULL;
g = GetTreeNode('G');
d = GetTreeNode('D', null, g);
b = GetTreeNode('B', d);
e = GetTreeNode('E');
f = GetTreeNode('F');
c = GetTreeNode('C', e, f);
root = GetTreeNode('A', b, c);
}
void Printchar(char item)
{
cout << item << " ";
}
void main(void)
{
BiTreeNode<char> *root1;
MakeCharTree(root1);
cout << "递归的前序遍历结点次序为: ";
PreOrder(root1, Printchar);
cout << "\n非递归的前序遍历结点次序为: ";
NotRecurPreOrder(root1, Printchar);
int c = LeafNum(root1);
cout << "c = " << c << endl;
Destroy(root1);
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?