C++ 二叉树的遍历

（本文的代码很多来源于网络，因为上机课的好多题并不会敲……我会尽量附上原作者）

（由于我学得不是很好，有些地方可能说的不对，请有取舍的看吧（捂脸））

（2）深度优先遍历。深度优先遍历又分为 前序（先序）、中序、后序。

一、广度优先遍历

#include <iostream>
#include <string>
using namespace std;

template <typename T>
class queue
{
private:
int maxsize;
int front;
int rear;
T *arr;
public:
queue(int size = 10)
{
maxsize = size;
arr = new T[maxsize];
front = rear = 0;
}
~queue()
{
delete[]arr;
}
void clear()
{
front = rear;
}
bool enqueue(T item)
{
if ((rear + 1) % maxsize == front)
{
cout << "队列已满，溢出" << endl;
return false;
}
arr[rear] = item;
rear = (rear + 1) % maxsize;
return true;
}
bool dequeue(T item)
{
if (front == rear)
{
cout << "队列为空" << endl;
return false;
}
item = arr[front];
front = (front + 1) % maxsize;
return true;
}
T getfront()
{
if (front == rear)
{
cout << "队列为空" << endl;
return false;
}
return arr[front];
}
void pop()
{
front++;
}
bool isesmpty()
{
if (front == rear)
{
cout << "队列为空！" << endl;
return 1;
}
else
return 0;
}
bool isfull()
{
if ((rear + 1) % maxsize == front)
{
cout << "队列已满！" << endl;
return 1;
}
else
return 0;
}
void showque()
{
int i = 0;
if (!isesmpty())
{
cout << "打印队列：" << endl;
for (i = front;i != rear;i++)
{
cout << arr[i];
if ((i + 1) % maxsize == 0)
{
i = -1;
}
}
cout << endl;
}
}
};<pre name="code" class="cpp">

二、深度优先遍历

template <typename T>
class BinaryTreeNode
{
public:
T data;//存储的数据
BinaryTreeNode<T> *leftchild;
BinaryTreeNode<T> *rightchild;
BinaryTreeNode()
{
leftchild = rightchild = NULL;
}
BinaryTreeNode(T dat)
{
data = dat;
leftchild = NULL;
rightchild = NULL;
}
BinaryTreeNode(T dat, BinaryTreeNode *left, BinaryTreeNode *right)
{
data = dat;
leftchild = left;
rightchild = right;
}
};
template <typename T>
class BinaryTree
{
public:
BinaryTreeNode<T> *root;
BinaryTree(BinaryTreeNode<T> *t = NULL)
{
root = t;
}
bool isempty()
{
if (root == NULL)
return true;
else
return false;
}
void makeTree(const T dat, BinaryTree<T> left, BinaryTree<T> right)
{
root = new BinaryTreeNode<T>(dat, left.root, right.root);
left.root = right.root = 0;
}
void visit(BinaryTreeNode<T> *t)
{
cout << t->data << "  ";
}
void levelorder()//广度优先遍历二叉树（即逐层遍历二叉树）
{
queue<BinaryTreeNode<T> *> nodequeue;
BinaryTreeNode<T> * pointer = root;
if (pointer)
nodequeue.enqueue(pointer);
while (!nodequeue.isesmpty())
{
pointer = nodequeue.getfront();
visit(pointer);
nodequeue.pop();
if (pointer->leftchild)
nodequeue.enqueue(pointer->leftchild);
if (pointer->rightchild)
nodequeue.enqueue(pointer->rightchild);
}
}
void preOrder(BinaryTreeNode<T> * root)//前序递归
{
if (root != NULL)
{
visit(root);
preOrder(root->leftchild);
preOrder(root->rightchild);
}
}
void inOrder(BinaryTreeNode<T> * root)//中序递归
{
if (root != NULL)
{
inOrder(root->leftchild);
visit(root);
inOrder(root->rightchild);
}
}
void postOrder(BinaryTreeNode<T> * root)//后序递归
{
if (root != NULL)
{
postOrder(root->leftchild);
postOrder(root->rightchild);
visit(root);
}
}
void preorderwithoutrecusion(BinaryTreeNode<T> * r)//非递归前序遍历二叉树
{
stack<BinaryTreeNode<T> *>nodestack;
BinaryTreeNode<T> * pointer = r;
while (!nodestack.empty() || pointer)
{
if (pointer)
{
visit(pointer);
if (pointer->rightchild != NULL)
nodestack.push(pointer->rightchild);
pointer = pointer->leftchild;
}
else
{
pointer = nodestack.Top();
nodestack.pop();
}
}
}
void inorderwithoutrecusion(BinaryTreeNode<T> * r)//非递归中序遍历二叉树
{
stack<BinaryTreeNode<T> *>nodestack;
BinaryTreeNode<T> * pointer = r;
while (!nodestack.empty() || pointer)
{
if (pointer)
{
nodestack.push(pointer);
pointer = pointer->leftchild;

}
else
{
pointer = nodestack.Top();
visit(pointer);
pointer = pointer->rightchild;
nodestack.pop();
}
}
}
void postorderwithoutrecusion(BinaryTreeNode<T> *r)//非递归后序遍历
{
stack<BinaryTreeNode<T> *> nodestack;
BinaryTreeNode<T> *pointer = r;
BinaryTreeNode<T> *pre = r;
while (pointer)
{
for (;pointer->leftchild != NULL;pointer = pointer->leftchild)
nodestack.push(pointer);
while (pointer != NULL && (pointer->rightchild == NULL || pointer->rightchild == pre))
{
visit(pointer);
pre = pointer;
if (nodestack.empty())
return;
pointer = nodestack.Top();
nodestack.pop();
}
nodestack.push(pointer);
pointer = pointer->rightchild;
}
}
};

