二叉树常用操作

一,二叉树常用操作

二叉树是一种常用的树结构,经常使用到的操作有创建二叉树、遍历二叉树、获取二叉树的高度,下面使用C++实现相关的算法,如下:

#include <iostream>
#include <stack>
#include <queue>

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<TreeNode *> 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<TreeNode *> 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<TreeNode *> 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<TreeNode *> 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<TreeNode *> 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;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值