二叉树常用操作

一,二叉树常用操作

二叉树是一种常用的树结构,经常使用到的操作有创建二叉树、遍历二叉树、获取二叉树的高度,下面使用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;
}

 

你可能感兴趣的:(数据结构)