有关二叉树的相关实现:建树,遍历(递归与非递归实现)

有关二叉树的相关实现:建树,遍历(递归与非递归实现)

定义二叉树的节点

struct BTNode {
    int data;
    BTNode *left;
    BTNode *right;
};

先序建立二叉树

思路:以数组中的元素先序构建二叉树,过程就是不断插入,直至数组中没有元素

// 先序建立二叉树
void insert_node(BTNode **root, int *data, int i) {
    if (i <= data[0]) {
        *root = new BTNode();
        (*root)->data = data[i];
        (*root)->left = NULL;
        (*root)->right = NULL;
        insert_node(&(*root)->left, data, 2 * i);
        insert_node(&(*root)->right, data, 2 * i + 1);
    }
}

遍历二叉树的操作,先序、中序、后续及层序遍历

递归实现二叉树的遍历

void pre_order(BTNode *root) {
    if (root != NULL) {
        cout << root->data << " ";
        pre_order(root->left);
        pre_order(root->right);
    }
}

void in_order(BTNode *root) {
    if (root != NULL) {
        in_order(root->left);
        cout << root->data << " ";
        in_order(root->right);
    }
}

void post_order(BTNode *root) {
    if (root != NULL) {
        post_order(root->left);
        post_order(root->right);
        cout << root->data << " ";
    }
}

非递归实现二叉树的遍历

先序遍历

void non_pre_order(BTNode *root) {
    stack<BTNode*> s;
    BTNode *cur = root;

    while (cur != NULL || !s.empty()) {
        while (cur != NULL) {
            cout << cur->data << " ";
            s.push(cur);
            cur = cur->left;
        }

        if (!s.empty()) {
            cur = s.top();
            s.pop();
            cur = cur->right;
        }
    }
}

中序遍历

void non_in_order(BTNode *root) {
    stack<BTNode*> s;
    BTNode *cur = root;

    while (cur != NULL || !s.empty()) {
        while (cur != NULL) {
            s.push(cur);
            cur = cur->left;
        }

        if (!s.empty()) {
            cur = s.top();
            s.pop();
            cout << cur->data << " ";
            cur = cur->right;
        }
    }
}

后序遍历

void non_post_order(BTNode *root) {
    stack<BTNode*> s1, s2;
    BTNode *cur = root;

    s1.push(cur);
    while (!s1.empty()) {
        cur = s1.top();
        s1.pop();
        s2.push(cur);

        if (cur->left != NULL)
            s1.push(cur->left);
        if (cur->right != NULL)
            s1.push(cur->right);
    }

    while (!s2.empty()) {
        cout << s2.top()->data << " ";
        s2.pop();
    }
}

层序遍历

void floor_order(BTNode *root) {
    queue<BTNode*> q;
    BTNode *cur;

    if (root != NULL)
        q.push(root);

    while (!q.empty()) {
        cur = q.front();
        cout << cur->data << " ";
        q.pop();

        if (cur->left != NULL)
            q.push(cur->left);
        if (cur->right != NULL)
            q.push(cur->right);
    }
}

测试代码

int main(void) {
    int data[] = {5, 1, 2, 3, 4, 5};
    BTNode *root = NULL;

    insert_node(&root, data, 1);

    // 先序遍历二叉树
    cout << "递归先序遍历二叉树" << endl;
    pre_order(root);
    cout << "\n非递归先序遍历二叉树\n";
    non_pre_order(root);
    cout << "\n递归中序遍历二叉树\n";
    // 中序遍历二叉树
    in_order(root);
    cout << "\n非递归中序遍历二叉树\n";
    non_in_order(root);
    cout << "\n递归后序遍历二叉树\n";
    // 后序遍历二叉树
    post_order(root);
    cout << "\n非递归后序遍历二叉树\n";
    non_post_order(root);
    cout << endl;
    // 层序遍历二叉树
    cout << "层序遍历二叉树\n";
    floor_order(root);
    cout << endl;

    return 0;
}

Read more

超快速,使用ChatGPT编写回归和分类算法

超快速,使用ChatGPT编写回归和分类算法

本文将使用一些 ChatGPT 提示,这些提示对于数据科学家在工作时非常重要。 微信搜索关注《Python学研大本营》,加入读者群,分享更多精彩 以下是一些示例ChatGPT 提示的列表以及数据科学家的响应。 ChatGPT 提示 为决策树回归算法生成 python 代码。 下面是使用scikit-learn在 Python 中进行决策树回归的示例代码: import numpy as np import matplotlib.pyplot as plt from sklearn.tree import DecisionTreeRegressor # Generate random data rng = np.random.default_rng() x = 5 * rng.random(100) y = np.sin(x) + 0.

By Ne0inhk
力扣每日一题:993.二叉树的堂兄弟节点 深度优先算法

力扣每日一题:993.二叉树的堂兄弟节点 深度优先算法

993.二叉树的堂兄弟节点 难度:简单 题目: 在二叉树中,根节点位于深度 0 处,每个深度为 k 的节点的子节点位于深度 k+1 处。 如果二叉树的两个节点深度相同,但 父节点不同 ,则它们是一对堂兄弟节点。 我们给出了具有唯一值的二叉树的根节点 root ,以及树中两个不同节点的值 x 和 y 。 只有与值 x 和 y 对应的节点是堂兄弟节点时,才返回 true 。否则,返回 false。 示例: 示例 1: 输入:root = [1,2,3,4], x = 4, y = 3 输出:false

By Ne0inhk
1239.串联字符串的最大长度 关于字符串的回溯算法!

1239.串联字符串的最大长度 关于字符串的回溯算法!

题目: 给定一个字符串数组 arr,字符串 s 是将 arr 某一子序列字符串连接所得的字符串, 如果 s 中的每一个字符都只出现过一次,那么它就是一个可行解。 请返回所有可行解 s 中最长长度。 提示: 1 <= arr.length <= 16 1 <= arr[i].length <= 26 arr[i] 中只含有小写英文字母 示例: 示例 1: 输入:arr = ["un","iq","ue"] 输出:4 解释:所有可能的串联组合是

By Ne0inhk