一份树的应用完整程序

现树与二叉树的转换。以及树的前序、后序的递归、非递归算法,层次序的非递归算法的实现,应包
含建树的实现

  • 这个问题的回答你可以参考下: https://ask.csdn.net/questions/1066809
  • 这篇博客也不错, 你可以看下编写一个程序,要求输入两个浮点数,然后打印出用二者的差值除以二者的乘积所得的结果。在用户键入非数字的输入之前程序循环处理每对输入值。
  • 除此之外, 这篇博客: 理解一级指针,二级指针,指针和数组,指针数组中的 指针的类型决定了指针向前或者向后走一步有多大。 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:
    • 您还可以看一下 陈虎老师的一小时快速学习屏幕录像和视频编辑实战教程课程中的 视频调速、滤镜的使用、画中画、蒙版、分屏小节, 巩固相关知识点
    • 以下回答由chatgpt基于相关博客总结生成:

      我可以为你提供一个实现树与二叉树之间转换以及遍历算法的示例程序。下面是一个用C++语言实现的程序: ```cpp

      include

      include

      using namespace std;

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

      // 将树转换为二叉树 TreeNode treeToBinaryTree(Node root) { if (root == nullptr) { return nullptr; } TreeNode* binaryRoot = new TreeNode(root->val); if (root->child != nullptr) { binaryRoot->right = treeToBinaryTree(root->child); } if (root->sibling != nullptr) { binaryRoot->left = treeToBinaryTree(root->sibling); } return binaryRoot; }

      // 将二叉树转换为树 Node binaryTreeToTree(TreeNode root) { if (root == nullptr) { return nullptr; } Node* treeRoot = new Node(root->val); if (root->right != nullptr) { treeRoot->child = binaryTreeToTree(root->right); } if (root->left != nullptr) { treeRoot->sibling = binaryTreeToTree(root->left); } return treeRoot; }

      // 树的前序遍历(递归) void preorderTraversalRecursive(Node root) { if (root == nullptr) { return; } cout << root->val << " "; Node child = root->child; while (child != nullptr) { preorderTraversalRecursive(child); child = child->sibling; } }

      // 树的前序遍历(非递归) void preorderTraversalNonRecursive(Node root) { stack nodeStack; nodeStack.push(root); while (!nodeStack.empty()) { Node node = nodeStack.top(); nodeStack.pop(); cout << node->val << " "; Node sibling = node->sibling; while (sibling != nullptr) { nodeStack.push(sibling); sibling = sibling->sibling; } Node child = node->child; while (child != nullptr) { nodeStack.push(child); child = child->sibling; } } }

      // 树的后序遍历(递归) void postorderTraversalRecursive(Node root) { if (root == nullptr) { return; } Node child = root->child; while (child != nullptr) { postorderTraversalRecursive(child); child = child->sibling; } cout << root->val << " "; }

      // 树的后序遍历(非递归) void postorderTraversalNonRecursive(Node root) { stack nodeStack1; stack nodeStack2; nodeStack1.push(root); while (!nodeStack1.empty()) { Node node = nodeStack1.top(); nodeStack1.pop(); nodeStack2.push(node); Node child = node->child; while (child != nullptr) { nodeStack1.push(child); child = child->sibling; } } while (!nodeStack2.empty()) { Node node = nodeStack2.top(); nodeStack2.pop(); cout << node->val << " "; } }

      // 树的层次序遍历(非递归) void levelOrderTraversal(Node root) { queue nodeQueue; if (root != nullptr) { nodeQueue.push(root); } while (!nodeQueue.empty()) { Node node = nodeQueue.front(); nodeQueue.pop(); cout << node->val << " "; Node* child = node->child; while (child != nullptr) { nodeQueue.push(child); child = child->sibling; } } }

      // 建树功能 Node buildTree() { int num; cout << "请输入节点个数:"; cin >> num; Node root = nullptr; queue nodeQueue; for (int i = 0; i < num; i++) { int val; cout << "请输入第" << i + 1 << "个节点的值:"; cin >> val; Node node = new Node(val); if (root == nullptr) { root = node; } else { Node parent = nodeQueue.front(); if (parent->child == nullptr) { parent->child = node; } else { parent = parent->child; while (parent->sibling != nullptr) { parent = parent->sibling; } parent->sibling = node; } } nodeQueue.push(node); cout << "请输入节点" << val << "的子节点个数:"; int childNum; cin >> childNum; for (int j = 0; j < childNum; j++) { int childVal; cout << "请输入节点" << val << "的第" << j + 1 << "个子节点的值:"; cin >> childVal; Node child = new Node(childVal); if (node->child == nullptr) { node->child = child; } else { Node sibling = node->child; while (sibling->sibling != nullptr) { sibling = sibling->sibling; } sibling->sibling = child; } nodeQueue.push(child); } } return root; }

      int main() { // 建立一个树 Node* treeRoot = buildTree();

      // 将树转换为二叉树
      TreeNode* binaryTreeRoot = treeToBinaryTree(treeRoot);
      
      // 将二叉树转换为树
      Node* treeRoot2 = binaryTreeToTree(binaryTreeRoot);
      
      // 树的前序遍历(递归)
      cout << "树的前序遍历(递归):";
      preorderTraversalRecursive(treeRoot2);
      cout << endl;
      
      // 树的前序遍历(非递归)
      cout << "树的前序遍历(非递归):";
      preorderTraversalNonRecursive(treeRoot2);
      cout << endl;
      
      // 树的后序遍历(递归)
      cout << "树的后序遍历(递归):";
      postorderTraversalRecursive(treeRoot2);
      cout << endl;
      
      // 树的后序遍历(非递归)
      cout << "树的