#include<iostream> #include<vector> using namespace std; struct TreeNode { int val; TreeNode* left; TreeNode* right; TreeNode(int x) :val(x), left(nullptr), right(nullptr) {} }; class SearchTree { private: int _a; public: void sum(int a) { _a = a; } void pri() { sum(5); cout << _a << endl; } void Insert_Node(TreeNode*& root, int val) { if (nullptr == root) { root = new TreeNode(val); return; } if (root->val < val) { if (root->right == nullptr) { root->right = new TreeNode(val); } else { Insert_Node(root->right, val); } } else if (root->val > val) { if (root->left == nullptr) { root->left = new TreeNode(val); } else { Insert_Node(root->left, val); } } } //根据数组构建一颗二叉搜索树 TreeNode* Create_SearchTree(vector<int>& vec) { TreeNode* root = nullptr; int length = vec.size(); for (int i = 0; i < length; i++) { Insert_Node(root, vec[i]); } return root; } void print(TreeNode*& root) { if (root == nullptr) { return; } print(root->left); cout << root->val << endl; print(root->right); } }; int main() { SearchTree tree; vector<int> vec{ 23,2,8,1,24,56,98,45}; TreeNode* root = tree.Create_SearchTree(vec); tree.print(root); //tree.pri(); }
//遇到的问题
//关于私有成员和公有有成员不熟悉
私有:不能继承,外部不能访问
保护:可以继承,外部不能访问
公有:都可