cpp-algo-cases/utils/tree_node.hpp

121 lines
3.9 KiB
C++
Raw Normal View History

2024-02-20 16:13:23 +08:00
/**
* File: tree_node.hpp
* Created Time: 2021-12-19
* Author: krahets (krahets@163.com)
*/
#pragma once
#include <limits.h>
#include <vector>
using namespace std;
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڵ<EFBFBD><DAB5><EFBFBD><E1B9B9> */
struct TreeNode
{
int val{}; // <20>ڵ<EFBFBD><DAB5><EFBFBD>ֵ val {} <20><>ʾ<EFBFBD><CABE>ʼ<EFBFBD><CABC>Ϊ 0 <20><> nullptr ({} <20><> C++11 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>: ֵ<><D6B5>ʼ<EFBFBD><CABC>)
int height = 0; // <20>ڵ<EFBFBD><DAB5>ĸ߶<C4B8>
TreeNode *parent{}; // <20><><EFBFBD>ڵ<EFBFBD> {} <20><>ʾ<EFBFBD><CABE>ʼ<EFBFBD><CABC>Ϊ nullptr
TreeNode *left{}; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> {} <20><>ʾ<EFBFBD><CABE>ʼ<EFBFBD><CABC>Ϊ nullptr
TreeNode *right{}; // <20>Һ<EFBFBD><D2BA><EFBFBD> {} <20><>ʾ<EFBFBD><CABE>ʼ<EFBFBD><CABC>Ϊ nullptr
TreeNode() = default; // Ĭ<>Ϲ<EFBFBD><CFB9><EFBFBD><ECBAAF>
explicit TreeNode(int x, TreeNode *parent = nullptr) : val(x), parent(parent)
{
} // <20><><EFBFBD><EFBFBD><ECBAAF>: <20><>ʼ<EFBFBD><CABC><EFBFBD>ڵ<EFBFBD><DAB5><EFBFBD>ֵΪx, <20><><EFBFBD>ڵ<EFBFBD>Ϊparent
};
// <20><><EFBFBD>л<EFBFBD><D0BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ο<EFBFBD><CEBF><EFBFBD>
// https://www.hello-algo.com/chapter_tree/array_representation_of_tree/
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾ<EFBFBD><CABE>
// [1, 2, 3, 4, None, 6, 7, 8, 9, None, None, 12, None, None, 15]
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾ<EFBFBD><CABE>
// /<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 15
// /<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 7
// /<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 3
// | \<5C><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 6
// | \<5C><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 12
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 1
// \<5C><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 2
// | /<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 9
// \<5C><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 4
// \<5C><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 8
/* <20><><EFBFBD>б<EFBFBD><D0B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>л<EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݹ<EFBFBD> */
/*
vectorToTreeDFS <EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD>ݹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڽ<EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>͵<EFBFBD><EFBFBD><EFBF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EFBFBD><EFBFBD>δ<EFBFBD><EFBFBD><EFBFBD>ÿ<EFBFBD><EFBFBD><EFBFBD>ڵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ķ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*/
TreeNode *vectorToTreeDFS(vector<int> &arr, int i)
{
if (i < 0 || i >= arr.size() || arr[i] == INT_MAX)
{
return nullptr;
}
TreeNode *root = new TreeNode(arr[i]);
root->left = vectorToTreeDFS(arr, 2 * i + 1); // <20>ݹ鹹<DDB9><E9B9B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
root->right = vectorToTreeDFS(arr, 2 * i + 2); // <20>ݹ鹹<DDB9><E9B9B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
return root; // <20><><EFBFBD>ظ<EFBFBD><D8B8>ڵ<EFBFBD>
}
/* <20><><EFBFBD>б<EFBFBD><D0B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>л<EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
TreeNode *vectorToTree(vector<int> arr)
{
return vectorToTreeDFS(arr, 0);
}
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>л<EFBFBD>Ϊ<EFBFBD>б<EFBFBD><D0B1><EFBFBD><EFBFBD>ݹ<EFBFBD> */
/*
treeToVecorDFS <EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD>ݹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ľڵ<EFBFBD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FBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݴ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*/
void treeToVecorDFS(TreeNode *root, int i, vector<int> &res)
{
if (root == nullptr)
return;
while (i >= res.size())
{
res.push_back(INT_MAX); // <20><>ʼ<EFBFBD><CABC>Ϊ INT_MAX
}
res[i] = root->val;
treeToVecorDFS(root->left, 2 * i + 1, res);
treeToVecorDFS(root->right, 2 * i + 2, res);
}
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>л<EFBFBD>Ϊ<EFBFBD>б<EFBFBD> */
vector<int> treeToVecor(TreeNode *root)
{
vector<int> res;
treeToVecorDFS(root, 0, res);
return res;
}
/* <20>ͷŶ<CDB7><C5B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD> */
void freeMemoryTree(TreeNode *root)
{
if (root == nullptr)
return;
// <20><>Ҫ<EFBFBD><D2AA><EFBFBD>εݹ<CEB5><DDB9>ͷ<EFBFBD><CDB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڴ棬<DAB4><E6A3AC><EFBFBD>ͷŸ<CDB7><C5B8>ڵ<EFBFBD><DAB5><EFBFBD><EFBFBD>ڴ<EFBFBD>
freeMemoryTree(root->left);
freeMemoryTree(root->right);
delete root;
}