写程序,实现一个二叉树的二叉链表的创建(递归、非递归均可),并实现二叉树的先序、中序和后序遍历(要求递归算法和非递归算法都要实现)和层次遍历。的代码
时间: 2023-06-27 12:08:03 浏览: 120
下面为您提供一个用C++实现的二叉树的二叉链表创建和遍历的完整代码:
```cpp
#include<iostream>
#include<queue>
using namespace std;
// 二叉树的节点结构体
struct TreeNode{
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int x) :val(x), left(NULL), right(NULL) {}
};
// 递归实现二叉树的创建
void createTree(TreeNode*& root){
int val;
cin >> val;
if (val == -1) // -1表示该节点为空
root = NULL;
else{
root = new TreeNode(val);
createTree(root->left);
createTree(root->right);
}
}
// 非递归实现二叉树的创建
void createTree2(TreeNode*& root){
queue<TreeNode*> q;
int val;
cin >> val;
if (val == -1) // -1表示该节点为空
root = NULL;
else{
root = new TreeNode(val);
q.push(root);
}
while (!q.empty()){
TreeNode* cur = q.front();
q.pop();
cin >> val;
if (val == -1)
cur->left = NULL;
else{
cur->left = new TreeNode(val);
q.push(cur->left);
}
cin >> val;
if (val == -1)
cur->right = NULL;
else{
cur->right = new TreeNode(val);
q.push(cur->right);
}
}
}
// 递归实现二叉树的先序遍历
void preOrder(TreeNode* root){
if (root == NULL)
return;
cout << root->val << " ";
preOrder(root->left);
preOrder(root->right);
}
// 非递归实现二叉树的先序遍历
void preOrder2(TreeNode* root){
if (root == NULL)
return;
stack<TreeNode*> s;
s.push(root);
while (!s.empty()){
TreeNode* cur = s.top();
s.pop();
cout << cur->val << " ";
if (cur->right != NULL)
s.push(cur->right);
if (cur->left != NULL)
s.push(cur->left);
}
}
// 递归实现二叉树的中序遍历
void inOrder(TreeNode* root){
if (root == NULL)
return;
inOrder(root->left);
cout << root->val << " ";
inOrder(root->right);
}
// 非递归实现二叉树的中序遍历
void inOrder2(TreeNode* root){
if (root == NULL)
return;
stack<TreeNode*> s;
TreeNode* cur = root;
while (cur != NULL || !s.empty()){
while (cur != NULL){
s.push(cur);
cur = cur->left;
}
cur = s.top();
s.pop();
cout << cur->val << " ";
cur = cur->right;
}
}
// 递归实现二叉树的后序遍历
void postOrder(TreeNode* root){
if (root == NULL)
return;
postOrder(root->left);
postOrder(root->right);
cout << root->val << " ";
}
// 非递归实现二叉树的后序遍历
void postOrder2(TreeNode* root){
if (root == NULL)
return;
stack<TreeNode*> s1, s2;
s1.push(root);
while (!s1.empty()){
TreeNode* 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()->val << " ";
s2.pop();
}
}
// 层次遍历二叉树
void levelOrder(TreeNode* root){
queue<TreeNode*> q;
if (root != NULL)
q.push(root);
while (!q.empty()){
TreeNode* cur = q.front();
q.pop();
cout << cur->val << " ";
if (cur->left != NULL)
q.push(cur->left);
if (cur->right != NULL)
q.push(cur->right);
}
}
int main(){
TreeNode* root;
// createTree(root); // 递归创建二叉树
createTree2(root); // 非递归创建二叉树
cout << "先序遍历结果为:" << endl;
preOrder(root); // 递归实现二叉树的先序遍历
// preOrder2(root); // 非递归实现二叉树的先序遍历
cout << endl;
cout << "中序遍历结果为:" << endl;
inOrder(root); // 递归实现二叉树的中序遍历
// inOrder2(root); // 非递归实现二叉树的中序遍历
cout << endl;
cout << "后序遍历结果为:" << endl;
postOrder(root); // 递归实现二叉树的后序遍历
// postOrder2(root); // 非递归实现二叉树的后序遍历
cout << endl;
cout << "层次遍历结果为:" << endl;
levelOrder(root); // 层次遍历二叉树
cout << endl;
return 0;
}
```
其中,递归创建二叉树的函数为 `createTree`,非递归创建二叉树的函数为 `createTree2`,递归实现二叉树的先序、中序和后序遍历的函数分别为 `preOrder`、`inOrder` 和 `postOrder`,非递归实现二叉树的先序、中序和后序遍历的函数分别为 `preOrder2`、`inOrder2` 和 `postOrder2`,层次遍历二叉树的函数为 `levelOrder`。在本代码中,通过注释的方式可以方便地切换递归和非递归算法的实现。
阅读全文