现树与二叉树的转换。以及树的前序、后序的递归、非递归算法,层次序的非递归算法的实现,应包
含建树的实现
我可以为你提供一个实现树与二叉树之间转换以及遍历算法的示例程序。下面是一个用C++语言实现的程序: ```cpp
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 << "树的