最近真是各种事情缠绕在一起,一直不敢挥霍时间刷题,惭愧惭愧。
像链表一样,在练习树的算法之前先开一章,打好基础,主要是遍历的方法:前中后序遍历,非递归版本,广度(层次)遍历。
PS:深度遍历暂缺,另外后序的非递归版本copy别人的方法需要巩固。
偷个懒,一股脑全部贴上来:
#include <vector>
#include <iostream>
#include <stdlib.h>
#include <stack>
#include <queue>
using namespace std;
struct TreeNode
{
TreeNode *left;
TreeNode *right;
char val;
TreeNode(char x);
~TreeNode();
};
TreeNode::TreeNode(char x)
{
val = x;
left = 0;
right = 0;
}
TreeNode::~TreeNode(){}
void visit(TreeNode *t)
{
if (t)
cout << t->val;
}
void preorder(TreeNode *root)
{
if(root)
{
visit(root);
preorder(root->left);
preorder(root->right);
}
}
void inorder(TreeNode *root)
{
if(root)
{
inorder(root->left);
visit(root);
inorder(root->right);
}
}
void postorder(TreeNode *root)
{
if(root)
{
postorder(root->left);
postorder(root->right);
visit(root);
}
}
void preorder2(TreeNode *root)
{
stack<TreeNode *> s;
while (root || !s.empty())
{
if (root)
{
s.push(root);
cout << root->val;
root = root->left;
}
else
{
root = s.top();
s.pop();
root = root->right;
}
}
}
void inorder2(TreeNode *root)
{
stack<TreeNode *> s;
while (root || !s.empty())
{
if (root)
{
s.push(root);
root = root->left;
}
else
{
root = s.top();
s.pop();
cout << root->val;
root = root->right;
}
}
}
typedef struct BiTNodePost{
TreeNode * biTree;
char tag;
}BiTNodePost,*BiTreePost;
void postorder2(TreeNode *root)
{
stack<BiTreePost> stack;
//p是遍历指针
TreeNode * p = root;
BiTreePost BT;
//栈不空或者p不空时循环
while(p != NULL || !stack.empty())
{
//遍历左子树
while(p != NULL)
{
BT = (BiTreePost)malloc(sizeof(BiTNodePost));
BT->biTree = p;
//访问过左子树
BT->tag = 'L';
stack.push(BT);
p = p->left;
}
//左右子树访问完毕访问根节点
while(!stack.empty() && (stack.top())->tag == 'R'){
BT = stack.top();
//退栈
stack.pop();
printf("%c",BT->biTree->val);
}
//遍历右子树
if(!stack.empty())
{
BT = stack.top();
//访问过右子树
BT->tag = 'R';
p = BT->biTree;
p = p->right;
}
}//while
}
void levelorder(TreeNode *root)
{
queue<TreeNode *> q;
do
{
if (root && q.empty())
{
q.push(root);
}
if (!q.empty())
{
root = q.front();
q.pop();
cout << root->val;
if (root->left)
q.push(root->left);
if (root->right)
q.push(root->right);
}
}while(!q.empty());
}
int main()
{
// 初始化树结构
// A
// / \
// B C
// \ / \
// D E F
// \ \
// G H
// / \
// I J
// / \
// K L
TreeNode nA('A');
TreeNode nB('B');
TreeNode nC('C');
TreeNode nD('D');
TreeNode nE('E');
TreeNode nF('F');
TreeNode nG('G');
TreeNode nH('H');
TreeNode nI('I');
TreeNode nJ('J');
TreeNode nK('K');
TreeNode nL('L');
nA.left = &nB;
nA.right = &nC;
nB.right = &nD;
nD.right = &nG;
nC.left = &nE;
nC.right = &nF;
nF.right = &nH;
nH.left = &nI;
nH.right = &nJ;
nI.left = &nK;
nI.right = &nL;
cout << "前序遍历:" << endl;
preorder(&nA);
cout << endl;
cout << "前序遍历(非递归):" << endl;
preorder2(&nA);
cout << endl;
cout << "中序遍历:" << endl;
inorder(&nA);
cout << endl;
cout << "中序遍历(非递归):" << endl;
inorder2(&nA);
cout << endl;
cout << "后序遍历:" << endl;
postorder(&nA);
cout << endl;
cout << "后序遍历(非递归):" << endl;
postorder2(&nA);
cout << endl;
cout << "广度(层次)遍历:" << endl;
levelorder(&nA);
cout << endl;
return 0;
}