ツリーの一般的な操作

4219 ワード

一、二叉木の常用操作
二叉木はよく使われる木の構造で、よく使われる操作は二叉木の作成、二叉木の遍歴、二叉木の高さの取得であり、以下にC++を用いて関連アルゴリズムを実現する.
#include 
#include 
#include 

using namespace std;

typedef struct Node {
    char data;
    struct Node *lchild;
    struct Node *rchild;
} TreeNode;

class BTree {
public:
    BTree();
    //                
    void createTree(const char *str);
    //      ,         
    void preOrder();
    //      ,         
    void inOrder();
    //      ,         
    void postOrder();
    //        
    void levelOrder();
    //         
    int getDepth();

private:
    TreeNode *m_pRootNode;
    int treeDepth(TreeNode *p);
};

BTree::BTree() {
    m_pRootNode = NULL;
}

void BTree::createTree(const char *str) {
    if (str == NULL) {
        return;
    }

    int i = 0;
    char ch = str[i];
    bool isLeftChild = false;
    TreeNode *p = NULL;
    stack st;

    while (ch != '\0'){
        switch(ch){
        case '(':
            st.push(p);
            isLeftChild = true;
            break;
        case ',':
            isLeftChild = false;
            break;
        case ')':
            st.pop();
            break;
        default:
            p = new TreeNode;
            p->data = ch;
            p->lchild = NULL;
            p->rchild = NULL;

            if (m_pRootNode == NULL) {
                m_pRootNode = p;
            } else {
                if (isLeftChild) {
                    st.top()->lchild = p;
                } else {
                    st.top()->rchild = p;
                }
            }
        }

        ch = str[++i];
    }
}

void BTree::preOrder() {
    if (m_pRootNode == NULL) {
        return;
    }

    stack st;
    TreeNode *p = NULL;
    st.push(m_pRootNode);

    while (!st.empty()) {
        p = st.top();
        st.pop();
        cout << p->data << " ";

        if (p->rchild != NULL) {
            st.push(p->rchild);
        }

        if (p->lchild != NULL) {
            st.push(p->lchild);
        }
    }
}

void BTree::inOrder() {
    if (m_pRootNode == NULL) {
        return;
    }

    stack st;
    TreeNode *p = m_pRootNode;

    while(!st.empty() || p != NULL) {
        while(p != NULL) {
            st.push(p);
            p = p->lchild;
        }

        if (!st.empty()) {
            p = st.top();
            st.pop();
            cout << p->data << " ";
            p = p->rchild;
        }
    }
}

void BTree::postOrder() {
    if (m_pRootNode == NULL) {
        return;
    }

    TreeNode *p = m_pRootNode;
    TreeNode *pre = NULL;
    stack st;

    do {
        while(p != NULL) {
            st.push(p);
            p = p->lchild;
        }

        while(!st.empty()) {
            p = st.top();
            if (p->rchild == pre) {
                cout << p->data << " ";
                pre = p;
                st.pop();
            } else {
                p = p->rchild;
                pre = NULL;
                break;
            }
        }
    }while(!st.empty());
}

void BTree::levelOrder() {
    if (m_pRootNode == NULL) {
        return;
    }

    queue qu;
    TreeNode *p = NULL;
    qu.push(m_pRootNode);

    while (!qu.empty()) {
        p = qu.front();
        qu.pop();

        cout << p->data << " ";

        if (p->lchild != NULL) {
            qu.push(p->lchild);
        }

        if (p->rchild != NULL) {
            qu.push(p->rchild);
        }
    }
}

int BTree::getDepth() {
    return treeDepth(m_pRootNode);
}

int BTree::treeDepth(TreeNode *p) {
    if (p == NULL) {
        return 0;
    }

    int leftDepth = treeDepth(p->lchild);
    int rightDepth = treeDepth(p->rchild);

    return leftDepth > rightDepth ? leftDepth + 1 : rightDepth + 1;
}

int main()
{
    char str[] = "a(b(d,e(h(f,o),j)),c)";
    BTree *pTree = new BTree;
    pTree->createTree(str);
    pTree->levelOrder();
    return 0;
}