二叉树的递归和迭代遍历-C++版

朴灿烈づ我的快乐病毒、 2022-11-05 03:22 255阅读 0赞

文章目录

        1. 前序遍历
        1. 中序遍历
        1. 后序遍历
        1. 层序遍历
        1. 测试函数

    // binary_tree.h
    // 结点
    struct TreeNode {
    int val;
    TreeNode left;
    TreeNode
    right;

    TreeNode(int value) : val(value), left(nullptr), right(nullptr) { }
    };

    /** 二叉树增加结点 **/
    void addTreeNode(TreeNode &root, TreeNode node) {
    if (node->val <= root->val && root->left) {
    addTreeNode(root->left, node);
    } else if (node->val <= root->val && root->left == nullptr) {
    root->left = node;
    } else if (root->right) {
    addTreeNode(root->right, node);
    } else {
    root->right = node;
    }
    }

1. 前序遍历

  1. /********************** 前序遍历-递归版 **********************/
  2. void preorderRecursive(TreeNode *root) {
  3. if (nullptr == root) {
  4. return;
  5. }
  6. cout << root->val << " ";
  7. preorderRecursive(root->left);
  8. preorderRecursive(root->right);
  9. return;
  10. }
  11. /********************** 前序遍历-迭代版 **********************/
  12. void preorderIterative(TreeNode *root) {
  13. if (nullptr == root) {
  14. return;
  15. }
  16. std::stack<TreeNode *> stk;
  17. TreeNode *node = root;
  18. stk.push(node);
  19. while (!stk.empty()) {
  20. node = stk.top();
  21. stk.pop();
  22. cout << node->val << " ";
  23. if (node->right) {
  24. stk.push(node->right);
  25. }
  26. if (node->left) {
  27. stk.push(node->left);
  28. }
  29. }
  30. return;
  31. }

2. 中序遍历

  1. /********************** 中序遍历-递归版 **********************/
  2. void inorderRecursive(TreeNode *root) {
  3. if (root) {
  4. inorderRecursive(root->left);
  5. cout << root->val << " ";
  6. inorderRecursive(root->right);
  7. }
  8. return;
  9. }
  10. /********************** 中序遍历-迭代版 **********************/
  11. void inorderIterative(TreeNode *root) {
  12. std::stack<TreeNode *> stk;
  13. TreeNode *node = root;
  14. while (node || !stk.empty()) {
  15. if (node) {
  16. stk.push(node);
  17. node = node->left;
  18. } else {
  19. node = stk.top();
  20. stk.pop();
  21. cout << node->val << " ";
  22. node = node->right;
  23. }
  24. }
  25. return;
  26. }

3. 后序遍历

  1. /********************** 后序遍历-递归版 **********************/
  2. void postorderRecursive(TreeNode *root) {
  3. if (root) {
  4. postorderRecursive(root->left);
  5. postorderRecursive(root->right);
  6. cout << root->val << " ";
  7. }
  8. return;
  9. }
  10. /********************** 后序遍历-迭代版 **********************/
  11. void postorderIterative(TreeNode *root) {
  12. if (nullptr == root) {
  13. return;
  14. }
  15. std::stack<TreeNode *> stk;
  16. std::vector<int> result;
  17. TreeNode *node = root;
  18. stk.push(node);
  19. while (!stk.empty()) {
  20. node = stk.top();
  21. stk.pop();
  22. result.emplace(result.begin(), node->val);
  23. if (node->left) {
  24. stk.push(node->left);
  25. }
  26. if (node->right) {
  27. stk.push(node->right);
  28. }
  29. }
  30. std::copy(result.begin(), result.end(), std::ostream_iterator<int>{ cout, " "});
  31. return;
  32. }

4. 层序遍历

  1. /********************** 层序遍历-迭代版 **********************/
  2. void levelorderTraversal(TreeNode *root) {
  3. if (nullptr == root) {
  4. return;
  5. }
  6. std::queue<TreeNode *> que;
  7. TreeNode *node = root;
  8. que.push(node);
  9. while (!que.empty()) {
  10. node = que.front();
  11. que.pop();
  12. cout << node->val << " ";
  13. if (node->left) {
  14. que.emplace(node->left);
  15. }
  16. if (node->right) {
  17. que.emplace(node->right);
  18. }
  19. }
  20. return;
  21. }

5. 测试函数

  1. void binaryTreeTest() {
  2. TreeNode *root = new TreeNode(3);
  3. std::vector<int> treeElem{ 1, 2, 4, 5};
  4. for (int i{ }; i < treeElem.size(); ++i) {
  5. TreeNode *node = new TreeNode(treeElem.at(i));
  6. addTreeNode(root, node);
  7. }
  8. cout << "\ntraversal result: " << endl;
  9. cout << std::string(32, '*') << endl;
  10. // preorderRecursive(root); // 3 1 2 4 5
  11. // preorderIterative(root); // 3 1 2 4 5
  12. // inorderRecursive(root); // 1 2 3 4 5
  13. // inorderIterative(root); // 1 2 3 4 5
  14. // postorderRecursive(root); // 2 1 5 4 3
  15. // postorderIterative(root); // 2 1 5 4 3
  16. levelorderTraversal(root); // 3 1 4 2 5
  17. cout << endl;
  18. cout << std::string(32, '*') << endl;
  19. return;
  20. }

发表评论

表情:
评论列表 (有 0 条评论,255人围观)

还没有评论,来说两句吧...

相关阅读

    相关

    网上的递归遍历代码很多,这里就不赘述了,说一下思考的角度: 1. 把每一个棵子树都看成是独立的树; 2. 每一个节点都会把递归的代码重新执行一次; 3. 想象压栈的过程

    相关

    二叉树又称为红黑树,是一种常用的数据结构,而二叉树的遍历则是一种非常基本的操作。遍历二叉树的方式有两大类:递归和非递归。递归方式算法较为简便,并且更便于理解,非递归方式则需要对

    相关

    问题描述: 以一定的方式输入数据(本文是以先序遍历的方式输入),程序根据数据进行解析,创建二叉树,然后对二叉树进行操作,主要操作包括以下几种: 1. 求二叉树的高度 2