您的位置:首页 > 其它

二叉树创建及遍历

2015-07-01 22:45 169 查看
声明:这篇文章是抄袭/article/1424392.html 请大家自己查看原博客

[code]#include<iostream>
#include<stack>
#include<queue>
using namespace std;

//二叉树结点数据结构
typedef struct BiTNode
{
    char data;              //数据
    struct BiTNode * lchild;//左右孩子指针
    struct BiTNode * rchild;
}BiTNode, *BiTree;

//后序遍历(非递归)需要使用
typedef struct BiTNodePost{
    BiTree biTree;
    char tag;
}BiTNodePost, *BiTreePost;

int CreateBiTree(BiTree & root); //创建二叉树
void PreOrder(BiTree  root);     //先序遍历二叉树 递归
void InOrder(BiTree  root);      //中序遍历二叉树 递归
void PostOrder(BiTree  root);    //后序遍历二叉树 递归
void PreOrder2(BiTree  root);    //先序遍历二叉树 非递归
void InOrder2(BiTree  root);     //中序遍历二叉树 非递归
void PostOrder2(BiTree  root);   //中序遍历二叉树 非递归
void LevelOrder(BiTree root);    //层次遍历二叉树  
int main()
{
    BiTree root = NULL;
    CreateBiTree(root);
    cout <<"\n先序遍历---递归"<<endl;
    PreOrder(root);
    cout << "\n先序遍历---非递归" << endl;
    PreOrder2(root);
    cout <<"\n中序遍历---递归" << endl;
    InOrder(root);
    cout << "\n中序遍历---非递归" << endl;
    InOrder2(root);
    cout << "\n后序遍历---递归" << endl;
    PostOrder(root);
    cout << "\n后序遍历---非递归" << endl;
    PostOrder2(root);
    cout << "\n层次遍历" << endl;
    LevelOrder(root);
    return 0;
}

/***********************
*函数功能:先序创建二叉树
*函数参数:二叉树的根节点
************************/
int CreateBiTree(BiTree & root)
{
    char data;
    cin >> data;
    if ('#' == data)
        root = NULL;
    else
    {
        root = (BiTree)malloc(sizeof(BiTNode));
        root->data = data;
        CreateBiTree(root->lchild);
        CreateBiTree(root->rchild);
    }
    return 0;
}

/***********************
*函数功能:先序遍历二叉树(递归)
*函数参数:二叉树的根节点
************************/
void PreOrder(BiTree  root)
{
    if (NULL == root)
    {
        return;
    }
    else
    {
        cout << root->data << " ";
        PreOrder(root->lchild);
        PreOrder(root->rchild);
    }
}

/***********************
*函数功能:中序遍历二叉树(递归)
*函数参数:二叉树的根节点
************************/
void InOrder(BiTree  root)
{
    if (NULL == root)
    {
        return;
    }
    else
    {
        InOrder(root->lchild);
        cout << root->data << " ";
        InOrder(root->rchild);
    }
}
/***********************
*函数功能:后续遍历二叉树(递归)
*函数参数:二叉树的根节点
************************/
void PostOrder(BiTree  root)
{
    if (NULL == root)
    {
        return;
    }
    else
    {
        PostOrder(root->lchild);
        PostOrder(root->rchild);
        cout << root->data << " ";
    }
}
/***********************
*函数功能:先序遍历二叉树(非递归)
*函数参数:二叉树的根节点
*思路:访问root->data后,将T入栈,
*遍历左子树;
*遍历完左子树返回时,栈顶元素应为T,
*出栈,再先序遍历T的右子树。
************************/
void PreOrder2(BiTree root){
    stack<BiTree> stack;
    BiTree p = root; //p为移动指针
    while (!stack.empty() || p)
    {
        if (p)
        {
            cout << p->data << " ";
            stack.push(p);
            p = p->lchild;
        }
        else
        {
            p = stack.top();
            stack.pop();
            p = p->rchild;
        }
    }
}
/***********************
*函数功能:中序遍历二叉树(非递归)
*函数参数:二叉树的根节点
************************/
void InOrder2(BiTree root){
    stack<BiTree> stack;
    BiTree p = root; //p为移动指针
    while (!stack.empty() || p)
    {
        if (p)
        {
            stack.push(p);
            p = p->lchild;
        }
        else
        {
            p = stack.top();
            cout << p->data << " ";
            stack.pop();
            p = p->rchild;
        }
    }
}

/***************************
*函数功能:后序遍历二叉树(非递归)
*函数参数:二叉树的根节点
*思路:T是要遍历树的根指针,
后序遍历要求在遍历完左右子树后,
再访问根。
需要判断根结点的左右子树是否均遍历过。
****************************/
void PostOrder2(BiTree root)
{
    stack<BiTreePost> stack;
    BiTree p = root;        //p是遍历指针
    BiTreePost BT;

    while (p != NULL || !stack.empty())//栈不空或者p不空时循环
    {
        while (p != NULL)   //遍历左子树
        {
            BT = (BiTreePost)malloc(sizeof(BiTNodePost));
            BT->biTree = p;
            BT->tag = 'L';      //访问过左子树
            stack.push(BT);
            p = p->lchild;
        }
        //左右子树访问完毕访问根节点
        while (!stack.empty() && (stack.top())->tag == 'R')
        {
            BT = stack.top();
            //退栈
            stack.pop();
            BT->biTree;
            printf("%c ", BT->biTree->data);
        }

        if (!stack.empty())//遍历右子树
        {
            BT = stack.top();
            //访问过右子树
            BT->tag = 'R';
            p = BT->biTree;
            p = p->rchild;
        }
    }//while
}
/***************************
*函数功能:层次遍历二叉树
*函数参数:二叉树的根节点
*思路:按从顶向下,
从左至右的顺序
来逐层访问每个节点,
层次遍历的过程中需要用队列。
****************************/
void LevelOrder(BiTree root)
{
    BiTree p = root;
    queue<BiTree> queue;
    queue.push(p);  //根节点入队
    //队列不空循环
    while (!queue.empty())
    {
        p = queue.front();//对头元素出队
        cout << p->data <<" ";//访问p指向的结点
        queue.pop();//退出队列
        //左子树不空,将左子树入队
        if (p->lchild != NULL)
        {
            queue.push(p->lchild);
        }
        //右子树不空,将右子树入队
        if (p->rchild != NULL)
        {
            queue.push(p->rchild);
        }
    }//while
}





测试输入:ABC##DE#G##F###

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: