西南交大数据结构实验6-完全二叉树的顺序存储与链式存储结构以及遍历算法。

输入一个字符串,存储于一维数组。以该一维数组作为完全二叉树的存储结构,实现先、中、后序遍历,输出遍历结果。

将该完全二叉树转换为二叉链表存储结构,然后基于二叉链表存储结构再次进行先、中、后序遍历并输出遍历结果。

#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<vector>
#include<stack>
#include<string>
using namespace std;

struct TreeNode {
    char	val;
	TreeNode* left;
	TreeNode* right;
    TreeNode(char x) : val(x), left(NULL), right(NULL) {}
};

//根据数组构造二叉树
TreeNode* construct_binary_tree(const vector<char>& vec) {
    vector<TreeNode*> vecTree(vec.size(), NULL);
    TreeNode* root = NULL;
    for (int i = 0; i < vec.size(); i++) {
        TreeNode* node = NULL;
        if (vec[i] != '#') node = new TreeNode(vec[i]);
        vecTree[i] = node;
        if (i == 0) root = node;

    }

    for (int i = 0; i * 2 + 1 < vec.size(); i++) {
        if (vecTree[i] != NULL) {
            vecTree[i]->left = vecTree[i * 2 + 1];
            if (i * 2 + 2 < vec.size())
                vecTree[i]->right = vecTree[i * 2 + 2];
        }
    }
    return root;
}
//迭代法前序遍历

vector<char> preorderTraversal(TreeNode* root) {
    vector<char> result;
    stack<TreeNode*> st;
    if (root != NULL) st.push(root);
    while (!st.empty()) {
        TreeNode* node = st.top();
        if (node != NULL) {
            st.pop();
            if (node->right) st.push(node->right);//右
            if (node->left) st.push(node->left);//左
            st.push(node);//中
            st.push(NULL);
        }
        else {
            st.pop();
            node = st.top();
            st.pop();
            result.push_back(node->val);
        }
    }
    return result;
}

vector<char> inorderTraversal(TreeNode* root) {
    vector<char> result;
    stack<TreeNode*> st;
    if (root != NULL) st.push(root);
    while (!st.empty()) {
        TreeNode* node = st.top();
        if (node != NULL) {
            st.pop();
            if (node->right) st.push(node->right);//右
            st.push(node);//中
            st.push(NULL);
            if (node->left) st.push(node->left);//左
           
        }
        else {
            st.pop();
            node = st.top();
            st.pop();
            result.push_back(node->val);
        }
    }
    return result;
}
vector<char> postorderTraversal(TreeNode* root) {
    vector<char> result;
    stack<TreeNode*> st;
    if (root != NULL) st.push(root);
    while (!st.empty()) {
        TreeNode* node = st.top();
        if (node != NULL) {
            st.pop();
            st.push(node);//中
            st.push(NULL);
            if (node->right) st.push(node->right);//右
            if (node->left) st.push(node->left);//左
        }
        else {
            st.pop();
            node = st.top();
            st.pop();
            result.push_back(node->val);
        }
    }
    return result;
}
void printf_v(vector<char>aaa) {
    for (int i = 0; i < aaa.size(); i++) {
        cout << aaa[i] << " ";
    }
}

void front(const char* tree, int length, int depth) {
    if (depth < length) {
        if (tree[depth] != '#')
        cout << tree[depth]<<" ";
        front(tree, length, depth * 2 + 1);
        front(tree, length, depth * 2 + 2);
    }
    else {
        return;
    }
}

void middle(const char* tree, int length, int depth) {
    if (depth < length) {
        middle(tree, length, depth * 2 + 1);
        if(tree[depth]!='#')
        cout << tree[depth] << " ";
        middle(tree, length, depth * 2 + 2);
    }
    else {
        return;
    }
}

void back(const char* tree, int length, int depth) {
    if (depth < length) {
        back(tree, length, depth * 2 + 1);
        back(tree, length, depth * 2 + 2);
        if (tree[depth] != '#')
        cout << tree[depth] << " ";
    }
}

int main() {
    cout << "请输入字符串空结点以'#'表示: " << endl;
    string ch;
    cin >> ch;
    const char* msg = ch.c_str();
    cout << "用数组存储的前序遍历: " << endl;
    front(msg, ch.size(), 0);
    cout << endl;
    cout << "用数组存储的中序遍历: " << endl;
    middle(msg, ch.size(), 0);
    cout << endl;
    cout << "用数组存储的后序遍历: " << endl;
    back(msg, ch.size(), 0);
    cout << endl;

    vector<char> A,a1,a2,a3;
    for (int i = 0; i < ch.length(); i++) {
        A.push_back(msg[i]);
    }
    TreeNode *r1 = construct_binary_tree(A);
    cout << "链式存储的前序遍历: " << endl;
    a1 = preorderTraversal(r1);
    printf_v(a1);
    cout << endl;
    cout << "链式存储的中序遍历: " << endl;
    a2 = inorderTraversal(r1);
    printf_v(a2);
    cout << endl;
    cout << "链式存储的后序遍历: " << endl;
    a3 = postorderTraversal(r1);
    printf_v(a3);
   
	return 0;
}

关于链式存储遍历用的是迭代法,没有用递归法

测试1:

输入:abcdefg

测试2:

输入:abcde##

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值