二叉树的基本操作

1._BiTree.h

#ifndef _BITREE_H
#define _BITREE_H
#include 
#include 

using namespace std;

typedef  char DataType;
//链表结构的二叉树构造

typedef struct Node
{
    DataType data;
    struct Node *LChild;
    struct Node *RChild;
}BiTNode,*BiTree;

#endif

2.BiTree.h

#ifndef BITREE_H
#define BITREE_H

#include "_BiTree.h"

void CreateBiTree(BiTree *root);//创建二叉树
void PreOrder(BiTree root);//递归先序遍历
void InOrder(BiTree root);//递归中序遍历
void PostOrder(BiTree root);//递归后序遍历
int PostBiTreeDepth(BiTree root);//后序遍历二叉树的高度
void PreOrder1(BiTree root);//非递归先序遍历
void InOrder1(BiTree root);//非递归中序遍历
void PostOrder1(BiTree root);//非递归后序遍历


#endif

3.BiTree.cpp

#include "_BiTree.h"

void CreateBiTree(BiTree *root)
{
    char c;
    cin >> c;
    if ('.' == c)
    {
        *root=NULL;
    }
    else
    {
        *root = new BiTNode;
        if (*root == NULL)
        {
            printf("free malloc failed!\n");
            return;
        }
        else
        {
            (*root)->data=c ;
            CreateBiTree(&(*root)->LChild);
            CreateBiTree(&(*root)->RChild);
        }
    }

}

void PreOrder(BiTree root)
{
    if (root != NULL)
    {
        cout<< root->data<<" ";
        PreOrder(root->LChild);
        PreOrder(root->RChild);
    }
}

void PreOrder1(BiTree root)
{
    stack<BiTNode*>s;
    BiTree p = root;
    while (p != NULL || !s.empty())
    {
        if (p != NULL)

        {
            //cout << p->data << " ";//输出根结点的顺序和出栈的顺序可以互换
            s.push(p);
            cout << p->data << " ";
            p = p->LChild;

        }


        else
        {
            p = s.top();
            s.pop();
            p = p->RChild;


        }
    }

}

void InOrder(BiTree root)
{
    if (root != NULL)
    {
        InOrder(root->LChild);
        cout << root->data << " ";
        InOrder(root->RChild);
    }
}

void PostOrder(BiTree root)
{
    if (root != NULL)
    {
        PostOrder(root->LChild);
        PostOrder(root->RChild);
        cout << root->data << " ";
    }

}

void InOrder1(BiTree root)
{
    stack<BiTNode*>s;
    BiTree p = root;
    while (p != NULL || !s.empty())
    {
        if (p != NULL)
        {
            s.push(p);  
            p = p->LChild;

        }
        else
        {
            p = s.top();
            s.pop();
            cout << p->data<<" ";
            p = p->RChild;

        }

    }
}

void PostOrder1(BiTree root)
{
    stack<BiTNode*>s;
    BiTNode* p = root;
    BiTNode* q = NULL;
    while (p != NULL || !s.empty())
    {
        while (p != NULL)
        {
            s.push(p);
            p = p->LChild;
        }
        if (!s.empty())
        {
            p=s.top();
            if (p->RChild == NULL || p->RChild == q)//无右孩子或右孩子已经被遍历过
            {
                cout << p->data << " ";
                s.pop();
                q = p;
                p = NULL;
            }
            else
            {
                p = p->RChild;
            }
        }
    }


}

int PostBiTreeDepth(BiTree root)
{
    int hl = 0;//求左子树的高度
    int hr = 0;//求右子树的高度
    int max = 0;//求左右子树中的最大的
    if (NULL==root)
    {
        return 0;

    }
    else
    {
        hl=PostBiTreeDepth(root->LChild);
        hr = PostBiTreeDepth(root->RChild);
        max = hl > hr ? hl : hr;
        return max + 1; 

    }

}

4.main.cpp

#include "_BiTree.h"
#include "BiTree.h"

int main(void)
{
    BiTree btree = NULL;
    CreateBiTree(&btree);
    PreOrder(btree);
    cout << endl;
    InOrder(btree);
    cout << endl;
    PostOrder(btree);
    cout << endl;

    int n = PostBiTreeDepth(btree);
    cout << n << endl;


    PreOrder1(btree);
    cout << endl;

    InOrder1(btree);
    cout << endl;

    PostOrder1(btree);
    cout << endl;

    return 0;
}

你可能感兴趣的:(二叉树的基本操作)