二叉树的四种遍历方式
二叉树的创建--先序非递归创建
#include<iostream>
#include<stack>
#include<queue>
#include<stdlib.h>
const int OK = 1;
const int ERROR = 0;
using namespace std;
typedef struct TreeNode{
int data;
struct TreeNode *lchild,*rchild;
}TreeNode;
void print(int data)
{
cout<<"-->"<<data<<" ";
}
TreeNode *creat_tree(int *arr,int size)
{
TreeNode *root,*pos,*father;
if(size < 1)
return NULL;
stack<TreeNode *> st;
root = (struct TreeNode *)malloc(sizeof(struct TreeNode));
root->data = arr[0];
root->lchild = root->rchild = NULL;
pos = root;
st.push(pos);
int i = 1;
while(i < size && !st.empty()){
while(i < size && arr[i] != -1){
pos = (struct TreeNode *)malloc(sizeof(TreeNode));
pos->data = arr[i];
pos->lchild = pos->rchild = NULL;
father = st.top();
father->lchild = pos;
st.push(pos);
i++;
}
father = st.top();
st.pop();
if(++i < size && arr[i]!= -1){
pos = (struct TreeNode *)malloc(sizeof(TreeNode));
pos->data = arr[i];
pos->lchild = pos->rchild = NULL;
father->rchild = pos;
st.push(pos);
i++;
}
else{
while(i < size && arr[i] == -1 && !st.empty()){
father = st.top();
st.pop();
i++;
if(i < size && arr[i]!= -1){
pos = (struct TreeNode *)malloc(sizeof(TreeNode));
pos->data = arr[i];
pos->lchild = pos->rchild = NULL;
father->rchild = pos;
st.push(pos);
i++;
break;
}
}
}
}
return root;
}先序遍历
递归实现
int pre_traver(TreeNode *bt)
{
if(NULL == bt)
return OK;
print(bt->data);
pre_traver(bt->lchild);
pre_traver(bt->rchild);
}非递归实现
int pre_traver2(TreeNode *bt)
{
if(NULL == bt){
cout<<"tree is empty!\n";
return OK;
}
TreeNode *pos = bt;
stack<TreeNode *> st;
while(bt || !st.empty()){
while(bt){
print(bt->data);
st.push(bt);
bt = bt->lchild;
}
bt = st.top();
st.pop();
bt = bt->rchild;
}
return OK;
}中序遍历
递归实现
int inorder_traver1(TreeNode *bt)
{
if(NULL == bt)
return OK;
inorder_traver1(bt->lchild);
print(bt->data);
inorder_traver1(bt->rchild);
}
非递归实现
int inorder_traver2(TreeNode *bt)
{
if(NULL == bt){
cout<<"tree is empty\n";
return OK;
}
stack<TreeNode *> st;
while(bt || !st.empty()){
while(bt){
st.push(bt);
bt = bt->lchild;
}
bt = st.top();
st.pop();
print(bt->data);
bt = bt->rchild;
}
return OK;
}后序遍历
递归实现
int post_traver1(TreeNode *bt)
{
if(NULL == bt)
return OK;
post_traver1(bt->lchild);
post_traver1(bt->rchild);
print(bt->data);
}非递归实现
int post_traver2(TreeNode *bt)
{
if(NULL == bt){
cout<<"tree is empty\n";
return OK;
}
stack<TreeNode *> st;
stack<int> flag;
int first = 1,second = 2;
while(bt || !st.empty()){
while(bt){
st.push(bt);
flag.push(first);
bt = bt->lchild;
}
if(1 == flag.top()){
flag.pop();
flag.push(second);
bt = st.top();
bt = bt->rchild;
}
else{
while(!flag.empty() && 2 == flag.top()){
flag.pop();
bt = st.top();
st.pop();
print(bt->data);
}
bt = NULL;
}
}
return OK;
}
层次遍历
代码实现
int level_traver(TreeNode *bt)
{
if(NULL == bt){
cout<<"tree is empty\n";
return OK;
}
queue<TreeNode *> que;
que.push(bt);
while(!que.empty()){
bt = que.front();
que.pop();
print(bt->data);
if(bt->lchild)
que.push(bt->lchild);
if(bt->rchild)
que.push(bt->rchild);
}
return OK;
}测试代码
int main(int argc, char **argv)
{
int arr[] = {1,2,3,-1,4,-1,-1,-1,5,6,-1,-1,7,8,-1,-1,-1};
int size = sizeof(arr)/sizeof(int);
TreeNode *root;
root = creat_tree(arr,size);
cout<<"pre--traver: ";
pre_traver2(root);
cout<<endl;
cout<<"inorder--traver: ";
inorder_traver2(root);
cout<<endl;
cout<<"post--traver: ";
//post_traver1(root);
post_traver2(root);
cout<<endl;
cout<<"level--traver: ";
level_traver(root);
cout<<endl;
//cout<<"tree--node: "<<count_tree(root)<<endl;
//cout<<"tree--depth: "<<depth_tree(root)<<endl;
return 0;
}
本文详细介绍了二叉树的创建方法及四种遍历方式:先序、中序、后序和层次遍历,并提供了非递归实现的代码示例。
658

被折叠的 条评论
为什么被折叠?



