The binary tree constructed by sequential storage is converted into a chained storage binary tree

Creation of binary tree nodes

class TreeNode
{
    
    
public:
    int value;              // 所对应的值
    TreeNode *left, *right; // 下一个结点的地址
    TreeNode(int val)
    {
    
    
        value = val;
        left = nullptr;
        right = nullptr;
    }
    ~TreeNode()
    {
    
    
        delete right;
        delete left;
    }
};

The binary tree constructed by sequential storage is converted into a chained storage binary tree

According to the relationship between the left subtree of the binary tree and the left subtree is the serial number of the root node:

  • Root node serial number: i
  • The number of the root node of the left subtree: i*2+1
  • The root node number of the right subtree: i*2+2

The above relations are in an array in sequential storage.

step:

  • Create an array to store the nodes of the binary tree and the root node
  • Initialize the array of binary tree nodes
  • Construct and convert the numerical array of formal parameters into a linked storage binary tree based on the relationship between the root node and the serial numbers of the left and right children
class BinaryTree
{
    
    
public:
    // 将顺序存储的二叉树转换成链式存储的二叉树
    static TreeNode *construct_BinaryTree(vector<int> nums)
    {
    
    
        // 创建存储二叉树结点的数组和根节点
        TreeNode *root = nullptr;
        vector<TreeNode *> vecTree(nums.size(), nullptr);

        // 初始化二叉树数组
        for (int i = 0; i < nums.size(); i++)
        {
    
    
            // 如果数组元素值不为-1则设置结点值
            TreeNode *node = nullptr;
            if (nums[i] != -1)
            {
    
    
                node = new TreeNode(nums[i]);
            }
            vecTree[i] = node;

            // 如果是根节点则更新root的值
            if (i == 0)
            {
    
    
                root = node;
                /* code */
            }
        }

        // 将存储二叉树子结点的数组转换成链式存储,
        for (int i = 0; i * 2 + 1 < vecTree.size(); i++)
        {
    
    
            if (vecTree[i * 2 + 1] != nullptr)
            {
    
    
                vecTree[i]->left = vecTree[i * 2 + 1];
                if (i * 2 + 2 < vecTree.size())
                {
    
    
                    /* code */
                    vecTree[i]->right = vecTree[i * 2 + 2];
                }
            }
        }

        // 返回链式存储的二叉树根节点
        return root;
    }

    // 前序遍历
    static void PreTaverse(TreeNode *node)
    {
    
    
        if (node == nullptr)
        {
    
    
            return;
        }
        // 输出节点的值
        cout << node->value << "\t";
        // 访问左子树
        PreTaverse(node->left);
        // 访问右子树
        PreTaverse(node->right);
    }
    // 中序遍历
    static void MidTaverse(TreeNode *node)
    {
    
    
        if (node == nullptr)
        {
    
    
            return;
        }

        // 访问左子树
        MidTaverse(node->left);
        // 输出节点的值
        cout << node->value << "\t";
        // 访问右子树
        MidTaverse(node->right);
    }
    // 后序遍历
    static void BackTaverse(TreeNode *node)
    {
    
    
        if (node == nullptr)
        {
    
    
            return;
        }

        // 访问左子树
        BackTaverse(node->left);

        // 访问右子树
        BackTaverse(node->right);

        // 输出节点的值
        cout << node->value << "\t";
    }
};

traversal output test

Use the traversal method in the binary tree to verify the converted linked storage binary tree

int main(int argc, char const *argv[])
{
    
    
    // 二叉树的数值数组
    vector<int> nums = {
    
    1, 2, 3, 4, 5, 6, 7};

    TreeNode *root = BinaryTree::construct_BinaryTree(nums);
    // 遍历输出
    cout << "先序遍历:";
    BinaryTree::PreTaverse(root);
    cout << endl;

    cout << "中序遍历:";
    BinaryTree::MidTaverse(root);
    cout << endl;

    cout << "后序遍历:";
    BinaryTree::BackTaverse(root);
    cout << endl;

    return 0;
}

Output result:
insert image description here

Guess you like

Origin blog.csdn.net/weixin_44848852/article/details/127456681