template <class T>
class stack
{
private:
int top;
int maxsize;
T * sta;
public:
stack(int size = 10)
{
maxsize = size;
sta = new T[maxsize];
top = -1;
}
~stack()
{
delete[]sta;
}
void push(T t)
{
sta[++top] = t;
}
bool empty()
{
}
bool full()
{
}
T Top()
{
if (empty())
{
cout << "栈为空";
return 0;
}
else
return sta[top];
}
void pop()
{
top--;
}
};

三、主函数

int main()
{
BinaryTreeNode<int> a(1), b(2), c(3);
BinaryTreeNode<int> d(4, &a, &b), e(5, NULL, &c);
BinaryTreeNode<int> f(6, NULL, &d), g(7, NULL, &e);
BinaryTreeNode<int> h(8, &f, &g);

BinaryTree<int> tree1(&h);
tree1.levelorder();
tree1.preOrder(&h);
cout << endl;
tree1.preorderwithoutrecusion(&h);
cout << endl;
tree1.inOrder(&h);
cout << endl;
tree1.inorderwithoutrecusion(&h);
cout << endl;
tree1.postOrder(&h);
cout << endl;
tree1.postorderwithoutrecusion(&h);
return 0;
}

C++二叉树实现+遍历

2017-10-20 21:09:03

树/二叉树 各种遍历源代码(C++)

2011年04月17日 36KB 下载

二叉树遍历c++实现

2016-10-28 17:30:10

C++ 二叉树遍历（三种遍历的递归实现）

2011-12-01 20:14:33

用C++实现二叉树的三种遍历方式

2017-08-29 16:35:22

二叉树各种遍历算法的C++实现

2015年05月19日 47KB 下载

用C++实现的二叉树递归遍历算法

2017-02-28 20:04:58

c++ 二叉树的遍历

2012年05月07日 2KB 下载

二叉树神级遍历算法——Morris遍历(C++版)

2015-11-27 14:36:57

C++实现二叉树遍历

2012-06-03 01:57:34