二叉树的遍历

来源:互联网 发布:快速赚钱 知乎 编辑:程序博客网 时间:2024/05/24 00:13

环境 codeblocks 16.01
主要实现了二叉树的几种遍历情况,代码如下:

#include <iostream>using namespace std;class BinaryTreeNode//二叉树节点{    friend class BinaryTree;private:    char data;    BinaryTreeNode *leftChild;    BinaryTreeNode *rightChild;public:    BinaryTreeNode();    BinaryTreeNode(char c,BinaryTreeNode *left=NULL,BinaryTreeNode *right=NULL)    {        data=c;        leftChild=left;        rightChild=right;    }    BinaryTreeNode* getLeftChild(){ return leftChild;}    BinaryTreeNode* getRightChild() {return rightChild;}    void setleftChild(BinaryTreeNode *l){        leftChild=l;    }    void setRightChild(BinaryTreeNode *r){        rightChild=r;    }    char getdata(){return this->data;}};class ArrayStack//栈的设定{private:    int maxsize;    int top;    BinaryTreeNode **st ;public:    ArrayStack(int size)    {        maxsize=size;        top=-1;        st=new BinaryTreeNode*[maxsize];    }    ~ArrayStack()    {        delete [] st;    }    void push(BinaryTreeNode *r)    {        if(top==maxsize-1)            cout<<"栈满溢出"<<endl;        else        {            st[++top]=r;        }    }    BinaryTreeNode* pop()    {        if(top==-1)            cout<<"栈为空不能进行删除操作"<<endl;        else        {            return st[top--];        }    }    bool IsEmpty()    {        if(top==-1)            return true;        else            return false;    }};class BinaryTree//二叉树{private:       BinaryTreeNode *root;public:    BinaryTree()//创建二叉树    {        BinaryTreeNode *temp;        BinaryTreeNode *templ=NULL;        BinaryTreeNode *tempr=NULL;        root=new BinaryTreeNode('A');        templ=new BinaryTreeNode('B');        root->setleftChild(templ);        tempr= new BinaryTreeNode('C');        root->setRightChild(tempr);        temp=templ;        templ=new BinaryTreeNode('D');        temp->setRightChild(templ);        temp=templ;        templ=new BinaryTreeNode('F');        temp->setleftChild(templ);        templ=new BinaryTreeNode('G');        temp->setRightChild(templ);        temp=tempr;        tempr=new BinaryTreeNode('E');        temp->setRightChild(tempr);        temp=tempr;        tempr= new BinaryTreeNode('H');        temp->setRightChild(tempr);    }    ~BinaryTree() { }    BinaryTreeNode * getroot(){return this->root;}    void preOrder(BinaryTreeNode *r)//前序递归遍历    {        if(r!=NULL)        {            cout<<r->getdata();            preOrder(r->leftChild);            preOrder(r->rightChild);        }    }    void preOrdern(BinaryTreeNode *r)//前序非递归遍历    {        ArrayStack s(100);        BinaryTreeNode *current=r;        while(current!=NULL||!s.IsEmpty())        {          cout<<current->getdata() ;            if(current->leftChild!=NULL)            {                s.push(current);                current=current->getLeftChild();            }            else            {                current=current->getRightChild();                if(current==NULL && !s.IsEmpty())                {                    current=s.pop();                    current=current->getRightChild();                }            }        }    }    void inOrder(BinaryTreeNode *r)//中序    {        if(r!=NULL)        {            inOrder(r->leftChild);            cout<<r->getdata();            inOrder(r->rightChild);        }    }    void inOrdern(BinaryTreeNode *r)    {        ArrayStack s(100);        BinaryTreeNode *current=r;        while(current!=NULL||!s.IsEmpty())        {            if(current->leftChild!=NULL)            {                s.push(current);                current=current->getLeftChild();            }            else            {                cout<<current->getdata() ;                current=current->getRightChild();                if(current==NULL && !s.IsEmpty())                {                    current=s.pop();                    cout<<current->getdata();                    current=current->getRightChild();                }            }        }    }    void postOrder(BinaryTreeNode *r)//后序    {        if(r!=NULL)        {            postOrder(r->leftChild);            postOrder(r->rightChild);            cout<<r->getdata();        }    }    void postOrdern(BinaryTreeNode *r)    {        ArrayStack s(100);        BinaryTreeNode *p=r;        BinaryTreeNode  *q=0;        while(p!=NULL || !s.IsEmpty())        {            if(p)            {                s.push(p);                p=p->leftChild;            }            else            {                BinaryTreeNode *temp;                while(!s.IsEmpty())                {                    temp=s.pop();                    if(temp->rightChild!=NULL&&temp->rightChild!=q)                    {                        s.push(temp);                        temp=temp->rightChild;                        p=temp;                        break;                    }                    else                    {                        cout<<temp->getdata();                        q=temp;                    }                }            }        }    }};int main(){    BinaryTree c;    BinaryTreeNode *r=c.getroot();    cout<<"前序递归遍历结果"<<endl;    c.preOrder(r);    cout<<endl;    cout<<"前序非递归遍历结果"<<endl;    c.preOrdern(r);    cout<<endl;    cout<<"中序递归遍历结果"<<endl;    c.inOrder(r);    cout<<endl;    cout<<"中序非递归遍历结果"<<endl;    c.inOrdern(r);    cout<<endl;    cout<<"后续递归遍历结果"<<endl;    c.postOrder(r);    cout<<endl;    cout<<"后续非递归遍历结果"<<endl;    c.postOrdern(r);    return 0;}

层次遍历的代码如下:

void levelOrder(BinaryTreeNode *root){        ArrayQueue nodeQueue(100);        BinaryTreeNode *pointer=root;        BinaryTreeNode *l,*r;        if(pointer)            nodeQueue.push(pointer);        while(!nodeQueue.IsEmpty())        {            pointer=nodeQueue.GetFront();            cout<<pointer->getdata();            nodeQueue.pop();            l=pointer->getLeftChild();            r=pointer->getRightChild();            if(l!=NULL)                nodeQueue.push(l);            if(r!=NULL)                nodeQueue.push(r);        }}
原创粉丝点击