关于二叉树的若干代码, 非递归遍历,打印..

#ifndef BINARY_TREE
#define BINARY_TREE

#include <iostream>
#include <stdio.h>
#include <stack>

using namespace std;

class binaryTree
{
private:
    struct btNode
    {
        char data;
        btNode *left, *right;
        btNode(char c):data(c){}
    };
    btNode *root;
    int size;

    //非递归后序遍历辅助数据的类型定义
    struct nodeTag
    {
        btNode *p;
        int tag;
        nodeTag(btNode *_p, int _tag):p(_p), tag(_tag){}
        nodeTag(){}
    };

    //线序遍历定义
    void preorderWalk(btNode *p)
    {
        if(p)
        {
            cout << p->data <<endl;
            preorderWalk(p->left);
            preorderWalk(p->right);
        }
    }

    //中序遍历定义
    void inorderWalk(btNode *p)
    {
        if(p)
        {
            inorderWalk(p->left);
            cout << p->data << endl;
            inorderWalk(p->right);
        }
    }

    //后序遍历定义
    void postorderWalk(btNode *p)
    {
        if(p)
        {
            postorderWalk(p->left);
            postorderWalk(p->right);
            cout << p->data << endl;
        }
    }

    //根据深度打印节点信息
    void printNode(char c, int h)
    {
        int i;
        for(i = 0; i < h; ++i)
        {
            cout << "  ";
        }
        cout << c << endl;
    }

    //打印树节点信息
    void printTree(btNode *t, int h)
    {
        if(!t)
        {
            printNode('*', h);
            return;
        }
        printTree(t->left, h + 1);
        printNode(t->data, h);
        printTree(t->right, h + 1);
    }

    //统计二叉树中节点个数
    int nodes(btNode *t)
    {
        if(!t)
        {
            return 0;
        }
        return nodes(t->left) + nodes(t->right);
    }

    //求二叉树的高度
    int height(btNode *t)
    {
        if(!t)
        {
            return 0;
        }
        int lh = height(t->left), rh = height(t->right);
        return 1 + (lh > rh ? lh : rh);
    }

    //求树中两节点的最长路径
    int heightForLR(btNode *t, int &lroad)
    {
        if(!t)
        {
            return 0;
        }
        int lh = height(t->left), rh = height(t->right);
        if(lh + rh > lroad)
        {
            lroad = lh + rh;
        }
        return 1 + (lh > rh ? lh : rh);
    }

    //建树函数
    btNode* buildBT()
    {
        char c;
        btNode *t = 0;
        c = getchar();
        if(c != ' ')
        {
            t = new btNode(c);
            ++size;
            t->left = buildBT();
            t->right = buildBT();
        }
        return t;
    }

    void drop(btNode* t)
    {
        if(t)
        {
            drop(t->left);
            drop(t->right);
            delete t;
        }
    }

public:
    binaryTree():root(0){}

    ~binaryTree()
    {
        drop(root);
    }

    //域外调用接口
    void walk()
    {
        preorderWalk(root);
    }

    void show()
    {
        printTree(root, 0);
    }

    int getSize()
    {
        return size;
    }

    int getHeight()
    {
        return height(root);
    }

    int longestRoad()
    {
        int x = 0;
        heightForLR(root, x);
        return x;
    }

    void build()
    {
        size = 0;
        root = buildBT();
    }

    //利用栈进行先序遍历
    void preWalkWithStack()
    {
        if(!root)
        {
            return;
        }
        stack<btNode*> aidS;
        aidS.push(root);
        btNode *p;
        while(!aidS.empty())
        {
            p = aidS.top();
            aidS.pop();
            cout << p->data <<endl;
            if(p->right)
            {
                aidS.push(p->right);
            }
            if(p->left)
            {
                aidS.push(p->left);
            }
        }
    }

    //利用栈实现中序遍历
    void inWalkWithStack()
    {
        stack<btNode*> aidS;
        btNode *p = root;
        while(p || !aidS.empty())
        {
            if(p)
            {
                aidS.push(p);
                p = p->left;
            }
            else
            {
                p = aidS.top();
                aidS.pop();
                cout << p->data <<endl;
                p = p->right;
            }
        }
    }

    //利用栈实现后序遍历
    void postWalkWithStack()
    {
        nodeTag nt;
        stack<nodeTag> aidS;
        aidS.push(nodeTag(root, 1));
        while(!aidS.empty())
        {
            nt = aidS.top();
            aidS.pop();
            if(nt.tag)
            {
                nt.tag = 0;
                aidS.push(nt);
                if(nt.p->right)
                {
                    aidS.push(nodeTag(nt.p->right, 1));
                }
                if(nt.p->left)
                {
                    aidS.push(nodeTag(nt.p->left, 1));
                }
            }
            else
            {
                cout << nt.p->data << endl;
            }
        }
    }
};

#endif

 

posted @ 2012-12-14 19:50  knull  Views(194)  Comments(0)    收藏  举报