二叉树的建立以及三种遍历方式的递归、非递归的实现
2013-10-04 00:02
801 查看
#include <iostream>
#include <stack>
using namespace std;
char src[]="abd ceg f ";
int index=0;
typedef struct BinaryNode
{
char key;
struct BinaryNode* left;
struct BinaryNode* right;
}BinaryNode,*BinaryTree;
void createBinaryTree(BinaryTree& root)
{
if (src[index]==' ')
{
root=NULL;
index++;
}
else
{
root=new BinaryNode();
root->key=src[index++];
createBinaryTree(root->left);
createBinaryTree(root->right);
}
}
void preOrderRecursive(BinaryTree root)
{
if (root)
{
cout<<root->key<<" ";
preOrderRecursive(root->left);
preOrderRecursive(root->right);
}
}
void preOrderIterative(BinaryTree root)
{
stack<BinaryTree> nodes;
while(!nodes.empty()||root!=NULL)
{
while(root!=NULL)
{
cout<<root->key<<" ";
nodes.push(root);
root=root->left;
}
root=nodes.top();
nodes.pop();
root=root->right;
}
}
void inOrderRecursive(BinaryTree root)
{
if (root)
{
inOrderRecursive(root->left);
cout<<root->key<<" ";
inOrderRecursive(root->right);
}
}
void inOrderInterative(BinaryTree root)
{
stack<BinaryTree> nodes;
while(!nodes.empty()||root!=NULL)
{
while(root!=NULL)
{
nodes.push(root);
root=root->left;
}
root=nodes.top();
cout<<root->key<<" ";
nodes.pop();
root=root->right;
}
}
void postOrderRecursive(BinaryTree root)
{
if (root)
{
postOrderRecursive(root->left);
postOrderRecursive(root->right);
cout<<root->key<<" ";
}
}
void poseOrderIterative(BinaryTree root)
{
if (root==NULL)
{
return;
}
stack<BinaryTree> nodes;
BinaryTree pre=NULL;
BinaryTree cur=NULL;
nodes.push(root);
while(!nodes.empty())
{
cur=nodes.top();
if ((cur->left==NULL&&cur->right==NULL)||(pre!=NULL&&(pre==cur->left||pre==cur->right)))
{
cout<<cur->key<<" ";
pre=cur;
nodes.pop();
}
else
{
if (cur->right!=NULL)
{
nodes.push(cur->right);
}
if (cur->left!=NULL)
{
nodes.push(cur->left);
}
}
}
}
int main()
{
BinaryTree root=NULL;
createBinaryTree(root);
preOrderRecursive(root);
cout<<endl;
preOrderIterative(root);
cout<<endl;
inOrderRecursive(root);
cout<<endl;
inOrderInterative(root);
cout<<endl;
postOrderRecursive(root);
cout<<endl;
poseOrderIterative(root);
return 0;
}
#include <stack>
using namespace std;
char src[]="abd ceg f ";
int index=0;
typedef struct BinaryNode
{
char key;
struct BinaryNode* left;
struct BinaryNode* right;
}BinaryNode,*BinaryTree;
void createBinaryTree(BinaryTree& root)
{
if (src[index]==' ')
{
root=NULL;
index++;
}
else
{
root=new BinaryNode();
root->key=src[index++];
createBinaryTree(root->left);
createBinaryTree(root->right);
}
}
void preOrderRecursive(BinaryTree root)
{
if (root)
{
cout<<root->key<<" ";
preOrderRecursive(root->left);
preOrderRecursive(root->right);
}
}
void preOrderIterative(BinaryTree root)
{
stack<BinaryTree> nodes;
while(!nodes.empty()||root!=NULL)
{
while(root!=NULL)
{
cout<<root->key<<" ";
nodes.push(root);
root=root->left;
}
root=nodes.top();
nodes.pop();
root=root->right;
}
}
void inOrderRecursive(BinaryTree root)
{
if (root)
{
inOrderRecursive(root->left);
cout<<root->key<<" ";
inOrderRecursive(root->right);
}
}
void inOrderInterative(BinaryTree root)
{
stack<BinaryTree> nodes;
while(!nodes.empty()||root!=NULL)
{
while(root!=NULL)
{
nodes.push(root);
root=root->left;
}
root=nodes.top();
cout<<root->key<<" ";
nodes.pop();
root=root->right;
}
}
void postOrderRecursive(BinaryTree root)
{
if (root)
{
postOrderRecursive(root->left);
postOrderRecursive(root->right);
cout<<root->key<<" ";
}
}
void poseOrderIterative(BinaryTree root)
{
if (root==NULL)
{
return;
}
stack<BinaryTree> nodes;
BinaryTree pre=NULL;
BinaryTree cur=NULL;
nodes.push(root);
while(!nodes.empty())
{
cur=nodes.top();
if ((cur->left==NULL&&cur->right==NULL)||(pre!=NULL&&(pre==cur->left||pre==cur->right)))
{
cout<<cur->key<<" ";
pre=cur;
nodes.pop();
}
else
{
if (cur->right!=NULL)
{
nodes.push(cur->right);
}
if (cur->left!=NULL)
{
nodes.push(cur->left);
}
}
}
}
int main()
{
BinaryTree root=NULL;
createBinaryTree(root);
preOrderRecursive(root);
cout<<endl;
preOrderIterative(root);
cout<<endl;
inOrderRecursive(root);
cout<<endl;
inOrderInterative(root);
cout<<endl;
postOrderRecursive(root);
cout<<endl;
poseOrderIterative(root);
return 0;
}
相关文章推荐
- 线程死锁(由哲学家就餐到售票系统)
- Java 通配符解惑
- asp.net中水印的具体实现代码
- asp.net无法加载oci.dll等错误的解决方法
- asp.net C#生成和解析二维码的实例代码
- php 批量替换程序的具体实现代码
- js中单引号与双引号冲突问题解决方法
- JS教程:window.location使用方法的区别介绍
- 浅谈mssql access数据库 top分页方法
- 使用PHP Simple HTML DOM像jQuery一样操作html文档
- innerHTML、outerHTML、innerText、outerText的区别
- CSS 优先级
- pinyin4j,获取拼音的首字母
- 本人有项专利,现在需要编程人员
- Beautiful Soup Documentation
- 【ZZ】python with...as...用法
- struts2 中同一个action的实现中对应多个input的处理方法
- 网站web服务器分析小程序
- 招聘PHP、C++或delphi等职位(可兼职)
- 用了XCode5和iOS7SDK原来的工程画面全乱了